LLVM 24.0.0git
DirectXCodeGenPassBuilder.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#include "DXILRootSignature.h"
10#include "DXILShaderFlags.h"
13#include "DirectX.h"
14#include "DirectXAsmPrinter.h"
20#include "llvm/Support/Debug.h"
27
28using namespace llvm;
29
30namespace {
31
32class DirectXCodeGenPassBuilder : public CodeGenPassBuilder {
33 DirectXTargetMachine &getTM() const {
34 return static_cast<DirectXTargetMachine &>(TM);
35 }
36
37public:
38 explicit DirectXCodeGenPassBuilder(DirectXTargetMachine &TM,
39 const CGPassBuilderOption &Opts,
40 PassInstrumentationCallbacks *PIC)
41 : CodeGenPassBuilder(TM, Opts, PIC) {}
42
43protected:
44 Error buildPipelineImpl(PassManagerWrapper &PMW, ModuleAnalysisManager &MAM,
45 raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
46 CodeGenFileType FileType, MCContext &Ctx) override;
47 void addCodeGenPrepare(PassManagerWrapper &PMW) override;
48 void addAsmPrinterBegin(PassManagerWrapper &PMW) override;
49 void addAsmPrinter(PassManagerWrapper &PMW) override;
50 void addAsmPrinterEnd(PassManagerWrapper &PMW) override;
51};
52
53} // namespace
54
55void DirectXCodeGenPassBuilder::addCodeGenPrepare(PassManagerWrapper &PMW) {
56 CodeGenOptLevel OptLevel = getOptLevel();
57
58 addFunctionPass(StripConvergenceIntrinsicsPass(), PMW);
59 flushFPMsToMPM(PMW);
60 addModulePass(DXILFinalizeLinkagePass(), PMW);
61 if (OptLevel != CodeGenOptLevel::None)
62 addModulePass(GlobalDCEPass(), PMW);
63 addModulePass(DXILMemIntrinsicsPass(), PMW);
64 addModulePass(DXILCBufferAccessPass(), PMW);
65 addFunctionPass(DXILRemoveUnusedResourcesPass(), PMW);
66 addFunctionPass(DXILResourceAccessPass(), PMW);
67 flushFPMsToMPM(PMW);
68 addModulePass(DXILIntrinsicExpansionPass(), PMW);
69 addModulePass(DXILDataScalarizationPass(), PMW);
70 if (getTM().getTargetTriple().getOSVersion() < VersionTuple(6, 9)) {
71 ScalarizerPassOptions DxilScalarOptions;
72 DxilScalarOptions.ScalarizeLoadStore = true;
73 addFunctionPass(ScalarizerPass(DxilScalarOptions), PMW);
74 flushFPMsToMPM(PMW);
75 }
76 addModulePass(DXILFlattenArraysPass(), PMW);
77 addFunctionPass(DXILForwardHandleAccessesPass(), PMW);
78 if (OptLevel != CodeGenOptLevel::None)
79 addFunctionPass(DSEPass(), PMW);
80 addFunctionPass(DXILLegalizePass(), PMW);
81 flushFPMsToMPM(PMW);
82 addModulePass(DXILResourceImplicitBindingPass(), PMW);
83 addModulePass(DXILTranslateMetadataPass(), PMW);
84 addModulePass(DXILPostOptimizationValidationPass(), PMW);
85 addModulePass(DXILOpLoweringPass(), PMW);
86 addModulePass(DXILPrepareModulePass(), PMW);
87 addModulePass(DXILDebugInfoPass(), PMW);
88}
89
90void DirectXCodeGenPassBuilder::addAsmPrinterBegin(PassManagerWrapper &PMW) {
91 addModulePass(DXILAsmPrinterBeginPass(), PMW, /*Force=*/true);
92}
93
94void DirectXCodeGenPassBuilder::addAsmPrinter(PassManagerWrapper &PMW) {
95 addMachineFunctionPass(DXILAsmPrinterPass(), PMW);
96}
97
98void DirectXCodeGenPassBuilder::addAsmPrinterEnd(PassManagerWrapper &PMW) {
99 addModulePass(DXILAsmPrinterEndPass(), PMW, /*Force=*/true);
100}
101
102Error DirectXCodeGenPassBuilder::buildPipelineImpl(
103 PassManagerWrapper &PMW, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out,
104 raw_pwrite_stream *DwoOut, CodeGenFileType FileType, MCContext &Ctx) {
105 addModulePass(RequireAnalysisPass<MachineModuleAnalysis, Module>(), PMW,
106 /*Force=*/true);
107 addCodeGenPrepare(PMW);
108 flushFPMsToMPM(PMW);
109
110 switch (FileType) {
111 case CodeGenFileType::AssemblyFile:
112 addModulePass(DXILPrettyPrinterPass(Out), PMW, /*Force=*/true);
113 break;
114 case CodeGenFileType::ObjectFile:
115 if (CompletesPipeline) {
116 addModulePass(DXILEmbedderPass(), PMW, /*Force=*/true);
117 // We embed the other DXContainer globals after embedding DXIL so that the
118 // globals don't pollute the DXIL.
119 addModulePass(DXILContainerGlobalsPass(), PMW, /*Force=*/true);
120 addModulePass(DXILContainerPDBPass(), PMW, /*Force=*/true);
121
122 if (auto Err = registerAsmPrinter(MAM, Out, DwoOut, FileType, Ctx))
123 return Err;
124
125 addAsmPrinterBegin(PMW);
126 addAsmPrinter(PMW);
127 flushFPMsToMPM(PMW, /*FreeMachineFunctions=*/true);
128 addAsmPrinterEnd(PMW);
129 } else {
130 addModulePass(DXILWriterPass(Out), PMW, /*Force=*/true);
131 }
132 break;
133 case CodeGenFileType::Null:
134 break;
135 }
136
137 return Error::success();
138}
139
141#define GET_PASS_REGISTRY "DirectXPassRegistry.def"
143}
144
147 raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
148 const CGPassBuilderOption &Opt, MCContext &Ctx,
150 auto CGPB = DirectXCodeGenPassBuilder(*this, Opt, PIC);
151 return CGPB.buildPipeline(MPM, MAM, Out, DwoOut, FileType, Ctx);
152}
Interfaces for producing common pass manager configurations.
This file provides a bitcode writing pass.
ModuleAnalysisManager MAM
PassBuilder PB(Mod->getContext(), Machine, PassOpts->PTO, std::nullopt, &PIC)
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
This pass converts vector operations into scalar operations (or, optionally, operations on smaller ve...
This pass strips convergence intrinsics and operand bundles as those are only useful when modifying t...
Defines the llvm::VersionTuple class, which represents a version in the form major[....
This class provides access to building LLVM's passes.
Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, const CGPassBuilderOption &Opt, MCContext &Ctx, PassInstrumentationCallbacks *PIC) override
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Context object for machine code objects.
Definition MCContext.h:84
This class provides access to building LLVM's passes.
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
An abstract base class for streams implementations that also support a pwrite operation.
Interfaces for registering analysis passes, producing common pass manager configurations,...
This is an optimization pass for GlobalISel generic memory operations.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:309
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
bool ScalarizeLoadStore
Allow the scalarizer pass to scalarize loads and store.
Definition Scalarizer.h:48