Skip to content

Commit 0360fe6

Browse files
authored
Merge pull request #34825 from eeckstein/fix-opt-hte
2 parents 1eb05b7 + a19ae5c commit 0360fe6

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/SILOptimizer/Mandatory/OptimizeHopToExecutor.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,44 @@ void OptimizeHopToExecutor::allocateBlockStates() {
144144
/// Solve the dataflow in forward direction.
145145
void OptimizeHopToExecutor::solveDataflowForward() {
146146
bool changed = false;
147+
bool firstRound = true;
147148
do {
148149
changed = false;
149150
for (BlockState &state : blockStates) {
150151
int newEntry = state.entry;
151152
for (SILBasicBlock *pred : state.block->getPredecessorBlocks()) {
152153
newEntry = BlockState::merge(newEntry, block2State[pred]->exit);
153154
}
154-
if (newEntry != state.entry || state.exit == BlockState::NotSet) {
155+
if (newEntry != state.entry || firstRound) {
155156
changed = true;
156157
state.entry = newEntry;
157158
if (state.intra == BlockState::NotSet)
158159
state.exit = state.entry;
159160
}
160161
}
162+
firstRound = false;
161163
} while (changed);
162164
}
163165

164166
/// Solve the dataflow in backward direction.
165167
void OptimizeHopToExecutor::solveDataflowBackward() {
166168
bool changed = false;
169+
bool firstRound = true;
167170
do {
168171
changed = false;
169172
for (BlockState &state : llvm::reverse(blockStates)) {
170173
int newExit = state.exit;
171174
for (SILBasicBlock *succ : state.block->getSuccessorBlocks()) {
172175
newExit = BlockState::merge(newExit, block2State[succ]->entry);
173176
}
174-
if (newExit != state.exit || state.entry == BlockState::NotSet) {
177+
if (newExit != state.exit || firstRound) {
175178
changed = true;
176179
state.exit = newExit;
177180
if (state.intra == BlockState::NotSet)
178181
state.entry = state.exit;
179182
}
180183
}
184+
firstRound = false;
181185
} while (changed);
182186
}
183187

@@ -255,7 +259,7 @@ bool OptimizeHopToExecutor::removeDeadHopToExecutors() {
255259
// might require a dedicated executor, don't remove a preceeding
256260
// hop_to_executor instruction.
257261
for (BlockState &state : blockStates) {
258-
state.exit = (state.block->getTerminator()->isFunctionExiting() ?
262+
state.exit = (state.block->getSuccessors().empty() ?
259263
BlockState::NoExecutorNeeded : BlockState::NotSet);
260264
state.intra = BlockState::NotSet;
261265
for (SILInstruction &inst : llvm::reverse(*state.block)) {

test/SILOptimizer/optimize_hop_to_executor.sil

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,53 @@ bb3:
156156
return %r : $()
157157
}
158158

159+
// CHECK-LABEL: sil [ossa] @handleUnreachable1 : $@convention(method) @async (@guaranteed MyActor) -> () {
160+
// CHECK: bb0(%0 : @guaranteed $MyActor):
161+
// CHECK-NOT: hop_to_executor
162+
// CHECK: } // end sil function 'handleUnreachable1'
163+
sil [ossa] @handleUnreachable1 : $@convention(method) @async (@guaranteed MyActor) -> () {
164+
bb0(%0 : @guaranteed $MyActor):
165+
hop_to_executor %0 : $MyActor
166+
cond_br undef, bb1, bb2
167+
bb1:
168+
unreachable
169+
bb2:
170+
%r = tuple ()
171+
return %r : $()
172+
}
173+
174+
// CHECK-LABEL: sil [ossa] @handleUnreachable2 : $@convention(method) @async (@guaranteed MyActor) -> () {
175+
// CHECK: bb0(%0 : @guaranteed $MyActor):
176+
// CHECK-NOT: hop_to_executor
177+
// CHECK: } // end sil function 'handleUnreachable2'
178+
sil [ossa] @handleUnreachable2 : $@convention(method) @async (@guaranteed MyActor) -> () {
179+
bb0(%0 : @guaranteed $MyActor):
180+
cond_br undef, bb1, bb2
181+
bb1:
182+
hop_to_executor %0 : $MyActor
183+
unreachable
184+
bb2:
185+
%r = tuple ()
186+
return %r : $()
187+
}
188+
189+
// CHECK-LABEL: sil [ossa] @handleUnreachableBlock : $@convention(method) @async (@guaranteed MyActor) -> () {
190+
// CHECK: bb0(%0 : @guaranteed $MyActor):
191+
// CHECK-NEXT: hop_to_executor
192+
// CHECK: bb2:
193+
// CHECK-NOT: hop_to_executor
194+
// CHECK: } // end sil function 'handleUnreachableBlock'
195+
sil [ossa] @handleUnreachableBlock : $@convention(method) @async (@guaranteed MyActor) -> () {
196+
bb0(%0 : @guaranteed $MyActor):
197+
hop_to_executor %0 : $MyActor
198+
%f = function_ref @requiredToRunOnActor : $@convention(method) (@guaranteed MyActor) -> ()
199+
apply %f(%0) : $@convention(method) (@guaranteed MyActor) -> ()
200+
br bb2
201+
bb1:
202+
br bb2
203+
bb2:
204+
hop_to_executor %0 : $MyActor
205+
%r = tuple ()
206+
return %r : $()
207+
}
208+

0 commit comments

Comments
 (0)