Skip to content

Commit

Permalink
[NewPM][CodeGen][llc] Add NPM support (#70922)
Browse files Browse the repository at this point in the history
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 <455423+yuanfang-chen@users.noreply.github.com>
  • Loading branch information
paperchalice and yuanfang-chen committed Jan 24, 2024
1 parent c663c8b commit 7e50f00
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 203 deletions.
21 changes: 2 additions & 19 deletions llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
Expand Up @@ -1113,30 +1113,13 @@ void CodeGenPassBuilder<Derived>::addTargetRegisterAllocator(
template <typename Derived>
void CodeGenPassBuilder<Derived>::addRegAllocPass(AddMachinePass &addPass,
bool Optimized) const {
if (Opt.RegAlloc == RegAllocType::Default)
// With no -regalloc= override, ask the target for a regalloc pass.
derived().addTargetRegisterAllocator(addPass, Optimized);
else if (Opt.RegAlloc == RegAllocType::Basic)
addPass(RABasicPass());
else if (Opt.RegAlloc == RegAllocType::Fast)
addPass(RAFastPass());
else if (Opt.RegAlloc == RegAllocType::Greedy)
addPass(RAGreedyPass());
else if (Opt.RegAlloc == RegAllocType::PBQP)
addPass(RAPBQPPass());
else
llvm_unreachable("unknonwn register allocator type");
// TODO: Parse Opt.RegAlloc to add register allocator.
}

template <typename Derived>
Error CodeGenPassBuilder<Derived>::addRegAssignmentFast(
AddMachinePass &addPass) const {
if (Opt.RegAlloc != RegAllocType::Default &&
Opt.RegAlloc != RegAllocType::Fast)
return make_error<StringError>(
"Must use fast (default) register allocator for unoptimized regalloc.",
inconvertibleErrorCode());

// TODO: Ensure allocator is default or fast.
addRegAllocPass(addPass, false);
return Error::success();
}
Expand Down
9 changes: 4 additions & 5 deletions llvm/include/llvm/CodeGen/TargetPassConfig.h
Expand Up @@ -171,11 +171,10 @@ class TargetPassConfig : public ImmutablePass {
/// set.
static bool willCompleteCodeGenPipeline();

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

struct StartStopInfo {
bool StartAfter;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Target/CGPassBuilderOption.h
Expand Up @@ -52,7 +52,7 @@ struct CGPassBuilderOption {
bool RequiresCodeGenSCCOrder = false;

RunOutliner EnableMachineOutliner = RunOutliner::TargetDefault;
RegAllocType RegAlloc = RegAllocType::Default;
StringRef RegAlloc = "default";
std::optional<GlobalISelAbortMode> EnableGlobalISelAbort;
std::string FSProfileFile;
std::string FSRemappingFile;
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/CodeGen/TargetPassConfig.cpp
Expand Up @@ -635,8 +635,7 @@ bool TargetPassConfig::hasLimitedCodeGenPipeline() {
!willCompleteCodeGenPipeline();
}

std::string
TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) {
std::string TargetPassConfig::getLimitedCodeGenPipelineReason() {
if (!hasLimitedCodeGenPipeline())
return std::string();
std::string Res;
Expand All @@ -648,7 +647,7 @@ TargetPassConfig::getLimitedCodeGenPipelineReason(const char *Separator) {
for (int Idx = 0; Idx < 4; ++Idx)
if (!PassNames[Idx]->empty()) {
if (!IsFirst)
Res += Separator;
Res += " and ";
IsFirst = false;
Res += OptNames[Idx];
}
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/X86/CMakeLists.txt
Expand Up @@ -29,6 +29,7 @@ set(sources
X86CallFrameOptimization.cpp
X86CallingConv.cpp
X86CmovConversion.cpp
X86CodeGenPassBuilder.cpp
X86DomainReassignment.cpp
X86DiscriminateMemOps.cpp
X86LowerTileCopy.cpp
Expand Down Expand Up @@ -98,9 +99,11 @@ add_llvm_target(X86CodeGen ${sources}
CodeGenTypes
Core
GlobalISel
IRPrinter
Instrumentation
MC
ProfileData
Scalar
SelectionDAG
Support
Target
Expand Down
56 changes: 56 additions & 0 deletions llvm/lib/Target/X86/X86CodeGenPassBuilder.cpp
@@ -0,0 +1,56 @@
//===-- X86CodeGenPassBuilder.cpp ---------------------------------*- C++ -*-=//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
/// This file contains X86 CodeGen pipeline builder.
/// TODO: Port CodeGen passes to new pass manager.
//===----------------------------------------------------------------------===//

#include "X86TargetMachine.h"

#include "llvm/CodeGen/CodeGenPassBuilder.h"
#include "llvm/MC/MCStreamer.h"

using namespace llvm;

namespace {

class X86CodeGenPassBuilder : public CodeGenPassBuilder<X86CodeGenPassBuilder> {
public:
explicit X86CodeGenPassBuilder(LLVMTargetMachine &TM,
CGPassBuilderOption Opts,
PassInstrumentationCallbacks *PIC)
: CodeGenPassBuilder(TM, Opts, PIC) {}
void addPreISel(AddIRPass &addPass) const;
void addAsmPrinter(AddMachinePass &, CreateMCStreamer) const;
Error addInstSelector(AddMachinePass &) const;
};

void X86CodeGenPassBuilder::addPreISel(AddIRPass &addPass) const {
// TODO: Add passes pre instruction selection.
}

void X86CodeGenPassBuilder::addAsmPrinter(AddMachinePass &addPass,
CreateMCStreamer) const {
// TODO: Add AsmPrinter.
}

Error X86CodeGenPassBuilder::addInstSelector(AddMachinePass &) const {
// TODO: Add instruction selector.
return Error::success();
}

} // namespace

Error X86TargetMachine::buildCodeGenPipeline(
ModulePassManager &MPM, MachineFunctionPassManager &MFPM,
MachineFunctionAnalysisManager &, raw_pwrite_stream &Out,
raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
CGPassBuilderOption Opt, PassInstrumentationCallbacks *PIC) {
auto CGPB = X86CodeGenPassBuilder(*this, Opt, PIC);
return CGPB.buildPipeline(MPM, MFPM, Out, DwoOut, FileType);
}
6 changes: 6 additions & 0 deletions llvm/lib/Target/X86/X86TargetMachine.h
Expand Up @@ -58,6 +58,12 @@ class X86TargetMachine final : public LLVMTargetMachine {
createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
const TargetSubtargetInfo *STI) const override;

Error buildCodeGenPipeline(ModulePassManager &, MachineFunctionPassManager &,
MachineFunctionAnalysisManager &,
raw_pwrite_stream &, raw_pwrite_stream *,
CodeGenFileType, CGPassBuilderOption,
PassInstrumentationCallbacks *) override;

bool isJIT() const { return IsJIT; }

bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override;
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/tools/llc/new-pm/lit.local.cfg
@@ -0,0 +1,2 @@
if not "X86" in config.root.targets:
config.unsupported = True
3 changes: 3 additions & 0 deletions llvm/test/tools/llc/new-pm/option-conflict.ll
@@ -0,0 +1,3 @@
; 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

; CHECK: warning: --passes cannot be used with start-before and stop-after.
5 changes: 5 additions & 0 deletions llvm/test/tools/llc/new-pm/pipeline.ll
@@ -0,0 +1,5 @@
; RUN: llc -mtriple=x86_64-pc-linux-gnu -enable-new-pm -print-pipeline-passes -filetype=null %s | FileCheck %s

; CHECK: require<profile-summary>,require<collector-metadata>
; CHECK: MachineSanitizerBinaryMetadata,FreeMachineFunctionPass

4 changes: 4 additions & 0 deletions llvm/test/tools/llc/new-pm/start-stop.ll
@@ -0,0 +1,4 @@
; 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

; CHECK: IR pipeline: function(mergeicmps,expand-memcmp,gc-lowering)

3 changes: 3 additions & 0 deletions llvm/tools/llc/CMakeLists.txt
Expand Up @@ -8,9 +8,11 @@ set(LLVM_LINK_COMPONENTS
CodeGen
CodeGenTypes
Core
IRPrinter
IRReader
MC
MIRParser
Passes
Remarks
ScalarOpts
SelectionDAG
Expand All @@ -23,6 +25,7 @@ set(LLVM_LINK_COMPONENTS

add_llvm_tool(llc
llc.cpp
NewPMDriver.cpp

DEPENDS
intrinsics_gen
Expand Down

0 comments on commit 7e50f00

Please sign in to comment.