Skip to content

Commit

Permalink
TempRValueOpt: don't allow copy_addr [take] from a projection of the …
Browse files Browse the repository at this point in the history
…temporary.

This fixes a memory lifetime failure.
  • Loading branch information
eeckstein committed Oct 16, 2020
1 parent 4557f15 commit 262c7b2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/SILOptimizer/Transforms/TempRValueElimination.cpp
Expand Up @@ -282,6 +282,10 @@ collectLoads(Operand *addressUse, CopyAddrInst *originalCopy,
LLVM_DEBUG(llvm::dbgs() << " Temp written or taken" << *user);
return false;
}
// As with load [take], only accept copy_addr [take] if it takes the whole
// temporary object.
if (copyFromTmp->isTakeOfSrc() && address != originalCopy->getDest())
return false;
loadInsts.insert(copyFromTmp);
return true;
}
Expand Down
25 changes: 25 additions & 0 deletions test/SILOptimizer/temp_rvalue_opt_ossa.sil
Expand Up @@ -16,6 +16,11 @@ struct GS<Base> {

class Klass {}

struct Two {
var a: Klass
var b: Klass
}

sil @unknown : $@convention(thin) () -> ()

sil [ossa] @guaranteed_user : $@convention(thin) (@guaranteed Klass) -> ()
Expand Down Expand Up @@ -208,6 +213,26 @@ bb0(%0 : $*Builtin.NativeObject):
%v = tuple ()
return %v : $()
}

// CHECK-LABEL: sil [ossa] @dont_allow_copy_take_from_projection
// CHECK: [[STK:%[0-9]+]] = alloc_stack
// CHECK: copy_addr [take] %1 to [initialization] [[STK]]
// CHECK: } // end sil function 'dont_allow_copy_take_from_projection'
sil [ossa] @dont_allow_copy_take_from_projection : $@convention(thin) (@in Two) -> @out Two {
bb0(%0 : $*Two, %1 : $*Two):
%a0 = struct_element_addr %0 : $*Two, #Two.a
%b0 = struct_element_addr %0 : $*Two, #Two.b
%s = alloc_stack $Two
copy_addr [take] %1 to [initialization] %s : $*Two
%as = struct_element_addr %s : $*Two, #Two.a
%bs = struct_element_addr %s : $*Two, #Two.b
copy_addr [take] %as to [initialization] %a0 : $*Klass
copy_addr [take] %bs to [initialization] %b0 : $*Klass
dealloc_stack %s : $*Two
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @load_in_wrong_block
// CHECK: bb0(%0 : $*GS<B>):
// CHECK-NEXT: alloc_stack
Expand Down

0 comments on commit 262c7b2

Please sign in to comment.