Author: Helena Kotas Date: 2025-03-14T10:12:01-07:00 New Revision: 86329ba4d904bb895d5d840c523dde9ddf0ded8e
URL: https://github.com/llvm/llvm-project/commit/86329ba4d904bb895d5d840c523dde9ddf0ded8e DIFF: https://github.com/llvm/llvm-project/commit/86329ba4d904bb895d5d840c523dde9ddf0ded8e.diff LOG: [HLSL] Remove old resource annotations (#130338) Fixes #114126 Added: Modified: clang/lib/CodeGen/CGDeclCXX.cpp clang/lib/CodeGen/CGHLSLRuntime.cpp clang/lib/CodeGen/CGHLSLRuntime.h llvm/include/llvm/Frontend/HLSL/HLSLResource.h llvm/lib/Frontend/HLSL/HLSLResource.cpp Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp index f5950f03673a1..1ad34ae61f96a 100644 --- a/clang/lib/CodeGen/CGDeclCXX.cpp +++ b/clang/lib/CodeGen/CGDeclCXX.cpp @@ -1071,9 +1071,6 @@ 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 5916fa6183a27..a273f1e50c8b5 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -291,135 +291,6 @@ void CGHLSLRuntime::finishCodeGen() { generateGlobalCtorDtorCalls(); } -void CGHLSLRuntime::addBufferResourceAnnotation(llvm::GlobalVariable *GV, - llvm::hlsl::ResourceClass RC, - llvm::hlsl::ResourceKind RK, - bool IsROV, - llvm::hlsl::ElementType ET, - BufferResBinding &Binding) { - llvm::Module &M = CGM.getModule(); - - NamedMDNode *ResourceMD = nullptr; - switch (RC) { - case llvm::hlsl::ResourceClass::UAV: - ResourceMD = M.getOrInsertNamedMetadata("hlsl.uavs"); - break; - case llvm::hlsl::ResourceClass::SRV: - ResourceMD = M.getOrInsertNamedMetadata("hlsl.srvs"); - break; - case llvm::hlsl::ResourceClass::CBuffer: - ResourceMD = M.getOrInsertNamedMetadata("hlsl.cbufs"); - break; - default: - assert(false && "Unsupported buffer type!"); - return; - } - assert(ResourceMD != nullptr && - "ResourceMD must have been set by the switch above."); - - llvm::hlsl::FrontendResource Res( - GV, RK, ET, IsROV, Binding.Reg.value_or(UINT_MAX), Binding.Space); - ResourceMD->addOperand(Res.getMetadata()); -} - -static llvm::hlsl::ElementType -calculateElementType(const ASTContext &Context, const clang::Type *ResourceTy) { - using llvm::hlsl::ElementType; - - // TODO: We may need to update this when we add things like ByteAddressBuffer - // that don't have a template parameter (or, indeed, an element type). - const auto *TST = ResourceTy->getAs<TemplateSpecializationType>(); - assert(TST && "Resource types must be template specializations"); - ArrayRef<TemplateArgument> Args = TST->template_arguments(); - assert(!Args.empty() && "Resource has no element type"); - - // At this point we have a resource with an element type, so we can assume - // that it's valid or we would have diagnosed the error earlier. - QualType ElTy = Args[0].getAsType(); - - // We should either have a basic type or a vector of a basic type. - if (const auto *VecTy = ElTy->getAs<clang::VectorType>()) - ElTy = VecTy->getElementType(); - - if (ElTy->isSignedIntegerType()) { - switch (Context.getTypeSize(ElTy)) { - case 16: - return ElementType::I16; - case 32: - return ElementType::I32; - case 64: - return ElementType::I64; - } - } else if (ElTy->isUnsignedIntegerType()) { - switch (Context.getTypeSize(ElTy)) { - case 16: - return ElementType::U16; - case 32: - return ElementType::U32; - case 64: - return ElementType::U64; - } - } else if (ElTy->isSpecificBuiltinType(BuiltinType::Half)) - return ElementType::F16; - else if (ElTy->isSpecificBuiltinType(BuiltinType::Float)) - return ElementType::F32; - else if (ElTy->isSpecificBuiltinType(BuiltinType::Double)) - return ElementType::F64; - - // TODO: We need to handle unorm/snorm float types here once we support them - llvm_unreachable("Invalid element type for resource"); -} - -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; - // the resource related attributes are on the handle member - // inside the record decl - for (auto *FD : RD->fields()) { - const auto *HLSLResAttr = FD->getAttr<HLSLResourceAttr>(); - const HLSLAttributedResourceType *AttrResType = - dyn_cast<HLSLAttributedResourceType>(FD->getType().getTypePtr()); - if (!HLSLResAttr || !AttrResType) - continue; - - llvm::hlsl::ResourceClass RC = AttrResType->getAttrs().ResourceClass; - if (RC == llvm::hlsl::ResourceClass::UAV || - RC == llvm::hlsl::ResourceClass::SRV) - // UAVs and SRVs have already been converted to use LLVM target types, - // we can disable generating of these resource annotations. This will - // enable progress on structured buffers with user defined types this - // resource annotations code does not handle and it crashes. - // This whole function is going to be removed as soon as cbuffers are - // converted to target types (llvm/llvm-project #114126). - return; - - bool IsROV = AttrResType->getAttrs().IsROV; - llvm::hlsl::ResourceKind RK = HLSLResAttr->getResourceKind(); - llvm::hlsl::ElementType ET = calculateElementType(CGM.getContext(), Ty); - - BufferResBinding Binding(D->getAttr<HLSLResourceBindingAttr>()); - addBufferResourceAnnotation(GV, RC, RK, IsROV, ET, Binding); - } -} - -CGHLSLRuntime::BufferResBinding::BufferResBinding( - HLSLResourceBindingAttr *Binding) { - if (Binding) { - llvm::APInt RegInt(64, 0); - Binding->getSlot().substr(1).getAsInteger(10, RegInt); - Reg = RegInt.getLimitedValue(); - llvm::APInt SpaceInt(64, 0); - Binding->getSpace().substr(5).getAsInteger(10, SpaceInt); - Space = SpaceInt.getLimitedValue(); - } else { - Space = 0; - } -} - void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes( const FunctionDecl *FD, llvm::Function *Fn) { const auto *ShaderAttr = FD->getAttr<HLSLShaderAttr>(); diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index c4550056175c1..23f187b24284f 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -125,15 +125,6 @@ class CGHLSLRuntime { // End of reserved area for HLSL intrinsic getters. //===----------------------------------------------------------------------===// - struct BufferResBinding { - // The ID like 2 in register(b2, space1). - std::optional<unsigned> Reg; - // The Space like 1 is register(b2, space1). - // Default value is 0. - unsigned Space; - BufferResBinding(HLSLResourceBindingAttr *Attr); - }; - protected: CodeGenModule &CGM; @@ -148,7 +139,6 @@ class CGHLSLRuntime { convertHLSLSpecificType(const Type *T, SmallVector<int32_t> *Packoffsets = nullptr); - void annotateHLSLResource(const VarDecl *D, llvm::GlobalVariable *GV); void generateGlobalCtorDtorCalls(); void addBuffer(const HLSLBufferDecl *D); @@ -169,11 +159,6 @@ class CGHLSLRuntime { void emitInitListOpaqueValues(CodeGenFunction &CGF, InitListExpr *E); private: - void addBufferResourceAnnotation(llvm::GlobalVariable *GV, - llvm::hlsl::ResourceClass RC, - llvm::hlsl::ResourceKind RK, bool IsROV, - llvm::hlsl::ElementType ET, - BufferResBinding &Binding); void emitBufferGlobalsAndMetadata(const HLSLBufferDecl *BufDecl, llvm::GlobalVariable *BufGV); llvm::Triple::ArchType getArch(); diff --git a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h index c59ad3f8d7b03..6dacbadb70e7b 100644 --- a/llvm/include/llvm/Frontend/HLSL/HLSLResource.h +++ b/llvm/include/llvm/Frontend/HLSL/HLSLResource.h @@ -13,39 +13,17 @@ #ifndef LLVM_FRONTEND_HLSL_HLSLRESOURCE_H #define LLVM_FRONTEND_HLSL_HLSLRESOURCE_H -#include "llvm/ADT/StringRef.h" #include "llvm/Support/DXILABI.h" namespace llvm { -class GlobalVariable; -class MDNode; - namespace hlsl { // For now we use DXIL ABI enum values directly. This may change in the future. using dxil::ResourceClass; -using dxil::ElementType; using dxil::ResourceKind; const unsigned CBufferRowSizeInBytes = 16U; -class FrontendResource { - MDNode *Entry; - -public: - FrontendResource(MDNode *E); - FrontendResource(GlobalVariable *GV, ResourceKind RK, ElementType ElTy, - bool IsROV, uint32_t ResIndex, uint32_t Space); - - GlobalVariable *getGlobalVariable(); - StringRef getSourceType(); - ResourceKind getResourceKind(); - ElementType getElementType(); - bool getIsROV(); - uint32_t getResourceIndex(); - uint32_t getSpace(); - MDNode *getMetadata() { return Entry; } -}; } // namespace hlsl } // namespace llvm diff --git a/llvm/lib/Frontend/HLSL/HLSLResource.cpp b/llvm/lib/Frontend/HLSL/HLSLResource.cpp index 48310d4f28e67..970edb9710cb6 100644 --- a/llvm/lib/Frontend/HLSL/HLSLResource.cpp +++ b/llvm/lib/Frontend/HLSL/HLSLResource.cpp @@ -11,59 +11,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Frontend/HLSL/HLSLResource.h" -#include "llvm/IR/IRBuilder.h" -#include "llvm/IR/Metadata.h" using namespace llvm; using namespace llvm::hlsl; -GlobalVariable *FrontendResource::getGlobalVariable() { - return cast<GlobalVariable>( - cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue()); -} - -ResourceKind FrontendResource::getResourceKind() { - return static_cast<ResourceKind>( - cast<ConstantInt>( - cast<ConstantAsMetadata>(Entry->getOperand(1))->getValue()) - ->getLimitedValue()); -} -ElementType FrontendResource::getElementType() { - return static_cast<ElementType>( - cast<ConstantInt>( - cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue()) - ->getLimitedValue()); -} -bool FrontendResource::getIsROV() { - return cast<ConstantInt>( - cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue()) - ->getLimitedValue(); -} -uint32_t FrontendResource::getResourceIndex() { - return cast<ConstantInt>( - cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue()) - ->getLimitedValue(); -} -uint32_t FrontendResource::getSpace() { - return cast<ConstantInt>( - cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue()) - ->getLimitedValue(); -} - -FrontendResource::FrontendResource(MDNode *E) : Entry(E) { - assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape"); -} - -FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK, - ElementType ElTy, bool IsROV, - uint32_t ResIndex, uint32_t Space) { - auto &Ctx = GV->getContext(); - IRBuilder<> B(Ctx); - Entry = MDNode::get( - Ctx, {ValueAsMetadata::get(GV), - ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))), - ConstantAsMetadata::get(B.getInt32(static_cast<int>(ElTy))), - ConstantAsMetadata::get(B.getInt1(IsROV)), - ConstantAsMetadata::get(B.getInt32(ResIndex)), - ConstantAsMetadata::get(B.getInt32(Space))}); -} +// Intentionally empty; this file can be removed when more cpp files are added +// to the HLSLFrontend lib. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits