tshort / StaticCompiler.jl

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

Demangle by default #124

Closed brenhinkeller closed 1 year ago

brenhinkeller commented 1 year ago

So believe it or not this actually doesn't break anything in StaticTools AFAICT, but it'll be breaking for anyone who's using StaticCompiler to compile shlibs that they then call from another language (since then all the function names won't have the extra julia_ added in front)

MasonProtter commented 1 year ago

Wouldnt demangling by default also break functions that call multiple methods from the same function?

i.e.

function f(x::Int)
    y = 1 + x
    return 1.0 + Float64(x)
end

If +’s methods dont get mangled, wouldnt that cause the wrong functions to get called?

brenhinkeller commented 1 year ago

Oh, so this just removes the julia_ that used to get put in front of each method name by GPUCompiler, anything else that happens to the name doesn't get changed..

For that particular example though since there's no dynamic dispatch in the compiled code, the choice of which + function to dispatch though would have been made (and inlined) before we get to compilation

MasonProtter commented 1 year ago

Oh, okay if it just affects the prefix that's fine. The suffixes are important though, even if you don't have dynamic dispatch, just a lack of inlining. See this:

julia> @noinline add(x, y) = x + y;

julia> function f(x::Int)
           y = add(1, x)
           add(1.0, Float64(y))
       end;

julia> code_llvm(f, Tuple{Int})
;  @ REPL[2]:1 within `f`
define double @julia_f_223(i64 signext %0) #0 {
top:
;  @ REPL[2]:2 within `f`
  %1 = call i64 @j_add_225(i64 signext 1, i64 signext %0) #0
;  @ REPL[2]:3 within `f`
; ┌ @ float.jl:159 within `Float64`
   %2 = sitofp i64 %1 to double
; └
  %3 = call double @j_add_226(double 1.000000e+00, double %2) #0
  ret double %3
}

If methods of add got demangled and written out as just add in the LLVM, then the function call in %1 wouldn't be distinct from %3.

So mangling method names is really important.

brenhinkeller commented 1 year ago

Oh yeah, this doesn't mess with the suffixes.. maybe should call it something else but this got introduced a bit back

MasonProtter commented 1 year ago

Yeah, I guess the term is a bit overloaded, but I'd call the prefix thing poor-man's-namespacing, whereas the suffix is true mangling because it's not predictable or stable.