Skip to content

Commit 46e9c8b

Browse files
committed
[NewPM][CodeGen] Port regallocfast to new pass manager
1 parent 9b0e1c2 commit 46e9c8b

35 files changed

+297
-82
lines changed

llvm/include/llvm/CodeGen/MachinePassManager.h

+3-3
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

+57
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "llvm/CodeGen/MachineModuleInfo.h"
4343
#include "llvm/CodeGen/MachinePassManager.h"
4444
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
45+
#include "llvm/CodeGen/RegAllocFast.h"
4546
#include "llvm/CodeGen/ReplaceWithVeclib.h"
4647
#include "llvm/CodeGen/SafeStack.h"
4748
#include "llvm/CodeGen/SelectOptimize.h"
@@ -1037,7 +1038,7 @@ void CodeGenPassBuilder<Derived, TargetMachineT>::addTargetRegisterAllocator(
10371038
if (Optimized)
10381039
addPass(RAGreedyPass());
10391040
else
1040-
addPass(RAFastPass());
1041+
addPass(RegAllocFastPass());
10411042
}
10421043

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

llvm/include/llvm/Passes/MachinePassRegistry.def

+13-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
132132
MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
133133
#undef MACHINE_FUNCTION_PASS
134134

135+
#ifndef MACHINE_FUNCTION_PASS_WITH_PARAMS
136+
#define MACHINE_FUNCTION_PASS_WITH_PARAMS(NAME, CLASS, CREATE_PASS, PARSER, \
137+
PARAMS)
138+
#endif
139+
MACHINE_FUNCTION_PASS_WITH_PARAMS(
140+
"regallocfast", "RegAllocFast",
141+
[](RegAllocFastPassOptions Opts) { return RegAllocFastPass(Opts); },
142+
[PB = this](StringRef Params) {
143+
return parseRegAllocFastPassOptions(*PB, Params);
144+
},
145+
"filter=all|reg-class;clear-vregs;no-clear-vregs")
146+
#undef MACHINE_FUNCTION_PASS_WITH_PARAMS
147+
135148
// After a pass is converted to new pass manager, its entry should be moved from
136149
// dummy table to the normal one. For example, for a machine function pass,
137150
// 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

+16
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,10 @@ class PassBuilder {
388389
/// returns false.
389390
Error parseAAPipeline(AAManager &AA, StringRef PipelineText);
390391

392+
/// Parse FilterName to get RegClassFilterFunc for given FilterName
393+
/// when register allocator need a RegClassFilterFunc.
394+
RegClassFilterFunc parseRegAllocFilter(StringRef FilterName);
395+
391396
/// Print pass names.
392397
void printPassNames(raw_ostream &OS);
393398

@@ -576,6 +581,14 @@ class PassBuilder {
576581
}
577582
/// @}}
578583

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

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

0 commit comments

Comments
 (0)