llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-backend-directx Author: Justin Bogner (bogner) <details> <summary>Changes</summary> This implements the DXILResourceAnalysis pass for `dx.TypedBuffer` and `dx.RawBuffer` types. This should be sufficient to lower `dx.handle.fromBinding` for this set of types, but it leaves a number of TODOs around for other resource types. This also includes a straightforward `print` method in `ResourceInfo` to make the analysis testable. This is deliberately different than the printer in `lib/Target/DirectX/DXILResource.cpp`, which attempts to print bindings in a format compatible with the comments `dxc` prints. We will eventually want to make that functionality driven by this analysis pass, but it isn't sufficient for testing so we need both. --- Full diff: https://github.com/llvm/llvm-project/pull/100699.diff 3 Files Affected: - (modified) llvm/include/llvm/Analysis/DXILResource.h (+3) - (modified) llvm/lib/Analysis/DXILResource.cpp (+252-2) - (added) llvm/test/Analysis/DXILResource/buffer-frombinding.ll (+126) ``````````diff diff --git a/llvm/include/llvm/Analysis/DXILResource.h b/llvm/include/llvm/Analysis/DXILResource.h index eef526b548f07..88a9d19b83d2d 100644 --- a/llvm/include/llvm/Analysis/DXILResource.h +++ b/llvm/include/llvm/Analysis/DXILResource.h @@ -18,6 +18,7 @@ namespace llvm { class CallInst; class MDTuple; +class TargetExtType; namespace dxil { @@ -213,6 +214,8 @@ class ResourceInfo { ResourceBinding getBinding() const { return Binding; } std::pair<uint32_t, uint32_t> getAnnotateProps() const; + + void print(raw_ostream &OS) const; }; } // namespace dxil diff --git a/llvm/lib/Analysis/DXILResource.cpp b/llvm/lib/Analysis/DXILResource.cpp index 4efcb0c15d2ff..b6c4ef6550279 100644 --- a/llvm/lib/Analysis/DXILResource.cpp +++ b/llvm/lib/Analysis/DXILResource.cpp @@ -8,9 +8,14 @@ #include "llvm/Analysis/DXILResource.h" #include "llvm/ADT/APInt.h" +#include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/Instructions.h" +#include "llvm/IR/Intrinsics.h" +#include "llvm/IR/IntrinsicsDirectX.h" #include "llvm/IR/Metadata.h" +#include "llvm/IR/Module.h" #include "llvm/InitializePasses.h" #define DEBUG_TYPE "dxil-resource" @@ -331,6 +336,249 @@ std::pair<uint32_t, uint32_t> ResourceInfo::getAnnotateProps() const { return {Word0, Word1}; } +void ResourceInfo::print(raw_ostream &OS) const { + OS << " Symbol: "; + Symbol->printAsOperand(OS); + OS << "\n"; + + OS << " Name: \"" << Name << "\"\n" + << " Binding:\n" + << " Unique ID: " << Binding.UniqueID << "\n" + << " Space: " << Binding.Space << "\n" + << " Lower Bound: " << Binding.LowerBound << "\n" + << " Size: " << Binding.Size << "\n" + << " Class: " << static_cast<unsigned>(RC) << "\n" + << " Kind: " << static_cast<unsigned>(Kind) << "\n"; + + if (isCBuffer()) { + OS << " CBuffer size: " << CBufferSize << "\n"; + } else if (isSampler()) { + OS << " Sampler Type: " << static_cast<unsigned>(SamplerTy) << "\n"; + } else { + if (isUAV()) { + OS << " Globally Coherent: " << UAVFlags.GloballyCoherent << "\n" + << " HasCounter: " << UAVFlags.HasCounter << "\n" + << " IsROV: " << UAVFlags.IsROV << "\n"; + } + if (isMultiSample()) + OS << " Sample Count: " << MultiSample.Count << "\n"; + + if (isStruct()) { + OS << " Buffer Stride: " << Struct.Stride << "\n"; + uint32_t AlignLog2 = Struct.Alignment ? Log2(*Struct.Alignment) : 0; + OS << " Alignment: " << AlignLog2 << "\n"; + } else if (isTyped()) { + OS << " Element Type: " << static_cast<unsigned>(Typed.ElementTy) << "\n" + << " Element Count: " << static_cast<unsigned>(Typed.ElementCount) + << "\n"; + } else if (isFeedback()) + OS << " Feedback Type: " << static_cast<unsigned>(Feedback.Type) << "\n"; + } +} + +//===----------------------------------------------------------------------===// +// ResourceMapper + +static dxil::ElementType toDXILElementType(Type *Ty, bool IsSigned) { + // TODO: Handle unorm, snorm, and packed. + Ty = Ty->getScalarType(); + + if (Ty->isIntegerTy()) { + switch (Ty->getIntegerBitWidth()) { + case 16: + return IsSigned ? ElementType::I16 : ElementType::U16; + case 32: + return IsSigned ? ElementType::I32 : ElementType::U32; + case 64: + return IsSigned ? ElementType::I64 : ElementType::U64; + case 1: + default: + return ElementType::Invalid; + } + } else if (Ty->isFloatTy()) { + return ElementType::F32; + } else if (Ty->isDoubleTy()) { + return ElementType::F64; + } else if (Ty->isHalfTy()) { + return ElementType::F16; + } + + return ElementType::Invalid; +} + +namespace { + +class ResourceMapper { + Module &M; + LLVMContext &Context; + DXILResourceMap &Resources; + + // Unique ID is per resource type to match DXC. + uint32_t NextUAV = 0; + uint32_t NextSRV = 0; + uint32_t NextCBuf = 0; + uint32_t NextSmp = 0; + +public: + ResourceMapper(Module &M, + MapVector<CallInst *, dxil::ResourceInfo> &Resources) + : M(M), Context(M.getContext()), Resources(Resources) {} + + void diagnoseHandle(CallInst *CI, const Twine &Msg, + DiagnosticSeverity Severity = DS_Error) { + std::string S; + raw_string_ostream SS(S); + CI->printAsOperand(SS); + DiagnosticInfoUnsupported Diag(*CI->getFunction(), Msg + ": " + SS.str(), + CI->getDebugLoc(), Severity); + Context.diagnose(Diag); + } + + ResourceInfo *mapBufferType(CallInst *CI, TargetExtType *HandleTy, + bool IsTyped) { + if (HandleTy->getNumTypeParameters() != 1 || + HandleTy->getNumIntParameters() != (IsTyped ? 3 : 2)) { + diagnoseHandle(CI, Twine("Invalid buffer target type")); + return nullptr; + } + + Type *ElTy = HandleTy->getTypeParameter(0); + unsigned IsWriteable = HandleTy->getIntParameter(0); + unsigned IsROV = HandleTy->getIntParameter(1); + bool IsSigned = IsTyped && HandleTy->getIntParameter(2); + + ResourceClass RC = IsWriteable ? ResourceClass::UAV : ResourceClass::SRV; + ResourceKind Kind; + if (IsTyped) + Kind = ResourceKind::TypedBuffer; + else if (ElTy->isIntegerTy(8)) + Kind = ResourceKind::RawBuffer; + else + Kind = ResourceKind::StructuredBuffer; + + // TODO: We need to lower to a typed pointer, can we smuggle the type + // through? + Value *Symbol = UndefValue::get(PointerType::getUnqual(Context)); + // TODO: We don't actually keep track of the name right now... + StringRef Name = ""; + + auto [It, Success] = Resources.try_emplace(CI, RC, Kind, Symbol, Name); + assert(Success && "Mapping the same CallInst again?"); + (void)Success; + // We grab a pointer into the map's storage, which isn't generally safe. + // Since we're just using this to fill in the info the map won't mutate and + // the pointer stays valid for as long as we need it to. + ResourceInfo *RI = &(It->second); + + if (RI->isUAV()) + // TODO: We need analysis for GloballyCoherent and HasCounter + RI->setUAV(false, false, IsROV); + + if (RI->isTyped()) { + dxil::ElementType ET = toDXILElementType(ElTy, IsSigned); + uint32_t Count = 1; + if (auto *VTy = dyn_cast<FixedVectorType>(ElTy)) + Count = VTy->getNumElements(); + RI->setTyped(ET, Count); + } else if (RI->isStruct()) { + const DataLayout &DL = M.getDataLayout(); + + // This mimics what DXC does. Notably, we only ever set the alignment if + // the type is actually a struct type. + uint32_t Stride = DL.getTypeAllocSize(ElTy); + MaybeAlign Alignment; + if (auto *STy = dyn_cast<StructType>(ElTy)) + Alignment = DL.getStructLayout(STy)->getAlignment(); + RI->setStruct(Stride, Alignment); + } + + return RI; + } + + ResourceInfo *mapHandleIntrin(CallInst *CI) { + FunctionType *FTy = CI->getFunctionType(); + Type *RetTy = FTy->getReturnType(); + auto *HandleTy = dyn_cast<TargetExtType>(RetTy); + if (!HandleTy) { + diagnoseHandle(CI, "dx.handle.fromBinding requires target type"); + return nullptr; + } + + StringRef TypeName = HandleTy->getName(); + if (TypeName == "dx.TypedBuffer") { + return mapBufferType(CI, HandleTy, /*IsTyped=*/true); + } else if (TypeName == "dx.RawBuffer") { + return mapBufferType(CI, HandleTy, /*IsTyped=*/false); + } else if (TypeName == "dx.CBuffer") { + // TODO: implement + diagnoseHandle(CI, "dx.CBuffer handles are not implemented yet"); + return nullptr; + } else if (TypeName == "dx.Sampler") { + // TODO: implement + diagnoseHandle(CI, "dx.Sampler handles are not implemented yet"); + return nullptr; + } else if (TypeName == "dx.Texture") { + // TODO: implement + diagnoseHandle(CI, "dx.Texture handles are not implemented yet"); + return nullptr; + } + + diagnoseHandle(CI, "Invalid target(dx) type"); + return nullptr; + } + + ResourceInfo *mapHandleFromBinding(CallInst *CI) { + assert(CI->getIntrinsicID() == Intrinsic::dx_handle_fromBinding && + "Must be dx.handle.fromBinding intrinsic"); + + ResourceInfo *RI = mapHandleIntrin(CI); + if (!RI) + return nullptr; + + uint32_t NextID; + if (RI->isCBuffer()) + NextID = NextCBuf++; + else if (RI->isSampler()) + NextID = NextSmp++; + else if (RI->isUAV()) + NextID = NextUAV++; + else + NextID = NextSRV++; + + uint32_t Space = cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue(); + uint32_t LowerBound = + cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); + uint32_t Size = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); + + RI->bind(NextID, Space, LowerBound, Size); + + return RI; + } + + void mapResources() { + for (Function &F : M.functions()) { + if (!F.isDeclaration()) + continue; + LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n"); + Intrinsic::ID ID = F.getIntrinsicID(); + switch (ID) { + default: + // TODO: handle `dx.op` functions. + continue; + case Intrinsic::dx_handle_fromBinding: + for (User *U : F.users()) { + LLVM_DEBUG(dbgs() << " Visiting: " << *U << "\n"); + if (CallInst *CI = dyn_cast<CallInst>(U)) + mapHandleFromBinding(CI); + } + break; + } + } + } +}; + +} // namespace + //===----------------------------------------------------------------------===// // DXILResourceAnalysis and DXILResourcePrinterPass @@ -340,6 +588,7 @@ AnalysisKey DXILResourceAnalysis::Key; DXILResourceMap DXILResourceAnalysis::run(Module &M, ModuleAnalysisManager &AM) { DXILResourceMap Data; + ResourceMapper(M, Data).mapResources(); return Data; } @@ -352,7 +601,7 @@ PreservedAnalyses DXILResourcePrinterPass::run(Module &M, OS << "Binding for "; Handle->print(OS); OS << "\n"; - // TODO: Info.print(OS); + Info.print(OS); OS << "\n"; } @@ -374,6 +623,7 @@ void DXILResourceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { bool DXILResourceWrapperPass::runOnModule(Module &M) { ResourceMap.reset(new DXILResourceMap()); + ResourceMapper(M, *ResourceMap).mapResources(); return false; } @@ -388,7 +638,7 @@ void DXILResourceWrapperPass::print(raw_ostream &OS, const Module *) const { OS << "Binding for "; Handle->print(OS); OS << "\n"; - // TODO: Info.print(OS); + Info.print(OS); OS << "\n"; } } diff --git a/llvm/test/Analysis/DXILResource/buffer-frombinding.ll b/llvm/test/Analysis/DXILResource/buffer-frombinding.ll new file mode 100644 index 0000000000000..a54b2188fd077 --- /dev/null +++ b/llvm/test/Analysis/DXILResource/buffer-frombinding.ll @@ -0,0 +1,126 @@ +; RUN: opt -S -disable-output -passes="print<dxil-resource>" < %s 2>&1 | FileCheck %s + +@G = external constant <4 x float>, align 4 + +define void @test_typedbuffer() { + ; RWBuffer<float4> Buf : register(u5, space3) + %typed0 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_f32_1_0( + i32 3, i32 5, i32 1, i32 0, i1 false) + ; CHECK: Binding for %typed0 + ; CHECK: Symbol: ptr undef + ; CHECK: Name: "" + ; CHECK: Binding: + ; CHECK: Unique ID: 0 + ; CHECK: Space: 3 + ; CHECK: Lower Bound: 5 + ; CHECK: Size: 1 + ; CHECK: Class: 1 + ; CHECK: Kind: 10 + ; CHECK: Globally Coherent: 0 + ; CHECK: HasCounter: 0 + ; CHECK: IsROV: 0 + ; CHECK: Element Type: 9 + ; CHECK: Element Count: 4 + + ; RWBuffer<int> Buf : register(u7, space2) + %typed1 = call target("dx.TypedBuffer", i32, 1, 0, 1) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0t( + i32 2, i32 7, i32 1, i32 0, i1 false) + ; CHECK: Binding for %typed1 + ; CHECK: Symbol: ptr undef + ; CHECK: Name: "" + ; CHECK: Binding: + ; CHECK: Unique ID: 1 + ; CHECK: Space: 2 + ; CHECK: Lower Bound: 7 + ; CHECK: Size: 1 + ; CHECK: Class: 1 + ; CHECK: Kind: 10 + ; CHECK: Globally Coherent: 0 + ; CHECK: HasCounter: 0 + ; CHECK: IsROV: 0 + ; CHECK: Element Type: 4 + ; CHECK: Element Count: 1 + + ; Buffer<uint4> Buf[24] : register(t3, space5) + %typed2 = call target("dx.TypedBuffer", <4 x i32>, 0, 0, 0) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_0_0t( + i32 2, i32 7, i32 24, i32 0, i1 false) + ; CHECK: Binding for %typed2 + ; CHECK: Symbol: ptr undef + ; CHECK: Name: "" + ; CHECK: Binding: + ; CHECK: Unique ID: 0 + ; CHECK: Space: 2 + ; CHECK: Lower Bound: 7 + ; CHECK: Size: 24 + ; CHECK: Class: 0 + ; CHECK: Kind: 10 + ; CHECK: Element Type: 5 + ; CHECK: Element Count: 4 + + ret void +} + +define void @test_structbuffer() { + ; struct S { float4 a; uint4 b; }; + ; StructuredBuffer<S> Buf : register(t2, space4) + %struct0 = call target("dx.RawBuffer", {<4 x float>, <4 x i32>}, 0, 0) + @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t( + i32 4, i32 2, i32 1, i32 0, i1 false) + ; CHECK: Binding for %struct0 + ; CHECK: Symbol: ptr undef + ; CHECK: Name: "" + ; CHECK: Binding: + ; CHECK: Unique ID: 1 + ; CHECK: Space: 4 + ; CHECK: Lower Bound: 2 + ; CHECK: Size: 1 + ; CHECK: Class: 0 + ; CHECK: Kind: 12 + ; CHECK: Buffer Stride: 32 + ; CHECK: Alignment: 4 + + ret void +} + +define void @test_bytebuffer() { + ; ByteAddressBuffer Buf : register(t8, space1) + %byteaddr0 = call target("dx.RawBuffer", i8, 0, 0) + @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t( + i32 1, i32 8, i32 1, i32 0, i1 false) + ; CHECK: Binding for %byteaddr0 + ; CHECK: Symbol: ptr undef + ; CHECK: Name: "" + ; CHECK: Binding: + ; CHECK: Unique ID: 2 + ; CHECK: Space: 1 + ; CHECK: Lower Bound: 8 + ; CHECK: Size: 1 + ; CHECK: Class: 0 + ; CHECK: Kind: 11 + + ret void +} + +; Note: We need declarations for each handle.fromBinding in the same +; order as they appear in source to ensure that we can put our CHECK +; lines along side the thing they're checking. +declare target("dx.TypedBuffer", <4 x float>, 1, 0, 0) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_v4f32_1_0_0t( + i32, i32, i32, i32, i1) #0 +declare target("dx.TypedBuffer", i32, 1, 0, 1) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_i32_1_0_1t( + i32, i32, i32, i32, i1) #0 +declare target("dx.TypedBuffer", <4 x i32>, 0, 0, 0) + @llvm.dx.handle.fromBinding.tdx.TypedBuffer_v4i32_0_0_0t( + i32, i32, i32, i32, i1) #0 +declare target("dx.RawBuffer", { <4 x float>, <4 x i32> }, 0, 0) + @llvm.dx.handle.fromBinding.tdx.RawBuffer_sl_v4f32v4i32s_0_0t( + i32, i32, i32, i32, i1) #0 +declare target("dx.RawBuffer", i8, 0, 0) + @llvm.dx.handle.fromBinding.tdx.RawBuffer_i8_0_0t( + i32, i32, i32, i32, i1) #0 + +attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) } `````````` </details> https://github.com/llvm/llvm-project/pull/100699 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits