Skip to content

Commit 1bc8b32

Browse files
authored
[NewPM][CodeGen] Port regallocfast to new pass manager (#94426)
This pull request port `regallocfast` to new pass manager. It exposes the parameter `filter` to handle different register classes for AMDGPU. IIUC AMDGPU need to allocate different register classes separately so it need implement its own `--<reg-class>-regalloc`. Now users can use e.g. `-passe=regallocfast<filter=sgpr>` to allocate specific register class. The command line option `--regalloc-npm` is still in work progress, plan to reuse the syntax of passes, e.g. use `--regalloc-npm=regallocfast<filter=sgpr>,greedy<filter=vgpr>` to replace `--sgpr-regalloc` and `--vgpr-regalloc`.
1 parent e96e7f1 commit 1bc8b32

36 files changed

+317
-82
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ struct MachinePassModel
6161
#ifndef NDEBUG
6262
if constexpr (is_detected<has_get_required_properties_t, PassT>::value) {
6363
auto &MFProps = IR.getProperties();
64-
auto RequiredProperties = PassT::getRequiredProperties();
64+
auto RequiredProperties = this->Pass.getRequiredProperties();
6565
if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
6666
errs() << "MachineFunctionProperties required by " << PassT::name()
6767
<< " pass are not met by function " << IR.getName() << ".\n"
@@ -78,9 +78,9 @@ struct MachinePassModel
7878
auto PA = this->Pass.run(IR, AM);
7979

8080
if constexpr (is_detected<has_get_set_properties_t, PassT>::value)
81-
IR.getProperties().set(PassT::getSetProperties());
81+
IR.getProperties().set(this->Pass.getSetProperties());
8282
if constexpr (is_detected<has_get_cleared_properties_t, PassT>::value)
83-
IR.getProperties().reset(PassT::getClearedProperties());
83+
IR.getProperties().reset(this->Pass.getClearedProperties());
8484
return PA;
8585
}
8686

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//==- RegAllocFast.h ----------- fast register allocator ----------*-C++-*-==//
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+
#ifndef LLVM_CODEGEN_REGALLOCFAST_H
10+
#define LLVM_CODEGEN_REGALLOCFAST_H
11+
12+
#include "llvm/CodeGen/MachinePassManager.h"
13+
#include "llvm/CodeGen/RegAllocCommon.h"
14+
15+
namespace llvm {
16+
17+
struct RegAllocFastPassOptions {
18+
RegClassFilterFunc Filter = allocateAllRegClasses;
19+
StringRef FilterName = "all";
20+
bool ClearVRegs = true;
21+
};
22+
23+
class RegAllocFastPass : public PassInfoMixin<RegAllocFastPass> {
24+
RegAllocFastPassOptions Opts;
25+
26+
public:
27+
RegAllocFastPass(RegAllocFastPassOptions Opts = RegAllocFastPassOptions())
28+
: Opts(Opts) {}
29+
30+
MachineFunctionProperties getRequiredProperties() {
31+
return MachineFunctionProperties().set(
32+
MachineFunctionProperties::Property::NoPHIs);
33+
}
34+
35+
MachineFunctionProperties getSetProperties() {
36+
if (Opts.ClearVRegs) {
37+
return MachineFunctionProperties().set(
38+
MachineFunctionProperties::Property::NoVRegs);
39+
}
40+
41+
return MachineFunctionProperties();
42+
}
43+
44+
MachineFunctionProperties getClearedProperties() {
45+
return MachineFunctionProperties().set(
46+
MachineFunctionProperties::Property::IsSSA);
47+
}
48+
49+
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &);
50+
51+
void printPipeline(raw_ostream &OS,
52+
function_ref<StringRef(StringRef)> MapClassName2PassName);
53+
};
54+
55+
} // namespace llvm
56+
57+
#endif // LLVM_CODEGEN_REGALLOCFAST_H

llvm/include/llvm/Passes/CodeGenPassBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/CodeGen/MachineModuleInfo.h"
4444
#include "llvm/CodeGen/MachinePassManager.h"
4545
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
46+
#include "llvm/CodeGen/RegAllocFast.h"
4647
#include "llvm/CodeGen/ReplaceWithVeclib.h"
4748
#include "llvm/CodeGen/SafeStack.h"
4849
#include "llvm/CodeGen/SelectOptimize.h"
@@ -1038,7 +1039,7 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
10381039
if (Optimized)
10391040
addPass(RAGreedyPass());
10401041
else
1041-
addPass(RAFastPass());
1042+
addPass(RegAllocFastPass());
10421043
}
10431044

