1//===- DXILShaderFlags.cpp - DXIL Shader Flags helper objects -------------===//
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 contains helper objects and APIs for working with DXIL
10/// Shader Flags.
11///
12//===----------------------------------------------------------------------===//
13
14#include "DXILShaderFlags.h"
15#include "DXILSignatureAnalysis.h"
16#include "DirectX.h"
17#include "llvm/ADT/SCCIterator.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/Analysis/CallGraph.h"
20#include "llvm/Analysis/DXILResource.h"
21#include "llvm/IR/Attributes.h"
22#include "llvm/IR/DiagnosticInfo.h"
23#include "llvm/IR/Instruction.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/IntrinsicsDirectX.h"
28#include "llvm/IR/Module.h"
29#include "llvm/InitializePasses.h"
30#include "llvm/Support/FormatVariadic.h"
31#include "llvm/Support/raw_ostream.h"
32
33using namespace llvm;
34using namespace llvm::dxil;
35
36static bool hasUAVsAtEveryStage(const DXILResourceMap &DRM,
37 const ModuleMetadataInfo &MMDI) {
38 // Heap resources do not count towards hasUAVsAtEveryStage.
39 bool HasUAVWithBinding = any_of(
40 Range: DRM.uavs(), P: [](const ResourceInfo &RI) { return RI.hasBinding(); });
41 if (!HasUAVWithBinding)
42 return false;
43
44 switch (MMDI.ShaderProfile) {
45 default:
46 return false;
47 case Triple::EnvironmentType::Compute:
48 case Triple::EnvironmentType::Pixel:
49 return false;
50 case Triple::EnvironmentType::Vertex:
51 case Triple::EnvironmentType::Geometry:
52 case Triple::EnvironmentType::Hull:
53 case Triple::EnvironmentType::Domain:
54 return true;
55 case Triple::EnvironmentType::Library:
56 case Triple::EnvironmentType::RayGeneration:
57 case Triple::EnvironmentType::Intersection:
58 case Triple::EnvironmentType::AnyHit:
59 case Triple::EnvironmentType::ClosestHit:
60 case Triple::EnvironmentType::Miss:
61 case Triple::EnvironmentType::Callable:
62 case Triple::EnvironmentType::Mesh:
63 case Triple::EnvironmentType::Amplification:
64 return MMDI.ValidatorVersion < VersionTuple(1, 8);
65 }
66}
67
68static bool checkWaveOps(Intrinsic::ID IID) {
69 // Currently unsupported intrinsics
70 // case Intrinsic::dx_wave_readfirst:
71 // case Intrinsic::dx_wave_reduce.and:
72 // case Intrinsic::dx_wave_reduce.or:
73 // case Intrinsic::dx_wave_reduce.xor:
74 // case Intrinsic::dx_wave_prefixop:
75 // case Intrinsic::dx_quad.readat:
76 // case Intrinsic::dx_quad.readacrossy:
77 // case Intrinsic::dx_quad.readacrossdiagonal:
78 // case Intrinsic::dx_wave_prefixballot:
79 // case Intrinsic::dx_wave_match:
80 // case Intrinsic::dx_wavemulti.*:
81 // case Intrinsic::dx_wavemulti.ballot:
82 switch (IID) {
83 default:
84 return false;
85 case Intrinsic::dx_wave_is_first_lane:
86 case Intrinsic::dx_wave_getlaneindex:
87 case Intrinsic::dx_wave_get_lane_count:
88 case Intrinsic::dx_wave_any:
89 case Intrinsic::dx_wave_all_equal:
90 case Intrinsic::dx_wave_all:
91 case Intrinsic::dx_wave_readlane:
92 case Intrinsic::dx_wave_readlane_first:
93 case Intrinsic::dx_wave_active_countbits:
94 case Intrinsic::dx_wave_ballot:
95 case Intrinsic::dx_wave_prefix_bit_count:
96 // Wave Active Op Variants
97 case Intrinsic::dx_wave_reduce_or:
98 case Intrinsic::dx_wave_reduce_xor:
99 case Intrinsic::dx_wave_reduce_and:
100 case Intrinsic::dx_wave_reduce_sum:
101 case Intrinsic::dx_wave_reduce_usum:
102 case Intrinsic::dx_wave_product:
103 case Intrinsic::dx_wave_uproduct:
104 case Intrinsic::dx_wave_reduce_max:
105 case Intrinsic::dx_wave_reduce_umax:
106 case Intrinsic::dx_wave_reduce_min:
107 case Intrinsic::dx_wave_reduce_umin:
108 // Wave Prefix Op Variants
109 case Intrinsic::dx_wave_prefix_sum:
110 case Intrinsic::dx_wave_prefix_usum:
111 case Intrinsic::dx_wave_prefix_product:
112 case Intrinsic::dx_wave_prefix_uproduct:
113 case Intrinsic::dx_wave_multi_prefix_bit_and:
114 case Intrinsic::dx_wave_multi_prefix_bit_or:
115 case Intrinsic::dx_wave_multi_prefix_bit_xor:
116 case Intrinsic::dx_wave_multi_prefix_bit_count:
117 case Intrinsic::dx_wave_multi_prefix_product:
118 case Intrinsic::dx_wave_multi_prefix_uproduct:
119 case Intrinsic::dx_wave_multi_prefix_sum:
120 case Intrinsic::dx_wave_multi_prefix_usum:
121 // Quad Op Variants
122 case Intrinsic::dx_quad_all:
123 case Intrinsic::dx_quad_read_across_x:
124 case Intrinsic::dx_quad_read_across_y:
125 case Intrinsic::dx_quad_read_across_diagonal:
126 return true;
127 }
128}
129
130static bool checkDoubleExtensionOps(Intrinsic::ID IID) {
131 switch (IID) {
132 default:
133 return false;
134 case Intrinsic::fma:
135 return true;
136 }
137}
138
139/// Texture load and sample operations accept "programmable offsets", i.e.
140/// offsets that are not compile-time constants. Such offsets require the
141/// AdvancedTextureOps shader feature flag. Returns true if \p II is one of
142/// those operations and its offsets operand is not a constant.
143static bool checkAdvancedTextureOps(const IntrinsicInst &II) {
144 // TODO: (#116137) Several other DXIL ops also require this feature flag, but
145 // none of them can be generated yet:
146 // - SampleCmp, SampleCmpBias, SampleCmpGrad and SampleCmpLevelZero set the
147 // flag for non-constant offsets, exactly like the ops handled below.
148 // - SampleCmpLevel, TextureGatherRaw and TextureStoreSample set the flag
149 // unconditionally, and have no intrinsics yet.
150
151 // The offsets operand index differs between the intrinsics.
152 unsigned OffsetsIdx;
153 switch (II.getIntrinsicID()) {
154 default:
155 return false;
156 case Intrinsic::dx_resource_load_level:
157 case Intrinsic::dx_resource_sample:
158 case Intrinsic::dx_resource_sample_clamp:
159 OffsetsIdx = 3;
160 break;
161 case Intrinsic::dx_resource_samplebias:
162 case Intrinsic::dx_resource_samplebias_clamp:
163 case Intrinsic::dx_resource_samplelevel:
164 OffsetsIdx = 4;
165 break;
166 case Intrinsic::dx_resource_samplegrad:
167 case Intrinsic::dx_resource_samplegrad_clamp:
168 OffsetsIdx = 5;
169 break;
170 }
171 return !isa<Constant>(Val: II.getArgOperand(i: OffsetsIdx));
172}
173
174static bool isOptimizationDisabled(const Module &M) {
175 const StringRef Key = "dx.disable_optimizations";
176 if (auto *Flag = mdconst::extract_or_null<ConstantInt>(MD: M.getModuleFlag(Key)))
177 return Flag->getValue().getBoolValue();
178 return false;
179}
180
181// Checks to see if the status bit from a load with status
182// instruction is ever extracted. If it is, the module needs
183// to have the TiledResources shader flag set.
184bool checkIfStatusIsExtracted(const IntrinsicInst &II) {
185 [[maybe_unused]] Intrinsic::ID IID = II.getIntrinsicID();
186 assert(IID == Intrinsic::dx_resource_load_typedbuffer ||
187 IID == Intrinsic::dx_resource_load_rawbuffer &&
188 "unexpected intrinsic ID");
189 for (const User *U : II.users()) {
190 if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(Val: U)) {
191 // Resource load operations return a {result, status} pair.
192 // Check if we extract the status
193 if (EVI->getNumIndices() == 1 && EVI->getIndices()[0] == 1)
194 return true;
195 }
196 }
197
198 return false;
199}
200
201/// Update the shader flags mask based on the given instruction.
202/// \param CSF Shader flags mask to update.
203/// \param I Instruction to check.
204void ModuleShaderFlags::updateFunctionFlags(ComputedShaderFlags &CSF,
205 const Instruction &I,
206 DXILResourceTypeMap &DRTM,
207 const ModuleMetadataInfo &MMDI) {
208 if (!CSF.Doubles)
209 CSF.Doubles = I.getType()->getScalarType()->isDoubleTy();
210
211 if (!CSF.Doubles) {
212 for (const Value *Op : I.operands()) {
213 if (Op->getType()->getScalarType()->isDoubleTy()) {
214 CSF.Doubles = true;
215 break;
216 }
217 }
218 }
219
220 if (CSF.Doubles) {
221 switch (I.getOpcode()) {
222 case Instruction::FDiv:
223 case Instruction::UIToFP:
224 case Instruction::SIToFP:
225 case Instruction::FPToUI:
226 case Instruction::FPToSI:
227 CSF.DX11_1_DoubleExtensions = true;
228 break;
229 }
230 }
231
232 if (!CSF.LowPrecisionPresent)
233 CSF.LowPrecisionPresent = I.getType()->getScalarType()->isIntegerTy(BitWidth: 16) ||
234 I.getType()->getScalarType()->isHalfTy();
235
236 if (!CSF.LowPrecisionPresent) {
237 for (const Value *Op : I.operands()) {
238 if (Op->getType()->getScalarType()->isIntegerTy(BitWidth: 16) ||
239 Op->getType()->getScalarType()->isHalfTy()) {
240 CSF.LowPrecisionPresent = true;
241 break;
242 }
243 }
244 }
245
246 if (CSF.LowPrecisionPresent) {
247 if (CSF.NativeLowPrecisionMode)
248 CSF.NativeLowPrecision = true;
249 else
250 CSF.MinimumPrecision = true;
251 }
252
253 if (!CSF.Int64Ops)
254 CSF.Int64Ops = I.getType()->getScalarType()->isIntegerTy(BitWidth: 64);
255
256 if (!CSF.Int64Ops && !isa<LifetimeIntrinsic>(Val: &I)) {
257 for (const Value *Op : I.operands()) {
258 if (Op->getType()->getScalarType()->isIntegerTy(BitWidth: 64)) {
259 CSF.Int64Ops = true;
260 break;
261 }
262 }
263 }
264
265 if (const auto *II = dyn_cast<IntrinsicInst>(Val: &I)) {
266 CSF.AdvancedTextureOps |= checkAdvancedTextureOps(II: *II);
267
268 switch (II->getIntrinsicID()) {
269 default:
270 break;
271 case Intrinsic::dx_start_vertex_location:
272 case Intrinsic::dx_start_instance_location:
273 CSF.ExtendedCommandInfo = true;
274 break;
275 case Intrinsic::dx_view_id:
276 CSF.ViewID = true;
277 break;
278 case Intrinsic::dx_resource_handlefrombinding: {
279 dxil::ResourceTypeInfo &RTI = DRTM[cast<TargetExtType>(Val: II->getType())];
280
281 // Set ResMayNotAlias if DXIL validator version >= 1.8 and the function
282 // uses UAVs
283 if (!CSF.ResMayNotAlias && CanSetResMayNotAlias &&
284 MMDI.ValidatorVersion >= VersionTuple(1, 8) && RTI.isUAV())
285 CSF.ResMayNotAlias = true;
286
287 switch (RTI.getResourceKind()) {
288 case dxil::ResourceKind::StructuredBuffer:
289 case dxil::ResourceKind::RawBuffer:
290 CSF.EnableRawAndStructuredBuffers = true;
291 break;
292 default:
293 break;
294 }
295 break;
296 }
297 case Intrinsic::dx_resource_handlefromheap: {
298 dxil::ResourceTypeInfo &RTI = DRTM[cast<TargetExtType>(Val: II->getType())];
299 bool IsSamplerHeap = RTI.isSampler();
300 CSF.SamplerDescriptorHeapIndexing |= IsSamplerHeap;
301 CSF.ResourceDescriptorHeapIndexing |= !IsSamplerHeap;
302
303 if (!CSF.ResMayNotAlias && CanSetResMayNotAlias && RTI.isUAV() &&
304 MMDI.ValidatorVersion >= VersionTuple(1, 8)) {
305 CSF.ResMayNotAlias = true;
306 }
307 break;
308 }
309 case Intrinsic::dx_resource_load_level:
310 case Intrinsic::dx_resource_load_typedbuffer: {
311 dxil::ResourceTypeInfo &RTI =
312 DRTM[cast<TargetExtType>(Val: II->getArgOperand(i: 0)->getType())];
313 if (RTI.isTyped() && RTI.isUAV())
314 CSF.TypedUAVLoadAdditionalFormats |= RTI.getTyped().ElementCount > 1;
315 if (II->getIntrinsicID() == Intrinsic::dx_resource_load_typedbuffer &&
316 !CSF.TiledResources && checkIfStatusIsExtracted(II: *II))
317 CSF.TiledResources = true;
318 break;
319 }
320 case Intrinsic::dx_resource_load_rawbuffer: {
321 if (!CSF.TiledResources && checkIfStatusIsExtracted(II: *II))
322 CSF.TiledResources = true;
323 break;
324 }
325 case Intrinsic::dx_resource_atomic_binop:
326 case Intrinsic::dx_resource_atomic_compare_exchange: {
327 if (II->getType()->isIntegerTy(BitWidth: 64)) {
328 dxil::ResourceTypeInfo &RTI =
329 DRTM[cast<TargetExtType>(Val: II->getArgOperand(i: 0)->getType())];
330 if (RTI.isTyped())
331 CSF.AtomicInt64OnTypedResource = true;
332 // TODO(https://github.com/llvm/llvm-project/issues/116152): Set
333 // AtomicInt64OnHeapResource when heap-resource intrinsics are added.
334 }
335 break;
336 }
337 }
338 }
339 // 64-bit atomics on groupshared memory (address space 3).
340 if (const auto *ARMW = dyn_cast<AtomicRMWInst>(Val: &I)) {
341 if (ARMW->getValOperand()->getType()->isIntegerTy(BitWidth: 64) &&
342 ARMW->getPointerAddressSpace() == 3)
343 CSF.AtomicInt64OnGroupShared = true;
344 } else if (const auto *AXCG = dyn_cast<AtomicCmpXchgInst>(Val: &I)) {
345 if (AXCG->getNewValOperand()->getType()->isIntegerTy(BitWidth: 64) &&
346 AXCG->getPointerAddressSpace() == 3)
347 CSF.AtomicInt64OnGroupShared = true;
348 }
349 // Handle call instructions
350 if (auto *CI = dyn_cast<CallInst>(Val: &I)) {
351 const Function *CF = CI->getCalledFunction();
352 // Merge-in shader flags mask of the called function in the current module
353 if (FunctionFlags.contains(Val: CF))
354 CSF.merge(CSF: FunctionFlags[CF]);
355
356 CSF.DX11_1_DoubleExtensions |=
357 checkDoubleExtensionOps(IID: CI->getIntrinsicID());
358 CSF.WaveOps |= checkWaveOps(IID: CI->getIntrinsicID());
359 }
360}
361
362/// Set shader flags that apply to all functions within the module
363ComputedShaderFlags
364ModuleShaderFlags::gatherGlobalModuleFlags(const Module &M,
365 const DXILResourceMap &DRM,
366 const ModuleMetadataInfo &MMDI) {
367
368 ComputedShaderFlags CSF;
369
370 CSF.DisableOptimizations = isOptimizationDisabled(M);
371
372 CSF.UAVsAtEveryStage = hasUAVsAtEveryStage(DRM, MMDI);
373
374 // Set the Max64UAVs flag if the number of UAVs is > 8
375 uint32_t NumUAVs = 0;
376 for (auto &UAV : DRM.uavs()) {
377 // Heap resources do not count towards Max64UAVs flag.
378 if (!UAV.hasBinding())
379 continue;
380 if (MMDI.ValidatorVersion < VersionTuple(1, 6)) {
381 NumUAVs++;
382 } else { // MMDI.ValidatorVersion >= VersionTuple(1, 6)
383 uint32_t Size = UAV.getSize();
384 uint32_t NewNum = NumUAVs + (Size == 0 ? ~0U : Size);
385 if (NewNum < NumUAVs)
386 NewNum = ~0U;
387 NumUAVs = NewNum;
388 }
389 }
390 if (NumUAVs > 8)
391 CSF.Max64UAVs = true;
392
393 // Set the module flag that enables native low-precision execution mode.
394 // NativeLowPrecisionMode can only be set when the command line option
395 // -enable-16bit-types is provided. This is indicated by the dx.nativelowprec
396 // module flag being set
397 // This flag is needed even if the module does not use 16-bit types because a
398 // corresponding debug module may include 16-bit types, and tools that use the
399 // debug module may expect it to have the same flags as the original
400 if (auto *NativeLowPrec = mdconst::extract_or_null<ConstantInt>(
401 MD: M.getModuleFlag(Key: "dx.nativelowprec")))
402 if (MMDI.ShaderModelVersion >= VersionTuple(6, 2))
403 CSF.NativeLowPrecisionMode = NativeLowPrec->getValue().getBoolValue();
404
405 // Set ResMayNotAlias to true if DXIL validator version < 1.8 and there
406 // are UAVs present globally.
407 if (CanSetResMayNotAlias && MMDI.ValidatorVersion < VersionTuple(1, 8))
408 CSF.ResMayNotAlias = !DRM.uavs().empty();
409
410 // The command line option -all-resources-bound will set the
411 // dx.allresourcesbound module flag to 1
412 if (auto *AllResourcesBound = mdconst::extract_or_null<ConstantInt>(
413 MD: M.getModuleFlag(Key: "dx.allresourcesbound")))
414 if (AllResourcesBound->getValue().getBoolValue())
415 CSF.AllResourcesBound = true;
416
417 return CSF;
418}
419
420/// Construct ModuleShaderFlags for module Module M
421void ModuleShaderFlags::initialize(Module &M, DXILResourceTypeMap &DRTM,
422 const DXILResourceMap &DRM,
423 const ModuleMetadataInfo &MMDI,
424 const ModuleSignatureInfo &Signatures) {
425
426 CanSetResMayNotAlias = MMDI.DXILVersion >= VersionTuple(1, 7);
427 // The command line option -res-may-alias will set the dx.resmayalias module
428 // flag to 1, thereby disabling the ability to set the ResMayNotAlias flag
429 if (auto *ResMayAlias = mdconst::extract_or_null<ConstantInt>(
430 MD: M.getModuleFlag(Key: "dx.resmayalias")))
431 if (ResMayAlias->getValue().getBoolValue())
432 CanSetResMayNotAlias = false;
433
434 ComputedShaderFlags GlobalSFMask = gatherGlobalModuleFlags(M, DRM, MMDI);
435
436 CallGraph CG(M);
437
438 // Compute Shader Flags Mask for all functions using post-order visit of SCC
439 // of the call graph.
440 for (scc_iterator<CallGraph *> SCCI = scc_begin(G: &CG); !SCCI.isAtEnd();
441 ++SCCI) {
442 const std::vector<CallGraphNode *> &CurSCC = *SCCI;
443
444 // Union of shader masks of all functions in CurSCC
445 ComputedShaderFlags SCCSF;
446 // List of functions in CurSCC that are neither external nor declarations
447 // and hence whose flags are collected
448 SmallVector<Function *> CurSCCFuncs;
449 for (CallGraphNode *CGN : CurSCC) {
450 Function *F = CGN->getFunction();
451 if (!F)
452 continue;
453
454 if (F->isDeclaration()) {
455 assert(!F->getName().starts_with("dx.op.") &&
456 "DXIL Shader Flag analysis should not be run post-lowering.");
457 continue;
458 }
459
460 ComputedShaderFlags CSF = GlobalSFMask;
461 for (const auto &BB : *F)
462 for (const auto &I : BB)
463 updateFunctionFlags(CSF, I, DRTM, MMDI);
464 // An unused signature element still contributes to the shader's type
465 // requirements even when no instruction uses that component type.
466 if (const EntrySignature *Sig = Signatures.get(F)) {
467 CSF.Barycentrics |= llvm::any_of(Range: Sig->Inputs, P: [](const auto &E) {
468 return E.SemanticKind == dxbc::PSV::SemanticKind::Barycentrics;
469 });
470 // Unallocated elements do not occupy signature registers. Their type
471 // requirements come from the instructions that access them.
472 auto HasLowPrecision = [](const auto &Elements) {
473 return llvm::any_of(Elements, [](const auto &E) {
474 return E.isAllocated() && (E.CompType == ElementType::F16 ||
475 E.CompType == ElementType::I16 ||
476 E.CompType == ElementType::U16);
477 });
478 };
479 if (HasLowPrecision(Sig->Inputs) || HasLowPrecision(Sig->Outputs)) {
480 CSF.LowPrecisionPresent = true;
481 if (Sig->UseNative16Bit)
482 CSF.NativeLowPrecision = true;
483 else
484 CSF.MinimumPrecision = true;
485 }
486 // Declaring SV_ShadingRate in either signature requires the feature,
487 // regardless of whether it is accessed.
488 auto HasShadingRate = [](const auto &Elements) {
489 return llvm::any_of(Elements, [](const auto &E) {
490 return E.SemanticKind == dxbc::PSV::SemanticKind::ShadingRate;
491 });
492 };
493 if (HasShadingRate(Sig->Inputs) || HasShadingRate(Sig->Outputs))
494 CSF.ShadingRate = true;
495 }
496 // Update combined shader flags mask for all functions in this SCC
497 SCCSF.merge(CSF);
498
499 CurSCCFuncs.push_back(Elt: F);
500 }
501
502 // Update combined shader flags mask for all functions of the module
503 CombinedSFMask.merge(CSF: SCCSF);
504
505 // Shader flags mask of each of the functions in an SCC of the call graph is
506 // the union of all functions in the SCC. Update shader flags masks of
507 // functions in CurSCC accordingly. This is trivially true if SCC contains
508 // one function.
509 for (Function *F : CurSCCFuncs)
510 // Merge SCCSF with that of F
511 FunctionFlags[F].merge(CSF: SCCSF);
512 }
513}
514
515void ComputedShaderFlags::print(raw_ostream &OS) const {
516 uint64_t FlagVal = (uint64_t) * this;
517 OS << formatv(Fmt: "; Shader Flags Value: {0:x8}\n;\n", Vals&: FlagVal);
518 if (FlagVal == 0)
519 return;
520 OS << "; Note: shader requires additional functionality:\n";
521#define SHADER_FEATURE_FLAG(FeatureBit, DxilModuleNum, FlagName, Str) \
522 if (FlagName) \
523 (OS << ";").indent(7) << Str << "\n";
524#include "llvm/BinaryFormat/DXContainerConstants.def"
525 OS << "; Note: extra DXIL module flags:\n";
526#define DXIL_MODULE_FLAG(DxilModuleBit, FlagName, Str) \
527 if (FlagName) \
528 (OS << ";").indent(7) << Str << "\n";
529#include "llvm/BinaryFormat/DXContainerConstants.def"
530 OS << ";\n";
531}
532
533/// Return the shader flags mask of the specified function Func.
534const ComputedShaderFlags &
535ModuleShaderFlags::getFunctionFlags(const Function *Func) const {
536 auto Iter = FunctionFlags.find(Val: Func);
537 assert((Iter != FunctionFlags.end() && Iter->first == Func) &&
538 "Get Shader Flags : No Shader Flags Mask exists for function");
539 return Iter->second;
540}
541
542//===----------------------------------------------------------------------===//
543// ShaderFlagsAnalysis and ShaderFlagsAnalysisPrinterPass
544
545// Provide an explicit template instantiation for the static ID.
546AnalysisKey ShaderFlagsAnalysis::Key;
547
548ModuleShaderFlags ShaderFlagsAnalysis::run(Module &M,
549 ModuleAnalysisManager &AM) {
550 DXILResourceTypeMap &DRTM = AM.getResult<DXILResourceTypeAnalysis>(IR&: M);
551 DXILResourceMap &DRM = AM.getResult<DXILResourceAnalysis>(IR&: M);
552 const ModuleMetadataInfo MMDI = AM.getResult<DXILMetadataAnalysis>(IR&: M);
553
554 ModuleShaderFlags MSFI;
555 MSFI.initialize(M, DRTM, DRM, MMDI, Signatures: AM.getResult<SignatureAnalysis>(IR&: M));
556
557 return MSFI;
558}
559
560PreservedAnalyses
561ShaderFlagsAnalysisPrinterPass::run(Module &M, ModuleAnalysisManager &AM) {
562 const ModuleShaderFlags &FlagsInfo = AM.getResult<ShaderFlagsAnalysis>(IR&: M);
563 // Print description of combined shader flags for all module functions
564 OS << "; Combined Shader Flags for Module\n";
565 FlagsInfo.getCombinedFlags().print(OS);
566 // Print shader flags mask for each of the module functions
567 OS << "; Shader Flags for Module Functions\n";
568 for (const auto &F : M.getFunctionList()) {
569 if (F.isDeclaration())
570 continue;
571 const ComputedShaderFlags &SFMask = FlagsInfo.getFunctionFlags(Func: &F);
572 OS << formatv(Fmt: "; Function {0} : {1:x8}\n;\n", Vals: F.getName(),
573 Vals: (uint64_t)(SFMask));
574 }
575
576 return PreservedAnalyses::all();
577}
578
579//===----------------------------------------------------------------------===//
580// ShaderFlagsAnalysis and ShaderFlagsAnalysisPrinterPass
581
582bool ShaderFlagsAnalysisWrapper::runOnModule(Module &M) {
583 DXILResourceTypeMap &DRTM =
584 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
585 DXILResourceMap &DRM =
586 getAnalysis<DXILResourceWrapperPass>().getResourceMap();
587 const ModuleMetadataInfo MMDI =
588 getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
589
590 MSFI.initialize(M, DRTM, DRM, MMDI,
591 Signatures: getAnalysis<SignatureAnalysisWrapper>().getSignatureInfo());
592 return false;
593}
594
595void ShaderFlagsAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const {
596 AU.setPreservesAll();
597 AU.addRequiredTransitive<DXILResourceTypeWrapperPass>();
598 AU.addRequiredTransitive<DXILResourceWrapperPass>();
599 AU.addRequired<DXILMetadataAnalysisWrapperPass>();
600 AU.addRequired<SignatureAnalysisWrapper>();
601}
602
603char ShaderFlagsAnalysisWrapper::ID = 0;
604
605INITIALIZE_PASS_BEGIN(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
606 "DXIL Shader Flag Analysis", true, true)
607INITIALIZE_PASS_DEPENDENCY(DXILResourceTypeWrapperPass)
608INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
609INITIALIZE_PASS_DEPENDENCY(SignatureAnalysisWrapper)
610INITIALIZE_PASS_END(ShaderFlagsAnalysisWrapper, "dx-shader-flag-analysis",
611 "DXIL Shader Flag Analysis", true, true)
612