LLVM 24.0.0git
SemanticSignatures.cpp
Go to the documentation of this file.
1//===- SemanticSignatures.cpp - HLSL Semantic Signature helpers -----------===//
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 implements a library for working with HLSL shader input and
10/// output semantic signatures and their DirectX metadata representation.
11///
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/Enum.h"
17#include "llvm/ADT/bit.h"
18#include "llvm/IR/Constants.h"
19#include "llvm/IR/Metadata.h"
20#include "llvm/IR/Type.h"
22#include <cassert>
23
24using namespace llvm;
25using namespace llvm::hlsl;
26
27namespace {
28
29// Inclusive upper bounds of the operand enums
30constexpr uint32_t MaxCompType =
32constexpr uint32_t MaxSemanticKind =
33 static_cast<uint32_t>(dxbc::PSV::SemanticKind::Invalid);
34constexpr uint32_t MaxInterpMode =
35 static_cast<uint32_t>(dxbc::PSV::InterpolationMode::Invalid);
36
37Error makeError(const Twine &Msg) {
39}
40
41Expected<uint64_t> extractInt(const MDNode *Node, unsigned OpId) {
42 auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(Node->getOperand(OpId));
43 if (!CI)
44 return makeError("expected integer operand " + Twine(OpId));
45 return CI->getZExtValue();
46}
47} // namespace
48
50 if (!SemanticName.consume_front_insensitive("SV_"))
51 return dxbc::PSV::SemanticKind::Arbitrary;
52
53 for (const auto &Kind : dxbc::PSV::getSemanticKinds())
54 if (SemanticName.equals_insensitive(Kind.name()))
55 return Kind.value();
56
57 return dxbc::PSV::SemanticKind::Invalid;
58}
59
62 switch (SemanticKind) {
63 case dxbc::PSV::SemanticKind::Arbitrary: {
64 static constexpr IOType OutOrPatchConstant =
66 static constexpr SemanticStageInfo Stages[] = {
73 };
74 return Stages;
75 }
76 case dxbc::PSV::SemanticKind::DispatchThreadID:
77 case dxbc::PSV::SemanticKind::GroupID:
78 case dxbc::PSV::SemanticKind::GroupIndex:
79 case dxbc::PSV::SemanticKind::GroupThreadID: {
80 static constexpr SemanticStageInfo Stages[] = {
85 };
86 return Stages;
87 }
88 case dxbc::PSV::SemanticKind::Target: {
89 static constexpr SemanticStageInfo Stages[] = {
91 return Stages;
92 }
93 case dxbc::PSV::SemanticKind::VertexID: {
94 static constexpr SemanticStageInfo Stages[] = {
96 return Stages;
97 }
98 case dxbc::PSV::SemanticKind::IsFrontFace: {
99 static constexpr SemanticStageInfo Stages[] = {
102 return Stages;
103 }
104 case dxbc::PSV::SemanticKind::Position: {
105 static constexpr SemanticStageInfo Stages[] = {
117 };
118 return Stages;
119 }
120 case dxbc::PSV::SemanticKind::ClipDistance:
121 case dxbc::PSV::SemanticKind::CullDistance: {
122 static constexpr SemanticStageInfo Stages[] = {
134 };
135 return Stages;
136 }
137 case dxbc::PSV::SemanticKind::TessFactor:
138 case dxbc::PSV::SemanticKind::InsideTessFactor: {
139 static constexpr SemanticStageInfo Stages[] = {
144 };
145 return Stages;
146 }
147 default:
148 return {};
149 }
150}
151
154 Triple::EnvironmentType ShaderStage, IOType IOTy) {
155 assert(llvm::has_single_bit(static_cast<unsigned>(IOTy)) &&
156 "a single IOType is expected, not a mask of IOTypes");
157 for (const SemanticStageInfo &Info : getAvailableStages(SemanticKind))
158 if (Info.Stage == ShaderStage && any(Info.AllowedIOTypesMask & IOTy))
159 return Info.Interpretation;
161}
162
165 // Operand positions within a signature element metadata node.
166 enum class OpIdx : unsigned {
167 SigId,
169 CompType,
173 Rows,
174 Cols,
175 StartRow,
176 StartCol,
177 UsageMask,
179 GSStream,
180 LastEntry = GSStream,
181 };
182 const unsigned NumElementOperands = to_underlying(OpIdx::LastEntry) + 1;
183
184 if (!Node)
185 return makeError("signature element node is null");
186 if (Node->getNumOperands() != NumElementOperands)
187 return makeError("signature element node has wrong number of operands");
188
190
191 Expected<uint64_t> SigId = extractInt(Node, to_underlying(OpIdx::SigId));
192 if (!SigId)
193 return SigId.takeError();
194 Elem.SigId = *SigId;
195
196 auto *Name =
197 dyn_cast<MDString>(Node->getOperand(to_underlying(OpIdx::SemanticName)));
198 if (!Name)
199 return makeError("expected semantic name string");
200 Elem.SemanticName = Name->getString();
201
203 extractInt(Node, to_underlying(OpIdx::CompType));
204 if (!CompType)
205 return CompType.takeError();
206 if (*CompType > MaxCompType)
207 return makeError("invalid component type");
208 Elem.CompType = static_cast<dxil::ElementType>(*CompType);
209
211 extractInt(Node, to_underlying(OpIdx::SemanticKind));
212 if (!SemanticKind)
213 return SemanticKind.takeError();
214 if (*SemanticKind > MaxSemanticKind)
215 return makeError("invalid semantic kind");
216 Elem.SemanticKind = static_cast<dxbc::PSV::SemanticKind>(*SemanticKind);
217
218 auto *Indices =
219 dyn_cast<MDNode>(Node->getOperand(to_underlying(OpIdx::SemanticIndices)));
220 if (!Indices)
221 return makeError("expected semantic indices node");
222 for (unsigned I = 0, E = Indices->getNumOperands(); I != E; ++I) {
223 Expected<uint64_t> Index = extractInt(Indices, I);
224 if (!Index)
225 return Index.takeError();
226 Elem.SemanticIndices.push_back(*Index);
227 }
228
230 extractInt(Node, to_underlying(OpIdx::InterpMode));
231 if (!InterpMode)
232 return InterpMode.takeError();
233 if (*InterpMode > MaxInterpMode)
234 return makeError("invalid interpolation mode");
235 Elem.InterpMode = static_cast<dxbc::PSV::InterpolationMode>(*InterpMode);
236
237 Expected<uint64_t> Rows = extractInt(Node, to_underlying(OpIdx::Rows));
238 if (!Rows)
239 return Rows.takeError();
240 Elem.Rows = *Rows;
241
242 Expected<uint64_t> Cols = extractInt(Node, to_underlying(OpIdx::Cols));
243 if (!Cols)
244 return Cols.takeError();
245 if (*Cols < 1 || *Cols > 4)
246 return makeError("number of components per row must be within 1-4");
247 Elem.Cols = *Cols;
248
250 extractInt(Node, to_underlying(OpIdx::StartRow));
251 if (!StartRow)
252 return StartRow.takeError();
253 Elem.StartRow = *StartRow;
254
256 extractInt(Node, to_underlying(OpIdx::StartCol));
257 if (!StartCol)
258 return StartCol.takeError();
259 if (*StartCol > 3 && *StartCol != UnallocatedCol)
260 return makeError("start column must be within 0-3 or unallocated");
261 Elem.StartCol = *StartCol;
262
263 // The row/col sentinels are always set together
264 if ((Elem.StartRow == UnallocatedRow) != (Elem.StartCol == UnallocatedCol))
265 return makeError("start row and column sentinels must be set together");
266
268 extractInt(Node, to_underlying(OpIdx::UsageMask));
269 if (!UsageMask)
270 return UsageMask.takeError();
271 if (*UsageMask > 0xF)
272 return makeError("usage mask must be a 4-bit value");
273 Elem.UsageMask = *UsageMask;
274
276 extractInt(Node, to_underlying(OpIdx::DynIndexMask));
277 if (!DynIndexMask)
278 return DynIndexMask.takeError();
279 if (*DynIndexMask > 0xF)
280 return makeError("dynamic index mask must be a 4-bit value");
282
284 extractInt(Node, to_underlying(OpIdx::GSStream));
285 if (!GSStream)
286 return GSStream.takeError();
287 if (*GSStream > 3)
288 return makeError("geometry shader stream index must be within 0-3");
289 Elem.GSStream = *GSStream;
290
291 if (Elem.SemanticIndices.size() != Elem.Rows)
292 return makeError(
293 "number of semantic indices must equal the number of rows");
294
295 return Elem;
296}
297
299 Type *I32Ty = Type::getInt32Ty(Ctx);
300 Type *I8Ty = Type::getInt8Ty(Ctx);
301 auto GetI32 = [&](uint32_t Val) -> Metadata * {
302 return ConstantAsMetadata::get(ConstantInt::get(I32Ty, Val));
303 };
304 auto GetI8 = [&](uint8_t Val) -> Metadata * {
305 return ConstantAsMetadata::get(ConstantInt::get(I8Ty, Val));
306 };
307
309 for (uint32_t Index : SemanticIndices)
310 IndexOps.push_back(GetI32(Index));
311
312 return MDNode::get(Ctx,
313 {GetI32(SigId), MDString::get(Ctx, SemanticName),
314 GetI32(static_cast<uint32_t>(CompType)),
315 GetI32(static_cast<uint32_t>(SemanticKind)),
316 MDNode::get(Ctx, IndexOps),
317 GetI32(static_cast<uint32_t>(InterpMode)), GetI32(Rows),
318 GetI8(Cols), GetI32(StartRow), GetI8(StartCol),
319 GetI8(UsageMask), GetI8(DynIndexMask), GetI32(GSStream)});
320}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
const char * Msg
This file contains library features backported from future STL versions.
This file implements the C++20 <bit> header.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:611
Root of the metadata hierarchy.
Definition Metadata.h:64
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition StringRef.h:170
bool consume_front_insensitive(StringRef Prefix)
Returns true if this StringRef has the given prefix, ignoring case, and removes that prefix.
Definition StringRef.h:681
@ Amplification
Definition Triple.h:411
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:297
LLVM_ABI EnumStrings< SemanticKind, 1 > getSemanticKinds()
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:68
static constexpr uint32_t UnallocatedRow
LLVM_ABI SemanticInterpretation getInterpretationKind(dxbc::PSV::SemanticKind SemanticKind, Triple::EnvironmentType ShaderStage, IOType IOTy)
LLVM_ABI ArrayRef< SemanticStageInfo > getAvailableStages(dxbc::PSV::SemanticKind SemanticKind)
LLVM_ABI dxbc::PSV::SemanticKind getSemanticKind(StringRef SemanticName)
static constexpr uint8_t UnallocatedCol
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract_or_null(Y &&MD)
Extract a Value from Metadata, if any, allowing null.
Definition Metadata.h:709
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
constexpr bool has_single_bit(T Value) noexcept
Definition bit.h:149
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
static LLVM_ABI Expected< SemanticSignatureElement > fromMetadata(const MDNode *Node)
dxbc::PSV::InterpolationMode InterpMode
LLVM_ABI MDNode * toMetadata(LLVMContext &Ctx) const