Skip to content

Add API for the new parsing options to JuliaPipelinePassOptions #440

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

Merged
merged 2 commits into from
Sep 2, 2024
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
63 changes: 45 additions & 18 deletions src/interop/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,54 @@ Base.string(options::GCInvariantVerifierPassOptions) = options.strong ? "<strong
@loop_pass "JuliaLICM" JuliaLICMPass
@loop_pass "LowerSIMDLoop" LowerSIMDLoopPass

struct JuliaPipelineOptions
opt_level::Int
lower_intrinsics::Bool
dump_native::Bool
external_use::Bool
llvm_only::Bool
Base.@kwdef struct JuliaPipelineOptions
opt_level::Int = Base.JLOptions().opt_level

lower_intrinsics::Union{Nothing,Bool} = nothing
dump_native::Union{Nothing,Bool} = nothing
external_use::Union{Nothing,Bool} = nothing
llvm_only::Union{Nothing,Bool} = nothing
always_inline::Union{Nothing,Bool} = nothing
enable_early_simplifications::Union{Nothing,Bool} = nothing
enable_early_optimizations::Union{Nothing,Bool} = nothing
enable_scalar_optimizations::Union{Nothing,Bool} = nothing
enable_loop_optimizations::Union{Nothing,Bool} = nothing
enable_vector_pipeline::Union{Nothing,Bool} = nothing
remove_ni::Union{Nothing,Bool} = nothing
cleanup::Union{Nothing,Bool} = nothing
warn_missed_transformations::Union{Nothing,Bool} = nothing
end
JuliaPipelineOptions(; opt_level=Base.JLOptions().opt_level,
lower_intrinsics::Bool=true,
dump_native::Bool=false, external_use::Bool=false,
llvm_only::Bool=false) =
JuliaPipelineOptions(convert(Int, opt_level), lower_intrinsics, dump_native,
external_use, llvm_only)

function Base.string(options::JuliaPipelineOptions)
optlevel = "level=$(options.opt_level)"
lower_intrinsics = options.lower_intrinsics ? "lower_intrinsics" : "no_lower_intrinsics"
dump_native = options.dump_native ? "dump_native" : "no_dump_native"
external_use = options.external_use ? "external_use" : "no_external_use"
llvm_only = options.llvm_only ? "llvm_only" : "no_llvm_only"
"<$optlevel;$lower_intrinsics;$dump_native;$external_use;$llvm_only>"
final_options = String[]
push!(final_options, "level=$(options.opt_level)")

function handle_option(name::Symbol)
val = getfield(options, name)
if val !== nothing
push!(final_options, val ? String(name) : "no_$name")
end
end
handle_option(:lower_intrinsics)
handle_option(:dump_native)
handle_option(:external_use)
handle_option(:llvm_only)
if VERSION >= v"1.12.0-DEV.1029" || # JuliaLang/julia#55407
(v"1.11.0-rc3" <= VERSION < v"1.12-") # backport
handle_option(:always_inline)
handle_option(:enable_early_simplifications)
handle_option(:enable_early_optimizations)
handle_option(:enable_scalar_optimizations)
handle_option(:enable_loop_optimizations)
handle_option(:enable_vector_pipeline)
handle_option(:remove_ni)
handle_option(:cleanup)
handle_option(:warn_missed_transformations)
end

"<" * join(final_options, ";") * ">"
end

@pipeline "julia" JuliaPipeline JuliaPipelineOptions

# XXX: if we go through the PassBuilder parser, Julia won't insert the PassBuilder's
Expand Down
82 changes: 59 additions & 23 deletions test/interop_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,35 +160,71 @@ end

end

function test_module()
mod = LLVM.Module("test")
ft = LLVM.FunctionType(LLVM.VoidType())
fn = LLVM.Function(mod, "SomeFunction", ft)

VERSION < v"1.11.0-DEV.428" && @testset "passes" begin

@dispose ctx=Context() mod=LLVM.Module("SomeModule") pm=ModulePassManager() begin

demote_float16!(pm)
julia_licm!(pm)
alloc_opt!(pm)
barrier_noop!(pm)
gc_invariant_verifier!(pm)
gc_invariant_verifier!(pm, true)
lower_exc_handlers!(pm)
combine_mul_add!(pm)
multi_versioning!(pm)
propagate_julia_addrsp!(pm)
lower_ptls!(pm)
lower_ptls!(pm, true)
lower_simdloop!(pm)
remove_ni!(pm)
late_lower_gc_frame!(pm)
final_lower_gc!(pm)
cpu_features!(pm)
@dispose builder=IRBuilder() begin
entry = BasicBlock(fn, "entry")
position!(builder, entry)

ret!(builder)
end

return mod
end

@test "we didn't crash!" != ""
@testset "passes" begin
if VERSION < v"1.11.0-DEV.428"
@dispose ctx=Context() mod=test_module() pm=ModulePassManager() begin

demote_float16!(pm)
julia_licm!(pm)
alloc_opt!(pm)
barrier_noop!(pm)
gc_invariant_verifier!(pm)
gc_invariant_verifier!(pm, true)
lower_exc_handlers!(pm)
combine_mul_add!(pm)
multi_versioning!(pm)
propagate_julia_addrsp!(pm)
lower_ptls!(pm)
lower_ptls!(pm, true)
lower_simdloop!(pm)
remove_ni!(pm)
late_lower_gc_frame!(pm)
final_lower_gc!(pm)
cpu_features!(pm)

end
end
@test "we didn't crash!" != ""
end

@dispose ctx=Context() mod=test_module() begin
# by string
@test run!("julia", mod) === nothing

# by object
@test run!(JuliaPipeline(), mod) === nothing

# by object with options
pipeline = JuliaPipeline(opt_level=2)
@test run!(pipeline, mod) === nothing

if VERSION >= v"1.12.0-DEV.1029" || # JuliaLang/julia#55407
(v"1.11.0-rc3" <= VERSION < v"1.12-") # backport
pipeline = JuliaPipeline(opt_level=3, enable_vector_pipeline=false)
@test run!(pipeline, mod) === nothing
# TODO: check that the vector pipeline didn't run

pipeline = JuliaPipeline(enable_early_simplifications=false,
enable_early_optimizations=false)
@test contains(string(pipeline), "no_enable_early_simplifications")
@test contains(string(pipeline), "no_enable_early_optimizations")
end
end
end

@testset "intrinsics" begin
@test assume(true) === nothing
Expand Down
13 changes: 0 additions & 13 deletions test/newpm_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,6 @@ end
end

@testset "julia" begin
@testset "pipeline" begin
@dispose ctx=Context() mod=test_module() begin
# by string
@test run!("julia", mod) === nothing

# by object
@test run!(JuliaPipeline(), mod) === nothing

# by object with options
@test run!(JuliaPipeline(opt_level=2), mod) === nothing
end
end

@testset "passes" begin
@dispose ctx=Context() pb=NewPMPassBuilder() begin
basicSimplifyCFGOptions =
Expand Down
Loading