Skip to content

Commit c6fa1b8

Browse files
committed
[CodeGen][NewPM] Plug greedy RA in codegen pipeline
1 parent 2d34d7a commit c6fa1b8

13 files changed

+75
-22
lines changed

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,9 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addMachineSSAOptimization(
10611061
///
10621062
/// A target that uses the standard regalloc pass order for fast or optimized
10631063
/// allocation may still override this for per-target regalloc
1064-
/// selection. But -regalloc=... always takes precedence.
1064+
/// selection. But -regalloc-npm=... always takes precedence.
1065+
/// If a target does not want to allow users to set -regalloc-npm=... at all,
1066+
/// check if Opt.RegAlloc == RegAllocType::Unset.
10651067
template <typename Derived, typename TargetMachineT>
10661068
void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
10671069
AddMachinePass &addPass, bool Optimized) const {
@@ -1074,10 +1076,29 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
10741076
/// Find and instantiate the register allocation pass requested by this target
10751077
/// at the current optimization level. Different register allocators are
10761078
/// defined as separate passes because they may require different analysis.
1079+
///
1080+
/// This helper ensures that the -regalloc-npm= option is always available,
1081+
/// even for targets that override the default allocator.
10771082
template <typename Derived, typename TargetMachineT>
10781083
void CodeGenPassBuilder<Derived, TargetMachineT>::addRegAllocPass(
10791084
AddMachinePass &addPass, bool Optimized) const {
1080-
// TODO: Parse Opt.RegAlloc to add register allocator.
1085+
// Use the specified -regalloc-npm={basic|greedy|fast|pbqp}
1086+
if (Opt.RegAlloc > RegAllocType::Default) {
1087+
switch (Opt.RegAlloc) {
1088+
case RegAllocType::Fast:
1089+
addPass(RegAllocFastPass());
1090+
break;
1091+
case RegAllocType::Greedy:
1092+
addPass(RAGreedyPass());
1093+
break;
1094+
default:
1095+
report_fatal_error("register allocator not supported yet.", false);
1096+
}
1097+
return;
1098+
}
1099+
// -regalloc=default or unspecified, so pick based on the optimization level
1100+
// or ask the target for the regalloc pass.
1101+
derived().addTargetRegisterAllocator(addPass, Optimized);
10811102
}
10821103

10831104
template <typename Derived, typename TargetMachineT>
@@ -1148,20 +1169,22 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addOptimizedRegAlloc(
11481169
// PreRA instruction scheduling.
11491170
addPass(MachineSchedulerPass());
11501171

1151-
if (derived().addRegAssignmentOptimized(addPass)) {
1152-
// Allow targets to expand pseudo instructions depending on the choice of
1153-
// registers before MachineCopyPropagation.
1154-
derived().addPostRewrite(addPass);
1172+
if (auto E = derived().addRegAssignmentOptimized(addPass)) {
1173+
// addRegAssignmentOptimized did not add a reg alloc pass, so do nothing.
1174+
return;
1175+
}
1176+
// Allow targets to expand pseudo instructions depending on the choice of
1177+
// registers before MachineCopyPropagation.
1178+
derived().addPostRewrite(addPass);
11551179

1156-
// Copy propagate to forward register uses and try to eliminate COPYs that
1157-
// were not coalesced.
1158-
addPass(MachineCopyPropagationPass());
1180+
// Copy propagate to forward register uses and try to eliminate COPYs that
1181+
// were not coalesced.
1182+
addPass(MachineCopyPropagationPass());
11591183

1160-
// Run post-ra machine LICM to hoist reloads / remats.
1161-
//
1162-
// FIXME: can this move into MachineLateOptimization?
1163-
addPass(MachineLICMPass());
1164-
}
1184+
// Run post-ra machine LICM to hoist reloads / remats.
1185+
//
1186+
// FIXME: can this move into MachineLateOptimization?
1187+
addPass(MachineLICMPass());
11651188
}
11661189

11671190
//===---------------------------------------------------------------------===//

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,12 @@ MACHINE_FUNCTION_PASS_WITH_PARAMS(
193193
},
194194
"filter=reg-filter;no-clear-vregs")
195195

