Skip to content

Commit eaaeb94

Browse files
authored
Merge pull request swiftlang#34936 from eeckstein/fix-templvalueopt
SILOptimizer: update alias analysis in TempRValueOpt and TempLValueOpt
2 parents c133e13 + 423169c commit eaaeb94

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,16 @@ class AliasAnalysis : public SILAnalysis {
164164
}
165165

166166
virtual void initialize(SILPassManager *PM) override;
167-
167+
168+
/// Explicitly invalidate an instruction.
169+
///
170+
/// This can be useful to update the alias analysis within a pass.
171+
/// It's needed if e.g. \p inst is an address projection and its operand gets
172+
/// replaced with a different underlying object.
173+
void invalidateInstruction(SILInstruction *inst) {
174+
handleDeleteNotification(inst);
175+
}
176+
168177
/// Perform an alias query to see if V1, V2 refer to the same values.
169178
AliasResult alias(SILValue V1, SILValue V2, SILType TBAAType1 = SILType(),
170179
SILType TBAAType2 = SILType());

lib/SILOptimizer/Transforms/TempLValueOpt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ bool TempLValueOptPass::tempLValueOpt(CopyAddrInst *copyInst) {
253253
user->eraseFromParent();
254254
break;
255255
default:
256+
AA->invalidateInstruction(user);
256257
use->set(destination);
257258
}
258259
}

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ bool TempRValueOptPass::tryOptimizeCopyIntoTemp(CopyAddrInst *copyInst) {
506506
while (!tempObj->use_empty()) {
507507
Operand *use = *tempObj->use_begin();
508508
SILInstruction *user = use->getUser();
509+
aa->invalidateInstruction(user);
510+
509511
switch (user->getKind()) {
510512
case SILInstructionKind::DestroyAddrInst:
511513
case SILInstructionKind::DeallocStackInst:

test/SILOptimizer/templvalueopt_ossa.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,36 @@ bb0(%0 : $*T, %1 : $*T):
249249
return %78 : $()
250250
}
251251

252+
class Child { }
253+
254+
struct Parent {
255+
var c : Child
256+
}
257+
258+
sil @gen_child : $@convention(thin) () -> @out Child
259+
260+
// Check that alias analysis is invalidated correctly and that the pass does
261+
// not produce invalid SIL (which would trigger a MemoryLifetime failure).
262+
//
263+
// CHECK-LABEL: sil [ossa] @invalidateAliasAnalysis :
264+
// CHECK: copy_addr
265+
// CHECK: } // end sil function 'invalidateAliasAnalysis'
266+
sil [ossa] @invalidateAliasAnalysis : $@convention(thin) (@owned Child) -> () {
267+
bb0(%0 :@owned $Child):
268+
%2 = alloc_stack $Parent
269+
%4 = alloc_stack $Child
270+
store %0 to [init] %4 : $*Child
271+
%7 = alloc_stack $Child
272+
%func = function_ref @gen_child : $@convention(thin) () -> @out Child
273+
%10 = apply %func(%7) : $@convention(thin)() -> @out Child
274+
%11 = struct_element_addr %2 : $*Parent, #Parent.c
275+
copy_addr [take] %7 to [initialization] %11 : $*Child
276+
dealloc_stack %7 : $*Child
277+
copy_addr [take] %4 to %11 : $*Child
278+
%17 = tuple ()
279+
dealloc_stack %4 : $*Child
280+
destroy_addr %2 : $*Parent
281+
dealloc_stack %2 : $*Parent
282+
%res = tuple ()
283+
return %res : $()
284+
}

0 commit comments

Comments
 (0)