10441045
/// Find and instantiate the register allocation pass requested by this target

llvm/include/llvm/Passes/MachinePassRegistry.def

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
133133
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
134134
#undef MACHINE_FUNCTION_PASS
135135

136+
#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS
137+
#define MACHINE_FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, \
138+
PARAMS)
139+
#endif
140+
MACHINE_FUNCTION_PASS_WITH_PARAMS(
141+
"regallocfast", "RegAllocFast",
142+
[](RegAllocFastPassOptions Opts) { return RegAllocFastPass(Opts); },
143+
[PB = this](StringRef Params) {
144+
return parseRegAllocFastPassOptions(*PB, Params);
145+
},
146+
"filter=reg-filter;no-clear-vregs")
147+
#undef MACHINE_FUNCTION_PASS_WITH_PARAMS
148+
136149
// After a pass is converted to new pass manager, its entry should be moved from
137150
// dummy table to the normal one. For example, for a machine function pass,
138151
// DUMMY_MACHINE_FUNCTION_PASS to MACHINE_FUNCTION_PASS.
@@ -211,7 +224,6 @@ DUMMY_MACHINE_FUNCTION_PASS("processimpdefs", ProcessImplicitDefsPass)
211224
DUMMY_MACHINE_FUNCTION_PASS("prologepilog", PrologEpilogInserterPass)
212225
DUMMY_MACHINE_FUNCTION_PASS("prologepilog-code", PrologEpilogCodeInserterPass)
213226
DUMMY_MACHINE_FUNCTION_PASS("ra-basic", RABasicPass)
214-
DUMMY_MACHINE_FUNCTION_PASS("ra-fast", RAFastPass)
215227
DUMMY_MACHINE_FUNCTION_PASS("ra-greedy", RAGreedyPass)
216228
DUMMY_MACHINE_FUNCTION_PASS("ra-pbqp", RAPBQPPass)
217229
DUMMY_MACHINE_FUNCTION_PASS("reg-usage-collector", RegUsageInfoCollectorPass)

llvm/include/llvm/Passes/PassBuilder.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "llvm/Analysis/CGSCCPassManager.h"
1919
#include "llvm/CodeGen/MachinePassManager.h"
20+
#include "llvm/CodeGen/RegAllocCommon.h"
2021
#include "llvm/IR/PassManager.h"
2122
#include "llvm/Passes/OptimizationLevel.h"
2223
#include "llvm/Support/Error.h"
@@ -388,6 +389,9 @@ class PassBuilder {
388389
/// returns false.
389390
Error parseAAPipeline(AAManager &AA, StringRef PipelineText);
390391

392+
/// Parse RegClassFilterName to get RegClassFilterFunc.
393+
RegClassFilterFunc parseRegAllocFilter(StringRef RegClassFilterName);
394+
391395
/// Print pass names.
392396
void printPassNames(raw_ostream &OS);
393397

@@ -576,6 +580,14 @@ class PassBuilder {
576580
}
577581
/// @}}
578582

583+
/// Register callbacks to parse target specific filter field if regalloc pass
584+
/// needs it. E.g. AMDGPU requires regalloc passes can handle sgpr and vgpr
585+
/// separately.
586+
void registerRegClassFilterParsingCallback(
587+
const std::function<RegClassFilterFunc(StringRef)> &C) {
588+
RegClassFilterParsingCallbacks.push_back(C);
589+
}
590+
579591
/// Register a callback for a top-level pipeline entry.
580592
///
581593
/// If the PassManager type is not given at the top level of the pipeline
@@ -792,6 +804,9 @@ class PassBuilder {
792804
ArrayRef<PipelineElement>)>,
793805
2>
794806
MachineFunctionPipelineParsingCallbacks;
807+
// Callbacks to parse `filter` parameter in register allocation passes
808+
SmallVector<std::function<RegClassFilterFunc(StringRef)>, 2>
809+
RegClassFilterParsingCallbacks;
795810
};
796811

797812
/// This utility template takes care of adding require<> and invalidate<>

0 commit comments

Comments
 (0)