Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NewPM][CodeGen][llc] Add NPM support #70922

Merged
merged 10 commits into from
Jan 24, 2024
21 changes: 2 additions & 19 deletions llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading