[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
This commit is contained in:
Chris Bieneman 2022-07-18 10:08:08 -05:00
parent f29a19b0b8
commit 5dbb92d8cd
4 changed files with 65 additions and 0 deletions

View File

@ -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();
}

View File

@ -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))}));
}

View File

@ -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();
};

View File

@ -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}