1//===- DXILWriterPass.cpp - Bitcode writing pass --------------------------===//
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// DXILWriterPass implementation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "DXILWriterPass.h"
14#include "DXILBitcodeWriter.h"
15#include "DirectX.h"
16#include "MCTargetDesc/DirectXContainerObjectWriter.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Analysis/ModuleSummaryAnalysis.h"
20#include "llvm/IR/Analysis.h"
21#include "llvm/IR/Constants.h"
22#include "llvm/IR/DebugInfo.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/GlobalVariable.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/PassManager.h"
30#include "llvm/InitializePasses.h"
31#include "llvm/Pass.h"
32#include "llvm/Support/Alignment.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Transforms/Utils/Cloning.h"
35#include "llvm/Transforms/Utils/ModuleUtils.h"
36
37using namespace llvm;
38using namespace llvm::dxil;
39
40cl::opt<bool> dxil::SourceInDebugModule(
41 "dx-source-in-debug-module",
42 cl::desc("Embed source code into debug module on DirectX target"),
43 cl::init(Val: false));
44
45namespace {
46class DXILWriterLegacy : public llvm::ModulePass {
47 raw_ostream &OS; // raw_ostream to print on
48
49public:
50 static char ID; // Pass identification, replacement for typeid
51 DXILWriterLegacy() : ModulePass(ID), OS(dbgs()) {
52 initializeDXILWriterLegacyPass(*PassRegistry::getPassRegistry());
53 }
54
55 explicit DXILWriterLegacy(raw_ostream &o) : ModulePass(ID), OS(o) {
56 initializeDXILWriterLegacyPass(*PassRegistry::getPassRegistry());
57 }
58
59 StringRef getPassName() const override { return "Bitcode Writer"; }
60
61 bool runOnModule(Module &M) override {
62 WriteDXILToFile(M, Out&: OS);
63 return false;
64 }
65 void getAnalysisUsage(AnalysisUsage &AU) const override {
66 AU.setPreservesAll();
67 }
68};
69
70static void legalizeLifetimeIntrinsics(Module &M) {
71 LLVMContext &Ctx = M.getContext();
72 Type *I64Ty = IntegerType::get(C&: Ctx, NumBits: 64);
73 Type *PtrTy = PointerType::get(C&: Ctx, AddressSpace: 0);
74 Intrinsic::ID LifetimeIIDs[2] = {Intrinsic::lifetime_start,
75 Intrinsic::lifetime_end};
76 for (Intrinsic::ID &IID : LifetimeIIDs) {
77 Function *F = M.getFunction(Name: Intrinsic::getName(Id: IID, OverloadTys: {PtrTy}, M: &M));
78 if (!F)
79 continue;
80
81 // Get or insert an LLVM 3.7-compliant lifetime intrinsic function of the
82 // form `void @llvm.lifetime.[start/end](i64, ptr)` with the NoUnwind
83 // attribute
84 AttributeList Attr;
85 Attr = Attr.addFnAttribute(C&: Ctx, Kind: Attribute::NoUnwind);
86 FunctionCallee LifetimeCallee = M.getOrInsertFunction(
87 Name: Intrinsic::getBaseName(id: IID), AttributeList: Attr, RetTy: Type::getVoidTy(C&: Ctx), Args: I64Ty, Args: PtrTy);
88
89 // Replace all calls to lifetime intrinsics with calls to the
90 // LLVM 3.7-compliant version of the lifetime intrinsic
91 for (User *U : make_early_inc_range(Range: F->users())) {
92 CallInst *CI = dyn_cast<CallInst>(Val: U);
93 assert(CI &&
94 "Expected user of a lifetime intrinsic function to be a CallInst");
95
96 // LLVM 3.7 lifetime intrinics require an i8* operand, so we insert
97 // a bitcast to ensure that is the case
98 Value *PtrOperand = CI->getArgOperand(i: 0);
99 PointerType *PtrOpPtrTy = cast<PointerType>(Val: PtrOperand->getType());
100 Value *NoOpBitCast = CastInst::Create(Instruction::BitCast, S: PtrOperand,
101 Ty: PtrOpPtrTy, Name: "", InsertBefore: CI->getIterator());
102
103 // LLVM 3.7 lifetime intrinsics have an explicit size operand, whose value
104 // we can obtain from the pointer operand which must be an AllocaInst (as
105 // of https://github.com/llvm/llvm-project/pull/149310)
106 AllocaInst *AI = dyn_cast<AllocaInst>(Val: PtrOperand);
107 assert(AI &&
108 "The pointer operand of a lifetime intrinsic call must be an "
109 "AllocaInst");
110 std::optional<TypeSize> AllocSize =
111 AI->getAllocationSize(DL: CI->getDataLayout());
112 assert(AllocSize.has_value() &&
113 "Expected the allocation size of AllocaInst to be known");
114 CallInst *NewCI = CallInst::Create(
115 Func: LifetimeCallee,
116 Args: {ConstantInt::get(Ty: I64Ty, V: AllocSize.value().getFixedValue()),
117 NoOpBitCast},
118 NameStr: "", InsertBefore: CI->getIterator());
119 for (Attribute ParamAttr : CI->getParamAttributes(ArgNo: 0))
120 NewCI->addParamAttr(ArgNo: 1, Attr: ParamAttr);
121
122 CI->eraseFromParent();
123 }
124
125 F->eraseFromParent();
126 }
127}
128
129static void removeLifetimeIntrinsics(Module &M) {
130 Intrinsic::ID LifetimeIIDs[2] = {Intrinsic::lifetime_start,
131 Intrinsic::lifetime_end};
132 for (Intrinsic::ID &IID : LifetimeIIDs) {
133 Function *F = M.getFunction(Name: Intrinsic::getBaseName(id: IID));
134 if (!F)
135 continue;
136
137 for (User *U : make_early_inc_range(Range: F->users())) {
138 CallInst *CI = dyn_cast<CallInst>(Val: U);
139 assert(CI && "Expected user of lifetime function to be a CallInst");
140 BitCastInst *BCI = dyn_cast<BitCastInst>(Val: CI->getArgOperand(i: 1));
141 assert(BCI && "Expected pointer operand of CallInst to be a BitCastInst");
142 CI->eraseFromParent();
143 BCI->eraseFromParent();
144 }
145 F->eraseFromParent();
146 }
147}
148
149static void replaceNamedMetadataArray(Module &M, StringRef Name,
150 ArrayRef<Metadata *> NewOps) {
151 NamedMDNode *NMD = M.getNamedMetadata(Name);
152 if (!NMD)
153 return;
154 NMD->eraseFromParent();
155 M.getOrInsertNamedMetadata(Name)->addOperand(
156 M: MDTuple::get(Context&: M.getContext(), MDs: NewOps));
157}
158
159class DXILEmbedderLegacy : public llvm::ModulePass {
160public:
161 static char ID; // Pass identification, replacement for typeid
162 DXILEmbedderLegacy() : ModulePass(ID) {
163 initializeDXILEmbedderLegacyPass(*PassRegistry::getPassRegistry());
164 }
165
166 StringRef getPassName() const override { return "DXIL Embedder"; }
167
168 bool runOnModule(Module &M) override;
169
170 void getAnalysisUsage(AnalysisUsage &AU) const override {
171 AU.setPreservesAll();
172 }
173};
174} // namespace
175
176static std::string writeModule(Module &M, bool HasDebugInfo, bool WriteDebug) {
177 std::string Data;
178 llvm::raw_string_ostream OS(Data);
179
180 if (HasDebugInfo) {
181 if (WriteDebug) {
182 if (!SourceInDebugModule) {
183 // Replace dx.source metadata nodes with stubs.
184 LLVMContext &Ctx = M.getContext();
185 MDString *EmptyString = MDString::get(Context&: Ctx, Str: "");
186 replaceNamedMetadataArray(M, Name: "dx.source.contents",
187 NewOps: {EmptyString, EmptyString});
188 replaceNamedMetadataArray(M, Name: "dx.source.defines", NewOps: {});
189 replaceNamedMetadataArray(M, Name: "dx.source.mainFileName", NewOps: {EmptyString});
190 replaceNamedMetadataArray(M, Name: "dx.source.args", NewOps: {});
191 }
192 } else {
193 // If we have an ILDB part, strip DXIL from all debug info.
194 StripDebugInfo(M);
195
196 // Also, manually remove debug version flags and dx.source nodes.
197 if (NamedMDNode *Flags = M.getModuleFlagsMetadata()) {
198 SmallVector<llvm::Module::ModuleFlagEntry, 4> FlagEntries;
199 M.getModuleFlagsMetadata(Flags&: FlagEntries);
200 Flags->eraseFromParent();
201 for (llvm::Module::ModuleFlagEntry &Entry : FlagEntries) {
202 if (Entry.Key->getString() == "Dwarf Version" ||
203 Entry.Key->getString() == "Debug Info Version") {
204 continue;
205 }
206 M.addModuleFlag(Behavior: Entry.Behavior, Key: Entry.Key->getString(), Val: Entry.Val);
207 }
208 }
209 for (NamedMDNode &NMD : llvm::make_early_inc_range(Range: M.named_metadata()))
210 if (NMD.getName().starts_with(Prefix: "dx.source"))
211 NMD.eraseFromParent();
212 }
213 } else {
214#ifdef EXPENSIVE_CHECKS
215 assert(StripDebugInfo(M) == false &&
216 "The module must not contain any debug info here."
217 "Shader modules with debug info must have !DICompileUnit metadata.");
218#endif
219 }
220 WriteDXILToFile(M, Out&: OS);
221 return Data;
222}
223
224static GlobalVariable *createSectionGlobal(Module &M, StringRef Data,
225 StringRef GlobalName,
226 StringRef SectionName) {
227 Constant *ModuleConstant =
228 ConstantDataArray::get(Context&: M.getContext(), Elts: arrayRefFromStringRef(Input: Data));
229 auto *GV = new llvm::GlobalVariable(M, ModuleConstant->getType(), true,
230 GlobalValue::PrivateLinkage,
231 ModuleConstant, GlobalName);
232 GV->setSection(SectionName);
233 GV->setAlignment(Align(4));
234 return GV;
235}
236
237static bool runEmbedderOnModule(Module &M) {
238 // Perform late legalization of lifetime intrinsics that would otherwise
239 // fail the Module Verifier if performed in an earlier pass
240 legalizeLifetimeIntrinsics(M);
241
242 bool HasDebugInfo = !M.debug_compile_units().empty();
243
244 if (SlimDebug && EmbedDebug)
245 reportFatalUsageError(reason: "/Qembed_debug is not compatible with /Zs");
246 if (!HasDebugInfo && EmbedDebug)
247 reportFatalUsageError(
248 reason: "Missing debug info for embedding into the container");
249 if (!HasDebugInfo && !PdbDebugPath.empty())
250 reportFatalUsageError(reason: "Missing debug info for writing to the PDB file");
251
252 std::string ILDBData;
253 if (HasDebugInfo) {
254 // Write DXIL with debug info to ILDB part.
255 // Clone the module to avoid alternating it with DebugInfoPass
256 // before stripping the debug info later.
257 ILDBData =
258 writeModule(M&: *llvm::CloneModule(M), HasDebugInfo, /*WriteDebug=*/true);
259 }
260
261 // Clone the module to save dx.source metadata nodes from stripping, as they
262 // are needed for DXILMetadataAnalysisWrapperPass.
263 std::string DXILData =
264 writeModule(M&: *llvm::CloneModule(M), HasDebugInfo, /*WriteDebug=*/false);
265
266 // We no longer need lifetime intrinsics after bitcode serialization, so we
267 // simply remove them to keep the Module Verifier happy after our
268 // not-so-legal legalizations
269 removeLifetimeIntrinsics(M);
270
271 SmallVector<GlobalValue *, 2> Globals;
272 if (HasDebugInfo) {
273 // Create a GV after both parts are written, otherwise it gets
274 // added to DXIL when `writeModule` is called the second time.
275 Globals.emplace_back(Args: createSectionGlobal(M, Data: ILDBData, GlobalName: "dx.ildb", SectionName: "ILDB"));
276 }
277 Globals.emplace_back(Args: createSectionGlobal(M, Data: DXILData, GlobalName: "dx.dxil", SectionName: "DXIL"));
278 appendToCompilerUsed(M, Values: Globals);
279 return true;
280}
281
282bool DXILEmbedderLegacy::runOnModule(Module &M) {
283 return runEmbedderOnModule(M);
284}
285
286PreservedAnalyses DXILEmbedderPass::run(Module &M, ModuleAnalysisManager &MAM) {
287 runEmbedderOnModule(M);
288 return PreservedAnalyses::all();
289}
290
291char DXILWriterLegacy::ID = 0;
292INITIALIZE_PASS_BEGIN(DXILWriterLegacy, "dxil-writer", "Write Bitcode", false,
293 true)
294INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass)
295INITIALIZE_PASS_END(DXILWriterLegacy, "dxil-writer", "Write Bitcode", false,
296 true)
297
298ModulePass *llvm::createDXILWriterLegacyPass(raw_ostream &Str) {
299 return new DXILWriterLegacy(Str);
300}
301
302char DXILEmbedderLegacy::ID = 0;
303INITIALIZE_PASS(DXILEmbedderLegacy, "dxil-embedder", "Embed DXIL", false, true)
304
305ModulePass *llvm::createDXILEmbedderLegacyPass() {
306 return new DXILEmbedderLegacy();
307}
308
309PreservedAnalyses DXILWriterPass::run(Module &M, ModuleAnalysisManager &MAM) {
310 WriteDXILToFile(M, Out&: OS);
311 return PreservedAnalyses::all();
312}
313