Skip to content
Open
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
18 changes: 15 additions & 3 deletions src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,26 @@ function irgen(@nospecialize(job::CompilerJob))
end
end

# sanitize defined functions (Julia doesn't when using the external codegen policy)
# sanitize defined functions (Julia doesn't when using the external codegen policy).
# Sanitized names can collide, e.g. `julia_+_0` and `julia_-_0` from Julia 1.14, which
# numbers functions per compilation. Uniquify them ourselves, as LLVM would add a `.`.
renamed = Dict{String,String}()
for val in collect(mod.functions)
isdeclaration(val) && continue
old_name = val.name
new_name = safe_name(old_name)
if old_name != new_name
val.name = new_name
old_name == new_name && continue
unique_name, i = new_name, 0
while haskey(mod.functions, unique_name)
i += 1
unique_name = "$(new_name)_$i"
end
val.name = unique_name
renamed[old_name] = unique_name
end
for (mi, entry) in compiled
compiled[mi] = (; entry..., func = get(renamed, entry.func, entry.func),
specfunc = get(renamed, entry.specfunc, entry.specfunc))
end

# rename and process the entry point
Expand Down
24 changes: 24 additions & 0 deletions test/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,30 @@ end
end
end

@testset "sanitized function names" begin
# `+` and `*` both sanitize to `_`, so the callees' names collide wherever Julia
# numbers functions per compilation.
mod = @eval module $(gensym())
@noinline var"f+"(x) = x + 1
@noinline var"f*"(x) = x * 3
kernel(x) = var"f+"(x) - var"f*"(x)
end

job, _ = Native.create_job(mod.kernel, (Int,); validate=false)
JuliaContext() do ctx
ir, meta = GPUCompiler.compile(:llvm, job)

specfuncs = Dict(mi.def.name => entry.specfunc for (mi, entry) in meta.compiled)
@test allunique(values(specfuncs))
for name in values(specfuncs)
@test haskey(ir.functions, name)
@test name == GPUCompiler.safe_name(name)
end
@test occursin("mul", string(ir.functions[specfuncs[Symbol("f*")]]))
@test !occursin("mul", string(ir.functions[specfuncs[Symbol("f+")]]))
end
end

@testset "cached compilation" begin
mod = @eval module $(gensym())
@noinline child(i) = i
Expand Down
Loading