Skip to content

Commit c3c7465

Browse files
authored
Merge pull request #71125 from meg-gupta/rdar121485860
Don't duplicate builtin "once"
2 parents 28f27c3 + 1369043 commit c3c7465

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/SIL/IR/SILInstruction.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,12 @@ bool SILInstruction::isTriviallyDuplicatable() const {
14361436
if (isa<BeginAccessInst>(this))
14371437
return false;
14381438

1439+
// All users of builtin "once" should directly operate on it (no phis etc)
1440+
if (auto *bi = dyn_cast<BuiltinInst>(this)) {
1441+
if (bi->getBuiltinInfo().ID == BuiltinValueKind::Once)
1442+
return false;
1443+
}
1444+
14391445
// begin_apply creates a token that has to be directly used by the
14401446
// corresponding end_apply and abort_apply.
14411447
if (isa<BeginApplyInst>(this))

lib/SILOptimizer/Utils/LoopUtils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ bool swift::canDuplicateLoopInstruction(SILLoop *L, SILInstruction *I) {
309309
return true;
310310
}
311311

312+
if (auto *bi = dyn_cast<BuiltinInst>(I)) {
313+
if (bi->getBuiltinInfo().ID == BuiltinValueKind::Once)
314+
return false;
315+
}
316+
312317
if (isa<DynamicMethodBranchInst>(I))
313318
return false;
314319

test/SILOptimizer/simplify_cfg_ossa_jump_threading.sil

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,49 @@ bb4:
229229
destroy_value %4 : $FakeOptional<Klass>
230230
return %5 : $Builtin.Int1
231231
}
232+
233+
sil_global @my_global : $S
234+
235+
sil @globalinit_func11 : $@convention(thin) () -> ()
236+
237+
struct S {
238+
}
239+
240+
// This test is not optimized because the dest bb of the branch has a single predecessor
241+
// CHECK-LABEL: sil [ossa] @test_simplify_switch_enum_jump_threading_sil_token :
242+
// CHECK: switch_enum
243+
// CHECK-NOT: switch_enum
244+
// CHECK-LABEL: } // end sil function 'test_simplify_switch_enum_jump_threading_sil_token'
245+
sil [ossa] @test_simplify_switch_enum_jump_threading_sil_token : $@convention(thin) (@owned Klass, Builtin.RawPointer) -> () {
246+
bb0(%0 : @owned $Klass, %1 : $Builtin.RawPointer):
247+
cond_br undef, bb1, bb2
248+
249+
bb1:
250+
specify_test "simplify-cfg-try-jump-threading @instruction[2]"
251+
%s = enum $FakeOptional<Klass>, #FakeOptional.some!enumelt, %0 : $Klass
252+
br bb3(%s : $FakeOptional<Klass>)
253+
254+
bb2:
255+
destroy_value %0 : $Klass
256+
%n = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
257+
br bb3(%n : $FakeOptional<Klass>)
258+
259+
bb3(%3 : @owned $FakeOptional<Klass>):
260+
%f = function_ref @globalinit_func11 : $@convention(thin) () -> ()
261+
%b = builtin "once"(%1 : $Builtin.RawPointer, %f : $@convention(thin) () -> ()) : $Builtin.SILToken
262+
%g = global_addr @my_global : $*S depends_on %b
263+
switch_enum %3 : $FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb5, case #FakeOptional.none!enumelt: bb4
264+
265+
bb4:
266+
br bb6
267+
268+
bb5(%6 : @owned $Klass):
269+
%ld = load [trivial] %g : $*S
270+
apply undef(%ld) : $@convention(thin) (S) -> ()
271+
destroy_value %6 : $Klass
272+
br bb6
273+
274+
bb6:
275+
%t = tuple ()
276+
return %t : $()
277+
}

0 commit comments

Comments
 (0)