Skip to content

Commit

Permalink
[AliasAnalysis] Builtin effects depend on escaping
Browse files Browse the repository at this point in the history
Mostly restore the behavior of getMemoryEffectsFn on builtins other than
`once` and `onceWithContext` to before
8a8a895 but keeping the improvement to
return `Instruction.memoryEffects` in the face of escaping.
  • Loading branch information
nate-chandler committed Jan 19, 2024
1 parent 6cdab78 commit 75ccbae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ private func getMemoryEffect(ofBuiltin builtin: BuiltinInst, for address: Value,
let callee = builtin.operands[1].value
return context.calleeAnalysis.getSideEffects(ofCallee: callee).memory
default:
return builtin.memoryEffects
if address.at(path).isEscaping(using: EscapesToInstructionVisitor(target: builtin, isAddress: true), context) {
return builtin.memoryEffects
}
return SideEffects.Memory()
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/SILOptimizer/mem-behavior.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,25 @@ bb0(%0 : $*C, %1 : $*C, %2 : @owned $C):
return %r : $()
}

// CHECK-LABEL: @test_builtin_zeroInitializer
// CHECK: PAIR #0.
// CHECK-NEXT: %2 = builtin "zeroInitializer"<C>(%1 : $*C)
// CHECK-NEXT: %0 = alloc_stack
// CHECK-NEXT: r=0,w=0
// CHECK: PAIR #1.
// CHECK-NEXT: %2 = builtin "zeroInitializer"<C>(%1 : $*C)
// CHECK-NEXT: %1 = alloc_stack
// CHECK-NEXT: r=0,w=1
sil @test_builtin_zeroInitializer : $@convention(thin) () -> () {
bb0:
%0 = alloc_stack $C
%1 = alloc_stack $C
%2 = builtin "zeroInitializer"<C>(%1 : $*C) : $()
%3 = apply undef<C>(%1) : $@convention(thin) <C> () -> @out C
copy_addr [take] %1 to [init] %0 : $*C
dealloc_stack %1 : $*C
destroy_addr %0 : $*C
dealloc_stack %0 : $*C
%4 = tuple ()
return %4 : $()
}
19 changes: 19 additions & 0 deletions test/SILOptimizer/templvalueopt.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Swift
import Builtin

// REQUIRES: swift_in_compiler

// CHECK-LABEL: sil @test_enum_with_initialize :
// CHECK: bb0(%0 : $*Optional<Any>, %1 : $*Any):
// CHECK-NEXT: [[E:%[0-9]+]] = init_enum_data_addr %0
Expand Down Expand Up @@ -249,3 +251,20 @@ bb0(%0 : $*T, %1 : $*T):
return %78 : $()
}


// CHECK-LABEL: sil @optimize_builtin_zeroInitialize : {{.*}} {
// CHECK: bb0([[RET_ADDR:%[^,]+]] :
// CHECK: builtin "zeroInitializer"<T>([[RET_ADDR]] : $*T) : $()
// CHECK: apply undef<T>([[RET_ADDR]])
// CHECK-LABEL: } // end sil function 'optimize_builtin_zeroInitialize'
sil @optimize_builtin_zeroInitialize : $@convention(thin) <T> () -> @out T {
bb0(%ret_addr : $*T):
%temporary = alloc_stack [lexical] $T
builtin "zeroInitializer"<T>(%temporary : $*T) : $()
apply undef<T>(%temporary) : $@convention(thin) <T> () -> @out T
copy_addr [take] %temporary to [init] %ret_addr : $*T
dealloc_stack %temporary : $*T
%15 = tuple ()
return %15 : $()
}

0 comments on commit 75ccbae

Please sign in to comment.