| 1 | //===- DXILSignatureAnalysis.h - DXIL semantic signatures -------*- C++ -*-===// |
| 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 | #ifndef LLVM_LIB_TARGET_DIRECTX_DXILSIGNATUREANALYSIS_H |
| 10 | #define LLVM_LIB_TARGET_DIRECTX_DXILSIGNATUREANALYSIS_H |
| 11 | |
| 12 | #include "llvm/ADT/DenseMap.h" |
| 13 | #include "llvm/ADT/StringSet.h" |
| 14 | #include "llvm/Frontend/HLSL/SemanticSignatures.h" |
| 15 | #include "llvm/IR/PassManager.h" |
| 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Support/VersionTuple.h" |
| 18 | |
| 19 | namespace llvm { |
| 20 | class MDTuple; |
| 21 | namespace mcdxbc { |
| 22 | class Signature; |
| 23 | struct PSVRuntimeInfo; |
| 24 | } // namespace mcdxbc |
| 25 | |
| 26 | namespace dxil { |
| 27 | |
| 28 | struct EntrySignature { |
| 29 | Triple::EnvironmentType Stage; |
| 30 | SmallVector<hlsl::SemanticSignatureElement> Inputs; |
| 31 | SmallVector<hlsl::SemanticSignatureElement> Outputs; |
| 32 | unsigned InputVectors = 0; |
| 33 | unsigned OutputVectors = 0; |
| 34 | SmallVector<uint32_t> InputOutputMap; |
| 35 | bool UseNative16Bit = false; |
| 36 | |
| 37 | /// Return the DXIL input/output/patch-constant signature triple. Empty input |
| 38 | /// and output lists become null operands; return null if both lists are |
| 39 | /// empty. ValidatorVersion controls which access-mask properties are emitted. |
| 40 | MDTuple *getAsMetadata(LLVMContext &Ctx, VersionTuple ValidatorVersion) const; |
| 41 | |
| 42 | /// Append ISG1/OSG1 parameters to the supplied builders. ValidatorVersion |
| 43 | /// selects legacy component-type and exclusive-mask encoding conventions. |
| 44 | void buildSignatures(mcdxbc::Signature &Input, mcdxbc::Signature &Output, |
| 45 | VersionTuple ValidatorVersion) const; |
| 46 | |
| 47 | /// Append PSV signature elements and set vector extents, the dependency map, |
| 48 | /// and stage-specific signature flags. Does not finalize or serialize PSV. |
| 49 | void updatePSV(mcdxbc::PSVRuntimeInfo &PSV, |
| 50 | VersionTuple ValidatorVersion) const; |
| 51 | |
| 52 | /// Return the serialized dx.viewIdState words for this entry: input scalar |
| 53 | /// count, output scalar count, then the input/output dependency table. Scalar |
| 54 | /// counts include padding and equal four times the respective vector extent. |
| 55 | /// Each input scalar has ceil(output scalar count / 32) consecutive words, |
| 56 | /// where bit (4 * row + column) identifies an output scalar. |
| 57 | SmallVector<uint32_t> getDependencyState() const; |
| 58 | void print(raw_ostream &OS) const; |
| 59 | }; |
| 60 | |
| 61 | class ModuleSignatureInfo { |
| 62 | public: |
| 63 | ModuleSignatureInfo() = default; |
| 64 | ModuleSignatureInfo(const ModuleSignatureInfo &) = delete; |
| 65 | ModuleSignatureInfo &operator=(const ModuleSignatureInfo &) = delete; |
| 66 | ModuleSignatureInfo(ModuleSignatureInfo &&) = default; |
| 67 | ModuleSignatureInfo &operator=(ModuleSignatureInfo &&) = default; |
| 68 | |
| 69 | DenseMap<const Function *, EntrySignature> Entries; |
| 70 | StringSet<> Names; |
| 71 | |
| 72 | const EntrySignature *get(const Function *F) const { |
| 73 | auto It = Entries.find(Val: F); |
| 74 | return It == Entries.end() ? nullptr : &It->second; |
| 75 | } |
| 76 | void print(raw_ostream &OS, const Module &M) const; |
| 77 | }; |
| 78 | |
| 79 | class SignatureAnalysis : public AnalysisInfoMixin<SignatureAnalysis> { |
| 80 | friend AnalysisInfoMixin<SignatureAnalysis>; |
| 81 | static AnalysisKey Key; |
| 82 | |
| 83 | public: |
| 84 | using Result = ModuleSignatureInfo; |
| 85 | Result run(Module &M, ModuleAnalysisManager &AM); |
| 86 | }; |
| 87 | |
| 88 | class SignatureAnalysisWrapper : public ModulePass { |
| 89 | std::unique_ptr<ModuleSignatureInfo> Info; |
| 90 | |
| 91 | public: |
| 92 | static char ID; |
| 93 | SignatureAnalysisWrapper() : ModulePass(ID) {} |
| 94 | const ModuleSignatureInfo &getSignatureInfo() const { return *Info; } |
| 95 | bool runOnModule(Module &M) override; |
| 96 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 97 | void releaseMemory() override { Info.reset(); } |
| 98 | void print(raw_ostream &OS, const Module *M) const override; |
| 99 | }; |
| 100 | |
| 101 | class SignatureAnalysisPrinter |
| 102 | : public OptionalPassInfoMixin<SignatureAnalysisPrinter> { |
| 103 | raw_ostream &OS; |
| 104 | |
| 105 | public: |
| 106 | explicit SignatureAnalysisPrinter(raw_ostream &OS) : OS(OS) {} |
| 107 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); |
| 108 | }; |
| 109 | |
| 110 | } // namespace dxil |
| 111 | } // namespace llvm |
| 112 | |
| 113 | #endif |
| 114 | |