@@ -482,9 +482,26 @@ function lower_byval(@nospecialize(job::CompilerJob), mod::LLVM.Module, f::LLVM.
482
482
value_map = Dict {LLVM.Value, LLVM.Value} (
483
483
param => new_args[i] for (i,param) in enumerate (parameters (f))
484
484
)
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)
488
505
489
506
# fall through
490
507
br! (builder, blocks (new_f)[2 ])
@@ -600,10 +617,25 @@ function add_kernel_state!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
600
617
return val
601
618
end
602
619
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)
607
639
608
640
# we can't remove this function yet, as we might still need to rewrite any called,
609
641
# but remove the IR already
0 commit comments