Skip to content

Commit da3824a

Browse files
authored
Merge pull request #39662 from meg-gupta/fixbeginapplyinl
2 parents f591868 + 2658cbe commit da3824a

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
374374

375375
/// This is called by either of the top-level visitors, cloneReachableBlocks
376376
/// or cloneSILFunction, after all other visitors are have been called.
377+
378+
/// `preFixUp` is called first.
379+
void preFixUp(SILFunction *F) {}
380+
/// After postFixUp, the SIL must be valid and semantically equivalent to the
381+
/// SIL before cloning.
377382
///
378-
/// After fixUp, the SIL must be valid and semantically equivalent to the SIL
379-
/// before cloning.
380-
///
381-
/// Common fix-ups are handled first in `doFixUp` and may not be overridden.
382-
void fixUp(SILFunction *F) {}
383+
/// Common fix-ups are handled first in `commonFixUp` and may not be
384+
/// overridden.
385+
void postFixUp(SILFunction *F) {}
386+
383387
private:
384388
/// MARK: SILCloner implementation details hidden from CRTP extensions.
385389

@@ -388,8 +392,8 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
388392
void visitBlocksDepthFirst(SILBasicBlock *StartBB);
389393

390394
/// Also perform fundamental cleanup first, then call the CRTP extension,
391-
/// `fixUp`.
392-
void doFixUp(SILFunction *F);
395+
/// `postFixUp`.
396+
void commonFixUp(SILFunction *F);
393397
};
394398

395399
/// A SILBuilder that automatically invokes postprocess on each
@@ -581,7 +585,7 @@ void SILCloner<ImplClass>::cloneReachableBlocks(
581585
// Discover and map the region to be cloned.
582586
visitBlocksDepthFirst(startBB);
583587

584-
doFixUp(F);
588+
commonFixUp(F);
585589
}
586590

587591
template <typename ImplClass>
@@ -606,7 +610,7 @@ void SILCloner<ImplClass>::cloneFunctionBody(SILFunction *F,
606610
// This will layout all newly cloned blocks immediate after clonedEntryBB.
607611
visitBlocksDepthFirst(&*F->begin());
608612

609-
doFixUp(F);
613+
commonFixUp(F);
610614
}
611615

612616
template<typename ImplClass>
@@ -709,9 +713,11 @@ void SILCloner<ImplClass>::visitBlocksDepthFirst(SILBasicBlock *startBB) {
709713
}
710714

