Skip to content

Commit a831fb4

Browse files
committed
[AMDGPU] Enable NewPM for AMDGPU backend
1 parent 72e20d5 commit a831fb4

File tree

7 files changed

+97
-10
lines changed

7 files changed

+97
-10
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "llvm/Bitcode/BitcodeReader.h"
2525
#include "llvm/Bitcode/BitcodeWriter.h"
2626
#include "llvm/Bitcode/BitcodeWriterPass.h"
27+
#include "llvm/CodeGen/MachineModuleInfo.h"
2728
#include "llvm/CodeGen/TargetSubtargetInfo.h"
29+
#include "llvm/CodeGen/TargetPassConfig.h"
2830
#include "llvm/Config/llvm-config.h"
2931
#include "llvm/Frontend/Driver/CodeGenOptions.h"
3032
#include "llvm/IR/DataLayout.h"
@@ -189,7 +191,12 @@ class EmitAssemblyHelper {
189191
std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS, BackendConsumer *BC);
190192
void RunCodegenPipeline(BackendAction Action,
191193
std::unique_ptr<raw_pwrite_stream> &OS,
192-
std::unique_ptr<llvm::ToolOutputFile> &DwoOS);
194+
std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
195+
TargetMachine &TM);
196+
void RunCodegenPipelineWithNewPM(BackendAction Action,
197+
std::unique_ptr<raw_pwrite_stream> &OS,
198+
std::unique_ptr<llvm::ToolOutputFile> &DwoOS,
199+
TargetMachine &TM);
193200

194201
/// Check whether we should emit a module summary for regular LTO.
195202
/// The module summary should be emitted by default for regular LTO
@@ -1211,10 +1218,16 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
12111218

12121219
void EmitAssemblyHelper::RunCodegenPipeline(
12131220
BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1214-
std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1221+
std::unique_ptr<llvm::ToolOutputFile> &DwoOS, TargetMachine &TM) {
12151222
// We still use the legacy PM to run the codegen pipeline since the new PM
12161223
// does not work with the codegen pipeline.
12171224
// FIXME: make the new PM work with the codegen pipeline.
1225+
1226+
if (TM.EnableNewPMForBackend()) {
1227+
RunCodegenPipelineWithNewPM(Action, OS, DwoOS, TM);
1228+
return;
1229+
}
1230+
12181231
legacy::PassManager CodeGenPasses;
12191232

12201233
// Append any output we need to the pass manager.
@@ -1259,6 +1272,74 @@ void EmitAssemblyHelper::RunCodegenPipeline(
12591272
}
12601273
}
12611274

