Skip to content

AliasAnalysis: fix a bug where an index_addr with zero offset can result in a false no-alias result #42339

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
Apr 13, 2022
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
6 changes: 5 additions & 1 deletion lib/SIL/Utils/Projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,11 @@ Optional<ProjectionPath> ProjectionPath::getProjectionPath(SILValue Start,
Projection AP(Iter);
if (!AP.isValid())
break;
P.Path.push_back(AP);
// We _must_ ignore zero-indexing projections so that alias analysis
// recognizes an "alias" between two addresses where one has such an
// `index_addr 0` instruction and the other doesn't.
if (AP.getKind() != ProjectionKind::Index || AP.getIndex() != 0)
P.Path.push_back(AP);
}
Iter = cast<SingleValueInstruction>(*Iter).getOperand(0);
}
Expand Down
53 changes: 45 additions & 8 deletions test/SILOptimizer/alias-analysis.sil
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ struct OuterStruct {

// Test overlaping access on a different path; the user has misused an index offset
// CHECK-LABEL: @testOffsetBehindProjection
// CHECK: PAIR #28.
// CHECK: %4 = load %3 : $*Int64
// CHECK: %6 = load %5 : $*Int64
// CHECK: MayAlias
// CHECK: PAIR #23.
// CHECK: %3 = index_addr %1 : $*Int64, %2 : $Builtin.Word
// CHECK: %5 = struct_element_addr %0 : $*MyStruct, #MyStruct.j
// CHECK: NoAlias
sil shared @testOffsetBehindProjection : $@convention(thin) (@inout MyStruct) -> () {
bb0(%0 : $*MyStruct):
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
Expand All @@ -40,10 +40,10 @@ bb0(%0 : $*MyStruct):
}

// CHECK-LABEL: @testOffsetsBehindProjectionOverlap
// CHECK: PAIR #28.
// CHECK: %3 = load %2 : $*Int64
// CHECK: %7 = load %6 : $*Int64
// CHECK: MayAlias
// CHECK: PAIR #21.
// CHECK: %2 = struct_element_addr %1 : $*MyStruct, #MyStruct.i
// CHECK: %6 = struct_element_addr %5 : $*MyStruct, #MyStruct.i
// CHECK: NoAlias
sil shared @testOffsetsBehindProjectionOverlap : $@convention(thin) (@inout OuterStruct) -> () {
bb0(%0 : $*OuterStruct):
%1 = struct_element_addr %0 : $*OuterStruct, #OuterStruct.s
Expand All @@ -58,3 +58,40 @@ bb0(%0 : $*OuterStruct):
%99 = tuple ()
return %99 : $()
}

// CHECK-LABEL: @testIndexOf0
// CHECK: PAIR #8.
// CHECK: %1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK: %3 = index_addr %0 : $*MyStruct, %2 : $Builtin.Word
// CHECK: MayAlias
// CHECK: PAIR #9.
// CHECK: %1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK: %4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
// CHECK: MustAlias
sil @testIndexOf0 : $@convention(thin) (@inout MyStruct) -> () {
bb0(%0 : $*MyStruct):
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
%2 = integer_literal $Builtin.Word, 0
%3 = index_addr %0 : $*MyStruct, %2 : $Builtin.Word
%4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
%99 = tuple ()
return %99 : $()
}

// CHECK-LABEL: @testUnknownIndex
// CHECK: PAIR #12.
// CHECK: %2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK: %3 = index_addr %0 : $*MyStruct, %1 : $Builtin.Word
// CHECK: MayAlias
// CHECK: PAIR #13.
// CHECK: %2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK: %4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
// CHECK: MayAlias
sil @testUnknownIndex : $@convention(thin) (@inout MyStruct, Builtin.Word) -> () {
bb0(%0 : $*MyStruct, %1 : $Builtin.Word):
%2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
%3 = index_addr %0 : $*MyStruct, %1 : $Builtin.Word
%4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
%99 = tuple ()
return %99 : $()
}