Skip to content

Commit 64095a6

Browse files
authored
Fix use of MemAcessUtils in LICM (#38784)
AccessPathWithBase::compute can return a valid access path with unidentified base. In such cases, we cannot LICM stores, because there is no base address to check if it is invariant
1 parent 33ff2be commit 64095a6

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,8 @@ LLVM_ATTRIBUTE_USED void AccessPath::dump() const { print(llvm::dbgs()); }
10151015
void AccessPathWithBase::print(raw_ostream &os) const {
10161016
if (base)
10171017
os << "Base: " << base;
1018-
1018+
else
1019+
os << "Base: unidentified\n";
10191020
accessPath.print(os);
10201021
}
10211022

lib/SILOptimizer/LoopTransforms/LICM.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ void LoopTreeOptimization::analyzeCurrentLoop(
939939
// how to rematerialize global_addr, then we don't need this base.
940940
auto access = AccessPathWithBase::compute(SI->getDest());
941941
auto accessPath = access.accessPath;
942-
if (accessPath.isValid() && isLoopInvariant(access.base, Loop)) {
942+
if (accessPath.isValid() &&
943+
(access.base && isLoopInvariant(access.base, Loop))) {
943944
if (isOnlyLoadedAndStored(AA, sideEffects, Loads, Stores, SI->getDest(),
944945
accessPath)) {
945946
if (!LoadAndStoreAddrs.count(accessPath)) {

test/SILOptimizer/licm.sil

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,3 +1318,82 @@ bb3:
13181318
%outer = tuple (%extract0 : $Int64, %inner: $(Int64, Int64))
13191319
return %outer : $(Int64, (Int64, Int64))
13201320
}
1321+
1322+
class C {}
1323+
1324+
// This won't be hoisted because we can't find a base to check if it is invariant
1325+
// CHECK-LABEL: sil @testLoopInvariantStoreNoBase1 :
1326+
// CHECK: bb6:
1327+
// CHECK-NOT: store
1328+
// CHECK-LABEL: } // end sil function 'testLoopInvariantStoreNoBase1'
1329+
sil @testLoopInvariantStoreNoBase1 : $@convention(thin) (Builtin.BridgeObject, Double) -> () {
1330+
bb0(%0 : $Builtin.BridgeObject, %1 : $Double):
1331+
cond_br undef, bb1, bb2
1332+
1333+
bb1:
1334+
%2 = unchecked_ref_cast %0 : $Builtin.BridgeObject to $C
1335+
%3 = ref_tail_addr [immutable] %2 : $C, $Double
1336+
%4 = address_to_pointer %3 : $*Double to $Builtin.RawPointer
1337+
br bb3(%4 : $Builtin.RawPointer)
1338+
1339+
bb2:
1340+
%6 = unchecked_ref_cast %0 : $Builtin.BridgeObject to $C
1341+
%7 = ref_tail_addr [immutable] %6 : $C, $Double
1342+
%8 = address_to_pointer %7 : $*Double to $Builtin.RawPointer
1343+
br bb3(%8 : $Builtin.RawPointer)
1344+
1345+
bb3(%9 : $Builtin.RawPointer):
1346+
br bb4
1347+
1348+
bb4:
1349+
%11 = pointer_to_address %9 : $Builtin.RawPointer to [strict] $*Double
1350+
store %1 to %11 : $*Double
1351+
cond_br undef, bb5, bb6
1352+
1353+
bb5:
1354+
br bb4
1355+
1356+
bb6:
1357+
%15 = tuple ()
1358+
return %15 : $()
1359+
}
1360+
1361+
// This won't be hoisted because we can't find a base to check if it is invariant
1362+
// CHECK-LABEL: sil @testLoopInvariantStoreNoBase2 :
1363+
// CHECK: bb6:
1364+
// CHECK-NOT: store
1365+
// CHECK-LABEL: } // end sil function 'testLoopInvariantStoreNoBase2'
1366+
sil @testLoopInvariantStoreNoBase2 : $@convention(thin) (Builtin.BridgeObject, Double) -> () {
1367+
bb0(%0 : $Builtin.BridgeObject, %1 : $Double):
1368+
cond_br undef, bb1, bb2
1369+
1370+
bb1:
1371+
%2 = unchecked_ref_cast %0 : $Builtin.BridgeObject to $C
1372+
%3 = ref_tail_addr [immutable] %2 : $C, $Double
1373+
%4 = address_to_pointer %3 : $*Double to $Builtin.RawPointer
1374+
br bb3(%4 : $Builtin.RawPointer)
1375+
1376+
bb2:
1377+
%6 = unchecked_ref_cast %0 : $Builtin.BridgeObject to $C
1378+
%7 = ref_tail_addr [immutable] %6 : $C, $Double
1379+
%8 = address_to_pointer %7 : $*Double to $Builtin.RawPointer
1380+
br bb3(%8 : $Builtin.RawPointer)
1381+
1382+
bb3(%9 : $Builtin.RawPointer):
1383+
br bb4
1384+
1385+
bb4:
1386+
%11 = pointer_to_address %9 : $Builtin.RawPointer to [strict] $*Double
1387+
%12 = integer_literal $Builtin.Word, 1
1388+
%13 = index_addr %11 : $*Double, %12 : $Builtin.Word
1389+
store %1 to %13 : $*Double
1390+
cond_br undef, bb5, bb6
1391+
1392+
bb5:
1393+
br bb4
1394+
1395+
bb6:
1396+
%15 = tuple ()
1397+
return %15 : $()
1398+
}
1399+

0 commit comments

Comments
 (0)