Skip to content

Commit

Permalink
[HLSL] Remove unused frontend-generated ID
Browse files Browse the repository at this point in the history
As @python3kgae pointed out we're going to want to assign these IDs
after optimization so that we can remove unused resrouces. This patch
just removes the unused ID value from the frontend metadata, clang code
generation, and updates associated test cases.

Reviewed By: python3kgae

Differential Revision: https://reviews.llvm.org/D136271
  • Loading branch information
llvm-beanz committed Oct 21, 2022
1 parent 9b809cc commit 4c7218e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 36 deletions.
3 changes: 1 addition & 2 deletions clang/lib/CodeGen/CGHLSLRuntime.cpp
Expand Up @@ -197,7 +197,6 @@ void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
llvm::hlsl::ResourceClass RC,
llvm::hlsl::ResourceKind RK,
BufferResBinding &Binding) {
uint32_t Counter = ResourceCounters[static_cast<uint32_t>(RC)]++;
llvm::Module &M = CGM.getModule();

NamedMDNode *ResourceMD = nullptr;
Expand All @@ -220,7 +219,7 @@ void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
"ResourceMD must have been set by the switch above.");

llvm::hlsl::FrontendResource Res(
GV, TyName, Counter, RK, Binding.Reg.value_or(UINT_MAX), Binding.Space);
GV, TyName, RK, Binding.Reg.value_or(UINT_MAX), Binding.Space);
ResourceMD->addOperand(Res.getMetadata());
}

Expand Down
2 changes: 0 additions & 2 deletions clang/lib/CodeGen/CGHLSLRuntime.h
Expand Up @@ -70,8 +70,6 @@ class CGHLSLRuntime {

protected:
CodeGenModule &CGM;
uint32_t ResourceCounters[static_cast<uint32_t>(
llvm::hlsl::ResourceClass::NumClasses)] = {0};

llvm::Value *emitInputSemantic(llvm::IRBuilder<> &B, const ParmVarDecl &D,
llvm::Type *Ty);
Expand Down
12 changes: 6 additions & 6 deletions clang/test/CodeGenHLSL/builtins/RWBuffer-annotations.hlsl
Expand Up @@ -16,9 +16,9 @@ void main() {
}

// CHECK: !hlsl.uavs = !{![[Single:[0-9]+]], ![[Array:[0-9]+]], ![[SingleAllocated:[0-9]+]], ![[ArrayAllocated:[0-9]+]], ![[SingleSpace:[0-9]+]], ![[ArraySpace:[0-9]+]]}
// CHECK-DAG: ![[Single]] = !{ptr @"?Buffer1@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 0, i32 10, i32 -1, i32 0}
// CHECK-DAG: ![[Array]] = !{ptr @"?BufferArray@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 1, i32 10, i32 -1, i32 0}
// CHECK-DAG: ![[SingleAllocated]] = !{ptr @"?Buffer2@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 2, i32 10, i32 3, i32 0}
// CHECK-DAG: ![[ArrayAllocated]] = !{ptr @"?BufferArray2@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 3, i32 10, i32 4, i32 0}
// CHECK-DAG: ![[SingleSpace]] = !{ptr @"?Buffer3@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 4, i32 10, i32 3, i32 1}
// CHECK-DAG: ![[ArraySpace]] = !{ptr @"?BufferArray3@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 5, i32 10, i32 4, i32 1}
// CHECK-DAG: ![[Single]] = !{ptr @"?Buffer1@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 -1, i32 0}
// CHECK-DAG: ![[Array]] = !{ptr @"?BufferArray@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 -1, i32 0}
// CHECK-DAG: ![[SingleAllocated]] = !{ptr @"?Buffer2@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 3, i32 0}
// CHECK-DAG: ![[ArrayAllocated]] = !{ptr @"?BufferArray2@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 4, i32 0}
// CHECK-DAG: ![[SingleSpace]] = !{ptr @"?Buffer3@@3V?$RWBuffer@M@hlsl@@A", !"RWBuffer<float>", i32 10, i32 3, i32 1}
// CHECK-DAG: ![[ArraySpace]] = !{ptr @"?BufferArray3@@3PAV?$RWBuffer@T?$__vector@M$03@__clang@@@hlsl@@A", !"RWBuffer<vector<float, 4> >", i32 10, i32 4, i32 1}
4 changes: 2 additions & 2 deletions clang/test/CodeGenHLSL/cbuf.hlsl
Expand Up @@ -24,5 +24,5 @@ float foo() {

// CHECK: !hlsl.cbufs = !{![[CBMD:[0-9]+]]}
// CHECK: !hlsl.srvs = !{![[TBMD:[0-9]+]]}
// CHECK: ![[CBMD]] = !{ptr @[[CB]], !"A.cb.ty", i32 0, i32 13, i32 0, i32 2}
// CHECK: ![[TBMD]] = !{ptr @[[TB]], !"A.tb.ty", i32 0, i32 15, i32 2, i32 1}
// CHECK: ![[CBMD]] = !{ptr @[[CB]], !"A.cb.ty", i32 13, i32 0, i32 2}
// CHECK: ![[TBMD]] = !{ptr @[[TB]], !"A.tb.ty", i32 15, i32 2, i32 1}
7 changes: 3 additions & 4 deletions llvm/include/llvm/Frontend/HLSL/HLSLResource.h
Expand Up @@ -60,15 +60,14 @@ class FrontendResource {

public:
FrontendResource(MDNode *E) : Entry(E) {
assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
assert(Entry->getNumOperands() == 5 && "Unexpected metadata shape");
}

FrontendResource(GlobalVariable *GV, StringRef TypeStr, uint32_t Counter,
ResourceKind RK, uint32_t ResIndex, uint32_t Space);
FrontendResource(GlobalVariable *GV, StringRef TypeStr, ResourceKind RK,
uint32_t ResIndex, uint32_t Space);

GlobalVariable *getGlobalVariable();
StringRef getSourceType();
Constant *getID();
uint32_t getResourceKind();
uint32_t getResourceIndex();
uint32_t getSpace();
Expand Down
15 changes: 5 additions & 10 deletions llvm/lib/Frontend/HLSL/HLSLResource.cpp
Expand Up @@ -27,34 +27,29 @@ StringRef FrontendResource::getSourceType() {
return cast<MDString>(Entry->getOperand(1))->getString();
}

Constant *FrontendResource::getID() {
return cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue();
}

uint32_t FrontendResource::FrontendResource::getResourceKind() {
return cast<ConstantInt>(
cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
->getLimitedValue();
}
uint32_t FrontendResource::getResourceIndex() {
return cast<ConstantInt>(
cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
->getLimitedValue();
}
uint32_t FrontendResource::getSpace() {
return cast<ConstantInt>(
cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue())
cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
->getLimitedValue();
}

FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr,
uint32_t Counter, ResourceKind RK,
uint32_t ResIndex, uint32_t Space) {
ResourceKind RK, uint32_t ResIndex,
uint32_t Space) {
auto &Ctx = GV->getContext();
IRBuilder<> B(Ctx);
Entry = MDNode::get(
Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr),
ConstantAsMetadata::get(B.getInt32(Counter)),
ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
ConstantAsMetadata::get(B.getInt32(ResIndex)),
ConstantAsMetadata::get(B.getInt32(Space))});
Expand Down
20 changes: 10 additions & 10 deletions llvm/test/CodeGen/DirectX/UAVMetadata.ll
Expand Up @@ -37,16 +37,16 @@ target triple = "dxil-pc-shadermodel6.0-compute"

!hlsl.uavs = !{!0, !1, !2, !3, !4, !5, !6, !7, !8, !9}

!0 = !{ptr @Zero, !"RWBuffer<half>", i32 0, i32 10, i32 0, i32 0}
!1 = !{ptr @One, !"Buffer<vector<float,4>>", i32 1, i32 10, i32 1, i32 0}
!2 = !{ptr @Two, !"Buffer<double>", i32 2, i32 10, i32 2, i32 0}
!3 = !{ptr @Three, !"Buffer<bool>", i32 3, i32 10, i32 3, i32 0}
!4 = !{ptr @Four, !"ByteAddressBuffer<int16_t>", i32 4, i32 11, i32 5, i32 0}
!5 = !{ptr @Five, !"StructuredBuffer<uint16_t>", i32 5, i32 12, i32 6, i32 0}
!6 = !{ptr @Six, !"RasterizerOrderedBuffer<int32_t>", i32 6, i32 10, i32 7, i32 0}
!7 = !{ptr @Seven, !"RasterizerOrderedStructuredBuffer<uint32_t>", i32 7, i32 12, i32 8, i32 0}
!8 = !{ptr @Eight, !"RasterizerOrderedByteAddressBuffer<int64_t>", i32 8, i32 11, i32 9, i32 0}
!9 = !{ptr @Nine, !"RWBuffer<uint64_t>", i32 9, i32 10, i32 10, i32 2}
!0 = !{ptr @Zero, !"RWBuffer<half>", i32 10, i32 0, i32 0}
!1 = !{ptr @One, !"Buffer<vector<float,4>>", i32 10, i32 1, i32 0}
!2 = !{ptr @Two, !"Buffer<double>", i32 10, i32 2, i32 0}
!3 = !{ptr @Three, !"Buffer<bool>", i32 10, i32 3, i32 0}
!4 = !{ptr @Four, !"ByteAddressBuffer<int16_t>", i32 11, i32 5, i32 0}
!5 = !{ptr @Five, !"StructuredBuffer<uint16_t>", i32 12, i32 6, i32 0}
!6 = !{ptr @Six, !"RasterizerOrderedBuffer<int32_t>", i32 10, i32 7, i32 0}
!7 = !{ptr @Seven, !"RasterizerOrderedStructuredBuffer<uint32_t>", i32 12, i32 8, i32 0}
!8 = !{ptr @Eight, !"RasterizerOrderedByteAddressBuffer<int64_t>", i32 11, i32 9, i32 0}
!9 = !{ptr @Nine, !"RWBuffer<uint64_t>", i32 10, i32 10, i32 2}

; CHECK: !dx.resources = !{[[ResList:[!][0-9]+]]}

Expand Down

0 comments on commit 4c7218e

Please sign in to comment.