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
19namespace llvm {
20class MDTuple;
21namespace mcdxbc {
22class Signature;
23struct PSVRuntimeInfo;
24} // namespace mcdxbc
25
26namespace dxil {
27
28struct 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 /// the view ID output mask if it is present, and stage-specific signature
49 /// flags. Does not finalize or serialize PSV.
50 void updatePSV(mcdxbc::PSVRuntimeInfo &PSV, VersionTuple ValidatorVersion,
51 bool UsesViewID) const;
52
53 /// Return the serialized dx.viewIdState words for this entry: input scalar
54 /// count, output scalar count, the outputs that depend on the view ID if
55 /// it is present, then the input/output dependency table. Scalar counts
56 /// include padding and equal four times the respective vector extent. Each
57 /// output mask has ceil(output scalar count / 32) consecutive words, where
58 /// bit (4 * row + column) identifies an output scalar. There is one such
59 /// mask for each input scalar in the dependency table.
60 SmallVector<uint32_t> getDependencyState(bool UsesViewID) const;
61
62 /// Return the mask of output scalars that depend on the view ID.
63 SmallVector<uint32_t> getViewIDOutputMask() const;
64
65 void print(raw_ostream &OS) const;
66};
67
68class ModuleSignatureInfo {
69public:
70 ModuleSignatureInfo() = default;
71 ModuleSignatureInfo(const ModuleSignatureInfo &) = delete;
72 ModuleSignatureInfo &operator=(const ModuleSignatureInfo &) = delete;
73 ModuleSignatureInfo(ModuleSignatureInfo &&) = default;
74 ModuleSignatureInfo &operator=(ModuleSignatureInfo &&) = default;
75
76 DenseMap<const Function *, EntrySignature> Entries;
77 StringSet<> Names;
78
79 const EntrySignature *get(const Function *F) const {
80 auto It = Entries.find(Val: F);
81 return It == Entries.end() ? nullptr : &It->second;
82 }
83 void print(raw_ostream &OS, const Module &M) const;
84};
85
86class SignatureAnalysis : public AnalysisInfoMixin<SignatureAnalysis> {
87 friend AnalysisInfoMixin<SignatureAnalysis>;
88 static AnalysisKey Key;
89
90public:
91 using Result = ModuleSignatureInfo;
92 Result run(Module &M, ModuleAnalysisManager &AM);
93};
94
95class SignatureAnalysisWrapper : public ModulePass {
96 std::unique_ptr<ModuleSignatureInfo> Info;
97
98public:
99 static char ID;
100 SignatureAnalysisWrapper() : ModulePass(ID) {}
101 const ModuleSignatureInfo &getSignatureInfo() const { return *Info; }
102 bool runOnModule(Module &M) override;
103 void getAnalysisUsage(AnalysisUsage &AU) const override;
104 void releaseMemory() override { Info.reset(); }
105 void print(raw_ostream &OS, const Module *M) const override;
106};
107
108class SignatureAnalysisPrinterPass
109 : public OptionalPassInfoMixin<SignatureAnalysisPrinterPass> {
110 raw_ostream &OS;
111
112public:
113 explicit SignatureAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
114 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
115};
116
117} // namespace dxil
118} // namespace llvm
119
120#endif
121