196+
// 'all' is the default filter
196197
MACHINE_FUNCTION_PASS_WITH_PARAMS(
197198
"greedy", "RAGreedyPass",
198199
[](RAGreedyPass::Options Opts) { return RAGreedyPass(Opts); },
199200
[PB = this](StringRef Params) {
200-
// TODO: parseRegAllocGreedyFilterFunc(*PB, Params);
201-
return Expected<RAGreedyPass::Options>(RAGreedyPass::Options{});
201+
return parseRegAllocGreedyFilterFunc(*PB, Params);
202202
}, "reg-filter"
203203
)
204204
#undef MACHINE_FUNCTION_PASS_WITH_PARAMS

llvm/include/llvm/Target/CGPassBuilderOption.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
namespace llvm {
2121

2222
enum class RunOutliner { TargetDefault, AlwaysOutline, NeverOutline };
23-
enum class RegAllocType { Default, Basic, Fast, Greedy, PBQP };
23+
enum class RegAllocType { Unset, Default, Basic, Fast, Greedy, PBQP };
2424

2525
// Not one-on-one but mostly corresponding to commandline options in
2626
// TargetPassConfig.cpp.
@@ -52,7 +52,7 @@ struct CGPassBuilderOption {
5252
bool RequiresCodeGenSCCOrder = false;
5353

5454
RunOutliner EnableMachineOutliner = RunOutliner::TargetDefault;
55-
StringRef RegAlloc = "default";
55+
RegAllocType RegAlloc = RegAllocType::Unset;
5656
std::optional<GlobalISelAbortMode> EnableGlobalISelAbort;
5757
std::string FSProfileFile;
5858
std::string FSRemappingFile;

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,20 @@ parseBoundsCheckingOptions(StringRef Params) {
14121412
return Options;
14131413
}
14141414

1415+
Expected<RAGreedyPass::Options>
1416+
parseRegAllocGreedyFilterFunc(PassBuilder &PB, StringRef Params) {
1417+
if (Params.empty() || Params == "all") {
1418+
return RAGreedyPass::Options();
1419+
}
1420+
std::optional<RegAllocFilterFunc> Filter = PB.parseRegAllocFilter(Params);
1421+
if (!Filter) {
1422+
return make_error<StringError>(
1423+
formatv("invalid regallocgreedy register filter '{0}' ", Params).str(),
1424+
inconvertibleErrorCode());
1425+
}
1426+
return RAGreedyPass::Options{*Filter, Params};
1427+
}
1428+
14151429
} // namespace
14161430

14171431
/// Tests whether a pass name starts with a valid prefix for a default pipeline

llvm/test/CodeGen/AArch64/implicit-def-remat-requires-impdef-check.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 4
22
# RUN: llc -mtriple=arm64-apple-macosx -mcpu=apple-m1 -stress-regalloc=4 -verify-regalloc -run-pass=greedy -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=arm64-apple-macosx -mcpu=apple-m1 -stress-regalloc=4 -verify-regalloc -passes=greedy -o - %s | FileCheck %s
34

45
--- |
56
define void @inst_stores_to_dead_spill_implicit_def_impdef() {

llvm/test/CodeGen/AArch64/implicit-def-with-impdef-greedy-assert.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
22
# RUN: llc -mtriple=arm64-apple-ios -run-pass=greedy -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=arm64-apple-ios -passes=greedy -o - %s | FileCheck %s
34

45
---
56
name: widget

llvm/test/CodeGen/AArch64/pr51516.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# RUN: llc -mtriple=aarch64-unknown-fuchsia -run-pass=greedy -verify-machineinstrs -o - %s | FileCheck %s
2+
# RUN: llc -mtriple=aarch64-unknown-fuchsia -passes=greedy -verify-machineinstrs -o - %s | FileCheck %s
23

34
# Check that we spill %31 and do not rematerialize it since the use operand
45
# of ADDXri is killed by the STRXui in this block.

llvm/test/CodeGen/AArch64/spill-fold.mir

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# RUN: llc -mtriple=aarch64-none-linux-gnu -run-pass greedy -verify-machineinstrs -o - %s | FileCheck %s
22
# RUN: llc -mtriple=aarch64_be-none-linux-gnu -run-pass greedy -verify-machineinstrs -o - %s | FileCheck %s
3+
# RUN: llc -mtriple=aarch64-none-linux-gnu -passes=greedy -o - %s | FileCheck %s
4+
# RUN: llc -mtriple=aarch64_be-none-linux-gnu -passes=greedy -o - %s | FileCheck %s
35
--- |
46
define i64 @test_subreg_spill_fold() { ret i64 0 }
57
define i64 @test_subreg_spill_fold2() { ret i64 0 }

llvm/test/CodeGen/MIR/Generic/runPass.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# RUN: llc -run-pass=regallocbasic -debug-pass=Arguments -o - %s | FileCheck %s
33
# RUN: llc -run-pass=regallocfast -debug-pass=Arguments -o - %s | FileCheck %s
44
# RUN: llc -passes=regallocfast -o - %s | FileCheck %s
5+
# RUN: llc -passes=greedy -o - %s | FileCheck %s
56

67
# Check that passes are initialized correctly, so that it's possible to
78
# use -run-pass.

llvm/test/CodeGen/SystemZ/clear-liverange-spillreg.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#RUN: llc -o - %s -mtriple=s390x-ibm-linux -run-pass=greedy
2+
#RUN: llc -o - %s -mtriple=s390x-ibm-linux -passes=greedy
23
#PR34502. Check HoistSpill works properly after the live range of spilled
34
#virtual register is cleared.
45
--- |

llvm/test/CodeGen/Thumb/high-reg-clobber.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# RUN: llc -mtriple thumbv6m-arm-none-eabi -run-pass greedy %s -o - | FileCheck %s
44
# RUN: llc -mtriple thumbv6m-arm-none-eabi -run-pass regallocfast %s -o - | FileCheck %s --check-prefix=FAST
55
# RUN: llc -mtriple thumbv6m-arm-none-eabi -passes=regallocfast %s -o - | FileCheck %s --check-prefix=FAST
6+
# RUN: llc -mtriple thumbv6m-arm-none-eabi -passes=greedy %s -o - | FileCheck %s
67

78
...
89
---

llvm/test/CodeGen/X86/limit-split-cost.mir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# REQUIRES: asserts
22
# RUN: llc -mtriple=x86_64-- -run-pass=greedy %s -debug-only=regalloc -huge-size-for-split=0 -o /dev/null 2>&1 | FileCheck %s
3+
# RUN: llc -mtriple=x86_64-- -passes=greedy %s -debug-only=regalloc -huge-size-for-split=0 -o /dev/null 2>&1 | FileCheck %s
34
# Check no global region split is needed because the live range to split is trivially rematerializable.
45
# CHECK-NOT: Compact region bundles
56
--- |

llvm/tools/llc/NewPMDriver.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@
4848

4949
using namespace llvm;
5050

51-
static cl::opt<std::string>
52-
RegAlloc("regalloc-npm",
53-
cl::desc("Register allocator to use for new pass manager"),
54-
cl::Hidden, cl::init("default"));
51+
static cl::opt<RegAllocType> RegAlloc(
52+
"regalloc-npm", cl::desc("Register allocator to use for new pass manager"),
53+
cl::Hidden, cl::init(RegAllocType::Unset),
54+
cl::values(
55+
clEnumValN(RegAllocType::Default, "default",
56+
"Default register allocator"),
57+
clEnumValN(RegAllocType::PBQP, "pbqp", "PBQP register allocator"),
58+
clEnumValN(RegAllocType::Fast, "fast", "Fast register allocator"),
59+
clEnumValN(RegAllocType::Basic, "basic", "Basic register allocator"),
60+
clEnumValN(RegAllocType::Greedy, "greedy",
61+
"Greedy register allocator")));
5562

5663
static cl::opt<bool>
5764
DebugPM("debug-pass-manager", cl::Hidden,

0 commit comments

Comments
 (0)