Author: Chris Bieneman Date: 2022-08-01T11:19:43-05:00 New Revision: 5dbb92d8cdf751d5960225e874c27d19597fa65e
URL: https://github.com/llvm/llvm-project/commit/5dbb92d8cdf751d5960225e874c27d19597fa65e DIFF: https://github.com/llvm/llvm-project/commit/5dbb92d8cdf751d5960225e874c27d19597fa65e.diff LOG: [HLSL] CodeGen HLSL Resource annotations HLSL Resource types need special annotations that the backend will use to build out metadata and resource annotations that are required by DirectX and Vulkan drivers in order to provide correct data bindings for shader exeuction. This patch adds some of the required data for unordered-access-views (UAV) resource binding into the module flags. This data will evolve over time to cover all the required use cases, but this should get things started. Depends on D130018. Differential Revision: https://reviews.llvm.org/D130019 Added: clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl Modified: clang/lib/CodeGen/CGDeclCXX.cpp clang/lib/CodeGen/CGHLSLRuntime.cpp clang/lib/CodeGen/CGHLSLRuntime.h Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp index 949112c63cc9e..620af1e633b73 100644 --- a/clang/lib/CodeGen/CGDeclCXX.cpp +++ b/clang/lib/CodeGen/CGDeclCXX.cpp @@ -11,6 +11,7 @@ //===----------------------------------------------------------------------===// #include "CGCXXABI.h" +#include "CGHLSLRuntime.h" #include "CGObjCRuntime.h" #include "CGOpenMPRuntime.h" #include "CodeGenFunction.h" @@ -977,6 +978,9 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit); } + if (getLangOpts().HLSL) + CGM.getHLSLRuntime().annotateHLSLResource(D, Addr); + FinishFunction(); } diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index 7dfcc65969a86..3321d4ad0afb5 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -20,6 +20,7 @@ using namespace clang; using namespace CodeGen; +using namespace hlsl; using namespace llvm; namespace { @@ -50,3 +51,38 @@ void CGHLSLRuntime::finishCodeGen() { llvm::Module &M = CGM.getModule(); addDxilValVersion(TargetOpts.DxilValidatorVersion, M); } + +void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) { + const Type *Ty = D->getType()->getPointeeOrArrayElementType(); + if (!Ty) + return; + const auto *RD = Ty->getAsCXXRecordDecl(); + if (!RD) + return; + const auto *Attr = RD->getAttr<HLSLResourceAttr>(); + if (!Attr) + return; + + HLSLResourceAttr::ResourceClass RC = Attr->getResourceType(); + uint32_t Counter = ResourceCounters[static_cast<uint32_t>(RC)]++; + + NamedMDNode *ResourceMD = nullptr; + switch (RC) { + case HLSLResourceAttr::ResourceClass::UAV: + ResourceMD = CGM.getModule().getOrInsertNamedMetadata("hlsl.uavs"); + break; + default: + assert(false && "Unsupported buffer type!"); + return; + } + + assert(ResourceMD != nullptr && + "ResourceMD must have been set by the switch above."); + + auto &Ctx = CGM.getModule().getContext(); + IRBuilder<> B(Ctx); + QualType QT(Ty, 0); + ResourceMD->addOperand(MDNode::get( + Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, QT.getAsString()), + ConstantAsMetadata::get(B.getInt32(Counter))})); +} diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index 268810f2ec9e6..5953be4117f94 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -15,7 +15,16 @@ #ifndef LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H #define LLVM_CLANG_LIB_CODEGEN_CGHLSLRUNTIME_H +#include "clang/Basic/HLSLRuntime.h" + +namespace llvm { +class Value; +class GlobalVariable; +} // namespace llvm namespace clang { +class CallExpr; +class Type; +class VarDecl; namespace CodeGen { @@ -24,11 +33,15 @@ class CodeGenModule; class CGHLSLRuntime { protected: CodeGenModule &CGM; + uint32_t ResourceCounters[static_cast<uint32_t>( + hlsl::ResourceClass::NumClasses)] = {0}; public: CGHLSLRuntime(CodeGenModule &CGM) : CGM(CGM) {} virtual ~CGHLSLRuntime() {} + void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV); + void finishCodeGen(); }; diff --git a/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl b/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl new file mode 100644 index 0000000000000..4d12fa197f5e2 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -x hlsl -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s + +RWBuffer<float> Buffer1; +RWBuffer<vector<float, 4> > BufferArray[4]; + +[numthreads(1,1,1)] +void main() { +} + +// CHECK: !hlsl.uavs = !{![[Single:[0-9]+]], ![[Array:[0-9]+]]} +// CHECK-DAG: ![[Single]] = !{ptr @"?Buffer1@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 0} +// CHECK-DAG: ![[Array]] = !{ptr @"?BufferArray@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 1} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits