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