Skip to content

Commit

Permalink
Add Julia newpm passes and pipeline.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchintalapudi authored and maleadt committed Sep 4, 2023
1 parent 987a230 commit 2a623f6
Show file tree
Hide file tree
Showing 14 changed files with 823 additions and 119 deletions.
13 changes: 8 additions & 5 deletions deps/LLVMExtra/include/NewPM.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ typedef struct LLVMOpaqueModuleAnalysisManager *LLVMModuleAnalysisManagerRef;
typedef struct LLVMOpaqueCGSCCAnalysisManager *LLVMCGSCCAnalysisManagerRef;
typedef struct LLVMOpaqueFunctionAnalysisManager *LLVMFunctionAnalysisManagerRef;
typedef struct LLVMOpaqueLoopAnalysisManager *LLVMLoopAnalysisManagerRef;
typedef struct LLVMOpaqueAAManager *LLVMAAManagerRef;

LLVMModuleAnalysisManagerRef LLVMCreateNewPMModuleAnalysisManager(void);
LLVMCGSCCAnalysisManagerRef LLVMCreateNewPMCGSCCAnalysisManager(void);
LLVMFunctionAnalysisManagerRef LLVMCreateNewPMFunctionAnalysisManager(void);
LLVMLoopAnalysisManagerRef LLVMCreateNewPMLoopAnalysisManager(void);
LLVMAAManagerRef LLVMCreateNewPMAAManager(void);

void LLVMDisposeNewPMModuleAnalysisManager(LLVMModuleAnalysisManagerRef AM);
void LLVMDisposeNewPMCGSCCAnalysisManager(LLVMCGSCCAnalysisManagerRef AM);
void LLVMDisposeNewPMFunctionAnalysisManager(LLVMFunctionAnalysisManagerRef AM);
void LLVMDisposeNewPMLoopAnalysisManager(LLVMLoopAnalysisManagerRef AM);
void LLVMDisposeNewPMAAManager(LLVMAAManagerRef AM);

