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