Skip to content

Commit c574bed

Browse files
committed
[HLSL] Remove old resource annotations
Fixes #114126
1 parent 8076575 commit c574bed

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
@@ -260,135 +260,6 @@ void CGHLSLRuntime::finishCodeGen() {
260260
generateGlobalCtorDtorCalls();
261261
}
262262

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

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

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)