1 //===- DXILResource.h - DXIL Resource helper objects ----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// \file This file contains helper objects for working with DXIL Resources.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TARGET_DIRECTX_DXILRESOURCE_H
14 #define LLVM_TARGET_DIRECTX_DXILRESOURCE_H
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"
29 class CBufferDataLayout
;
39 ResourceBase(uint32_t I
, hlsl::FrontendResource R
);
41 void write(LLVMContext
&Ctx
, MutableArrayRef
<Metadata
*> Entries
) const;
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);
49 static StringRef
getElementTypeName(dxil::ElementType CompType
);
50 static void printElementType(dxil::ResourceKind Kind
,
51 dxil::ElementType CompType
, unsigned Alignment
,
55 struct ExtendedProperties
{
56 std::optional
<dxil::ElementType
> ElementType
;
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
,
67 MDNode
*write(LLVMContext
&Ctx
) const;
71 class UAVResource
: public ResourceBase
{
72 dxil::ResourceKind Shape
;
73 bool GloballyCoherent
;
76 ResourceBase::ExtendedProperties ExtProps
;
78 void parseSourceType(StringRef S
);
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()} {}
85 MDNode
*write() const;
86 void print(raw_ostream
&O
) const;
89 class ConstantBuffer
: public ResourceBase
{
90 uint32_t CBufferSizeInBytes
= 0; // Cbuffer used size in bytes.
92 ConstantBuffer(uint32_t I
, hlsl::FrontendResource R
);
93 void setSize(CBufferDataLayout
&DL
);
94 MDNode
*write() const;
95 void print(raw_ostream
&O
) const;
98 template <typename T
> class ResourceTable
{
101 llvm::SmallVector
<T
> Data
;
104 ResourceTable(StringRef Name
) : MDName(Name
) {}
105 void collect(Module
&M
);
106 MDNode
*write(Module
&M
) const;
107 void print(raw_ostream
&O
) const;
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.
115 ResourceTable
<UAVResource
> UAVs
= {"hlsl.uavs"};
116 ResourceTable
<ConstantBuffer
> CBuffers
= {"hlsl.cbufs"};
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;
128 #endif // LLVM_TARGET_DIRECTX_DXILRESOURCE_H