Skip to content

Commit 51cb757

Browse files
committed
RedundantLoadElimination: don't crash when loading a non-trivial enum which was stored as trivial non-payload case.
rdar://114648847
1 parent de4370b commit 51cb757

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,15 @@ private func shrinkMemoryLifetime(from load: LoadInst, to availableValue: Availa
307307
case .assign:
308308
builder.createDestroyAddr(address: availableStore.destination)
309309
context.erase(instruction: availableStore)
310-
case .initialize:
310+
case .initialize,
311+
// It can be the case that e non-payload case is stored as trivial enum and the enum is loaded as [take], e.g.
312+
// %1 = enum $Optional<Class>, #Optional.none
313+
// store %1 to [trivial] %addr : $*Optional<Class>
314+
// %2 = load [take] %addr : $*Optional<Class>
315+
.trivial:
311316
context.erase(instruction: availableStore)
312-
case .trivial, .unqualified:
313-
fatalError("store ownership doesn't fit a later load [take]")
317+
case .unqualified:
318+
fatalError("unqualified store in ossa function?")
314319
}
315320
return valueToAdd
316321
}

test/SILOptimizer/redundant_load_elim_ossa.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,3 +1446,19 @@ bb0(%0 : $Builtin.RawPointer, %1 : $A2):
14461446
%30 = tuple(%3 : $A, %20 : $A)
14471447
return %30 : $(A, A)
14481448
}
1449+
1450+
// CHECK-LABEL: sil [ossa] @store_trivial_load_non_trivial_enum
1451+
// CHECK: %0 = enum $Optional<B>, #Optional.none!enumelt
1452+
// CHECK-NOT: store
1453+
// CHECK-NOT: load
1454+
// CHECK: return %0
1455+
// CHECK-LABEL: } // end sil function 'store_trivial_load_non_trivial_enum'
1456+
sil [ossa] @store_trivial_load_non_trivial_enum : $@convention(thin) () -> @owned Optional<B> {
1457+
bb0:
1458+
%0 = enum $Optional<B>, #Optional.none!enumelt
1459+
%1 = alloc_stack $Optional<B>
1460+
store %0 to [trivial] %1 : $*Optional<B>
1461+
%3 = load [take] %1 : $*Optional<B>
1462+
dealloc_stack %1 : $*Optional<B>
1463+
return %3 : $Optional<B>
1464+
}

0 commit comments

Comments
 (0)