1//===- DXILPrepare.cpp - Prepare LLVM Module for DXIL encoding ------------===//
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 passes and utilities to convert a modern LLVM
10/// module into a module compatible with the LLVM 3.7-based DirectX Intermediate
11/// Language (DXIL).
12//===----------------------------------------------------------------------===//
13
14#include "DXILRootSignature.h"
15#include "DXILShaderFlags.h"
16#include "DXILSignatureAnalysis.h"
17#include "DirectX.h"
18#include "DirectXIRPasses/DXILAttributes.h"
19#include "DirectXIRPasses/PointerTypeAnalysis.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringSet.h"
22#include "llvm/Analysis/DXILMetadataAnalysis.h"
23#include "llvm/Analysis/DXILResource.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/Analysis.h"
26#include "llvm/IR/AttributeMask.h"
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Instruction.h"
29#include "llvm/IR/Module.h"
30#include "llvm/IR/PassManager.h"
31#include "llvm/InitializePasses.h"
32#include "llvm/Pass.h"
33#include "llvm/Support/VersionTuple.h"
34
35#define DEBUG_TYPE "dxil-prepare"
36
37using namespace llvm;
38using namespace llvm::dxil;
39
40namespace {
41
42static void collectDeadStringAttrs(AttributeMask &DeadAttrs, AttributeSet &&AS,
43 const StringSet<> &LiveKeys,
44 bool AllowExperimental) {
45 for (auto &Attr : AS) {
46 if (!Attr.isStringAttribute())
47 continue;
48 StringRef Key = Attr.getKindAsString();
49 if (LiveKeys.contains(key: Key))
50 continue;
51 if (AllowExperimental && Key.starts_with(Prefix: "exp-"))
52 continue;
53 DeadAttrs.addAttribute(A: Key);
54 }
55}
56
57static void removeStringFunctionAttributes(Function &F,
58 bool AllowExperimental) {
59 AttributeList Attrs = F.getAttributes();
60 const StringSet<> LiveKeys = {"waveops-include-helper-lanes",
61 "fp32-denorm-mode"};
62 // Collect DeadKeys in FnAttrs.
63 AttributeMask DeadAttrs;
64 collectDeadStringAttrs(DeadAttrs, AS: Attrs.getFnAttrs(), LiveKeys,
65 AllowExperimental);
66 collectDeadStringAttrs(DeadAttrs, AS: Attrs.getRetAttrs(), LiveKeys,
67 AllowExperimental);
68
69 F.removeFnAttrs(Attrs: DeadAttrs);
70 F.removeRetAttrs(Attrs: DeadAttrs);
71}
72
73class DXILPrepareModuleLegacy : public ModulePass {
74public:
75 DXILPrepareModuleLegacy() : ModulePass(ID) {}
76 void getAnalysisUsage(AnalysisUsage &AU) const override {
77 AU.addRequired<DXILMetadataAnalysisWrapperPass>();
78
79 AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
80 AU.addPreserved<DXILResourceWrapperPass>();
81 AU.addPreserved<RootSignatureAnalysisWrapper>();
82 AU.addPreserved<SignatureAnalysisWrapper>();
83 AU.addPreserved<ShaderFlagsAnalysisWrapper>();
84 }
85 bool runOnModule(Module &M) override;
86 static char ID; // Pass identification.
87};
88char DXILPrepareModuleLegacy::ID = 0;
89
90} // end anonymous namespace
91
92static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
93 PointerTypeMap &PointerTypes,
94 Instruction &Inst, Value *Operand,
95 Type *Ty) {
96 // Omit bitcasts if the incoming value matches the instruction type.
97 auto It = PointerTypes.find(Val: Operand);
98 if (It != PointerTypes.end()) {
99 auto *OpTy = cast<TypedPointerType>(Val: It->second)->getElementType();
100 if (OpTy == Ty)
101 return nullptr;
102 }
103
104 Type *ValTy = Operand->getType();
105 // Also omit the bitcast for matching global array types
106 if (auto *GlobalVar = dyn_cast<GlobalVariable>(Val: Operand))
107 ValTy = GlobalVar->getValueType();
108
109 if (auto *AI = dyn_cast<AllocaInst>(Val: Operand))
110 ValTy = AI->getAllocatedType();
111
112 if (auto *ArrTy = dyn_cast<ArrayType>(Val: ValTy)) {
113 Type *ElTy = ArrTy->getElementType();
114 if (ElTy == Ty)
115 return nullptr;
116 }
117
118 // finally, drill down GEP instructions until we get the array
119 // that is being accessed, and compare element types
120 if (ConstantExpr *GEPInstr = dyn_cast<ConstantExpr>(Val: Operand)) {
121 while (GEPInstr->getOpcode() == Instruction::GetElementPtr) {
122 Value *OpArg = GEPInstr->getOperand(i_nocapture: 0);
123 if (ConstantExpr *NewGEPInstr = dyn_cast<ConstantExpr>(Val: OpArg)) {
124 GEPInstr = NewGEPInstr;
125 continue;
126 }
127
128 if (auto *GlobalVar = dyn_cast<GlobalVariable>(Val: OpArg))
129 ValTy = GlobalVar->getValueType();
130 if (auto *AI = dyn_cast<AllocaInst>(Val: Operand))
131 ValTy = AI->getAllocatedType();
132 if (auto *ArrTy = dyn_cast<ArrayType>(Val: ValTy)) {
133 Type *ElTy = ArrTy->getElementType();
134 if (ElTy == Ty)
135 return nullptr;
136 }
137 break;
138 }
139 }
140
141 // Insert bitcasts where we are removing the instruction.
142 Builder.SetInsertPoint(&Inst);
143 // This code only gets hit in opaque-pointer mode, so the type of the
144 // pointer doesn't matter.
145 PointerType *PtrTy = cast<PointerType>(Val: Operand->getType());
146 return Builder.Insert(
147 I: CastInst::Create(Instruction::BitCast, S: Operand,
148 Ty: Builder.getPtrTy(AddrSpace: PtrTy->getAddressSpace())));
149}
150
151static bool prepareModule(
152 Module &M,
153 function_ref<dxil::ModuleMetadataInfo &()> GetModuleMetadataInfo) {
154 PointerTypeMap PointerTypes = PointerTypeAnalysis::run(M);
155 const AttributeMask &AttrMask = getNonDXILAttributeMask();
156
157 const dxil::ModuleMetadataInfo MetadataInfo = GetModuleMetadataInfo();
158 VersionTuple ValVer = MetadataInfo.ValidatorVersion;
159 bool AllowExperimental = ValVer.getMajor() == 0 && ValVer.getMinor() == 0;
160
161 for (auto &F : M.functions()) {
162 F.removeFnAttrs(Attrs: AttrMask);
163 F.removeRetAttrs(Attrs: AttrMask);
164 // Only remove string attributes if we are not skipping validation.
165 // This will reserve the experimental attributes when validation version
166 // is 0.0 for experiment mode.
167 removeStringFunctionAttributes(F, AllowExperimental);
168 for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
169 F.removeParamAttrs(ArgNo: Idx, Attrs: AttrMask);
170
171 for (auto &BB : F) {
172 IRBuilder<> Builder(&BB);
173 for (auto &I : make_early_inc_range(Range&: BB)) {
174
175 if (auto *CB = dyn_cast<CallBase>(Val: &I)) {
176 CB->removeFnAttrs(AttrsToRemove: AttrMask);
177 CB->removeRetAttrs(AttrsToRemove: AttrMask);
178 for (size_t Idx = 0, End = CB->arg_size(); Idx < End; ++Idx)
179 CB->removeParamAttrs(ArgNo: Idx, AttrsToRemove: AttrMask);
180 continue;
181 }
182
183 // Emtting NoOp bitcast instructions allows the ValueEnumerator to be
184 // unmodified as it reserves instruction IDs during contruction.
185 if (auto *LI = dyn_cast<LoadInst>(Val: &I)) {
186 if (Value *NoOpBitcast = maybeGenerateBitcast(
187 Builder, PointerTypes, Inst&: I, Operand: LI->getPointerOperand(),
188 Ty: LI->getType())) {
189 LI->replaceAllUsesWith(
190 V: Builder.CreateLoad(Ty: LI->getType(), Ptr: NoOpBitcast));
191 LI->eraseFromParent();
192 }
193 continue;
194 }
195 if (auto *SI = dyn_cast<StoreInst>(Val: &I)) {
196 if (Value *NoOpBitcast = maybeGenerateBitcast(
197 Builder, PointerTypes, Inst&: I, Operand: SI->getPointerOperand(),
198 Ty: SI->getValueOperand()->getType())) {
199
200 SI->replaceAllUsesWith(
201 V: Builder.CreateStore(Val: SI->getValueOperand(), Ptr: NoOpBitcast));
202 SI->eraseFromParent();
203 }
204 continue;
205 }
206 if (auto *GEP = dyn_cast<GetElementPtrInst>(Val: &I)) {
207 if (Value *NoOpBitcast = maybeGenerateBitcast(
208 Builder, PointerTypes, Inst&: I, Operand: GEP->getPointerOperand(),
209 Ty: GEP->getSourceElementType()))
210 GEP->setOperand(i_nocapture: 0, Val_nocapture: NoOpBitcast);
211 continue;
212 }
213 // An atomic on a float allocation exchanges the bit pattern as an
214 // integer, so the pointer element type does not match the value
215 // type. Typed pointers need a cast to keep the two in agreement.
216 if (auto *RMW = dyn_cast<AtomicRMWInst>(Val: &I)) {
217 if (Value *NoOpBitcast = maybeGenerateBitcast(
218 Builder, PointerTypes, Inst&: I, Operand: RMW->getPointerOperand(),
219 Ty: RMW->getValOperand()->getType()))
220 RMW->setOperand(i_nocapture: AtomicRMWInst::getPointerOperandIndex(),
221 Val_nocapture: NoOpBitcast);
222 continue;
223 }
224 if (auto *CmpXchg = dyn_cast<AtomicCmpXchgInst>(Val: &I)) {
225 if (Value *NoOpBitcast = maybeGenerateBitcast(
226 Builder, PointerTypes, Inst&: I, Operand: CmpXchg->getPointerOperand(),
227 Ty: CmpXchg->getNewValOperand()->getType()))
228 CmpXchg->setOperand(i_nocapture: AtomicCmpXchgInst::getPointerOperandIndex(),
229 Val_nocapture: NoOpBitcast);
230 continue;
231 }
232 }
233 }
234 }
235
236 return true;
237}
238
239bool DXILPrepareModuleLegacy::runOnModule(Module &M) {
240 return prepareModule(M, GetModuleMetadataInfo: [&]() -> dxil::ModuleMetadataInfo & {
241 return getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
242 });
243}
244
245PreservedAnalyses DXILPrepareModulePass::run(Module &M,
246 ModuleAnalysisManager &MAM) {
247 bool Changed = prepareModule(M, GetModuleMetadataInfo: [&]() -> dxil::ModuleMetadataInfo & {
248 return MAM.getResult<DXILMetadataAnalysis>(IR&: M);
249 });
250 if (!Changed)
251 return PreservedAnalyses::all();
252 return PreservedAnalyses::none()
253 .preserve<DXILMetadataAnalysis>()
254 .preserve<DXILResourceAnalysis>()
255 .preserve<RootSignatureAnalysis>()
256 .preserve<SignatureAnalysis>()
257 .preserve<ShaderFlagsAnalysis>();
258}
259
260INITIALIZE_PASS_BEGIN(DXILPrepareModuleLegacy, DEBUG_TYPE,
261 "DXIL Prepare Module", false, false)
262INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
263INITIALIZE_PASS_END(DXILPrepareModuleLegacy, DEBUG_TYPE, "DXIL Prepare Module",
264 false, false)
265
266ModulePass *llvm::createDXILPrepareModuleLegacyPass() {
267 return new DXILPrepareModuleLegacy();
268}
269