Skip to content

Commit f3138b7

Browse files
committed
[DebugInfo] [Mem2Reg] Salvage removed uninhabited variables
For instance, Never.init(from:) will allocate self, and never store into it as that is impossible, so mem2reg will remove it.
1 parent b85e699 commit f3138b7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/Basic/GraphNodeWorklist.h"
2525
#include "swift/Basic/TaggedUnion.h"
2626
#include "swift/SIL/BasicBlockDatastructures.h"
27+
#include "swift/SIL/DebugUtils.h"
2728
#include "swift/SIL/Dominance.h"
2829
#include "swift/SIL/OSSALifetimeCompletion.h"
2930
#include "swift/SIL/Projection.h"
@@ -2124,6 +2125,14 @@ bool MemoryToRegisters::promoteAllocation(AllocStackInst *alloc,
21242125
// Remove write-only AllocStacks.
21252126
if (isWriteOnlyAllocation(alloc) && !lexicalLifetimeEnsured(alloc)) {
21262127
LLVM_DEBUG(llvm::dbgs() << "*** Deleting store-only AllocStack: "<< *alloc);
2128+
if (alloc->getElementType().getASTType()->isUninhabited()) {
2129+
if (auto VarInfo = alloc->getVarInfo())
2130+
SILBuilder(alloc, alloc->getDebugScope())
2131+
.createDebugValue(alloc->getLoc(),
2132+
SILUndef::get(alloc), *alloc->getVarInfo());
2133+
for (Operand *use : getDebugUses(alloc))
2134+
use->set(SILUndef::get(alloc));
2135+
}
21272136
deleter.forceDeleteWithUsers(alloc);
21282137
return true;
21292138
}

test/DebugInfo/mem2reg.sil

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all -sil-print-debuginfo -mem2reg %s | %FileCheck %s
2+
sil_stage canonical
3+
4+
import Builtin
5+
import Swift
6+
7+
// CHECK-LABEL: sil {{.+}} @uninhabited
8+
sil [ossa] @uninhabited : $@convention(thin) () -> () {
9+
bb0:
10+
// CHECK: debug_value undef : $*Never, var, name "self", loc "never.swift":90:10
11+
%2 = alloc_stack [var_decl] $Never, var, name "self", loc "never.swift":90:10
12+
dealloc_stack %2 : $*Never, loc "never.swift":90:10
13+
%11 = tuple (), loc "never.swift":94:57
14+
return %11 : $(), loc "never.swift":94:57
15+
} // end sil function '$uninhabited'

0 commit comments

Comments
 (0)