tshort / StaticCompiler.jl

Compiles Julia code to a standalone library (experimental)
Other
488 stars 31 forks source link

Fix for print and throw #148

Open MasonProtter opened 7 months ago

MasonProtter commented 7 months ago

Ref conversation in https://github.com/tshort/StaticCompiler.jl/issues/145. The idea is that adding a ::Union{} here lets the compiler know that it can remove throw calls. E.g. compare

julia> using StaticTools

julia> g() = throw(f());

julia> @noinline f() = StaticTools.libcexit(Int32(1));

julia> code_typed(g, ())
1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = invoke Main.f()::Any
│        Main.throw(%1)::Union{}  # <----- Bad!
└──      unreachable
) => Union{}

against

julia> @noinline f() = StaticTools.libcexit(Int32(1))::Union{};

julia> code_typed(g, ())
1-element Vector{Any}:
 CodeInfo(
1 ─     invoke Main.f()::Union{}
└──     unreachable
) => Union{}
MasonProtter commented 7 months ago

Ah, damn. This breaks the idea that statically compiled executables return exit codes. :(

tshort commented 7 months ago

Also, it looks like mixtape.jl came back by accident.

brenhinkeller commented 7 months ago

Hmm, interesting

gbaraldi commented 7 months ago

So the thing we need here is to make the ccall return Union{}. Latest master has a way of doing what we do with symbolcall but correctly via ccall. Though it hasn't been plumbed through GPUCompiler just yet. It being https://github.com/JuliaLang/julia/pull/51123