Skip to content

[AliasAnalysis] Improve builtin's effects to depend on escapingness. #70997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 : $()
}