1 //===- DXILResource.h - DXIL Resource helper objects ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file This file contains helper objects for working with DXIL Resources. 10 /// 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_TARGET_DIRECTX_DXILRESOURCE_H 14 #define LLVM_TARGET_DIRECTX_DXILRESOURCE_H 15 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/StringRef.h" 18 #include "llvm/Frontend/HLSL/HLSLResource.h" 19 #include "llvm/IR/Metadata.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm/Support/DXILABI.h" 22 #include <cstdint> 23 24 namespace llvm { 25 class Module; 26 class GlobalVariable; 27 28 namespace dxil { 29 class CBufferDataLayout; 30 31 class ResourceBase { 32 protected: 33 uint32_t ID; 34 GlobalVariable *GV; 35 StringRef Name; 36 uint32_t Space; 37 uint32_t LowerBound; 38 uint32_t RangeSize; 39 ResourceBase(uint32_t I, hlsl::FrontendResource R); 40 41 void write(LLVMContext &Ctx, MutableArrayRef<Metadata *> Entries) const; 42 43 void print(raw_ostream &O, StringRef IDPrefix, StringRef BindingPrefix) const; 44 static StringRef getKindName(dxil::ResourceKind Kind); 45 static void printKind(dxil::ResourceKind Kind, unsigned Alignment, 46 raw_ostream &OS, bool SRV = false, 47 bool HasCounter = false, uint32_t SampleCount = 0); 48 49 static StringRef getElementTypeName(dxil::ElementType CompType); 50 static void printElementType(dxil::ResourceKind Kind, 51 dxil::ElementType CompType, unsigned Alignment, 52 raw_ostream &OS); 53 54 public: 55 struct ExtendedProperties { 56 std::optional<dxil::ElementType> ElementType; 57 58 // The value ordering of this enumeration is part of the DXIL ABI. Elements 59 // can only be added to the end, and not removed. 60 enum Tags : uint32_t { 61 TypedBufferElementType = 0, 62 StructuredBufferElementStride, 63 SamplerFeedbackKind, 64 Atomic64Use 65 }; 66 67 MDNode *write(LLVMContext &Ctx) const; 68 }; 69 }; 70 71 class UAVResource : public ResourceBase { 72 dxil::ResourceKind Shape; 73 bool GloballyCoherent; 74 bool HasCounter; 75 bool IsROV; 76 ResourceBase::ExtendedProperties ExtProps; 77 78 void parseSourceType(StringRef S); 79 80 public: UAVResource(uint32_t I,hlsl::FrontendResource R)81 UAVResource(uint32_t I, hlsl::FrontendResource R) 82 : ResourceBase(I, R), Shape(R.getResourceKind()), GloballyCoherent(false), 83 HasCounter(false), IsROV(R.getIsROV()), ExtProps{R.getElementType()} {} 84 85 MDNode *write() const; 86 void print(raw_ostream &O) const; 87 }; 88 89 class ConstantBuffer : public ResourceBase { 90 uint32_t CBufferSizeInBytes = 0; // Cbuffer used size in bytes. 91 public: 92 ConstantBuffer(uint32_t I, hlsl::FrontendResource R); 93 void setSize(CBufferDataLayout &DL); 94 MDNode *write() const; 95 void print(raw_ostream &O) const; 96 }; 97 98 template <typename T> class ResourceTable { 99 StringRef MDName; 100 101 llvm::SmallVector<T> Data; 102 103 public: ResourceTable(StringRef Name)104 ResourceTable(StringRef Name) : MDName(Name) {} 105 void collect(Module &M); 106 MDNode *write(Module &M) const; 107 void print(raw_ostream &O) const; 108 }; 109 110 // FIXME: Fully computing the resource structures requires analyzing the IR 111 // because some flags are set based on what operations are performed on the 112 // resource. This partial patch handles some of the leg work, but not all of it. 113 // See issue https://github.com/llvm/llvm-project/issues/57936. 114 class Resources { 115 ResourceTable<UAVResource> UAVs = {"hlsl.uavs"}; 116 ResourceTable<ConstantBuffer> CBuffers = {"hlsl.cbufs"}; 117 118 public: 119 void collect(Module &M); 120 void write(Module &M) const; 121 void print(raw_ostream &O) const; 122 LLVM_DUMP_METHOD void dump() const; 123 }; 124 125 } // namespace dxil 126 } // namespace llvm 127 128 #endif // LLVM_TARGET_DIRECTX_DXILRESOURCE_H 129