Skip to content

Commit 86329ba

Browse files
authored
[HLSL] Remove old resource annotations (#130338)
Fixes #114126
1 parent a8949b1 commit 86329ba

File tree

5 files changed

+2
-221
lines changed

5 files changed

+2
-221
lines changed

clang/lib/CodeGen/CGDeclCXX.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,9 +1071,6 @@ void CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
10711071
EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
10721072
}
10731073

1074-
if (getLangOpts().HLSL)
1075-
CGM.getHLSLRuntime().annotateHLSLResource(D, Addr);
1076-
10771074
FinishFunction();
10781075
}
10791076

clang/lib/CodeGen/CGHLSLRuntime.cpp

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -291,135 +291,6 @@ void CGHLSLRuntime::finishCodeGen() {
291291
generateGlobalCtorDtorCalls();
292292
}
293293

294-
void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV,
295-
llvm::hlsl::ResourceClass RC,
296-
llvm::hlsl::ResourceKind RK,
297-
bool IsROV,
298-
llvm::hlsl::ElementType ET,
299-
BufferResBinding &Binding) {
300-
llvm::Module &M = CGM.getModule();
301-
302-
NamedMDNode *ResourceMD = nullptr;
303-
switch (RC) {
304-
case llvm::hlsl::ResourceClass::UAV:
305-
ResourceMD = M.getOrInsertNamedMetadata("hlsl.uavs");
306-
break;
307-
case llvm::hlsl::ResourceClass::SRV:
308-
ResourceMD = M.getOrInsertNamedMetadata("hlsl.srvs");
309-
break;
310-
case llvm::hlsl::ResourceClass::CBuffer:
311-
ResourceMD = M.getOrInsertNamedMetadata("hlsl.cbufs");
312-
break;
313-
default:
314-
assert(false && "Unsupported buffer type!");
315-
return;
316-
}
317-
assert(ResourceMD != nullptr &&
318-
"ResourceMD must have been set by the switch above.");
319-
320-
llvm::hlsl::FrontendResource Res(
321-
GV, RK, ET, IsROV, Binding.Reg.value_or(UINT_MAX), Binding.Space);
322-
ResourceMD->addOperand(Res.getMetadata());
323-
}
324-
325-
static llvm::hlsl::ElementType
326-
calculateElementType(const ASTContext &Context, const clang::Type *ResourceTy) {
327-
using llvm::hlsl::ElementType;
328-
329-
// TODO: We may need to update this when we add things like ByteAddressBuffer
330-
// that don't have a template parameter (or, indeed, an element type).
331-
const auto *TST = ResourceTy->getAs<TemplateSpecializationType>();
332-
assert(TST && "Resource types must be template specializations");
333-
ArrayRef<TemplateArgument> Args = TST->template_arguments();
334-
assert(!Args.empty() && "Resource has no element type");
335-
336-
// At this point we have a resource with an element type, so we can assume
337-
// that it's valid or we would have diagnosed the error earlier.
338-
QualType ElTy = Args[0].getAsType();
339-
340-
// We should either have a basic type or a vector of a basic type.
341-
if (const auto *VecTy = ElTy->getAs<clang::VectorType>())
342-
ElTy = VecTy->getElementType();
343-
344-
if (ElTy->isSignedIntegerType()) {
345-
switch (Context.getTypeSize(ElTy)) {
346-
case 16:
347-
return ElementType::I16;
348-
case 32:
349-
return ElementType::I32;
350-
case 64:
351-
return ElementType::I64;
352-
}
353-
} else if (ElTy->isUnsignedIntegerType()) {
354-
switch (Context.getTypeSize(ElTy)) {
355-
case 16:
356-
return ElementType::U16;
357-
case 32:
358-
return ElementType::U32;
359-
case 64:
360-
return ElementType::U64;
361-
}
362-
} else if (ElTy->isSpecificBuiltinType(BuiltinType::Half))
363-
return ElementType::F16;
364-
else if (ElTy->isSpecificBuiltinType(BuiltinType::Float))
365-
return ElementType::F32;
366-
else if (ElTy->isSpecificBuiltinType(BuiltinType::Double))
367-
return ElementType::F64;
368-
369-
// TODO: We need to handle unorm/snorm float types here once we support them
370-
llvm_unreachable("Invalid element type for resource");
371-
}
372-
373-
void CGHLSLRuntime::annotateHLSLResource(const VarDecl *D, GlobalVariable *GV) {
374-
const Type *Ty = D->getType()->getPointeeOrArrayElementType();
375-
if (!Ty)
376-
return;
377-
const auto *RD = Ty->getAsCXXRecordDecl();
378-
if (!RD)
379-
return;
380-
// the resource related attributes are on the handle member
381-
// inside the record decl
382-
for (auto *FD : RD->fields()) {
383-
const auto *HLSLResAttr = FD->getAttr<HLSLResourceAttr>();
384-
const HLSLAttributedResourceType *AttrResType =
385-
dyn_cast<HLSLAttributedResourceType>(FD->getType().getTypePtr());
386-
if (!HLSLResAttr || !AttrResType)
387-
continue;
388-
389-
llvm::hlsl::ResourceClass RC = AttrResType->getAttrs().ResourceClass;
390-
if (RC == llvm::hlsl::ResourceClass::UAV ||
391-
RC == llvm::hlsl::ResourceClass::SRV)
392-
// UAVs and SRVs have already been converted to use LLVM target types,
393-
// we can disable generating of these resource annotations. This will
394-
// enable progress on structured buffers with user defined types this
395-
// resource annotations code does not handle and it crashes.
396-
// This whole function is going to be removed as soon as cbuffers are
397-
// converted to target types (llvm/llvm-project #114126).
398-
return;
399-
400-
bool IsROV = AttrResType->getAttrs().IsROV;
401-
llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind();
402-
llvm::hlsl::ElementType ET = calculateElementType(CGM.getContext(), Ty);
403-
404-
BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>());
405-
addBufferResourceAnnotation(GV, RC, RK, IsROV, ET, Binding);
406-
}
407-
}
408-
409-
CGHLSLRuntime::BufferResBinding::BufferResBinding(
410-
HLSLResourceBindingAttr *Binding) {
411-
if (Binding) {
412-
llvm::APInt RegInt(64, 0);
413-
Binding->getSlot().substr(1).getAsInteger(10, RegInt);
414-
Reg = RegInt.getLimitedValue();
415-
llvm::APInt SpaceInt(64, 0);
416-
Binding->getSpace().substr(5).getAsInteger(10, SpaceInt);
417-
Space = SpaceInt.getLimitedValue();
418-
} else {
419-
Space = 0;
420-
}
421-
}
422-
423294
void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
424295
const FunctionDecl *FD, llvm::Function *Fn) {
425296
const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>();

clang/lib/CodeGen/CGHLSLRuntime.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ class CGHLSLRuntime {
125125
// End of reserved area for HLSL intrinsic getters.
126126
//===----------------------------------------------------------------------===//
127127

128-
struct BufferResBinding {
129-
// The ID like 2 in register(b2, space1).
130-
std::optional<unsigned> Reg;
131-
// The Space like 1 is register(b2, space1).
132-
// Default value is 0.
133-
unsigned Space;
134-
BufferResBinding(HLSLResourceBindingAttr *Attr);
135-
};
136-
137128
protected:
138129
CodeGenModule &CGM;
139130

@@ -148,7 +139,6 @@ class CGHLSLRuntime {
148139
convertHLSLSpecificType(const Type *T,
149140
SmallVector<int32_t> *Packoffsets = nullptr);
150141

151-
void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV);
152142
void generateGlobalCtorDtorCalls();
153143

154144
void addBuffer(const HLSLBufferDecl *D);
@@ -169,11 +159,6 @@ class CGHLSLRuntime {
169159
void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E);
170160

171161
private:
172-
void addBufferResourceAnnotation(llvm::GlobalVariable *GV,
173-
llvm::hlsl::ResourceClass RC,
174-
llvm::hlsl::ResourceKind RK, bool IsROV,
175-
llvm::hlsl::ElementType ET,
176-
BufferResBinding &Binding);
177162
void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl,
178163
llvm::GlobalVariable *BufGV);
179164
llvm::Triple::ArchType getArch();

llvm/include/llvm/Frontend/HLSL/HLSLResource.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,17 @@
1313
#ifndef LLVM_FRONTEND_HLSL_HLSLRESOURCE_H
1414
#define LLVM_FRONTEND_HLSL_HLSLRESOURCE_H
1515

16-
#include "llvm/ADT/StringRef.h"
1716
#include "llvm/Support/DXILABI.h"
1817

1918
namespace llvm {
20-
class GlobalVariable;
21-
class MDNode;
22-
2319
namespace hlsl {
2420

2521
// For now we use DXIL ABI enum values directly. This may change in the future.
2622
using dxil::ResourceClass;
27-
using dxil::ElementType;
2823
using dxil::ResourceKind;
2924

3025
const unsigned CBufferRowSizeInBytes = 16U;
3126

32-
class FrontendResource {
33-
MDNode *Entry;
34-
35-
public:
36-
FrontendResource(MDNode *E);
37-
FrontendResource(GlobalVariable *GV, ResourceKind RK, ElementType ElTy,
38-
bool IsROV, uint32_t ResIndex, uint32_t Space);
39-
40-
GlobalVariable *getGlobalVariable();
41-
StringRef getSourceType();
42-
ResourceKind getResourceKind();
43-
ElementType getElementType();
44-
bool getIsROV();
45-
uint32_t getResourceIndex();
46-
uint32_t getSpace();
47-
MDNode *getMetadata() { return Entry; }
48-
};
4927
} // namespace hlsl
5028
} // namespace llvm
5129

llvm/lib/Frontend/HLSL/HLSLResource.cpp

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Frontend/HLSL/HLSLResource.h"
14-
#include "llvm/IR/IRBuilder.h"
15-
#include "llvm/IR/Metadata.h"
1614

1715
using namespace llvm;
1816
using namespace llvm::hlsl;
1917

20-
GlobalVariable *FrontendResource::getGlobalVariable() {
21-
return cast<GlobalVariable>(
22-
cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
23-
}
24-
25-
ResourceKind FrontendResource::getResourceKind() {
26-
return static_cast<ResourceKind>(
27-
cast<ConstantInt>(
28-
cast<ConstantAsMetadata>(Entry->getOperand(1))->getValue())
29-
->getLimitedValue());
30-
}
31-
ElementType FrontendResource::getElementType() {
32-
return static_cast<ElementType>(
33-
cast<ConstantInt>(
34-
cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
35-
->getLimitedValue());
36-
}
37-
bool FrontendResource::getIsROV() {
38-
return cast<ConstantInt>(
39-
cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
40-
->getLimitedValue();
41-
}
42-
uint32_t FrontendResource::getResourceIndex() {
43-
return cast<ConstantInt>(
44-
cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
45-
->getLimitedValue();
46-
}
47-
uint32_t FrontendResource::getSpace() {
48-
return cast<ConstantInt>(
49-
cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue())
50-
->getLimitedValue();
51-
}
52-
53-
FrontendResource::FrontendResource(MDNode *E) : Entry(E) {
54-
assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
55-
}
56-
57-
FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
58-
ElementType ElTy, bool IsROV,
59-
uint32_t ResIndex, uint32_t Space) {
60-
auto &Ctx = GV->getContext();
61-
IRBuilder<> B(Ctx);
62-
Entry = MDNode::get(
63-
Ctx, {ValueAsMetadata::get(GV),
64-
ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
65-
ConstantAsMetadata::get(B.getInt32(static_cast<int>(ElTy))),
66-
ConstantAsMetadata::get(B.getInt1(IsROV)),
67-
ConstantAsMetadata::get(B.getInt32(ResIndex)),
68-
ConstantAsMetadata::get(B.getInt32(Space))});
69-
}
18+
// Intentionally empty; this file can be removed when more cpp files are added
19+
// to the HLSLFrontend lib.

0 commit comments

Comments
 (0)