1//===- SPIRVModuleAnalysis.h - analysis of global instrs & regs -*- 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// The analysis collects instructions that should be output at the module level
10// and performs the global register numbering.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVMODULEANALYSIS_H
15#define LLVM_LIB_TARGET_SPIRV_SPIRVMODULEANALYSIS_H
16
17#include "MCTargetDesc/SPIRVBaseInfo.h"
18#include "SPIRVGlobalRegistry.h"
19#include "SPIRVUtils.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/MapVector.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/IR/PassManager.h"
25
26namespace llvm {
27class SPIRVSubtarget;
28class MachineFunction;
29
30namespace SPIRV {
31// The enum contains logical module sections for the instruction collection.
32enum ModuleSectionType {
33 // MB_Capabilities, MB_Extensions, MB_ExtInstImports, MB_MemoryModel,
34 MB_EntryPoints, // All OpEntryPoint instructions (if any).
35 // MB_ExecutionModes, MB_DebugSourceAndStrings,
36 MB_DebugNames, // All OpName and OpMemberName intrs.
37 MB_DebugStrings, // All OpString intrs.
38 MB_DebugModuleProcessed, // All OpModuleProcessed instructions.
39 MB_AliasingInsts, // SPV_INTEL_memory_access_aliasing instructions.
40 MB_Annotations, // OpDecorate, OpMemberDecorate etc.
41 MB_TypeConstVars, // OpTypeXXX, OpConstantXXX, and global OpVariables.
42 MB_NonSemanticGlobalDI, // OpExtInst with e.g. DebugSource, DebugTypeBasic.
43 MB_ExtFuncDecls, // OpFunction etc. to declare for external funcs.
44 NUM_MODULE_SECTIONS // Total number of sections requiring basic blocks.
45};
46
47struct Requirements {
48 const bool IsSatisfiable;
49 const std::optional<Capability::Capability> Cap;
50 const ExtensionList Exts;
51 const VersionTuple MinVer; // 0 if no min version is required.
52 const VersionTuple MaxVer; // 0 if no max version is required.
53
54 Requirements(bool IsSatisfiable = false,
55 std::optional<Capability::Capability> Cap = {},
56 ExtensionList Exts = {}, VersionTuple MinVer = VersionTuple(),
57 VersionTuple MaxVer = VersionTuple())
58 : IsSatisfiable(IsSatisfiable), Cap(Cap), Exts(std::move(Exts)),
59 MinVer(MinVer), MaxVer(MaxVer) {}
60 Requirements(Capability::Capability Cap) : Requirements(true, {Cap}) {}
61};
62
63struct RequirementHandler {
64private:
65 CapabilityList MinimalCaps;
66
67 // AllCaps and AvailableCaps are related but different. AllCaps is a subset of
68 // AvailableCaps. AvailableCaps is the complete set of capabilities that are
69 // available to the current target. AllCaps is the set of capabilities that
70 // are required by the current module.
71 SmallSet<Capability::Capability, 8> AllCaps;
72 DenseSet<unsigned> AvailableCaps;
73
74 SmallSet<Extension::Extension, 4> AllExtensions;
75 VersionTuple MinVersion; // 0 if no min version is defined.
76 VersionTuple MaxVersion; // 0 if no max version is defined.
77 // Add capabilities to AllCaps, recursing through their implicitly declared
78 // capabilities too.
79 void recursiveAddCapabilities(const CapabilityList &ToPrune);
80
81 void initAvailableCapabilitiesForOpenCL(const SPIRVSubtarget &ST);
82 void initAvailableCapabilitiesForVulkan(const SPIRVSubtarget &ST);
83
84public:
85 RequirementHandler() = default;
86 void clear() {
87 MinimalCaps.clear();
88 AllCaps.clear();
89 AvailableCaps.clear();
90 AllExtensions.clear();
91 MinVersion = VersionTuple();
92 MaxVersion = VersionTuple();
93 }
94 const CapabilityList &getMinimalCapabilities() const { return MinimalCaps; }
95 const SmallSet<Extension::Extension, 4> &getExtensions() const {
96 return AllExtensions;
97 }
98 // Add a list of capabilities, ensuring AllCaps captures all the implicitly
99 // declared capabilities, and MinimalCaps has the minimal set of required
100 // capabilities (so all implicitly declared ones are removed).
101 void addCapabilities(const CapabilityList &ToAdd);
102 void addCapability(Capability::Capability ToAdd) { addCapabilities(ToAdd: {ToAdd}); }
103 void addExtensions(const ExtensionList &ToAdd) {
104 AllExtensions.insert_range(R: ToAdd);
105 }
106 void addExtension(Extension::Extension ToAdd) { AllExtensions.insert(V: ToAdd); }
107 // Add the given requirements to the lists. If constraints conflict, or these
108 // requirements cannot be satisfied, then abort the compilation.
109 void addRequirements(const Requirements &Req);
110 // Get requirement and add it to the list.
111 void getAndAddRequirements(SPIRV::OperandCategory::OperandCategory Category,
112 uint32_t i, const SPIRVSubtarget &ST);
113 // Check if all the requirements can be satisfied for the given subtarget, and
114 // if not abort compilation.
115 void checkSatisfiable(const SPIRVSubtarget &ST) const;
116 void initAvailableCapabilities(const SPIRVSubtarget &ST);
117 // Add the given capabilities to available and all their implicitly defined
118 // capabilities too.
119 void addAvailableCaps(const CapabilityList &ToAdd);
120 bool isCapabilityAvailable(Capability::Capability Cap) const {
121 return AvailableCaps.contains(V: Cap);
122 }
123
124 // Remove capability ToRemove, but only if IfPresent is present.
125 void removeCapabilityIf(const Capability::Capability ToRemove,
126 const Capability::Capability IfPresent);
127};
128
129using InstrList = SmallVector<const MachineInstr *>;
130// Maps a local register to the corresponding global alias.
131using LocalToGlobalRegTable = std::map<Register, MCRegister>;
132using RegisterAliasMapTy =
133 std::map<const MachineFunction *, LocalToGlobalRegTable>;
134using FPFastMathDefaultInfoMapTy =
135 MapVector<const Function *, FPFastMathDefaultInfoVector>;
136
137// The struct contains results of the module analysis and methods
138// to access them.
139struct ModuleAnalysisInfo {
140 RequirementHandler Reqs;
141 MemoryModel::MemoryModel Mem;
142 AddressingModel::AddressingModel Addr;
143 SourceLanguage::SourceLanguage SrcLang;
144 unsigned SrcLangVersion;
145 StringSet<> SrcExt;
146 // Maps ExtInstSet to corresponding ID register.
147 DenseMap<unsigned, MCRegister> ExtInstSetMap;
148 // Contains the list of all global OpVariables in the module.
149 SmallVector<const MachineInstr *, 4> GlobalVarList;
150 // Maps functions and global variables to corresponding ID registers.
151 DenseMap<const GlobalObject *, MCRegister> GlobalObjMap;
152 // The set contains machine instructions which are necessary
153 // for correct MIR but will not be emitted in function bodies.
154 DenseSet<const MachineInstr *> InstrsToDelete;
155 // The table contains global aliases of local registers for each machine
156 // function. The aliases are used to substitute local registers during
157 // code emission.
158 RegisterAliasMapTy RegisterAliasTable;
159 // The counter holds the maximum ID we have in the module.
160 unsigned MaxID;
161 // The array contains lists of MIs for each module section.
162 InstrList MS[NUM_MODULE_SECTIONS];
163 // The table maps MBB number to SPIR-V unique ID register.
164 DenseMap<std::pair<const MachineFunction *, int>, MCRegister> BBNumToRegMap;
165 // Basic block IDs and names to emit as OpName.
166 SmallVector<std::pair<MCRegister, StringRef>> MBBNames;
167 // The table maps function pointers to their default FP fast math info. It can
168 // be assumed that the SmallVector is sorted by the bit width of the type. The
169 // first element is the smallest bit width, and the last element is the
170 // largest bit width, therefore, we will have {half, float, double} in
171 // the order of their bit widths.
172 FPFastMathDefaultInfoMapTy FPFastMathDefaultInfoMap;
173
174 enum class AMDGPUAtomicMDKind : uint8_t {
175 NoFineGrainedMemory,
176 NoRemoteMemory,
177 IgnoreDenormalMode,
178
179 Last = IgnoreDenormalMode,
180 };
181 struct InstrAuxDataRecord {
182 const MachineFunction *MF;
183 Register TargetReg;
184 AMDGPUAtomicMDKind Kind;
185 };
186 SmallVector<InstrAuxDataRecord> InstrAuxDataRecords;
187 SmallVector<const MachineInstr *> DebugValueMIs;
188
189 MCRegister getGlobalObjReg(const GlobalObject *GO) {
190 assert(GO && "GlobalObject is null");
191 return GlobalObjMap.lookup(Val: GO);
192 }
193 MCRegister getExtInstSetReg(unsigned SetNum) { return ExtInstSetMap[SetNum]; }
194 InstrList &getMSInstrs(unsigned MSType) { return MS[MSType]; }
195 void setSkipEmission(const MachineInstr *MI) { InstrsToDelete.insert(V: MI); }
196 bool getSkipEmission(const MachineInstr *MI) {
197 return InstrsToDelete.contains(V: MI);
198 }
199 void setRegisterAlias(const MachineFunction *MF, Register Reg,
200 MCRegister AliasReg) {
201 RegisterAliasTable[MF][Reg] = AliasReg;
202 }
203 MCRegister getRegisterAlias(const MachineFunction *MF, Register Reg) {
204 auto &RegTable = RegisterAliasTable[MF];
205 auto RI = RegTable.find(x: Reg);
206 if (RI == RegTable.end()) {
207 return MCRegister();
208 }
209 return RI->second;
210 }
211 bool hasRegisterAlias(const MachineFunction *MF, Register Reg) {
212 auto RI = RegisterAliasTable.find(x: MF);
213 if (RI == RegisterAliasTable.end())
214 return false;
215 return RI->second.find(x: Reg) != RI->second.end();
216 }
217 unsigned getNextID() { return MaxID++; }
218 MCRegister getNextIDRegister() {
219 return MCRegister((1U << 31) | getNextID());
220 }
221 // Convert MBB's number to corresponding ID register.
222 MCRegister getOrCreateMBBRegister(const MachineBasicBlock &MBB) {
223 auto Key = std::make_pair(x: MBB.getParent(), y: MBB.getNumber());
224 auto [It, Inserted] = BBNumToRegMap.try_emplace(Key);
225 if (Inserted)
226 It->second = getNextIDRegister();
227 return It->second;
228 }
229 static StringRef getAMDGPUAtomicMDName(AMDGPUAtomicMDKind Kind) {
230 switch (Kind) {
231 case AMDGPUAtomicMDKind::NoFineGrainedMemory:
232 return "amdgpu.no.fine.grained.memory";
233 case AMDGPUAtomicMDKind::NoRemoteMemory:
234 return "amdgpu.no.remote.memory";
235 case AMDGPUAtomicMDKind::IgnoreDenormalMode:
236 return "atomic.ignore.denormal.mode";
237 }
238 llvm_unreachable("unknown AMDGPUAtomicMDKind");
239 }
240 // Must stay alive until the AsmPrinter consumes it.
241 bool invalidate(Module &, const PreservedAnalyses &,
242 ModuleAnalysisManager::Invalidator &) {
243 return false;
244 }
245};
246} // namespace SPIRV
247
248using InstrSignature = SmallVector<size_t>;
249using InstrTraces = std::set<InstrSignature>;
250using InstrGRegsMap = std::map<SmallVector<size_t>, unsigned>;
251using MachineFunctionGetter = function_ref<MachineFunction *(const Function &)>;
252
253class SPIRVModuleAnalysisImpl {
254public:
255 SPIRVModuleAnalysisImpl(const SPIRVSubtarget &ST,
256 SPIRV::ModuleAnalysisInfo &MAI,
257 MachineFunctionGetter GetMF);
258
259 void run(const Module &M);
260
261private:
262 void setBaseInfo(const Module &M);
263 void collectFuncNames(MachineInstr &MI, const Function *F);
264 void processOtherInstrs(const Module &M);
265 void numberRegistersGlobally(const Module &M);
266
267 // analyze dependencies to collect module scope definitions
268 void collectDeclarations(const Module &M);
269 void visitDecl(const MachineRegisterInfo &MRI, InstrGRegsMap &SignatureToGReg,
270 std::map<const Value *, unsigned> &GlobalToGReg,
271 const MachineFunction *MF, const MachineInstr &MI);
272 MCRegister handleVariable(const MachineFunction *MF, const MachineInstr &MI,
273 std::map<const Value *, unsigned> &GlobalToGReg);
274 MCRegister handleTypeDeclOrConstant(const MachineInstr &MI,
275 InstrGRegsMap &SignatureToGReg);
276 MCRegister
277 handleFunctionOrParameter(const MachineFunction *MF, const MachineInstr &MI,
278 std::map<const Value *, unsigned> &GlobalToGReg,
279 bool &IsFunDef);
280 void visitFunPtrUse(Register OpReg, const MachineOperand *FunPtrOp,
281 InstrGRegsMap &SignatureToGReg,
282 std::map<const Value *, unsigned> &GlobalToGReg,
283 const MachineFunction *MF);
284 bool isDeclSection(const MachineRegisterInfo &MRI, const MachineInstr &MI);
285
286 const SPIRVSubtarget *ST;
287 SPIRVGlobalRegistry *GR;
288 const SPIRVInstrInfo *TII;
289 SPIRV::ModuleAnalysisInfo &MAI;
290 MachineFunctionGetter GetMF;
291};
292
293struct SPIRVModuleAnalysisWrapperPass : public ModulePass {
294 static char ID;
295
296public:
297 SPIRVModuleAnalysisWrapperPass() : ModulePass(ID) {}
298
299 bool runOnModule(Module &M) override;
300 void getAnalysisUsage(AnalysisUsage &AU) const override;
301 SPIRV::ModuleAnalysisInfo MAI;
302};
303
304class SPIRVModuleAnalysis : public AnalysisInfoMixin<SPIRVModuleAnalysis> {
305 friend AnalysisInfoMixin<SPIRVModuleAnalysis>;
306 static AnalysisKey Key;
307
308public:
309 using Result = SPIRV::ModuleAnalysisInfo;
310
311 Result run(Module &M, ModuleAnalysisManager &MAM);
312};
313} // namespace llvm
314#endif // LLVM_LIB_TARGET_SPIRV_SPIRVMODULEANALYSIS_H
315