Skip to content

Commit 5f2bd7c

Browse files
committed
Clone with global changes on LLVM 12.
But remove dbg instructions before doing so.
1 parent 6301d41 commit 5f2bd7c

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

src/irgen.jl

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,26 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
482482
value_map = Dict{LLVM.Value, LLVM.Value}(
483483
param => new_args[i] for (i,param) in enumerate(parameters(f))
484484
)
485-
clone_into!(new_f, f; value_map,
486-
changes=LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly)
487-
# NOTE: we need global changes because LLVM 12 wants to clone debug metadata
485+
486+
# before D96531 (part of LLVM 13), clone_into! wants to duplicate debug metadata
487+
# when the functions are part of the same module. that is invalid, because it
488+
# results in desynchronized debug intrinsics (GPUCompiler#284), so remove those.
489+
if LLVM.version() < v"13"
490+
removals = LLVM.Instruction[]
491+
for bb in blocks(f), inst in instructions(bb)
492+
if inst isa LLVM.CallInst && LLVM.name(called_value(inst)) == "llvm.dbg.declare"
493+
push!(removals, inst)
494+
end
495+
end
496+
for inst in removals
497+
@assert isempty(uses(inst))
498+
unsafe_delete!(LLVM.parent(inst), inst)
499+
end
500+
changes = LLVM.API.LLVMCloneFunctionChangeTypeGlobalChanges
501+
else
502+
changes = LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly
503+
end
504+
clone_into!(new_f, f; value_map, changes)
488505

489506
# fall through
490507
br!(builder, blocks(new_f)[2])
@@ -600,10 +617,25 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
600617
return val
601618
end
602619

603-
# we don't want module-level changes, because otherwise LLVM will clone metadata,
604-
# resulting in mismatching references between `!dbg` metadata and `dbg` instructions
605-
clone_into!(new_f, f; value_map, materializer,
606-
changes=LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly)
620+
# before D96531 (part of LLVM 13), clone_into! wants to duplicate debug metadata
621+
# when the functions are part of the same module. that is invalid, because it
622+
# results in desynchronized debug intrinsics (GPUCompiler#284), so remove those.
623+
if LLVM.version() < v"13"
624+
removals = LLVM.Instruction[]
625+
for bb in blocks(f), inst in instructions(bb)
626+
if inst isa LLVM.CallInst && LLVM.name(called_value(inst)) == "llvm.dbg.declare"
627+
push!(removals, inst)
628+
end
629+
end
630+
for inst in removals
631+
@assert isempty(uses(inst))
632+
unsafe_delete!(LLVM.parent(inst), inst)
633+
end
634+
changes = LLVM.API.LLVMCloneFunctionChangeTypeGlobalChanges
635+
else
636+
changes = LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly
637+
end
638+
clone_into!(new_f, f; value_map, materializer, changes)
607639

608640
# we can't remove this function yet, as we might still need to rewrite any called,
609641
# but remove the IR already

src/spirv.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,26 @@ function wrap_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.F
292292
value_map = Dict{LLVM.Value, LLVM.Value}(
293293
param => new_args[i] for (i,param) in enumerate(parameters(f))
294294
)
295-
clone_into!(new_f, f; value_map,
296-
changes=LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly)
295+
296+
# before D96531 (part of LLVM 13), clone_into! wants to duplicate debug metadata
297+
# when the functions are part of the same module. that is invalid, because it
298+
# results in desynchronized debug intrinsics (GPUCompiler#284), so remove those.
299+
if LLVM.version() < v"13"
300+
removals = LLVM.Instruction[]
301+
for bb in blocks(f), inst in instructions(bb)
302+
if inst isa LLVM.CallInst && LLVM.name(called_value(inst)) == "llvm.dbg.declare"
303+
push!(removals, inst)
304+
end
305+
end
306+
for inst in removals
307+
@assert isempty(uses(inst))
308+
unsafe_delete!(LLVM.parent(inst), inst)
309+
end
310+
changes = LLVM.API.LLVMCloneFunctionChangeTypeGlobalChanges
311+
else
312+
changes = LLVM.API.LLVMCloneFunctionChangeTypeLocalChangesOnly
313+
end
314+
clone_into!(new_f, f; value_map, changes)
297315

298316
# apply byval attributes again (`clone_into!` didn't due to the type mismatch)
299317
for i in 1:length(byval)

0 commit comments

Comments
 (0)