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