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::ViewID: {
89 static constexpr IOType InOrPatchConstant =
91 static constexpr SemanticStageInfo Stages[] = {
94 {Triple::Domain, InOrPatchConstant,
101 };
102 return Stages;
103 }
104 case dxbc::PSV::SemanticKind::Target: {
105 static constexpr SemanticStageInfo Stages[] = {
107 return Stages;
108 }
109 case dxbc::PSV::SemanticKind::VertexID: {
110 static constexpr SemanticStageInfo Stages[] = {
112 return Stages;
113 }
114 case dxbc::PSV::SemanticKind::IsFrontFace: {
115 static constexpr SemanticStageInfo Stages[] = {
118 return Stages;
119 }
120 case dxbc::PSV::SemanticKind::Position: {
121 static constexpr SemanticStageInfo Stages[] = {
133 };
134 return Stages;
135 }
136 case dxbc::PSV::SemanticKind::ClipDistance:
137 case dxbc::PSV::SemanticKind::CullDistance: {
138 static constexpr SemanticStageInfo Stages[] = {
150 };
151 return Stages;
152 }
153 case dxbc::PSV::SemanticKind::TessFactor:
154 case dxbc::PSV::SemanticKind::InsideTessFactor: {
155 static constexpr SemanticStageInfo Stages[] = {
160 };
161 return Stages;
162 }
163 default:
164 return {};
165 }
166}
167
170 Triple::EnvironmentType ShaderStage, IOType IOTy) {
171 assert(llvm::has_single_bit(static_cast<unsigned>(IOTy)) &&
172 "a single IOType is expected, not a mask of IOTypes");
173 for (const SemanticStageInfo &Info : getAvailableStages(SemanticKind))
174 if (Info.Stage == ShaderStage && any(Info.AllowedIOTypesMask & IOTy))
175 return Info.Interpretation;
177}
178
181 // Operand positions within a signature element metadata node.
182 enum class OpIdx : unsigned {
183 SigId,
185 CompType,
189 Rows,
190 Cols,
191 StartRow,
192 StartCol,
193 UsageMask,
195 GSStream,
196 LastEntry = GSStream,
197 };
198 const unsigned NumElementOperands = to_underlying(OpIdx::LastEntry) + 1;
199
200 if (!Node)
201 return makeError("signature element node is null");
202 if (Node->getNumOperands() != NumElementOperands)
203 return makeError("signature element node has wrong number of operands");
204
206
207 Expected<uint64_t> SigId = extractInt(Node, to_underlying(OpIdx::SigId));
208 if (!SigId)
209 return SigId.takeError();
210 Elem.SigId = *SigId;
211
212 auto *Name =
213 dyn_cast<MDString>(Node->getOperand(to_underlying(OpIdx::SemanticName)));
214 if (!Name)
215 return makeError("expected semantic name string");
216 Elem.SemanticName = Name->getString();
217
219 extractInt(Node, to_underlying(OpIdx::CompType));
220 if (!CompType)
221 return CompType.takeError();
222 if (*CompType > MaxCompType)
223 return makeError("invalid component type");
224 Elem.CompType = static_cast<dxil::ElementType>(*CompType);
225
227 extractInt(Node, to_underlying(OpIdx::SemanticKind));
228 if (!SemanticKind)
229 return SemanticKind.takeError();
230 if (*SemanticKind > MaxSemanticKind)
231 return makeError("invalid semantic kind");
232 Elem.SemanticKind = static_cast<dxbc::PSV::SemanticKind>(*SemanticKind);
233
234 auto *Indices =
235 dyn_cast<MDNode>(Node->getOperand(to_underlying(OpIdx::SemanticIndices)));
236 if (!Indices)
237 return makeError("expected semantic indices node");
238 for (unsigned I = 0, E = Indices->getNumOperands(); I != E; ++I) {
239 Expected<uint64_t> Index = extractInt(Indices, I);
240 if (!Index)
241 return Index.takeError();
242 Elem.SemanticIndices.push_back(*Index);
243 }
244
246 extractInt(Node, to_underlying(OpIdx::InterpMode));
247 if (!InterpMode)
248 return InterpMode.takeError();
249 if (*InterpMode > MaxInterpMode)
250 return makeError("invalid interpolation mode");
251 Elem.InterpMode = static_cast<dxbc::PSV::InterpolationMode>(*InterpMode);
252
253 Expected<uint64_t> Rows = extractInt(Node, to_underlying(OpIdx::Rows));
254 if (!Rows)
255 return Rows.takeError();
256 Elem.Rows = *Rows;
257
258 Expected<uint64_t> Cols = extractInt(Node, to_underlying(OpIdx::Cols));
259 if (!Cols)
260 return Cols.takeError();
261 if (*Cols < 1 || *Cols > 4)
262 return makeError("number of components per row must be within 1-4");
263 Elem.Cols = *Cols;
264
266 extractInt(Node, to_underlying(OpIdx::StartRow));
267 if (!StartRow)
268 return StartRow.takeError();
269 Elem.StartRow = *StartRow;
270
272 extractInt(Node, to_underlying(OpIdx::StartCol));
273 if (!StartCol)
274 return StartCol.takeError();
275 if (*StartCol > 3 && *StartCol != UnallocatedCol)
276 return makeError("start column must be within 0-3 or unallocated");
277 Elem.StartCol = *StartCol;
278
279 // The row/col sentinels are always set together
280 if ((Elem.StartRow == UnallocatedRow) != (Elem.StartCol == UnallocatedCol))
281 return makeError("start row and column sentinels must be set together");
282
284 extractInt(Node, to_underlying(OpIdx::UsageMask));
285 if (!UsageMask)
286 return UsageMask.takeError();
287 if (*UsageMask > 0xF)
288 return makeError("usage mask must be a 4-bit value");
289 Elem.UsageMask = *UsageMask;
290
292 extractInt(Node, to_underlying(OpIdx::DynIndexMask));
293 if (!DynIndexMask)
294 return DynIndexMask.takeError();
295 if (*DynIndexMask > 0xF)
296 return makeError("dynamic index mask must be a 4-bit value");
298
300 extractInt(Node, to_underlying(OpIdx::GSStream));
301 if (!GSStream)
302 return GSStream.takeError();
303 if (*GSStream > 3)
304 return makeError("geometry shader stream index must be within 0-3");
305 Elem.GSStream = *GSStream;
306
307 if (Elem.SemanticIndices.size() != Elem.Rows)
308 return makeError(
309 "number of semantic indices must equal the number of rows");
310
311 return Elem;
312}
313
315 Type *I32Ty = Type::getInt32Ty(Ctx);
316 Type *I8Ty = Type::getInt8Ty(Ctx);
317 auto GetI32 = [&](uint32_t Val) -> Metadata * {
318 return ConstantAsMetadata::get(ConstantInt::get(I32Ty, Val));
319 };
320 auto GetI8 = [&](uint8_t Val) -> Metadata * {
321 return ConstantAsMetadata::get(ConstantInt::get(I8Ty, Val));
322 };
323
325 for (uint32_t Index : SemanticIndices)
326 IndexOps.push_back(GetI32(Index));
327
328 return MDNode::get(Ctx,
329 {GetI32(SigId), MDString::get(Ctx, SemanticName),
330 GetI32(static_cast<uint32_t>(CompType)),
331 GetI32(static_cast<uint32_t>(SemanticKind)),
332 MDNode::get(Ctx, IndexOps),
333 GetI32(static_cast<uint32_t>(InterpMode)), GetI32(Rows),
334 GetI8(Cols), GetI32(StartRow), GetI8(StartCol),
335 GetI8(UsageMask), GetI8(DynIndexMask), GetI32(GSStream)});
336}
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