typedef struct LLVMOpaqueModulePassManager *LLVMModulePassManagerRef;
typedef struct LLVMOpaqueCGSCCPassManager *LLVMCGSCCPassManagerRef;
Expand Down Expand Up @@ -79,7 +76,6 @@ LLVMErrorRef LLVMPassBuilderParseModulePassPipeline(LLVMPassBuilderRef PB, LLVMM
LLVMErrorRef LLVMPassBuilderParseCGSCCPassPipeline(LLVMPassBuilderRef PB, LLVMCGSCCPassManagerRef PM, const char *PipelineText, size_t PipelineTextLength);
LLVMErrorRef LLVMPassBuilderParseFunctionPassPipeline(LLVMPassBuilderRef PB, LLVMFunctionPassManagerRef PM, const char *PipelineText, size_t PipelineTextLength);
LLVMErrorRef LLVMPassBuilderParseLoopPassPipeline(LLVMPassBuilderRef PB, LLVMLoopPassManagerRef PM, const char *PipelineText, size_t PipelineTextLength);
LLVMErrorRef LLVMPassBuilderParseAAPipeline(LLVMPassBuilderRef PB, LLVMAAManagerRef AM, const char *PipelineText, size_t PipelineTextLength);

void LLVMPassBuilderRegisterModuleAnalyses(LLVMPassBuilderRef PB, LLVMModuleAnalysisManagerRef AM);
void LLVMPassBuilderRegisterCGSCCAnalyses(LLVMPassBuilderRef PB, LLVMCGSCCAnalysisManagerRef AM);
Expand All @@ -95,7 +91,7 @@ void LLVMLPMAddLPM(LLVMLoopPassManagerRef PM, LLVMLoopPassManagerRef NestedPM);

void LLVMMPMAddCGPM(LLVMModulePassManagerRef PM, LLVMCGSCCPassManagerRef NestedPM);
void LLVMCGPMAddFPM(LLVMCGSCCPassManagerRef PM, LLVMFunctionPassManagerRef NestedPM);
void LLVMFPMAddLPM(LLVMFunctionPassManagerRef PM, LLVMLoopPassManagerRef NestedPM);
void LLVMFPMAddLPM(LLVMFunctionPassManagerRef PM, LLVMLoopPassManagerRef NestedPM, LLVMBool UseMemorySSA);
void LLVMMPMAddFPM(LLVMModulePassManagerRef PM, LLVMFunctionPassManagerRef NestedPM);

typedef LLVMPreservedAnalysesRef (*LLVMJuliaModulePassCallback)(LLVMModuleRef M, LLVMModuleAnalysisManagerRef AM, void *Thunk);
Expand All @@ -104,6 +100,13 @@ typedef LLVMPreservedAnalysesRef (*LLVMJuliaFunctionPassCallback)(LLVMValueRef F
void LLVMMPMAddJuliaPass(LLVMModulePassManagerRef PM, LLVMJuliaModulePassCallback Callback, void *Thunk);
void LLVMFPMAddJuliaPass(LLVMFunctionPassManagerRef PM, LLVMJuliaFunctionPassCallback Callback, void *Thunk);

// Target Analyses
LLVMBool LLVMRegisterTargetIRAnalysis(LLVMFunctionAnalysisManagerRef FAM, LLVMTargetMachineRef TM);
LLVMBool LLVMRegisterTargetLibraryAnalysis(LLVMFunctionAnalysisManagerRef FAM, const char *Triple, size_t TripleLength);

// Analyses
LLVMErrorRef LLVMRegisterAliasAnalyses(LLVMFunctionAnalysisManagerRef FAM, LLVMPassBuilderRef PB, LLVMTargetMachineRef TM, const char *Analyses, size_t AnalysesLength);

LLVM_C_EXTERN_C_END

#endif
Expand Down
39 changes: 26 additions & 13 deletions deps/LLVMExtra/lib/newpm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#if LLVM_VERSION_MAJOR >= 15

#include <llvm/Analysis/AliasAnalysis.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/StandardInstrumentations.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/Target/TargetMachine.h>

#include "llvm/Support/CBindingWrapping.h"
#include <llvm/Support/CBindingWrapping.h>

using llvm::wrap;
using llvm::unwrap;
Expand Down Expand Up @@ -43,7 +45,6 @@ DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::ModuleAnalysisManager, LLVMModuleAnalys
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::CGSCCAnalysisManager, LLVMCGSCCAnalysisManagerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::FunctionAnalysisManager, LLVMFunctionAnalysisManagerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::LoopAnalysisManager, LLVMLoopAnalysisManagerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::AAManager, LLVMAAManagerRef)

LLVMModuleAnalysisManagerRef LLVMCreateNewPMModuleAnalysisManager(void) {
return wrap(new llvm::ModuleAnalysisManager());
Expand All @@ -57,9 +58,6 @@ LLVMFunctionAnalysisManagerRef LLVMCreateNewPMFunctionAnalysisManager(void) {
LLVMLoopAnalysisManagerRef LLVMCreateNewPMLoopAnalysisManager(void) {
return wrap(new llvm::LoopAnalysisManager());
}
LLVMAAManagerRef LLVMCreateNewPMAAManager(void) {
return wrap(new llvm::AAManager());
}

void LLVMDisposeNewPMModuleAnalysisManager(LLVMModuleAnalysisManagerRef AM) {
delete unwrap(AM);
Expand All @@ -73,9 +71,6 @@ void LLVMDisposeNewPMFunctionAnalysisManager(LLVMFunctionAnalysisManagerRef AM)
void LLVMDisposeNewPMLoopAnalysisManager(LLVMLoopAnalysisManagerRef AM) {
delete unwrap(AM);
}
void LLVMDisposeNewPMAAManager(LLVMAAManagerRef AM) {
delete unwrap(AM);
}

DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::ModulePassManager, LLVMModulePassManagerRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::CGSCCPassManager, LLVMCGSCCPassManagerRef)
Expand Down Expand Up @@ -158,9 +153,6 @@ LLVMErrorRef LLVMPassBuilderParseFunctionPassPipeline(LLVMPassBuilderRef PB, LLV
LLVMErrorRef LLVMPassBuilderParseLoopPassPipeline(LLVMPassBuilderRef PB, LLVMLoopPassManagerRef PM, const char *PipelineText, size_t PipelineTextLength) {
return wrap(unwrap(PB)->parsePassPipeline(*unwrap(PM), llvm::StringRef(PipelineText, PipelineTextLength)));
}
LLVMErrorRef LLVMPassBuilderParseAAPipeline(LLVMPassBuilderRef PB, LLVMAAManagerRef AM, const char *PipelineText, size_t PipelineTextLength) {
return wrap(unwrap(PB)->parseAAPipeline(*unwrap(AM), llvm::StringRef(PipelineText, PipelineTextLength)));
}

void LLVMPassBuilderRegisterModuleAnalyses(LLVMPassBuilderRef PB, LLVMModuleAnalysisManagerRef AM) {
unwrap(PB)->registerModuleAnalyses(*unwrap(AM));
Expand Down Expand Up @@ -198,8 +190,8 @@ void LLVMMPMAddCGPM(LLVMModulePassManagerRef PM, LLVMCGSCCPassManagerRef NestedP
void LLVMCGPMAddFPM(LLVMCGSCCPassManagerRef PM, LLVMFunctionPassManagerRef NestedPM) {
unwrap(PM)->addPass(llvm::createCGSCCToFunctionPassAdaptor(std::move(*unwrap(NestedPM))));
}
void LLVMFPMAddLPM(LLVMFunctionPassManagerRef PM, LLVMLoopPassManagerRef NestedPM) {
unwrap(PM)->addPass(llvm::createFunctionToLoopPassAdaptor(std::move(*unwrap(NestedPM))));
void LLVMFPMAddLPM(LLVMFunctionPassManagerRef PM, LLVMLoopPassManagerRef NestedPM, LLVMBool UseMemorySSA) {
unwrap(PM)->addPass(llvm::createFunctionToLoopPassAdaptor(std::move(*unwrap(NestedPM)), UseMemorySSA));
}
void LLVMMPMAddFPM(LLVMModulePassManagerRef PM, LLVMFunctionPassManagerRef NestedPM) {
unwrap(PM)->addPass(llvm::createModuleToFunctionPassAdaptor(std::move(*unwrap(NestedPM))));
Expand Down Expand Up @@ -239,4 +231,25 @@ void LLVMFPMAddJuliaPass(LLVMFunctionPassManagerRef PM, LLVMJuliaFunctionPassCal
unwrap(PM)->addPass(JuliaCustomFunctionPass(Callback, Thunk));
}

// Target Analyses

LLVMBool LLVMRegisterTargetIRAnalysis(LLVMFunctionAnalysisManagerRef FAM, LLVMTargetMachineRef TM) {
return unwrap(FAM)->registerPass([&] { return llvm::TargetIRAnalysis(unwrap(TM)->getTargetIRAnalysis()); });
}
LLVMBool LLVMRegisterTargetLibraryAnalysis(LLVMFunctionAnalysisManagerRef FAM, const char *Triple, size_t TripleLength) {
return unwrap(FAM)->registerPass([&] { return llvm::TargetLibraryAnalysis(llvm::TargetLibraryInfoImpl(llvm::Triple(llvm::StringRef(Triple, TripleLength)))); });
}

// Alias Analyses
LLVMErrorRef LLVMRegisterAliasAnalyses(LLVMFunctionAnalysisManagerRef FAM, LLVMPassBuilderRef PB, LLVMTargetMachineRef TM, const char *Analyses, size_t AnalysesLength) {
llvm::AAManager AA;
auto err = unwrap(PB)->parseAAPipeline(AA, llvm::StringRef(Analyses, AnalysesLength));
if (err)
return wrap(std::move(err));
if (TM)
unwrap(TM)->registerDefaultAliasAnalyses(AA);
unwrap(FAM)->registerPass([&] { return std::move(AA); });
return LLVMErrorSuccess;
}

#endif
33 changes: 14 additions & 19 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,6 @@ mutable struct LLVMOpaqueLoopAnalysisManager end

const LLVMLoopAnalysisManagerRef = Ptr{LLVMOpaqueLoopAnalysisManager}

mutable struct LLVMOpaqueAAManager end

const LLVMAAManagerRef = Ptr{LLVMOpaqueAAManager}

function LLVMCreateNewPMModuleAnalysisManager()
ccall((:LLVMCreateNewPMModuleAnalysisManager, libLLVMExtra), LLVMModuleAnalysisManagerRef, ())
end
Expand All @@ -530,10 +526,6 @@ function LLVMCreateNewPMLoopAnalysisManager()
ccall((:LLVMCreateNewPMLoopAnalysisManager, libLLVMExtra), LLVMLoopAnalysisManagerRef, ())
end

function LLVMCreateNewPMAAManager()
ccall((:LLVMCreateNewPMAAManager, libLLVMExtra), LLVMAAManagerRef, ())
end

function LLVMDisposeNewPMModuleAnalysisManager(AM)
ccall((:LLVMDisposeNewPMModuleAnalysisManager, libLLVMExtra), Cvoid, (LLVMModuleAnalysisManagerRef,), AM)
end
Expand All @@ -550,10 +542,6 @@ function LLVMDisposeNewPMLoopAnalysisManager(AM)
ccall((:LLVMDisposeNewPMLoopAnalysisManager, libLLVMExtra), Cvoid, (LLVMLoopAnalysisManagerRef,), AM)
end

function LLVMDisposeNewPMAAManager(AM)
ccall((:LLVMDisposeNewPMAAManager, libLLVMExtra), Cvoid, (LLVMAAManagerRef,), AM)
end

mutable struct LLVMOpaqueModulePassManager end

const LLVMModulePassManagerRef = Ptr{LLVMOpaqueModulePassManager}
Expand Down Expand Up @@ -666,10 +654,6 @@ function LLVMPassBuilderParseLoopPassPipeline(PB, PM, PipelineText, PipelineText
ccall((:LLVMPassBuilderParseLoopPassPipeline, libLLVMExtra), LLVMErrorRef, (LLVMPassBuilderRef, LLVMLoopPassManagerRef, Cstring, Csize_t), PB, PM, PipelineText, PipelineTextLength)
end

function LLVMPassBuilderParseAAPipeline(PB, AM, PipelineText, PipelineTextLength)
ccall((:LLVMPassBuilderParseAAPipeline, libLLVMExtra), LLVMErrorRef, (LLVMPassBuilderRef, LLVMAAManagerRef, Cstring, Csize_t), PB, AM, PipelineText, PipelineTextLength)
end

function LLVMPassBuilderRegisterModuleAnalyses(PB, AM)
ccall((:LLVMPassBuilderRegisterModuleAnalyses, libLLVMExtra), Cvoid, (LLVMPassBuilderRef, LLVMModuleAnalysisManagerRef), PB, AM)
end
Expand Down Expand Up @@ -714,8 +698,8 @@ function LLVMCGPMAddFPM(PM, NestedPM)
ccall((:LLVMCGPMAddFPM, libLLVMExtra), Cvoid, (LLVMCGSCCPassManagerRef, LLVMFunctionPassManagerRef), PM, NestedPM)
end

function LLVMFPMAddLPM(PM, NestedPM)
ccall((:LLVMFPMAddLPM, libLLVMExtra), Cvoid, (LLVMFunctionPassManagerRef, LLVMLoopPassManagerRef), PM, NestedPM)
function LLVMFPMAddLPM(PM, NestedPM, UseMemorySSA)
ccall((:LLVMFPMAddLPM, libLLVMExtra), Cvoid, (LLVMFunctionPassManagerRef, LLVMLoopPassManagerRef, LLVMBool), PM, NestedPM, UseMemorySSA)
end

function LLVMMPMAddFPM(PM, NestedPM)
Expand All @@ -735,5 +719,16 @@ end
function LLVMFPMAddJuliaPass(PM, Callback, Thunk)
ccall((:LLVMFPMAddJuliaPass, libLLVMExtra), Cvoid, (LLVMFunctionPassManagerRef, LLVMJuliaFunctionPassCallback, Ptr{Cvoid}), PM, Callback, Thunk)
end
end # v"15" <= version()

function LLVMRegisterTargetIRAnalysis(FAM, TM)
ccall((:LLVMRegisterTargetIRAnalysis, libLLVMExtra), LLVMBool, (LLVMFunctionAnalysisManagerRef, LLVMTargetMachineRef), FAM, TM)
end

function LLVMRegisterTargetLibraryAnalysis(FAM, Triple, TripleLength)
ccall((:LLVMRegisterTargetLibraryAnalysis, libLLVMExtra), LLVMBool, (LLVMFunctionAnalysisManagerRef, Cstring, Csize_t), FAM, Triple, TripleLength)
end

function LLVMRegisterAliasAnalyses(FAM, PB, TM, Analyses, AnalysesLength)
ccall((:LLVMRegisterAliasAnalyses, libLLVMExtra), LLVMErrorRef, (LLVMFunctionAnalysisManagerRef, LLVMPassBuilderRef, LLVMTargetMachineRef, Cstring, Csize_t), FAM, PB, TM, Analyses, AnalysesLength)
end
end # v"15" <= version()
15 changes: 14 additions & 1 deletion lib/libLLVM_julia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ function LLVMAddCPUFeaturesPass(PM)
ccall(:LLVMExtraAddCPUFeaturesPass,Cvoid,(LLVMPassManagerRef,), PM)
end

if VERSION >= v"1.10.0-DEV.1622"

function LLVMRegisterJuliaPassBuilderCallbacks(PB)
ccall(:jl_register_passbuilder_callbacks,Cvoid,(LLVMPassBuilderRef,), PB)
end

function LLVMAddJuliaPipelinePass(PM, PB, Speedup, Size, lower_intrinsics, dump_native, external_use, llvm_only)
ccall(:jl_build_newpm_pipeline,Cvoid,(LLVMModulePassManagerRef,LLVMPassBuilderRef,Cint,Cint,Cint,Cint,Cint,Cint), PM, PB, Speedup, Size, lower_intrinsics, dump_native, external_use, llvm_only)
end

end


if VERSION >= v"1.10.0-DEV.1395"

mutable struct JLOpaqueJuliaOJIT end
Expand Down Expand Up @@ -110,4 +123,4 @@ function JLJITGetIRCompileLayer(JIT)
ccall(:JLJITGetIRCompileLayer, LLVMOrcIRCompileLayerRef, (JuliaOJITRef,), JIT)
end

end # VERSION >= v"1.10.0-DEV.1395"
end
4 changes: 4 additions & 0 deletions src/interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ include("interop/pointer.jl")
include("interop/utils.jl")
include("interop/intrinsics.jl")

if LLVM.has_newpm() && VERSION >= v"1.10.0-DEV.1622"
include("interop/newpm.jl")
end

end
70 changes: 70 additions & 0 deletions src/interop/newpm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import ..LLVM: @module_pass, @function_pass, @loop_pass
import ..LLVM: is_module_pass, is_cgscc_pass, is_function_pass, is_loop_pass
import ..LLVM: pass_string, options_string, add!

@module_pass "CPUFeatures" CPUFeaturesPass
@module_pass "RemoveNI" RemoveNIPass
@module_pass "LowerSIMDLoop" LowerSIMDLoopPass
@module_pass "FinalLowerGC" FinalLowerGCPass
@module_pass "RemoveJuliaAddrspaces" RemoveJuliaAddrspacesPass
@module_pass "RemoveAddrspaces" RemoveAddrspacesPass

struct MultiVersioningPassOptions
external::Core.Bool
end
MultiVersioningPassOptions(; external::Core.Bool=false) = MultiVersioningPassOptions(external)
options_string(options::MultiVersioningPassOptions) = ifelse(options.external, "<external>", "")
@module_pass "JuliaMultiVersioning" MultiVersioningPass MultiVersioningPassOptions

struct LowerPTLSPassOptions
imaging::Core.Bool
end
LowerPTLSPassOptions(; imaging::Core.Bool=false) = LowerPTLSPassOptions(imaging)
options_string(options::LowerPTLSPassOptions) = ifelse(options.imaging, "<imaging>", "")
@module_pass "LowerPTLSPass" LowerPTLSPass LowerPTLSPassOptions

@function_pass "DemoteFloat16" DemoteFloat16Pass
@function_pass "CombineMulAdd" CombineMulAddPass
@function_pass "LateLowerGCFrame" LateLowerGCPass
@function_pass "AllocOpt" AllocOptPass
@function_pass "PropagateJuliaAddrspaces" PropagateJuliaAddrspacesPass
@function_pass "LowerExcHandlers" LowerExcHandlersPass

struct GCInvariantVerifierPassOptions
strong::Core.Bool
end
GCInvariantVerifierPassOptions(; strong::Core.Bool=false) = GCInvariantVerifierPassOptions(strong)
options_string(options::GCInvariantVerifierPassOptions) = ifelse(options.strong, "<strong>", "")
@function_pass "GCInvariantVerifier" GCInvariantVerifierPass GCInvariantVerifierPassOptions

@loop_pass "JuliaLICM" JuliaLICMPass

# The entire Julia pipeline
struct JuliaPipelinePassOptions
speedup::Int
lower_intrinsics::Core.Bool
dump_native::Core.Bool
external_use::Core.Bool
llvm_only::Core.Bool
end
JuliaPipelinePassOptions(; speedup=Base.JLOptions().opt_level, lower_intrinsics::Core.Bool=true,
dump_native::Core.Bool=false, external_use::Core.Bool=false,
llvm_only::Core.Bool=false) = JuliaPipelinePassOptions(convert(Int, speedup), lower_intrinsics, dump_native, external_use, llvm_only)
function options_string(options::JuliaPipelinePassOptions)
optlevel = "O$(options.speedup)"
lower_intrinsics = ifelse(options.lower_intrinsics, "lower_intrinsics", "no_lower_intrinsics")
dump_native = ifelse(options.dump_native, "dump_native", "no_dump_native")
external_use = ifelse(options.external_use, "external_use", "no_external_use")
llvm_only = ifelse(options.llvm_only, "llvm_only", "no_llvm_only")
"<$optlevel;$lower_intrinsics;$dump_native;$external_use;$llvm_only>"
end
@module_pass "julia" JuliaPipelinePass JuliaPipelinePassOptions

# We specialize the add! here because if we go through the PassBuilder parser,
# Julia won't insert the PassBuilder's callbacks in the right spots.
# Using the specific method call here allows insertion of those callbacks.
function add!(pm::NewPMModulePassManager, pb::PassBuilder, pass::JuliaPipelinePass)
API.LLVMAddJuliaPipelinePass(pm, pb, pass.options.speedup, 0, pass.options.lower_intrinsics,
pass.options.dump_native, pass.options.external_use,
pass.options.llvm_only)
end
1 change: 1 addition & 0 deletions src/newpm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ include("newpm/analysismanager.jl")
include("newpm/passmanager.jl")
include("newpm/instrumentation.jl")
include("newpm/passbuilder.jl")
include("newpm/analyses.jl")
include("newpm/passes.jl")
include("newpm/custompass.jl")
71 changes: 71 additions & 0 deletions src/newpm/analyses.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
export NewPMAnalysis, NewPMAliasAnalysis

export add!, analysis_string, analysis_managers

abstract type NewPMAnalysis end
abstract type NewPMAliasAnalysis end

macro alias_analysis(analysis_name, class_name)
quote
export $class_name
struct $class_name <: NewPMAliasAnalysis end
@eval analysis_string(::$class_name) = $analysis_name
end
end

analysis_string(aa::AAManager) = join(aa.aas, ",")

Base.show(io::IO, analysis::NewPMAnalysis) = print(io, analysis_string(analysis))
Base.show(io::IO, aa::AAManager) = print(io, analysis_string(aa))

# Module Alias Analysis
@alias_analysis "globals-aa" GlobalsAA

# Function Alias Analysis
@alias_analysis "basic-aa" BasicAA
@alias_analysis "cfl-anders-aa" CFLAndersAA
@alias_analysis "cfl-steens-aa" CFLSteensAA
@alias_analysis "objc-arc-aa" ObjCARCAA
@alias_analysis "scev-aa" SCEVAA
@alias_analysis "scoped-noalias-aa" ScopedNoAliasAA
@alias_analysis "tbaa" TypeBasedAA

add!(am::AAManager, aa::NewPMAliasAnalysis) = push!(am.aas, analysis_string(aa))
add!(am::AAManager, aas::AbstractVector{<:NewPMAliasAnalysis}) = append!(am.aas, analysis_string.(aas))
add!(am::AAManager, tm::TargetMachine) = am.tm = tm

export TargetIRAnalysis, TargetLibraryAnalysis

struct TargetIRAnalysis
tm::TargetMachine
end
analysis_string(::TargetIRAnalysis) = "target-ir-analysis"

struct TargetLibraryAnalysis
triple::String
end
analysis_string(::TargetLibraryAnalysis) = "target-library-analysis"

add!(fam::FunctionAnalysisManager, analysis::TargetIRAnalysis) = convert(Core.Bool, API.LLVMRegisterTargetIRAnalysis(fam, analysis.tm))
add!(fam::FunctionAnalysisManager, analysis::TargetLibraryAnalysis) = convert(Core.Bool, API.LLVMRegisterTargetLibraryAnalysis(fam, analysis.triple, length(analysis.triple)))

function analysis_managers(f::Core.Function, pb::Union{Nothing,PassBuilder}=nothing, tm::Union{Nothing,TargetMachine}=nothing, aa_stack::AbstractVector{<:NewPMAliasAnalysis}=NewPMAliasAnalysis[])
@dispose lam=LoopAnalysisManager() fam=FunctionAnalysisManager() cam=CGSCCAnalysisManager() mam=ModuleAnalysisManager() begin
if !isempty(aa_stack)
add!(fam, AAManager) do aam
add!(aam, aa_stack)
if !isnothing(tm)
add!(aam, tm)
end
end
end
if !isnothing(tm)
add!(fam, TargetIRAnalysis(tm))
add!(fam, TargetLibraryAnalysis(triple(tm)))
end
if !isnothing(pb)
register!(pb, lam, fam, cam, mam)
end
f(lam, fam, cam, mam)
end
end
Loading

0 comments on commit 2a623f6

Please sign in to comment.