Skip to content

Commit 7e50f00

Browse files
[NewPM][CodeGen][llc] Add NPM support (#70922)
Add new pass manager support to `llc`. Users can use `--passes=pass1,pass2...` to run mir passes, and use `--enable-new-pm` to run default codegen pipeline. This patch is taken from [D83612](https://reviews.llvm.org/D83612), the original author is @yuanfang-chen. --------- Co-authored-by: Yuanfang Chen <[email protected]>
1 parent c663c8b commit 7e50f00

File tree

17 files changed

+411
-203
lines changed

17 files changed

+411
-203
lines changed

llvm/include/llvm/CodeGen/CodeGenPassBuilder.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,30 +1113,13 @@ void CodeGenPassBuilder<Derived>::addTargetRegisterAllocator(
11131113
template <typename Derived>
11141114
void CodeGenPassBuilder<Derived>::addRegAllocPass(AddMachinePass &addPass,
11151115
bool Optimized) const {
1116-
if (Opt.RegAlloc == RegAllocType::Default)
1117-
// With no -regalloc= override, ask the target for a regalloc pass.
1118-
derived().addTargetRegisterAllocator(addPass, Optimized);
1119-
else if (Opt.RegAlloc == RegAllocType::Basic)
1120-
addPass(RABasicPass());
1121-
else if (Opt.RegAlloc == RegAllocType::Fast)
1122-
addPass(RAFastPass());
1123-
else if (Opt.RegAlloc == RegAllocType::Greedy)
1124-
addPass(RAGreedyPass());
1125-
else if (Opt.RegAlloc == RegAllocType::PBQP)
1126-
addPass(RAPBQPPass());
1127-
else
1128-
llvm_unreachable("unknonwn register allocator type");
1116+
// TODO: Parse Opt.RegAlloc to add register allocator.
11291117
}
11301118

11311119
template <typename Derived>
11321120
Error CodeGenPassBuilder<Derived>::addRegAssignmentFast(
11331121
AddMachinePass &addPass) const {
1134-
if (Opt.RegAlloc != RegAllocType::Default &&
1135-
Opt.RegAlloc != RegAllocType::Fast)
1136-
return make_error<StringError>(
1137-
"Must use fast (default) register allocator for unoptimized regalloc.",
1138-
inconvertibleErrorCode());
1139-
1122+
// TODO: Ensure allocator is default or fast.
11401123
addRegAllocPass(addPass, false);
11411124
return Error::success();
11421125
}

llvm/include/llvm/CodeGen/TargetPassConfig.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ class TargetPassConfig : public ImmutablePass {
171171
/// set.
172172
static bool willCompleteCodeGenPipeline();
173173

174-
/// If hasLimitedCodeGenPipeline is true, this method
175-
/// returns a string with the name of the options, separated
176-
/// by \p Separator that caused this pipeline to be limited.
177-
static std::string
178-
getLimitedCodeGenPipelineReason(const char *Separator = "/");
174+
/// If hasLimitedCodeGenPipeline is true, this method returns
175+
/// a string with the name of the options that caused this
176+
/// pipeline to be limited.
177+
static std::string getLimitedCodeGenPipelineReason();
179178

180179
struct StartStopInfo {
181180
bool StartAfter;

llvm/include/llvm/Target/CGPassBuilderOption.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct CGPassBuilderOption {
5252
bool RequiresCodeGenSCCOrder = false;
5353

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

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,7 @@ bool TargetPassConfig::hasLimitedCodeGenPipeline() {
635635
!willCompleteCodeGenPipeline();
636636
}
637637

638-
std::string
639-
TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) {
638+
std::string TargetPassConfig::getLimitedCodeGenPipelineReason() {
640639
if (!hasLimitedCodeGenPipeline())
641640
return std::string();
642641
std::string Res;
@@ -648,7 +647,7 @@ TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) {
648647
for (int Idx = 0; Idx < 4; ++Idx)
649648
if (!PassNames[Idx]->empty()) {
650649
if (!IsFirst)
651-
Res += Separator;
650+
Res += " and ";
652651
IsFirst = false;
653652
Res += OptNames[Idx];
654653
}

llvm/lib/Target/X86/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(sources
2929
X86CallFrameOptimization.cpp
3030
X86CallingConv.cpp
3131
X86CmovConversion.cpp
32+
X86CodeGenPassBuilder.cpp
3233
X86DomainReassignment.cpp
3334
X86DiscriminateMemOps.cpp
3435
X86LowerTileCopy.cpp
@@ -98,9 +99,11 @@ add_llvm_target(X86CodeGen ${sources}
9899
CodeGenTypes
99100
Core
100101
GlobalISel
102+
IRPrinter
101103
Instrumentation
102104
MC
103105
ProfileData
106+
Scalar
104107
SelectionDAG
105108
Support
106109
Target
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===-- X86CodeGenPassBuilder.cpp ---------------------------------*- 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+
/// \file
9+
/// This file contains X86 CodeGen pipeline builder.
10+
/// TODO: Port CodeGen passes to new pass manager.
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "X86TargetMachine.h"
14+
15+
#include "llvm/CodeGen/CodeGenPassBuilder.h"
16+
#include "llvm/MC/MCStreamer.h"
17+
18+
using namespace llvm;
19+
20+
namespace {
21+
22+
class X86CodeGenPassBuilder : public CodeGenPassBuilder<X86CodeGenPassBuilder> {
23+
public:
24+
explicit X86CodeGenPassBuilder(LLVMTargetMachine &TM,
25+
CGPassBuilderOption Opts,
26+
PassInstrumentationCallbacks *PIC)
27+
: CodeGenPassBuilder(TM, Opts, PIC) {}
28+
void addPreISel(AddIRPass &addPass) const;
29+
void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const;
30+
Error addInstSelector(AddMachinePass &) const;
31+
};
32+
33+
void X86CodeGenPassBuilder::addPreISel(AddIRPass &addPass) const {
34+
// TODO: Add passes pre instruction selection.
35+
}
36+
37+
void X86CodeGenPassBuilder::addAsmPrinter(AddMachinePass &addPass,
38+
CreateMCStreamer) const {
39+
// TODO: Add AsmPrinter.
40+
}
41+
42+
Error X86CodeGenPassBuilder::addInstSelector(AddMachinePass &) const {
43+
// TODO: Add instruction selector.
44+
return Error::success();
45+
}
46+
47+
} // namespace
48+
49+
Error X86TargetMachine::buildCodeGenPipeline(
50+
ModulePassManager &MPM, MachineFunctionPassManager &MFPM,
51+
MachineFunctionAnalysisManager &, raw_pwrite_stream &Out,
52+
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
53+
CGPassBuilderOption Opt, PassInstrumentationCallbacks *PIC) {
54+
auto CGPB = X86CodeGenPassBuilder(*this, Opt, PIC);
55+
return CGPB.buildPipeline(MPM, MFPM, Out, DwoOut, FileType);
56+
}

llvm/lib/Target/X86/X86TargetMachine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ class X86TargetMachine final : public LLVMTargetMachine {
5858
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
5959
const TargetSubtargetInfo *STI) const override;
6060

61+
Error buildCodeGenPipeline(ModulePassManager &, MachineFunctionPassManager &,
62+
MachineFunctionAnalysisManager &,
63+
raw_pwrite_stream &, raw_pwrite_stream *,
64+
CodeGenFileType, CGPassBuilderOption,
65+
PassInstrumentationCallbacks *) override;
66+
6167
bool isJIT() const { return IsJIT; }
6268

6369
bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
if not "X86" in config.root.targets:
2+
config.unsupported = True
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
; RUN: not llc -mtriple=x86_64-pc-linux-gnu -passes=foo -start-before=mergeicmps -stop-after=gc-lowering -filetype=null %s 2>&1 | FileCheck %s
2+
3+
; CHECK: warning: --passes cannot be used with start-before and stop-after.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
; RUN: llc -mtriple=x86_64-pc-linux-gnu -enable-new-pm -print-pipeline-passes -filetype=null %s | FileCheck %s
2+
3+
; CHECK: require<profile-summary>,require<collector-metadata>
4+
; CHECK: MachineSanitizerBinaryMetadata,FreeMachineFunctionPass
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
; RUN: llc -mtriple=x86_64-pc-linux-gnu -enable-new-pm -print-pipeline-passes -start-before=mergeicmps -stop-after=gc-lowering -filetype=null %s | FileCheck --match-full-lines %s
2+
3+
; CHECK: IR pipeline: function(mergeicmps,expand-memcmp,gc-lowering)
4+

llvm/tools/llc/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ set(LLVM_LINK_COMPONENTS
88
CodeGen
99
CodeGenTypes
1010
Core
11+
IRPrinter
1112
IRReader
1213
MC
1314
MIRParser
15+
Passes
1416
Remarks
1517
ScalarOpts
1618
SelectionDAG
@@ -23,6 +25,7 @@ set(LLVM_LINK_COMPONENTS
2325

2426
add_llvm_tool(llc
2527
llc.cpp
28+
NewPMDriver.cpp
2629

2730
DEPENDS
2831
intrinsics_gen

0 commit comments

Comments
 (0)