Skip to content

Commit 3e8e99f

Browse files
authored
Save and restore function attributes across optimization. (#438)
Works around a Julia issue.
1 parent d5086fb commit 3e8e99f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/optim.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,31 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
238238
combine_mul_add!(pm)
239239
div_rem_pairs!(pm)
240240

241+
if VERSION < v"1.10.0-DEV.1144"
242+
# save function attributes to work around JuliaGPU/GPUCompiler#437
243+
current_attrs = Dict{String,Any}()
244+
for f in functions(mod)
245+
attrs = function_attributes(f)
246+
length(attrs) == 0 && continue
247+
current_attrs[LLVM.name(f)] = collect(attrs)
248+
end
249+
end
250+
241251
run!(pm, mod)
252+
253+
if VERSION < v"1.10.0-DEV.1144"
254+
# restore function attributes
255+
for (fn, attrs) in current_attrs
256+
haskey(functions(mod), fn) || continue
257+
f = functions(mod)[fn]
258+
259+
for attr in attrs
260+
# NOTE: there's no function attributes that contain a type,
261+
# so we can just blindly add them back
262+
push!(function_attributes(f), attr)
263+
end
264+
end
265+
end
242266
end
243267

244268
# target-specific optimizations

test/native.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,25 @@ end
305305
@test occursin(r"^define.*julia_f_expensive"m, ir)
306306
end
307307

308+
@testset "function attributes" begin
309+
@inline function convergent_barrier()
310+
Base.llvmcall(("""
311+
declare void @barrier() #1
312+
313+
define void @entry() #0 {
314+
call void @barrier()
315+
ret void
316+
}
317+
318+
attributes #0 = { alwaysinline }
319+
attributes #1 = { convergent }""", "entry"),
320+
Nothing, Tuple{})
321+
end
322+
323+
ir = sprint(io->native_code_llvm(io, convergent_barrier, Tuple{}; dump_module=true, raw=true))
324+
@test occursin(r"attributes #. = \{ convergent \}", ir)
325+
end
326+
308327
end
309328

310329
############################################################################################

0 commit comments

Comments
 (0)