Skip to content

Commit 56c09c3

Browse files
committed
CSE: cse builtin "once" calls
`builtin "once"` calls are a result of inlining global accessors
1 parent 47a7acd commit 56c09c3

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,17 @@ bool CSE::canHandle(SILInstruction *Inst) {
11871187
return false;
11881188
}
11891189
if (auto *BI = dyn_cast<BuiltinInst>(Inst)) {
1190-
// Although the onFastPath builtin has no side-effects we don't want to
1191-
// (re-)move it.
1192-
if (BI->getBuiltinInfo().ID == BuiltinValueKind::OnFastPath)
1190+
switch (BI->getBuiltinInfo().ID) {
1191+
case BuiltinValueKind::OnFastPath:
1192+
// Although the onFastPath builtin has no side-effects we don't want to
1193+
// (re-)move it.
11931194
return false;
1194-
return !BI->mayReadOrWriteMemory();
1195+
case BuiltinValueKind::Once:
1196+
case BuiltinValueKind::OnceWithContext:
1197+
return true;
1198+
default:
1199+
return !BI->mayReadOrWriteMemory();
1200+
}
11951201
}
11961202
if (auto *EMI = dyn_cast<ExistentialMetatypeInst>(Inst)) {
11971203
return !EMI->getOperand()->getType().isAddress();

test/SILOptimizer/cse.sil

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,3 +1411,40 @@ entry(%instance : $FC):
14111411
return %retval : $()
14121412
}
14131413

1414+
sil_global private @g_token : $Builtin.Word
1415+
sil_global private @g2_token : $Builtin.Word
1416+
1417+
sil @g_init : $@convention(c) () -> ()
1418+
1419+
// CHECK-LABEL: sil @cse_builtin_once :
1420+
// CHECK: builtin
1421+
// CHECK-NOT: builtin
1422+
// CHECK: } // end sil function 'cse_builtin_once'
1423+
sil @cse_builtin_once : $@convention(thin) () -> () {
1424+
bb0:
1425+
%1 = global_addr @g_token : $*Builtin.Word
1426+
%2 = address_to_pointer %1 : $*Builtin.Word to $Builtin.RawPointer
1427+
%3 = function_ref @g_init : $@convention(c) () -> ()
1428+
%4 = builtin "once"(%2 : $Builtin.RawPointer, %3 : $@convention(c) () -> ()) : $()
1429+
%5 = builtin "once"(%2 : $Builtin.RawPointer, %3 : $@convention(c) () -> ()) : $()
1430+
%r1 = tuple ()
1431+
return %r1 : $()
1432+
}
1433+
1434+
// CHECK-LABEL: sil @dont_cse_builtin_once_different_token :
1435+
// CHECK: builtin
1436+
// CHECK: builtin
1437+
// CHECK: } // end sil function 'dont_cse_builtin_once_different_token'
1438+
sil @dont_cse_builtin_once_different_token : $@convention(thin) () -> () {
1439+
bb0:
1440+
%1 = global_addr @g_token : $*Builtin.Word
1441+
%2 = address_to_pointer %1 : $*Builtin.Word to $Builtin.RawPointer
1442+
%3 = global_addr @g2_token : $*Builtin.Word
1443+
%4 = address_to_pointer %3 : $*Builtin.Word to $Builtin.RawPointer
1444+
%5 = function_ref @g_init : $@convention(c) () -> ()
1445+
%6 = builtin "once"(%2 : $Builtin.RawPointer, %5 : $@convention(c) () -> ()) : $()
1446+
%7 = builtin "once"(%4 : $Builtin.RawPointer, %5 : $@convention(c) () -> ()) : $()
1447+
%r1 = tuple ()
1448+
return %r1 : $()
1449+
}
1450+

0 commit comments

Comments
 (0)