Skip to content

Commit 9b16a94

Browse files
committed
TempRValueOpt: small fixes/improvements for extending access scopes
Don't move an end_access over a (non-aliasing) end_access. This would destroy the proper nesting of accesses. Also, add some comments, asserts and tests.
1 parent 67ce72c commit 9b16a94

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -367,19 +367,27 @@ bool TempRValueOptPass::extendAccessScopes(
367367

368368
for (SILInstruction &inst : make_range(begin, end)) {
369369
if (auto *endAccess = dyn_cast<EndAccessInst>(&inst)) {
370+
// To keep things simple, we can just move a single end_access. Also, we
371+
// cannot move an end_access over a (non-aliasing) end_access.
372+
if (endAccessToMove)
373+
return false;
370374
// Is this the end of an access scope of the copy-source?
371375
if (!aa->isNoAlias(copySrc, endAccess->getSource())) {
372-
// To keep things simple, we can just move a single end_access.
373-
if (endAccessToMove)
374-
return false;
376+
assert(endAccess->getBeginAccess()->getAccessKind() ==
377+
SILAccessKind::Read &&
378+
"a may-write end_access should not be in the copysrc lifetime");
375379
endAccessToMove = endAccess;
376380
}
377381
} else if (endAccessToMove) {
378382
// We cannot move an end_access over a begin_access. This would destroy
379383
// the proper nesting of accesses.
380-
if (isa<BeginAccessInst>(&inst))
384+
if (isa<BeginAccessInst>(&inst) || isa<BeginUnpairedAccessInst>(inst))
381385
return false;
382386
// Don't extend a read-access scope over a (potential) write.
387+
// Note that inst can be a function call containing other access scopes.
388+
// But doing the mayWriteToMemory check, we know that the function can
389+
// only contain read accesses (to the same memory location). So it's fine
390+
// to move endAccessToMove even over such a function call.
383391
if (aa->mayWriteToMemory(&inst, endAccessToMove->getSource()))
384392
return false;
385393
}

test/SILOptimizer/temp_rvalue_opt_ossa.sil

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,50 @@ bb0:
879879
return %a : $String
880880
}
881881

882+
// CHECK-LABEL: sil [ossa] @dontExtendModifyAccess
883+
// CHECK: begin_access
884+
// CHECK-NEXT: alloc_stack
885+
// CHECK-NEXT: copy_addr
886+
// CHECK-NEXT: end_access
887+
// CHECK-NEXT: load
888+
// CHECK-LABEL: } // end sil function 'dontExtendModifyAccess'
889+
sil [ossa] @dontExtendModifyAccess : $@convention(thin) () -> @owned String {
890+
bb0:
891+
%1 = global_addr @globalString : $*String
892+
%3 = begin_access [modify] [dynamic] %1 : $*String
893+
%4 = alloc_stack $String
894+
copy_addr %3 to [initialization] %4 : $*String
895+
end_access %3 : $*String
896+
%6 = load [copy] %4 : $*String
897+
destroy_addr %4 : $*String
898+
dealloc_stack %4 : $*String
899+
return %6 : $String
900+
}
901+
902+
// CHECK-LABEL: sil [ossa] @dontExtendAccessScopeOverEndAccess
903+
// CHECK: begin_access [read] [dynamic] %0 : $*Int
904+
// CHECK-NEXT: begin_access [read] [dynamic] %{{[0-9]+}} : $*String
905+
// CHECK-NEXT: alloc_stack
906+
// CHECK-NEXT: copy_addr
907+
// CHECK-NEXT: end_access %{{[0-9]+}} : $*String
908+
// CHECK-NEXT: end_access %{{[0-9]+}} : $*Int
909+
// CHECK-NEXT: load
910+
// CHECK-LABEL: } // end sil function 'dontExtendAccessScopeOverEndAccess'
911+
sil [ossa] @dontExtendAccessScopeOverEndAccess : $@convention(thin) (@in_guaranteed Int) -> @owned String {
912+
bb0(%0 : $*Int):
913+
%1 = global_addr @globalString : $*String
914+
%2 = begin_access [read] [dynamic] %0 : $*Int
915+
%3 = begin_access [read] [dynamic] %1 : $*String
916+
%4 = alloc_stack $String
917+
copy_addr %3 to [initialization] %4 : $*String
918+
end_access %3 : $*String
919+
end_access %2 : $*Int
920+
%6 = load [copy] %4 : $*String
921+
destroy_addr %4 : $*String
922+
dealloc_stack %4 : $*String
923+
return %6 : $String
924+
}
925+
882926
// CHECK-LABEL: sil [ossa] @dontExtendAccessScopeOverBeginAccess : $@convention(thin) (@in Klass) -> () {
883927
// CHECK: bb0(%0 : $*Klass):
884928
// CHECK: [[STACK:%.*]] = alloc_stack $Klass

0 commit comments

Comments
 (0)