1275+
void EmitAssemblyHelper::RunCodegenPipelineWithNewPM(BackendAction Action,
1276+
std::unique_ptr<raw_pwrite_stream> &OS,
1277+
std::unique_ptr<llvm::ToolOutputFile> &DwoOS, TargetMachine &TM) {
1278+
MachineModuleInfo MMI(&TM);
1279+
CGPassBuilderOption Opt = getCGPassBuilderOption();
1280+
1281+
Opt.DisableVerify = !CodeGenOpts.VerifyModule;
1282+
Opt.DebugPM = CodeGenOpts.DebugPassManager;
1283+
1284+
PassInstrumentationCallbacks PIC;
1285+
StandardInstrumentations SI(TheModule->getContext(), CodeGenOpts.DebugPassManager,
1286+
CodeGenOpts.VerifyEach);
1287+
registerCodeGenCallback(PIC, TM);
1288+
1289+
MachineFunctionAnalysisManager MFAM;
1290+
LoopAnalysisManager LAM;
1291+
FunctionAnalysisManager FAM;
1292+
CGSCCAnalysisManager CGAM;
1293+
ModuleAnalysisManager MAM;
1294+
PassBuilder PB(&TM, PipelineTuningOptions(), std::nullopt, &PIC);
1295+
1296+
PB.registerModuleAnalyses(MAM);
1297+
PB.registerCGSCCAnalyses(CGAM);
1298+
PB.registerFunctionAnalyses(FAM);
1299+
PB.registerLoopAnalyses(LAM);
1300+
PB.registerMachineFunctionAnalyses(MFAM);
1301+
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);
1302+
SI.registerCallbacks(PIC, &MAM);
1303+
1304+
TargetLibraryInfoImpl TLII(TheModule->getTargetTriple());
1305+
1306+
FAM.registerPass([&] { return TargetLibraryAnalysis(TLII); });
1307+
MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });
1308+
1309+
ModulePassManager MPM;
1310+
FunctionPassManager FPM;
1311+
1312+
if (!TM.buildCodeGenPipeline(MPM, *OS, DwoOS ? &DwoOS->os() : nullptr,
1313+
getCodeGenFileType(Action), Opt, MMI.getContext(), &PIC)) {
1314+
Diags.Report(diag::err_fe_unable_to_interface_with_target);
1315+
return;
1316+
}
1317+
1318+
if (PrintPipelinePasses) {
1319+
std::string PipelineStr;
1320+
raw_string_ostream OutS(PipelineStr);
1321+
MPM.printPipeline(OutS, [&PIC](StringRef ClassName) {
1322+
auto PassName = PIC.getPassNameForClassName(ClassName);
1323+
return PassName.empty() ? ClassName : PassName;
1324+
});
1325+
outs() << PipelineStr << '\n';
1326+
return;
1327+
}
1328+
1329+
{
1330+
PrettyStackTraceString CrashInfo("Code generation");
1331+
llvm::TimeTraceScope TimeScope("CodeGenPasses");
1332+
Timer timer;
1333+
if (CI.getCodeGenOpts().TimePasses) {
1334+
timer.init("codegen", "Machine code generation", CI.getTimerGroup());
1335+
CI.getFrontendTimer().yieldTo(timer);
1336+
}
1337+
MPM.run(*TheModule, MAM);
1338+
if (CI.getCodeGenOpts().TimePasses)
1339+
timer.yieldTo(CI.getFrontendTimer());
1340+
}
1341+
}
1342+
12621343
void EmitAssemblyHelper::emitAssembly(BackendAction Action,
12631344
std::unique_ptr<raw_pwrite_stream> OS,
12641345
BackendConsumer *BC) {
@@ -1277,7 +1358,7 @@ void EmitAssemblyHelper::emitAssembly(BackendAction Action,
12771358

12781359
std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
12791360
RunOptimizationPipeline(Action, OS, ThinLinkOS, BC);
1280-
RunCodegenPipeline(Action, OS, DwoOS);
1361+
RunCodegenPipeline(Action, OS, DwoOS, *TM);
12811362

12821363
if (ThinLinkOS)
12831364
ThinLinkOS->keep();

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ Error CodeGenPassBuilder<Derived, TargetMachineT>::buildPipeline(
679679
addPass(PrintMIRPass(Out), /*Force=*/true);
680680
}
681681

682-
{
682+
if (PrintAsm) {
683683
AddIRPass addIRPass(MPM, derived());
684684
addIRPass(AsmPrinterFinalizePass(PrinterImpl));
685685
}

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class LLVM_ABI TargetMachine {
116116

117117
unsigned RequireStructuredCFG : 1;
118118
unsigned O0WantsFastISel : 1;
119+
unsigned NewPMForBackend : 1;
119120

120121
// PGO related tunables.
121122
std::optional<PGOOptions> PGOOption;
@@ -255,6 +256,9 @@ class LLVM_ABI TargetMachine {
255256
bool requiresStructuredCFG() const { return RequireStructuredCFG; }
256257
void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
257258

259+
bool EnableNewPMForBackend() const { return NewPMForBackend; }
260+
void setNewPMForBackend(bool Value) { NewPMForBackend = Value; }
261+
258262
/// Returns the code generation relocation model. The choices are static, PIC,
259263
/// and dynamic-no-pic, and target default.
260264
Reloc::Model getRelocationModel() const;

llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
751751
getEffectiveCodeModel(CM, CodeModel::Small), OptLevel),
752752
TLOF(createTLOF(getTargetTriple())) {
753753
initAsmInfo();
754+
setNewPMForBackend(true);
754755
if (TT.isAMDGCN()) {
755756
if (getMCSubtargetInfo()->checkFeatures("+wavefrontsize64"))
756757
MRI.reset(llvm::createGCNMCRegisterInfo(AMDGPUDwarfFlavour::Wave64));

llvm/lib/Target/TargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
4242
: TheTarget(T), DL(DataLayoutString), TargetTriple(TT),
4343
TargetCPU(std::string(CPU)), TargetFS(std::string(FS)), AsmInfo(nullptr),
4444
MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
45-
O0WantsFastISel(false), Options(Options) {}
45+
O0WantsFastISel(false), NewPMForBackend(false), Options(Options) {}
4646

4747
TargetMachine::~TargetMachine() = default;
4848

llvm/test/CodeGen/AMDGPU/v_mov_b64_expansion.mir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -run-pass postrapseudos -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX900,NOT-GFX1250 %s
2-
# RUN: llc -mtriple=amdgcn -mcpu=gfx90a -run-pass postrapseudos -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX90A,NOT-GFX1250 %s
3-
# RUN: llc -mtriple=amdgcn -mcpu=gfx942 -run-pass postrapseudos -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX942,NOT-GFX1250 %s
4-
# RUN: llc -mtriple=amdgcn -mcpu=gfx1250 -run-pass postrapseudos %s -o - | FileCheck -check-prefixes=GCN,GFX1250 %s
1+
# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -passes="post-ra-pseudos" -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX900,NOT-GFX1250 %s
2+
# RUN: llc -mtriple=amdgcn -mcpu=gfx90a -passes="post-ra-pseudos" -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX90A,NOT-GFX1250 %s
3+
# RUN: llc -mtriple=amdgcn -mcpu=gfx942 -passes="post-ra-pseudos" -verify-machineinstrs %s -o - | FileCheck -check-prefixes=GCN,GFX942,NOT-GFX1250 %s
4+
# RUN: llc -mtriple=amdgcn -mcpu=gfx1250 -passes="post-ra-pseudos" %s -o - | FileCheck -check-prefixes=GCN,GFX1250 %s
55

66
# GCN-LABEL: name: v_mov_b64_from_vgpr
77
# GFX900: $vgpr0 = V_MOV_B32_e32 $vgpr2, implicit $exec, implicit-def $vgpr0_vgpr1

llvm/tools/llc/llc.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
655655
else if (VerifyEach)
656656
VK = VerifierKind::EachPass;
657657

658-
if (EnableNewPassManager || !PassPipeline.empty()) {
658+
if (EnableNewPassManager || !PassPipeline.empty() ||
659+
Target->EnableNewPMForBackend()) {
659660
return compileModuleWithNewPM(argv[0], std::move(M), std::move(MIR),
660661
std::move(Target), std::move(Out),
661662
std::move(DwoOut), Context, TLII, VK,

0 commit comments

Comments
 (0)