Skip to content

Commit

Permalink
Add a default AA pipeline, for convenience.
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt committed Sep 5, 2023
1 parent 05aa03f commit fdd74bd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/newpm/analyses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Base.show(io::IO, aa::AAManager) = print(io, analysis_string(aa))
@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, aas::AbstractVector{<:NewPMAliasAnalysis}) =
append!(am.aas, analysis_string.(aas))
add!(am::AAManager, tm::TargetMachine) = am.tm = tm

export TargetIRAnalysis, TargetLibraryAnalysis
Expand All @@ -46,10 +47,15 @@ struct TargetLibraryAnalysis
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)))
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[])
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
Expand Down
14 changes: 14 additions & 0 deletions src/newpm/passbuilder.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,17 @@ function passbuilder(pm::NewPMPassManager)::PassBuilder
end
pm.pb
end


## pipelines

# we generally don't need to define pipelines here, as we can reuse LLVM's,
# but the AA stack needs to be passed explicitly, so define a default one here.

function default_aa_pipeline(; global_analyses::Core.Bool=false)
aa_stack = NewPMAliasAnalysis[BasicAA(), ScopedNoAliasAA(), TypeBasedAA()]
if global_analyses
push!(aa_stack, GlobalsAA())
end
return aa_stack
end
13 changes: 5 additions & 8 deletions src/newpm/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ end

function run!(pm::NewPMModulePassManager, m::Module,
tm::Union{Nothing,TargetMachine} = nothing,
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = NewPMAliasAnalysis[])
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = default_aa_pipeline(global_analyses=true))
pb = passbuilder(pm)
analysis_managers(pb, tm, aa_stack) do lam, fam, cam, mam
dispose(run!(pm, m, mam))
Expand All @@ -704,15 +704,15 @@ end

function run!(pm::NewPMFunctionPassManager, f::Function,
tm::Union{Nothing,TargetMachine} = nothing,
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = NewPMAliasAnalysis[])
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = default_aa_pipeline())
pb = passbuilder(pm)
analysis_managers(pb, tm, aa_stack) do lam, fam, cam, mam
dispose(run!(pm, f, fam))
end
end

function run!(pass::NewPMLLVMPass, m::Module, tm::Union{Nothing,TargetMachine} = nothing,
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = NewPMAliasAnalysis[])
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = default_aa_pipeline(global_analyses=true))
needs_globals_aa_recompute = any(aa_stack) do aa
isa(aa, GlobalsAA)
end
Expand Down Expand Up @@ -744,11 +744,8 @@ function run!(pass::NewPMLLVMPass, m::Module, tm::Union{Nothing,TargetMachine} =
end

function run!(pass::NewPMLLVMPass, f::Function, tm::Union{Nothing,TargetMachine} = nothing,
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = NewPMAliasAnalysis[])
needs_globals_aa_recompute = any(aa_stack) do aa
isa(aa, GlobalsAA)
end
if needs_globals_aa_recompute
aa_stack::AbstractVector{<:NewPMAliasAnalysis} = default_aa_pipeline())
if any(aa->isa(aa, GlobalsAA), aa_stack)
throw(ArgumentError("GlobalsAA needs to be computed on a module, not a function!"))
end
@dispose pb=PassBuilder(tm) fpm=NewPMFunctionPassManager(pb) begin
Expand Down
15 changes: 10 additions & 5 deletions test/newpm_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,15 @@ host_t = Target(triple=host_triple)
register!(pb, lam, fam, cam, mam)
end
@test "Registered an empty AA manager!" != ""

analysis_managers() do lam, fam, cam, mam
add!(fam, AAManager) do aam
add!(aam, BasicAA())
end
register!(pb, lam, fam, cam, mam)
end
@test "Registered a single alias analysis pass!" != ""

analysis_managers() do lam, fam, cam, mam
add!(fam, AAManager) do aam
add!(aam, BasicAA())
Expand All @@ -359,13 +361,16 @@ host_t = Target(triple=host_triple)
register!(pb, lam, fam, cam, mam)
end
@test "Registered all alias analysis passes!" != ""

analysis_managers() do lam, fam, cam, mam
add!(fam, TargetIRAnalysis(tm))
add!(fam, TargetLibraryAnalysis(host_triple))
register!(pb, lam, fam, cam, mam)
end
@test "Registered target analyses!" != ""
analysis_managers(nothing, tm, [BasicAA(), ScopedNoAliasAA(), TypeBasedAA()]) do lam, fam, cam, mam

aa_stack = [BasicAA(), ScopedNoAliasAA(), TypeBasedAA()]
analysis_managers(nothing, tm, aa_stack) do lam, fam, cam, mam
register!(pb, lam, fam, cam, mam)
end
@test "Implicitly registered alias analyses and target analyses!" != ""
Expand Down Expand Up @@ -412,7 +417,7 @@ end # testset "newpm analyses"
host_t = Target(triple=host_triple)

@dispose tm=TargetMachine(host_t, host_triple) begin
@test isnothing(run!(SimplifyCFGPass(), fn, tm, [BasicAA(), ScopedNoAliasAA(), TypeBasedAA()]))
@test isnothing(run!(SimplifyCFGPass(), fn, tm))
end

@test "Successfully forwarded target machine and alias analyses!" != ""
Expand Down Expand Up @@ -468,8 +473,8 @@ end
host_t = Target(triple=host_triple)

@dispose tm=TargetMachine(host_t, host_triple) begin
@test isnothing(run!(SimplifyCFGPass(), mod, tm, [BasicAA(), ScopedNoAliasAA(), TypeBasedAA(), GlobalsAA()]))
@test_throws ArgumentError run!(SimplifyCFGPass(), fn, tm, [BasicAA(), ScopedNoAliasAA(), TypeBasedAA(), GlobalsAA()])
@test isnothing(run!(SimplifyCFGPass(), mod, tm, [GlobalsAA()]))
@test_throws ArgumentError run!(SimplifyCFGPass(), fn, tm, [GlobalsAA()])
end
end

Expand Down Expand Up @@ -618,7 +623,7 @@ host_t = Target(triple=host_triple)

ret!(builder)

run!(mpm, mod, tm, [BasicAA(), ScopedNoAliasAA(), TypeBasedAA()])
run!(mpm, mod, tm)
end
end
end
Expand Down

0 comments on commit fdd74bd

Please sign in to comment.