Skip to content

Commit 97b8501

Browse files
author
Joe Shajrawi
committed
DominatedAccessRemoval: fix a bug wherein we incorrectly removes nested conflicting accesses
radar rdar://problem/48323041
1 parent 8e5d5ed commit 97b8501

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

lib/SILOptimizer/Transforms/AccessEnforcementDom.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ bool DominatedAccessRemoval::visitInstruction(
145145
return true;
146146
}
147147

148+
static EndAccessInst *getSingleEndAccess(BeginAccessInst *inst) {
149+
EndAccessInst *end = nullptr;
150+
for (auto *currEnd : inst->getEndAccesses()) {
151+
if (end == nullptr)
152+
end = currEnd;
153+
else
154+
return nullptr;
155+
}
156+
return end;
157+
}
158+
148159
void DominatedAccessRemoval::visitBeginAccess(
149160
BeginAccessInst *beginAccess, AccessedStorage storage,
150161
AccessedStorageInfo &visitedDomAccessesToStorageInfo) {
@@ -185,6 +196,20 @@ void DominatedAccessRemoval::visitBeginAccess(
185196
}
186197

187198
auto *parentBegin = visitedAccessesIt->first;
199+
200+
// If this beginAccess is a modify, and is nested inside parent
201+
// then we have to bail: can't change it to static
202+
// If, on the other hand, it is a read, we can continue.
203+
if (beginAccess->getAccessKind() >= SILAccessKind::Modify) {
204+
auto *endP = getSingleEndAccess(parentBegin);
205+
if (!endP) {
206+
return;
207+
}
208+
if (!domInfo->properlyDominates(endP, beginAccess)) {
209+
return;
210+
}
211+
}
212+
188213
if (beginAccess->getAccessKind() > parentBegin->getAccessKind()) {
189214
// we can't change to static: dominating access has a > access kind
190215
// change parent's access kind so we would be able to do so

test/SILOptimizer/access_dom.sil

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,47 @@ bb4:
540540
%10 = tuple ()
541541
return %10 : $()
542542
}
543+
544+
// public func testBailOnNestedDomWrite() {
545+
// Checks that we bail when the dominated begin comes before the dominating begin
546+
//
547+
// CHECK-LABEL: sil @testBailOnNestedDomWrite : $@convention(thin) () -> () {
548+
// CHECK: bb0:
549+
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
550+
// CHECK: [[BEGIN:%.*]] = begin_access [modify] [dynamic] [[GLOBAL]] : $*X
551+
// CHECK: [[BEGIN2:%.*]] = begin_access [modify] [dynamic] [no_nested_conflict] [[GLOBAL]] : $*X
552+
// CHECK-LABEL: } // end sil function 'testBailOnNestedDomWrite'
553+
sil @testBailOnNestedDomWrite : $@convention(thin) () -> () {
554+
bb0:
555+
%0 = global_addr @globalX: $*X
556+
%4 = begin_access [modify] [dynamic] %0 : $*X
557+
%5 = load %4 : $*X
558+
%7 = begin_access [modify] [dynamic] [no_nested_conflict] %0 : $*X
559+
%8 = load %7 : $*X
560+
end_access %7 : $*X
561+
end_access %4 : $*X
562+
%10 = tuple ()
563+
return %10 : $()
564+
}
565+
566+
// public func testOptOnNestedDomRead() {
567+
// Checks that we bail when the dominated begin comes before the dominating begin
568+
//
569+
// CHECK-LABEL: sil @testOptOnNestedDomRead : $@convention(thin) () -> () {
570+
// CHECK: bb0:
571+
// CHECK: [[GLOBAL:%.*]] = global_addr @globalX : $*X
572+
// CHECK: [[BEGIN:%.*]] = begin_access [read] [dynamic] [[GLOBAL]] : $*X
573+
// CHECK: [[BEGIN2:%.*]] = begin_access [read] [static] [no_nested_conflict] [[GLOBAL]] : $*X
574+
// CHECK-LABEL: } // end sil function 'testOptOnNestedDomRead'
575+
sil @testOptOnNestedDomRead : $@convention(thin) () -> () {
576+
bb0:
577+
%0 = global_addr @globalX: $*X
578+
%4 = begin_access [read] [dynamic] %0 : $*X
579+
%5 = load %4 : $*X
580+
%7 = begin_access [read] [dynamic] [no_nested_conflict] %0 : $*X
581+
%8 = load %7 : $*X
582+
end_access %7 : $*X
583+
end_access %4 : $*X
584+
%10 = tuple ()
585+
return %10 : $()
586+
}

0 commit comments

Comments
 (0)