Skip to content

Commit e30b20f

Browse files
authored
Merge pull request #27664 from eeckstein/inout-aliasing
SILOptimizer: always assume that inout arguments are not aliasing
2 parents 7032e65 + 8e21379 commit e30b20f

File tree

6 files changed

+30
-32
lines changed

6 files changed

+30
-32
lines changed

include/swift/SIL/SILArgumentConvention.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@
1717

1818
namespace swift {
1919

20-
enum class InoutAliasingAssumption {
21-
/// Assume that an inout indirect parameter may alias other objects.
22-
/// This is the safe assumption an optimization should make if it may break
23-
/// memory safety in case the inout aliasing rule is violation.
24-
Aliasing,
25-
26-
/// Assume that an inout indirect parameter cannot alias other objects.
27-
/// Optimizations should only use this if they can guarantee that they will
28-
/// not break memory safety even if the inout aliasing rule is violated.
29-
NotAliasing
30-
};
31-
3220
/// Conventions for apply operands and function-entry arguments in SIL.
3321
///
3422
/// This is simply a union of ParameterConvention and ResultConvention
@@ -142,19 +130,14 @@ struct SILArgumentConvention {
142130
}
143131

144132
/// Returns true if \p Value is a not-aliasing indirect parameter.
145-
/// The \p isInoutAliasing specifies what to assume about the inout
146-
/// convention.
147-
/// See InoutAliasingAssumption.
148-
bool isNotAliasedIndirectParameter(InoutAliasingAssumption isInoutAliasing) {
133+
bool isNotAliasedIndirectParameter() {
149134
switch (Value) {
150135
case SILArgumentConvention::Indirect_In:
151136
case SILArgumentConvention::Indirect_In_Constant:
152137
case SILArgumentConvention::Indirect_Out:
153138
case SILArgumentConvention::Indirect_In_Guaranteed:
154-
return true;
155-
156139
case SILArgumentConvention::Indirect_Inout:
157-
return isInoutAliasing == InoutAliasingAssumption::NotAliasing;
140+
return true;
158141

159142
case SILArgumentConvention::Indirect_InoutAliasable:
160143
case SILArgumentConvention::Direct_Unowned:

include/swift/SILOptimizer/Analysis/ValueTracking.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ namespace swift {
2626
/// any other pointer in the function.
2727
/// The \p assumeInoutIsNotAliasing specifies in no-aliasing is assumed for
2828
/// the @inout convention. See swift::isNotAliasedIndirectParameter().
29-
bool isNotAliasingArgument(SILValue V, InoutAliasingAssumption isInoutAliasing =
30-
InoutAliasingAssumption::Aliasing);
29+
bool isNotAliasingArgument(SILValue V);
3130

3231
/// Returns true if \p V is local inside its function. This means its underlying
3332
/// object either is a non-aliasing function argument or a locally allocated
3433
/// object.
3534
/// The \p assumeInoutIsNotAliasing specifies in no-aliasing is assumed for
3635
/// the @inout convention. See swift::isNotAliasedIndirectParameter().
37-
bool pointsToLocalObject(SILValue V, InoutAliasingAssumption isInoutAliasing =
38-
InoutAliasingAssumption::Aliasing);
36+
bool pointsToLocalObject(SILValue V);
3937

4038
enum class IsZeroKind {
4139
Zero,

lib/SILOptimizer/Analysis/ValueTracking.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@
2323
using namespace swift;
2424
using namespace swift::PatternMatch;
2525

26-
bool swift::isNotAliasingArgument(SILValue V,
27-
InoutAliasingAssumption isInoutAliasing) {
26+
bool swift::isNotAliasingArgument(SILValue V) {
2827
auto *Arg = dyn_cast<SILFunctionArgument>(V);
2928
if (!Arg)
3029
return false;
3130

3231
SILArgumentConvention Conv = Arg->getArgumentConvention();
33-
return Conv.isNotAliasedIndirectParameter(isInoutAliasing);
32+
return Conv.isNotAliasedIndirectParameter();
3433
}
3534

3635
/// Check if the parameter \V is based on a local object, e.g. it is an
@@ -81,10 +80,9 @@ static bool isLocalObject(SILValue Obj) {
8180
return true;
8281
}
8382

84-
bool swift::pointsToLocalObject(SILValue V,
85-
InoutAliasingAssumption isInoutAliasing) {
83+
bool swift::pointsToLocalObject(SILValue V) {
8684
V = getUnderlyingObject(V);
87-
return isLocalObject(V) || isNotAliasingArgument(V, isInoutAliasing);
85+
return isLocalObject(V) || isNotAliasingArgument(V);
8886
}
8987

9088
/// Check if the value \p Value is known to be zero, non-zero or unknown.

test/SILOptimizer/basic-aa.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bb1(%1 : $*Builtin.NativeObject, %2 : $*Builtin.NativeObject):
114114
// CHECK: PAIR #1.
115115
// CHECK-NEXT: %0 = argument of bb0
116116
// CHECK-NEXT: %1 = argument of bb0
117-
// CHECK-NEXT: MayAlias
117+
// CHECK-NEXT: NoAlias
118118
sil @inout_args_may_alias: $@convention(thin) (@inout Builtin.NativeObject, @inout Builtin.NativeObject) -> () {
119119
bb0(%0 : $*Builtin.NativeObject, %1 : $*Builtin.NativeObject):
120120
%2 = tuple()

test/SILOptimizer/dead_store_elim.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,24 @@ bb3:
469469
return %9999 : $()
470470
}
471471

472+
sil @unknown : $@convention(thin) () -> ()
473+
474+
// CHECK-LABEL: sil @inout_is_not_aliasing : $@convention(thin) (@inout Builtin.Int32) -> () {
475+
// CHECK: bb0
476+
// CHECK-NEXT: integer_literal
477+
// CHECK-NEXT: function_ref
478+
// CHECK: return
479+
sil @inout_is_not_aliasing : $@convention(thin) (@inout Builtin.Int32) -> () {
480+
bb0(%0 : $*Builtin.Int32):
481+
%1 = integer_literal $Builtin.Int32, 0
482+
store %1 to %0 : $*Builtin.Int32
483+
%f = function_ref @unknown : $@convention(thin) () -> ()
484+
%3 = apply %f() : $@convention(thin) () -> ()
485+
store %1 to %0 : $*Builtin.Int32
486+
%9999 = tuple()
487+
return %9999 : $()
488+
}
489+
472490
// We should be able to remove the store in bb0, but we currently
473491
// can't due to deficiency in alias analysis.
474492
//

test/SILOptimizer/redundant_load_elim.sil

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,9 @@ bb5:
588588
// CHECK: store
589589
// CHECK-NOT: = load
590590
// CHECK: bb2:
591-
// CHECK: bb3:
592-
// CHECK: = load
591+
// CHECK: bb3([[A:%[0-9]+]] : $Builtin.Int32):
592+
// CHECK-NOT: = load
593+
// CHECK: apply %{{[0-9]+}}([[A]])
593594
sil @load_to_load_conflicting_branches_diamond : $@convention(thin) (@inout Builtin.Int32) -> () {
594595
// %0 // users: %1, %4, %9, %11, %16, %21
595596
bb0(%0 : $*Builtin.Int32):

0 commit comments

Comments
 (0)