Skip to content

Add adjustPassManager to run target specific passes #283

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
27 changes: 27 additions & 0 deletions deps/LLVMExtra/include/LLVMExtra.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "llvm/Config/llvm-config.h"
#include <llvm-c/Core.h>
#include <llvm-c/Types.h>
typedef struct LLVMOpaqueTargetMachine *LLVMTargetMachineRef;
// Can't include TargetMachine since that would inclue LLVMInitializeNativeTarget
// #include <llvm-c/TargetMachine.h>
#include <llvm-c/Transforms/PassManagerBuilder.h>

LLVM_C_EXTERN_C_BEGIN

Expand Down Expand Up @@ -159,6 +163,29 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
LLVMValueRef *Args, unsigned NumArgs,
LLVMOperandBundleDefRef *Bundles, unsigned NumBundles,
const char *Name);
void LLVMAdjustPassManager(LLVMTargetMachineRef TM, LLVMPassManagerBuilderRef PMB);

typedef void (*LLVMPassManagerBuilderExtensionFunction)(
void *Ctx, LLVMPassManagerBuilderRef PMB, LLVMPassManagerRef PM);

typedef enum {
EP_EarlyAsPossible,
EP_ModuleOptimizerEarly,
EP_LoopOptimizerEnd,
EP_ScalarOptimizerLate,
EP_OptimizerLast,
EP_VectorizerStart,
EP_EnabledOnOptLevel0,
EP_Peephole,
EP_LateLoopOptimizations,
EP_CGSCCOptimizerLate,
EP_FullLinkTimeOptimizationEarly,
EP_FullLinkTimeOptimizationLast,
} LLVMPassManagerBuilderExtensionPointTy;

void LLVMPassManagerBuilderAddExtension(LLVMPassManagerBuilderRef PMB,
LLVMPassManagerBuilderExtensionPointTy Ty,
LLVMPassManagerBuilderExtensionFunction Fn, void *Ctx);

LLVM_C_EXTERN_C_END
#endif
16 changes: 16 additions & 0 deletions deps/LLVMExtra/lib/llvm-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#endif
#include <llvm/Transforms/Utils/Cloning.h>
#include <llvm/Transforms/Utils/ModuleUtils.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>

using namespace llvm;
using namespace llvm::legacy;
Expand Down Expand Up @@ -548,3 +550,17 @@ LLVMValueRef LLVMBuildCallWithOpBundle(LLVMBuilderRef B, LLVMValueRef Fn,
return wrap(unwrap(B)->CreateCall(FnT, unwrap(Fn), makeArrayRef(unwrap(Args), NumArgs),
BundleArray, Name));
}

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
void LLVMAdjustPassManager(LLVMTargetMachineRef TM, LLVMPassManagerBuilderRef PMB) {
unwrap(TM)->adjustPassManager(*unwrap(PMB));
}

