Skip to content

Commit 9d10068

Browse files
authored
Merge pull request #20568 from aschwaighofer/fix_global_opts
2 parents 0d2787f + d862e3d commit 9d10068

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

lib/SILOptimizer/IPO/GlobalOpt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class InstructionsCloner : public SILClonerWithScopes<InstructionsCloner> {
200200
// If this is a call to a global initializer, map it.
201201
void SILGlobalOpt::collectGlobalInitCall(ApplyInst *AI) {
202202
SILFunction *F = AI->getReferencedFunction();
203-
if (!F || !F->isGlobalInit())
203+
if (!F || !F->isGlobalInit() || !ApplySite(AI).canOptimize())
204204
return;
205205

206206
GlobalInitCallMap[F].push_back(AI);

test/SILOptimizer/globalopt.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,39 @@ bb2:
348348
cond_br %0, bb2, bb1
349349
}
350350

351+
// libg.MyGlobal.mutableAddressor : Swift.Int32
352+
sil [global_init] [dynamically_replacable] @_TF4libga8MyGlobalSi_dynamic : $@convention(thin) () -> Builtin.RawPointer
353+
354+
// Don't hoist dynamic_function_ref calls.
355+
// CHECK-LABEL: sil @test_loopexit_and_loop_dynamic
356+
// CHECK: bb0(%0 : $Builtin.Int1):
357+
// CHECK: bb1:
358+
// CHECK: apply
359+
// CHECK: return
360+
// CHECK: bb2:
361+
// CHECK: apply
362+
// CHECK: cond_br
363+
sil @test_loopexit_and_loop_dynamic : $@convention(thin) (Builtin.Int1) -> Int64 {
364+
bb0(%0 : $Builtin.Int1):
365+
br bb2
366+
367+
bb1:
368+
%1 = dynamic_function_ref @_TF4libga8MyGlobalSi_dynamic : $@convention(thin) () -> Builtin.RawPointer
369+
%2 = apply %1() : $@convention(thin) () -> Builtin.RawPointer
370+
%3 = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*Int64
371+
%4 = struct_element_addr %3 : $*Int64, #Int64._value
372+
%5 = load %4 : $*Builtin.Int64
373+
%r1 = struct $Int64 (%5 : $Builtin.Int64)
374+
return %r1 : $Int64
375+
376+
bb2:
377+
%11 = dynamic_function_ref @_TF4libga8MyGlobalSi_dynamic : $@convention(thin) () -> Builtin.RawPointer
378+
%12 = apply %11() : $@convention(thin) () -> Builtin.RawPointer
379+
%13 = pointer_to_address %12 : $Builtin.RawPointer to [strict] $*Int64
380+
%14 = struct_element_addr %13 : $*Int64, #Int64._value
381+
%15 = load %14 : $*Builtin.Int64
382+
cond_br %0, bb2, bb1
383+
}
351384
// An init-call, which is guarded by an availability-check may not be speculated.
352385
// In this test it may not be hoisted out of the loop.
353386
// CHECK-LABEL: sil @test_availability_loop

0 commit comments

Comments
 (0)