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 <algorithm>
23#include <cassert>
24
25using namespace llvm;
26using namespace llvm::hlsl;
28
29namespace {
30
31// Inclusive upper bounds of the operand enums
32constexpr uint32_t MaxCompType =
34constexpr uint32_t MaxSemanticKind =
35 static_cast<uint32_t>(dxbc::PSV::SemanticKind::Invalid);
36constexpr uint32_t MaxInterpMode =
37 static_cast<uint32_t>(dxbc::PSV::InterpolationMode::Invalid);
38
39Error makeError(const Twine &Msg) {
41}
42
43Expected<uint64_t> extractInt(const MDNode *Node, unsigned OpId) {
44 auto *CI = mdconst::dyn_extract_or_null<ConstantInt>(Node->getOperand(OpId));
45 if (!CI)
46 return makeError("expected integer operand " + Twine(OpId));
47 return CI->getZExtValue();
48}
49} // namespace
50
57
60 if (Modifiers == InterpolationModifier::None)
61 return InterpolationMode::Undefined;
62 if (any(Modifiers & InterpolationModifier::NoInterpolation))
64 ? InterpolationMode::Constant
65 : InterpolationMode::Invalid;
66
68 switch (getInterpolationSamplingLocation(Modifiers)) {
70 return NoPerspective ? InterpolationMode::LinearNoperspectiveSample
71 : InterpolationMode::LinearSample;
73 return NoPerspective ? InterpolationMode::LinearNoperspectiveCentroid
74 : InterpolationMode::LinearCentroid;
77 return NoPerspective ? InterpolationMode::LinearNoperspective
78 : InterpolationMode::Linear;
79 default:
80 llvm_unreachable("invalid interpolation sampling location");
81 }
82}
83
87 if (!((Stage == Triple::Pixel && IO == IOType::In) ||
88 (Stage == Triple::Vertex && IO == IOType::Out)))
89 return InterpolationMode::Undefined;
90
91 if (Mode == InterpolationMode::Undefined) {
92 switch (CompType) {
99 Mode = InterpolationMode::Linear;
100 break;
101 default:
102 Mode = InterpolationMode::Constant;
103 break;
104 }
105 }
106
107 if (Kind == dxbc::PSV::SemanticKind::Position) {
108 switch (Mode) {
109 case InterpolationMode::Linear:
110 return InterpolationMode::LinearNoperspective;
111 case InterpolationMode::LinearCentroid:
112 return InterpolationMode::LinearNoperspectiveCentroid;
113 case InterpolationMode::LinearSample:
114 return InterpolationMode::LinearNoperspectiveSample;
115 default:
116 break;
117 }
118 }
119 return Mode;
120}
121
123 if (!SemanticName.consume_front_insensitive("SV_"))
124 return dxbc::PSV::SemanticKind::Arbitrary;
125
126 for (const auto &Kind : dxbc::PSV::getSemanticKinds())
127 if (SemanticName.equals_insensitive(Kind.name()))
128 return Kind.value();
129
130 return dxbc::PSV::SemanticKind::Invalid;
131}
132
135 switch (SemanticKind) {
136 case dxbc::PSV::SemanticKind::Arbitrary: {
137 static constexpr IOType OutOrPatchConstant =
139 static constexpr SemanticStageInfo Stages[] = {
145 {Triple::Mesh, OutOrPatchConstant, SemanticInterpretation::Arbitrary},
146 };
147 return Stages;
148 }
149 case dxbc::PSV::SemanticKind::DispatchThreadID:
150 case dxbc::PSV::SemanticKind::GroupID:
151 case dxbc::PSV::SemanticKind::GroupIndex:
152 case dxbc::PSV::SemanticKind::GroupThreadID: {
153 static constexpr SemanticStageInfo Stages[] = {
158 };
159 return Stages;
160 }
161 case dxbc::PSV::SemanticKind::ViewID: {
162 static constexpr IOType InOrPatchConstant =
164 static constexpr SemanticStageInfo Stages[] = {
167 {Triple::Domain, InOrPatchConstant,
174 };
175 return Stages;
176 }
177 case dxbc::PSV::SemanticKind::Target: {
178 static constexpr SemanticStageInfo Stages[] = {
180 return Stages;
181 }
182 case dxbc::PSV::SemanticKind::VertexID:
183 case dxbc::PSV::SemanticKind::InstanceID: {
184 static constexpr SemanticStageInfo Stages[] = {
186 return Stages;
187 }
188 case dxbc::PSV::SemanticKind::IsFrontFace: {
189 static constexpr SemanticStageInfo Stages[] = {
192 return Stages;
193 }
194 case dxbc::PSV::SemanticKind::Position: {
195 static constexpr SemanticStageInfo Stages[] = {
207 };
208 return Stages;
209 }
210 case dxbc::PSV::SemanticKind::ClipDistance:
211 case dxbc::PSV::SemanticKind::CullDistance: {
212 static constexpr SemanticStageInfo Stages[] = {
224 };
225 return Stages;
226 }
227 case dxbc::PSV::SemanticKind::TessFactor:
228 case dxbc::PSV::SemanticKind::InsideTessFactor: {
229 static constexpr SemanticStageInfo Stages[] = {
234 };
235 return Stages;
236 }
237 default:
238 return {};
239 }
240}
241
244 Triple::EnvironmentType ShaderStage, IOType IOTy) {
245 assert(llvm::has_single_bit(static_cast<unsigned>(IOTy)) &&
246 "a single IOType is expected, not a mask of IOTypes");
247 for (const SemanticStageInfo &Info : getAvailableStages(SemanticKind))
248 if (Info.Stage == ShaderStage && any(Info.AllowedIOTypesMask & IOTy))
249 return Info.Interpretation;
251}
252
255 // Operand positions within a signature element metadata node.
256 enum class OpIdx : unsigned {
257 SigId,
259 CompType,
263 Rows,
264 Cols,
265 StartRow,
266 StartCol,
267 UsageMask,
269 GSStream,
270 LastEntry = GSStream,
271 };
272 const unsigned NumElementOperands = to_underlying(OpIdx::LastEntry) + 1;
273
274 if (!Node)
275 return makeError("signature element node is null");
276 if (Node->getNumOperands() != NumElementOperands)
277 return makeError("signature element node has wrong number of operands");
278
280
281 Expected<uint64_t> SigId = extractInt(Node, to_underlying(OpIdx::SigId));
282 if (!SigId)
283 return SigId.takeError();
284 Elem.SigId = *SigId;
285
286 auto *Name =
287 dyn_cast<MDString>(Node->getOperand(to_underlying(OpIdx::SemanticName)));
288 if (!Name)
289 return makeError("expected semantic name string");
290 Elem.SemanticName = Name->getString();
291
293 extractInt(Node, to_underlying(OpIdx::CompType));
294 if (!CompType)
295 return CompType.takeError();
296 if (*CompType > MaxCompType)
297 return makeError("invalid component type");
298 Elem.CompType = static_cast<dxil::ElementType>(*CompType);
299
301 extractInt(Node, to_underlying(OpIdx::SemanticKind));
302 if (!SemanticKind)
303 return SemanticKind.takeError();
304 if (*SemanticKind > MaxSemanticKind)
305 return makeError("invalid semantic kind");
306 Elem.SemanticKind = static_cast<dxbc::PSV::SemanticKind>(*SemanticKind);
307
308 auto *Indices =
309 dyn_cast<MDNode>(Node->getOperand(to_underlying(OpIdx::SemanticIndices)));
310 if (!Indices)
311 return makeError("expected semantic indices node");
312 for (unsigned I = 0, E = Indices->getNumOperands(); I != E; ++I) {
313 Expected<uint64_t> Index = extractInt(Indices, I);
314 if (!Index)
315 return Index.takeError();
316 Elem.SemanticIndices.push_back(*Index);
317 }
318
320 extractInt(Node, to_underlying(OpIdx::InterpMode));
321 if (!InterpMode)
322 return InterpMode.takeError();
323 if (*InterpMode > MaxInterpMode)
324 return makeError("invalid interpolation mode");
325 Elem.InterpMode = static_cast<dxbc::PSV::InterpolationMode>(*InterpMode);
326
327 Expected<uint64_t> Rows = extractInt(Node, to_underlying(OpIdx::Rows));
328 if (!Rows)
329 return Rows.takeError();
330 Elem.Rows = *Rows;
331
332 Expected<uint64_t> Cols = extractInt(Node, to_underlying(OpIdx::Cols));
333 if (!Cols)
334 return Cols.takeError();
335 if (*Cols < 1 || *Cols > 4)
336 return makeError("number of components per row must be within 1-4");
337 Elem.Cols = *Cols;
338
340 extractInt(Node, to_underlying(OpIdx::StartRow));
341 if (!StartRow)
342 return StartRow.takeError();
343 Elem.StartRow = *StartRow;
344
346 extractInt(Node, to_underlying(OpIdx::StartCol));
347 if (!StartCol)
348 return StartCol.takeError();
349 if (*StartCol > 3 && *StartCol != UnallocatedCol)
350 return makeError("start column must be within 0-3 or unallocated");
351 Elem.StartCol = *StartCol;
352
353 // The row/col sentinels are always set together
354 if ((Elem.StartRow == UnallocatedRow) != (Elem.StartCol == UnallocatedCol))
355 return makeError("start row and column sentinels must be set together");
356
358 extractInt(Node, to_underlying(OpIdx::UsageMask));
359 if (!UsageMask)
360 return UsageMask.takeError();
361 if (*UsageMask > 0xF)
362 return makeError("usage mask must be a 4-bit value");
363 Elem.UsageMask = *UsageMask;
364
366 extractInt(Node, to_underlying(OpIdx::DynIndexMask));
367 if (!DynIndexMask)
368 return DynIndexMask.takeError();
369 if (*DynIndexMask > 0xF)
370 return makeError("dynamic index mask must be a 4-bit value");
372
374 extractInt(Node, to_underlying(OpIdx::GSStream));
375 if (!GSStream)
376 return GSStream.takeError();
377 if (*GSStream > 3)
378 return makeError("geometry shader stream index must be within 0-3");
379 Elem.GSStream = *GSStream;
380
381 if (Elem.SemanticIndices.size() != Elem.Rows)
382 return makeError(
383 "number of semantic indices must equal the number of rows");
384
385 return Elem;
386}
387
389 Type *I32Ty = Type::getInt32Ty(Ctx);
390 Type *I8Ty = Type::getInt8Ty(Ctx);
391 auto GetI32 = [&](uint32_t Val) -> Metadata * {
392 return ConstantAsMetadata::get(ConstantInt::get(I32Ty, Val));
393 };
394 auto GetI8 = [&](uint8_t Val) -> Metadata * {
395 return ConstantAsMetadata::get(ConstantInt::get(I8Ty, Val));
396 };
397
399 for (uint32_t Index : SemanticIndices)
400 IndexOps.push_back(GetI32(Index));
401
402 return MDNode::get(Ctx,
403 {GetI32(SigId), MDString::get(Ctx, SemanticName),
404 GetI32(static_cast<uint32_t>(CompType)),
405 GetI32(static_cast<uint32_t>(SemanticKind)),
406 MDNode::get(Ctx, IndexOps),
407 GetI32(static_cast<uint32_t>(InterpMode)), GetI32(Rows),
408 GetI8(Cols), GetI32(StartRow), GetI8(StartCol),
409 GetI8(UsageMask), GetI8(DynIndexMask), GetI32(GSStream)});
410}
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:548
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:1081
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:597
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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::InterpolationMode getInterpolationMode(InterpolationModifier Modifiers)
LLVM_ABI dxbc::PSV::InterpolationMode normalizeInterpolationMode(dxbc::PSV::InterpolationMode Mode, dxil::ElementType CompType, dxbc::PSV::SemanticKind Kind, Triple::EnvironmentType Stage, IOType IO)
LLVM_ABI InterpolationModifier getInterpolationSamplingLocation(InterpolationModifier Modifiers)
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:720
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