void LLVMPassManagerBuilderAddExtension(LLVMPassManagerBuilderRef PMB,
LLVMPassManagerBuilderExtensionPointTy Ty,
LLVMPassManagerBuilderExtensionFunction Fn, void *Ctx) {
PassManagerBuilder &Builder = *unwrap(PMB);
Builder.addExtension((PassManagerBuilder::ExtensionPointTy)Ty,
[&](const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
Fn(Ctx, wrap(const_cast<PassManagerBuilder*>(&Builder)), wrap(&PM)); });
}
25 changes: 25 additions & 0 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,28 @@ function LLVMBuildCallWithOpBundle(B, Fn, Args, NumArgs, Bundles, NumBundles, Na
ccall((:LLVMBuildCallWithOpBundle, libLLVMExtra), LLVMValueRef, (LLVMBuilderRef, LLVMValueRef, Ptr{LLVMValueRef}, Cuint, Ptr{LLVMOperandBundleDefRef}, Cuint, Cstring), B, Fn, Args, NumArgs, Bundles, NumBundles, Name)
end

function LLVMAdjustPassManager(TM, PMB)
ccall((:LLVMAdjustPassManager, libLLVMExtra), Cvoid, (LLVMTargetMachineRef, LLVMPassManagerBuilderRef), TM, PMB)
end

const LLVMPassManagerBuilderExtensionFunction = Ptr{Cvoid}

# See llvm/include/llvm/Transforms/IPO/PassManagerBuilder.h
@cenum LLVMPassManagerBuilderExtensionPointTy::UInt32 begin
EP_EarlyAsPossible = 0
EP_ModuleOptimizerEarly = 1
EP_LoopOptimizerEnd = 2
EP_ScalarOptimizerLate = 3
EP_OptimizerLast = 4
EP_VectorizerStart = 5
EP_EnabledOnOptLevel0 = 6
EP_Peephole = 7
EP_LateLoopOptimizations= 8
EP_CGSCCOptimizerLate = 9
EP_FullLinkTimeOptimizationEarly = 10
EP_FullLinkTimeOptimizationLast = 11
end

function LLVMPassManagerBuilderAddExtension(PMB, Ty, Fn, Ctx)
ccall((:LLVMPassManagerBuilderAddExtension, libLLVMExtra), Cvoid, (LLVMPassManagerBuilderRef, LLVMPassManagerBuilderExtensionPointTy, LLVMPassManagerBuilderExtensionFunction, Ptr{Cvoid}), PMB, Ty, Fn, Ctx)
end
6 changes: 6 additions & 0 deletions src/targetmachine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ TargetMachine(t::Target, triple::String, cpu::String="", features::String="";
TargetMachine(API.LLVMCreateTargetMachine(t, triple, cpu, features, optlevel,
reloc, code))

function TargetMachine()
host_triple = triple()
host_t = Target(triple=host_triple)
TargetMachine(host_t, host_triple)
end

dispose(tm::TargetMachine) = API.LLVMDisposeTargetMachine(tm)

function TargetMachine(f::Core.Function, args...; kwargs...)
Expand Down
25 changes: 24 additions & 1 deletion src/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
export PassManagerBuilder, dispose,
optlevel!, sizelevel!,
unit_at_a_time!, unroll_loops!, simplify_libcalls!, inliner!,
populate!
populate!, adjust!, extend!

mutable struct Callback
fn
end

@checked struct PassManagerBuilder
ref::API.LLVMPassManagerBuilderRef
rooted::Vector{Callback}
end
PassManagerBuilder(ref) = PassManagerBuilder(ref, Callback[])

Base.unsafe_convert(::Type{API.LLVMPassManagerBuilderRef}, pmb::PassManagerBuilder) = pmb.ref

Expand Down Expand Up @@ -50,6 +56,23 @@ populate!(fpm::FunctionPassManager, pmb::PassManagerBuilder) =
populate!(mpm::ModulePassManager, pmb::PassManagerBuilder) =
API.LLVMPassManagerBuilderPopulateModulePassManager(pmb, mpm)

adjust!(pmb::PassManagerBuilder, tm::TargetMachine) =
API.LLVMAdjustPassManager(tm, pmb)

function extend_callback(ctx, pmb, pm)
cb = Base.unsafe_pointer_to_objref(ctx)
cb.fn(PassManagerBuilder(pmb), PassManager(pm))
return nothing
end

function extend!(pmb::PassManagerBuilder, ep, fn)
cb = Callback(fn)
push!(pmb.rooted, cb) # root through pmb
c_cb = @cfunction(extend_callback, Cvoid, (Ptr{Cvoid}, API.LLVMPassManagerBuilderRef, API.LLVMPassManagerRef))
API.LLVMPassManagerBuilderAddExtension(pmb, ep, c_cb, Base.pointer_from_objref(cb))
end

extend!(fn, pmb::PassManagerBuilder, ep) = extend!(pmb, ep, fn)

## auxiliary

Expand Down
8 changes: 8 additions & 0 deletions test/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ PassManagerBuilder() do pmb
simplify_libcalls!(pmb, false)
inliner!(pmb, 0)

TargetMachine() do tm
adjust!(pmb, tm)
end

extend!(pmb, LLVM.API.EP_EarlyAsPossible) do pmb, pm
verifier!(pm)
end

Context() do ctx
LLVM.Module("SomeModule"; ctx) do mod
FunctionPassManager(mod) do fpm
Expand Down