Files
llvm-project/clang/lib/AST/HLSLResource.cpp
Helena Kotas 87a8d40fdd [HLSL] Add codegen for accessing resource members of a struct (2nd merge attempt) (#193584)
Any expression that accesses a resource or resource array member of a global struct instance must be during codegen replaced by an access of the corresponding implicit global resource variable.

When codegen encounters a `MemberExpr` of a resource type, it traverses the AST to locate the parent struct declaration, building the expected global resource variable name along the way. If the parent declaration is a non-static global struct instance, codegen searches its `HLSLAssociatedResourceDeclAttr` attributes to locate the matching global resource variable and then generates IR code to access the resource global in place of the member access.

Fixes #182989

This is the second try to land this. The [first one](https://github.com/llvm/llvm-project/pull/187127)collided with #188792 and both PRs had to be reverted. No updates needed to this change. I synced with @inbelic and we agreed
that this one should go in first.
2026-04-23 09:20:36 -07:00

61 lines
1.9 KiB
C++

//===--- HLSLResource.cpp - Helper routines for HLSL resources -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides shared routines to help analyze HLSL resources and
// their bindings during Sema and CodeGen.
//
//===----------------------------------------------------------------------===//
#include "clang/AST/HLSLResource.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Type.h"
using namespace clang;
namespace clang {
namespace hlsl {
void EmbeddedResourceNameBuilder::pushBaseName(llvm::StringRef N) {
pushName(N, FieldDelim);
Name.append(BaseClassDelim);
}
void EmbeddedResourceNameBuilder::pushName(llvm::StringRef N,
llvm::StringRef Delim) {
Offsets.push_back(Name.size());
if (!Name.empty() && !Name.ends_with(BaseClassDelim))
Name.append(Delim);
Name.append(N);
}
void EmbeddedResourceNameBuilder::pushArrayIndex(uint64_t Index) {
llvm::raw_svector_ostream OS(Name);
Offsets.push_back(Name.size());
OS << ArrayIndexDelim;
OS << Index;
}
void EmbeddedResourceNameBuilder::pushBaseNameHierarchy(
CXXRecordDecl *DerivedRD, CXXRecordDecl *BaseRD) {
assert(BaseRD != DerivedRD && DerivedRD->isDerivedFrom(BaseRD));
Offsets.push_back(Name.size());
Name.append(FieldDelim);
while (BaseRD != DerivedRD) {
assert(DerivedRD->getNumBases() == 1 &&
"HLSL does not support multiple inheritance");
DerivedRD = DerivedRD->bases_begin()->getType()->getAsCXXRecordDecl();
assert(DerivedRD && "base class not found");
Name.append(DerivedRD->getName());
Name.append(BaseClassDelim);
}
}
} // namespace hlsl
} // namespace clang