[clang] [HLSL] Codegen for `cbuffer` declarations without embedded arrays or structs (PR #119755)
Chris B via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 20 07:55:30 PST 2024
================
@@ -54,78 +54,108 @@ void addDxilValVersion(StringRef ValVersionStr, llvm::Module &M) {
auto *DXILValMD = M.getOrInsertNamedMetadata(DXILValKey);
DXILValMD->addOperand(Val);
}
+
void addDisableOptimizations(llvm::Module &M) {
StringRef Key = "dx.disable_optimizations";
M.addModuleFlag(llvm::Module::ModFlagBehavior::Override, Key, 1);
}
-// cbuffer will be translated into global variable in special address space.
-// If translate into C,
-// cbuffer A {
-// float a;
-// float b;
-// }
-// float foo() { return a + b; }
+
+// Creates resource handle representing the constant buffer.
+// For cbuffer declaration:
//
-// will be translated into
+// cbuffer MyConstants {
+// float a;
+// }
//
-// struct A {
-// float a;
-// float b;
-// } cbuffer_A __attribute__((address_space(4)));
-// float foo() { return cbuffer_A.a + cbuffer_A.b; }
+// creates a structure type MyConstants and then returns the resource handle
+// that would be spelled as:
//
-// layoutBuffer will create the struct A type.
-// replaceBuffer will replace use of global variable a and b with cbuffer_A.a
-// and cbuffer_A.b.
+// __hlsl_resource_t [[hlsl::resource_class(CBuffer)]]
+// [[contained_type(MyConstants)]]
//
-void layoutBuffer(CGHLSLRuntime::Buffer &Buf, const DataLayout &DL) {
- if (Buf.Constants.empty())
- return;
-
- std::vector<llvm::Type *> EltTys;
- for (auto &Const : Buf.Constants) {
- GlobalVariable *GV = Const.first;
- Const.second = EltTys.size();
- llvm::Type *Ty = GV->getValueType();
- EltTys.emplace_back(Ty);
+static const clang::Type *getBufferHandleType(CGHLSLRuntime::Buffer &Buf) {
+ HLSLBufferDecl *BD = Buf.Decl;
+ ASTContext &AST = BD->getASTContext();
+
+ // create struct type for the constant buffer; filter out any declarations
+ // that are not a VarDecls or that are static
+ CXXRecordDecl *StructDecl = CXXRecordDecl::Create(
+ BD->getASTContext(), TagDecl::TagKind::Class, BD->getDeclContext(),
+ BD->getLocation(), BD->getLocation(), BD->getIdentifier());
+ StructDecl->startDefinition();
----------------
llvm-beanz wrote:
This concerns me. We really shouldn't be generating AST types during CodeGen.
We should generate the struct decl during Sema and attach it to the buffer decl so that we can dump and inspect the AST.
https://github.com/llvm/llvm-project/pull/119755
More information about the cfe-commits
mailing list