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

[LLVM] Switch to using New Pass Manager (NPM) with LLVM 16+ #13515

Merged
merged 1 commit into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/target/llvm/codegen_amdgpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,12 @@ class CodeGenAMDGPU : public CodeGenLLVM {
}
}

#if TVM_LLVM_VERSION < 160
// This function only works with the legacy pass manager.
void InitPassManagerBuilder(llvm::PassManagerBuilder* builder) final {
// Additional optimization hook to tweak the builder.
}
#endif

unsigned GetGlobalAddressSpace() const final { return 1; }

Expand Down
69 changes: 67 additions & 2 deletions src/target/llvm/codegen_llvm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/MDBuilder.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Linker/Linker.h>
#include <llvm/Pass.h>
#if TVM_LLVM_VERSION >= 160
#include <llvm/IR/Verifier.h> // For VerifierPass
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/StandardInstrumentations.h>
#else
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#endif
#if TVM_LLVM_VERSION >= 100
#include <llvm/Support/Alignment.h>
#include <llvm/Support/TypeSize.h>
Expand All @@ -75,7 +82,6 @@
#include <llvm/Support/SourceMgr.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Transforms/Utils/ModuleUtils.h>
#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/crt/error_codes.h>
Expand Down Expand Up @@ -351,6 +357,64 @@ llvm::Value* CodeGenLLVM::CreateStorageSync(const CallNode* op) {
return nullptr;
}

#if TVM_LLVM_VERSION >= 160

// Use new pass manager

void CodeGenLLVM::Optimize() {
llvm::TargetMachine* tm = llvm_target_->GetOrCreateTargetMachine();

bool debug_logging = false;
bool verify_each = false;

llvm::PipelineTuningOptions pto = llvm::PipelineTuningOptions();
llvm::PassInstrumentationCallbacks pic;
llvm::PassBuilder builder(tm, pto, llvm::None, &pic);

llvm::LoopAnalysisManager lam;
llvm::FunctionAnalysisManager fam;
llvm::CGSCCAnalysisManager cgam;
llvm::ModuleAnalysisManager mam;
builder.registerLoopAnalyses(lam);
builder.registerFunctionAnalyses(fam);
builder.registerCGSCCAnalyses(cgam);
builder.registerModuleAnalyses(mam);
builder.crossRegisterProxies(lam, fam, cgam, mam);

// Construct the default pass pipeline depending on the opt level.
std::string pipeline;
switch (llvm_target_->GetOptLevel()) {
case llvm::CodeGenOpt::Level::None:
pipeline = "default<O0>";
break;
case llvm::CodeGenOpt::Level::Less:
pipeline = "default<O1>";
break;
case llvm::CodeGenOpt::Level::Default:
pipeline = "default<O2>";
break;
default:
// CodeGenOpt::Level::Aggressive
pipeline = "default<O3>";
break;
}

llvm::StandardInstrumentations si(*llvm_target_->GetContext(), debug_logging, verify_each);
si.registerCallbacks(pic, &fam);
llvm::ModulePassManager mpass;
if (verify_each) {
mpass.addPass(llvm::VerifierPass());
}
if (auto err = builder.parsePassPipeline(mpass, pipeline)) {
LOG(FATAL) << "error parsing pass pipeline '" << pipeline
<< "':" << llvm::toString(std::move(err)) << '\n';
}

mpass.run(*module_, mam);
}

#else // TVM_LLVM_VERSION

class FPassManager : public llvm::legacy::FunctionPassManager {
public:
explicit FPassManager(llvm::Module* m) : llvm::legacy::FunctionPassManager(m) {}
Expand Down Expand Up @@ -420,6 +484,7 @@ void CodeGenLLVM::Optimize() {
fpass.doFinalization();
mpass.run(*module_);
}
#endif // TVM_LLVM_VERSION

int CodeGenLLVM::NativeVectorBits(const runtime::StorageScope& storage_scope) const {
return native_vector_bits_;
Expand Down
3 changes: 3 additions & 0 deletions src/target/llvm/codegen_llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,11 @@ class CodeGenLLVM : public ExprFunctor<llvm::Value*(const PrimExpr&)>,
virtual llvm::Value* GetThreadIndex(const IterVar& iv);
// Get the corresponding thread index
virtual llvm::Value* CreateStorageSync(const CallNode* op);
#if TVM_LLVM_VERSION < 160
// This function only works with the legacy pass manager.
// apply optimization on the module.
virtual void InitPassManagerBuilder(llvm::PassManagerBuilder* builder);
#endif
// Scalarize by iterating elements of e.
// f is a callback that takes index and v.
void Scalarize(const PrimExpr& e, std::function<void(int i, llvm::Value* v)> f);
Expand Down
3 changes: 3 additions & 0 deletions src/target/llvm/codegen_nvptx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,12 @@ class CodeGenNVPTX : public CodeGenLLVM {
}
}

#if TVM_LLVM_VERSION < 160
// This function only works with the legacy pass manager.
void InitPassManagerBuilder(llvm::PassManagerBuilder* builder) final {
// Additional optimization hook to tweak the builder.
}
#endif

void Optimize() final {
for (auto& f : *module_) {
Expand Down