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