711715
/// Clean-up after cloning.
712-
template<typename ImplClass>
713-
void
714-
SILCloner<ImplClass>::doFixUp(SILFunction *F) {
716+
template <typename ImplClass>
717+
void SILCloner<ImplClass>::commonFixUp(SILFunction *F) {
718+
// Call any cleanup specific to the CRTP extensions.
719+
asImpl().preFixUp(F);
720+
715721
// If our source function is in ossa form, but the function into which we are
716722
// cloning is not in ossa, after we clone, eliminate default arguments.
717723
if (!getBuilder().hasOwnership() && F->hasOwnership()) {
@@ -745,7 +751,7 @@ SILCloner<ImplClass>::doFixUp(SILFunction *F) {
745751
}
746752

747753
// Call any cleanup specific to the CRTP extensions.
748-
asImpl().fixUp(F);
754+
asImpl().postFixUp(F);
749755
}
750756

751757
template<typename ImplClass>

include/swift/SILOptimizer/Utils/GenericCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class GenericCloner
7171
return SC.getCloned();
7272
}
7373

74-
void fixUp(SILFunction *calleeFunction);
74+
void postFixUp(SILFunction *calleeFunction);
7575

7676
static SILFunction *createDeclaration(SILOptFunctionBuilder &FuncBuilder,
7777
SILFunction *Orig,

lib/SILOptimizer/Utils/GenericCloner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ const SILDebugScope *GenericCloner::remapScope(const SILDebugScope *DS) {
209209
return RemappedScope;
210210
}
211211

212-
void GenericCloner::fixUp(SILFunction *f) {
212+
void GenericCloner::postFixUp(SILFunction *f) {
213213
for (auto *apply : noReturnApplies) {
214214
auto applyBlock = apply->getParent();
215215
applyBlock->split(std::next(SILBasicBlock::iterator(apply)));

lib/SILOptimizer/Utils/SILInliner.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,12 @@ class SILInlineCloner
293293
/// This hook is called after either of the top-level visitors:
294294
/// cloneReachableBlocks or cloneSILFunction.
295295
///
296-
/// After fixUp, the SIL must be valid and semantically equivalent to the SIL
297-
/// before cloning.
298-
void fixUp(SILFunction *calleeFunction);
296+
/// After `preFixUp` is called `commonFixUp` will be called.
297+
void preFixUp(SILFunction *calleeFunction);
298+
299+
/// After postFixUp, the SIL must be valid and semantically equivalent to the
300+
/// SIL before cloning.
301+
void postFixUp(SILFunction *calleeFunction);
299302

300303
const SILDebugScope *getOrCreateInlineScope(const SILDebugScope *DS);
301304

@@ -602,12 +605,14 @@ void SILInlineCloner::visitTerminator(SILBasicBlock *BB) {
602605
visit(BB->getTerminator());
603606
}
604607

605-
void SILInlineCloner::fixUp(SILFunction *calleeFunction) {
608+
void SILInlineCloner::preFixUp(SILFunction *calleeFunction) {
606609
// "Completing" the BeginApply only fixes the end of the apply scope. The
607610
// begin_apply itself lingers.
608611
if (BeginApply)
609612
BeginApply->complete();
613+
}
610614

615+
void SILInlineCloner::postFixUp(SILFunction *calleeFunction) {
611616
NextIter = std::next(Apply.getInstruction()->getIterator());
612617

613618
assert(!Apply.getInstruction()->hasUsesOfAnyResult());

test/SILOptimizer/inline_begin_apply.sil

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ unwind:
4444
unwind
4545
}
4646

47+
sil [transparent] [ossa] @test_unreachable : $@yield_once <C: SomeClass> () -> (@yields @in Indirect<C>) {
48+
entry:
49+
unreachable
50+
}
51+
4752
// CHECK-LABEL: sil @test_simple_call
4853
// CHECK: bb0(%0 : $Builtin.Int1):
4954
// CHECK: [[MARKER:%.*]] = function_ref @marker
@@ -109,6 +114,40 @@ cont:
109114
return %ret : $()
110115
}
111116

117+
// CHECK-LABEL: sil @test_unreachable_call :
118+
// CHECK: bb0(%0 : $Builtin.Int1):
119+
// CHECK: [[MARKER:%.*]] = function_ref @marker
120+
// CHECK: unreachable
121+
// CHECK: } // end sil function 'test_unreachable_call'
122+
sil @test_unreachable_call : $(Builtin.Int1) -> () {
123+
entry(%flag : $Builtin.Int1):
124+
%marker = function_ref @marker : $@convention(thin) (Builtin.Int32) -> ()
125+
%0 = function_ref @test_unreachable : $@convention(thin) @yield_once <T: SomeClass> () -> (@yields @in Indirect<T>)
126+
(%value, %token) = begin_apply %0<SomeSubclass>() : $@convention(thin) @yield_once <T: SomeClass> () -> (@yields @in Indirect<T>)
127+
destroy_addr %value : $*Indirect<SomeSubclass>
128+
cond_br %flag, yes, no
129+
130+
yes:
131+
%10 = integer_literal $Builtin.Int32, 10
132+
apply %marker(%10) : $@convention(thin) (Builtin.Int32) -> ()
133+
end_apply %token
134+
%20 = integer_literal $Builtin.Int32, 20
135+
apply %marker(%20) : $@convention(thin) (Builtin.Int32) -> ()
136+
br cont
137+
138+
no:
139+
%11 = integer_literal $Builtin.Int32, 11
140+
apply %marker(%11) : $@convention(thin) (Builtin.Int32) -> ()
141+
abort_apply %token
142+
%21 = integer_literal $Builtin.Int32, 21
143+
apply %marker(%21) : $@convention(thin) (Builtin.Int32) -> ()
144+
br cont
145+
146+
cont:
147+
%ret = tuple ()
148+
return %ret : $()
149+
}
150+
112151
sil [transparent] [ossa] @test_two_yield : $@yield_once <C: SomeClass> (Builtin.Int1) -> (@yields @in Indirect<C>, @yields Builtin.Int64) {
113152
entry(%0 : $Builtin.Int1):
114153
%marker = function_ref @marker : $@convention(thin) (Builtin.Int32) -> ()

0 commit comments

Comments
 (0)