Skip to content

Commit 614a635

Browse files
committed
StripObjectHeaders: make sure that a dealloc_ref comes from the original root allocation.
Fixes a miscompile
1 parent 5bc0366 commit 614a635

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/StripObjectHeaders.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ let stripObjectHeadersPass = FunctionPass(name: "strip-object-headers") {
3737

3838
private extension Value {
3939
func needObjectHeader() -> Bool {
40-
var walker = IsBareObjectWalker()
40+
var walker = IsBareObjectWalker(rootDef: self)
4141
return walker.walkDownUses(ofValue: self, path: SmallProjectionPath()) == .abortWalk
4242
}
4343
}
4444

45-
private struct IsBareObjectWalker : ValueDefUseWalker {
45+
private struct IsBareObjectWalker : ValueDefUseWalker, ValueUseDefWalker {
46+
var walkUpCache = WalkerCache<SmallProjectionPath>()
4647
var walkDownCache = WalkerCache<SmallProjectionPath>()
48+
let rootDef: Value
4749

4850
mutating func walkDown(value operand: Operand, path: Path) -> WalkResult {
4951
switch operand.instruction {
@@ -67,11 +69,20 @@ private struct IsBareObjectWalker : ValueDefUseWalker {
6769
switch operand.instruction {
6870
// White-list all instructions which don't use the object header.
6971
case is RefElementAddrInst, is RefTailAddrInst,
70-
is DeallocRefInst, is DeallocStackRefInst,
72+
is DeallocStackRefInst,
7173
is DebugValueInst, is FixLifetimeInst:
7274
return .continueWalk
75+
case let deallocRef as DeallocRefInst:
76+
// Check if the final dealloc_ref comes from the single `rootDef`.
77+
// In case of phi-arguments it might come from multiple root definitions.
78+
return walkUp(value: deallocRef.operand.value, path: path)
7379
default:
7480
return .abortWalk
7581
}
7682
}
83+
84+
mutating func rootDef(value: Value, path: SmallProjectionPath) -> WalkResult {
85+
return value == rootDef ? .continueWalk : .abortWalk
86+
}
87+
7788
}

0 commit comments

Comments
 (0)