Skip to content

Commit 27c88a9

Browse files
authored
Merge pull request #15575 from dcci/loadable-debuginfo
2 parents af38053 + dfaf133 commit 27c88a9

File tree

9 files changed

+101
-16
lines changed

9 files changed

+101
-16
lines changed

include/swift/SIL/DebugUtils.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@ inline bool isDebugInst(SILInstruction *Inst) {
4949
return isa<DebugValueInst>(Inst) || isa<DebugValueAddrInst>(Inst);
5050
}
5151

52-
/// Returns true if the instruction \p Inst is a maintenance instructions which
53-
/// is relevant for debug information and does not get lowered to an instruction.
54-
inline bool isMaintenanceInst(SILInstruction *Inst) {
55-
return isDebugInst(Inst) || isa<AllocStackInst>(Inst);
56-
}
57-
5852
/// Deletes all of the debug instructions that use \p Inst.
5953
inline void deleteAllDebugUses(ValueBase *Inst) {
6054
for (auto UI = Inst->use_begin(), E = Inst->use_end(); UI != E;) {

include/swift/SIL/SILBasicBlock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
344344
/// Used by llvm::LoopInfo.
345345
bool isLegalToHoistInto() const;
346346

347+
/// Returns the debug scope of the first non-meta instructions in the
348+
/// basic block. SILBuilderWithScope uses this to correctly set up
349+
/// the debug scope for newly created instructions.
350+
const SILDebugScope *getScopeOfFirstNonMetaInstruction();
351+
347352
//===--------------------------------------------------------------------===//
348353
// Debugging
349354
//===--------------------------------------------------------------------===//

include/swift/SIL/SILBuilder.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,15 @@ class SILBuilderWithScope : public SILBuilder {
20822082
: SILBuilder(BB) {
20832083
inheritScopeFrom(InheritScopeFrom);
20842084
}
2085+
2086+
/// Creates a new SILBuilder with an insertion point at the
2087+
/// beginning of BB and the debug scope from the first
2088+
/// non-metainstruction in the BB.
2089+
explicit SILBuilderWithScope(SILBasicBlock *BB) : SILBuilder(BB->begin()) {
2090+
const SILDebugScope *DS = BB->getScopeOfFirstNonMetaInstruction();
2091+
assert(DS && "Instruction without debug scope associated!");
2092+
setCurrentDebugScope(DS);
2093+
}
20852094
};
20862095

20872096
class SavedInsertionPointRAII {

include/swift/SIL/SILInstruction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ class SILInstruction
597597
/// you perform such optimizations like e.g. jump-threading.
598598
bool isTriviallyDuplicatable() const;
599599

600+
/// Returns true if the instruction is a meta instruction which is
601+
/// relevant for debug information and does not get lowered to a real
602+
/// instruction.
603+
bool isMetaInstruction() const;
604+
600605
/// Verify that all operands of this instruction have compatible ownership
601606
/// with this instruction.
602607
void verifyOperandOwnership() const;

lib/IRGen/LoadableByAddress.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ void LoadableStorageAllocation::replaceLoadWithCopyAddr(
946946
LoadInst *optimizableLoad) {
947947
SILValue value = optimizableLoad->getOperand();
948948

949-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
949+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
950950
AllocStackInst *allocInstr =
951951
allocBuilder.createAllocStack(value.getLoc(), value->getType());
952952

@@ -1069,7 +1069,7 @@ void LoadableStorageAllocation::replaceLoadWithCopyAddrForModifiable(
10691069
}
10701070
SILValue value = unoptimizableLoad->getOperand();
10711071

1072-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1072+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
10731073
AllocStackInst *allocInstr =
10741074
allocBuilder.createAllocStack(value.getLoc(), value->getType());
10751075

@@ -1411,7 +1411,7 @@ void LoadableStorageAllocation::allocateForArg(SILValue value) {
14111411

14121412
assert(!ApplySite::isa(value) && "Unexpected instruction");
14131413

1414-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1414+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
14151415
AllocStackInst *allocInstr =
14161416
allocBuilder.createAllocStack(value.getLoc(), value->getType());
14171417

@@ -1438,7 +1438,7 @@ void LoadableStorageAllocation::allocateForArg(SILValue value) {
14381438
AllocStackInst *
14391439
LoadableStorageAllocation::allocateForApply(SILInstruction *apply,
14401440
SILType type) {
1441-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1441+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
14421442
auto *allocInstr = allocBuilder.createAllocStack(apply->getLoc(), type);
14431443

14441444
pass.largeLoadableArgs.push_back(allocInstr);
@@ -1524,7 +1524,7 @@ static void setInstrUsers(StructLoweringState &pass, AllocStackInst *allocInstr,
15241524
static void allocateAndSetForInstrOperand(StructLoweringState &pass,
15251525
SingleValueInstruction *instrOperand){
15261526
assert(instrOperand->getType().isObject());
1527-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1527+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
15281528
AllocStackInst *allocInstr = allocBuilder.createAllocStack(
15291529
instrOperand->getLoc(), instrOperand->getType());
15301530

@@ -1558,7 +1558,7 @@ static void allocateAndSetForArgumentOperand(StructLoweringState &pass,
15581558
auto *arg = dyn_cast<SILArgument>(value);
15591559
assert(arg && "non-instr operand must be an argument");
15601560

1561-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1561+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
15621562
AllocStackInst *allocInstr =
15631563
allocBuilder.createAllocStack(applyInst->getLoc(), value->getType());
15641564

@@ -1677,7 +1677,8 @@ static SILValue createCopyOfEnum(StructLoweringState &pass,
16771677
auto value = orig->getOperand();
16781678
auto type = value->getType();
16791679
if (type.isObject()) {
1680-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1680+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
1681+
16811682
// support for non-address operands / enums
16821683
auto *allocInstr = allocBuilder.createAllocStack(orig->getLoc(), type);
16831684
SILBuilderWithScope storeBuilder(orig);
@@ -1696,7 +1697,7 @@ static SILValue createCopyOfEnum(StructLoweringState &pass,
16961697
}
16971698
value = allocInstr;
16981699
}
1699-
SILBuilderWithScope allocBuilder(pass.F->begin()->begin());
1700+
SILBuilderWithScope allocBuilder(&*pass.F->begin());
17001701
auto *allocInstr = allocBuilder.createAllocStack(value.getLoc(), type);
17011702

17021703
SILBuilderWithScope copyBuilder(orig);

lib/SIL/SILBasicBlock.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "llvm/ADT/STLExtras.h"
18+
#include "swift/SIL/DebugUtils.h"
1819
#include "swift/SIL/SILBasicBlock.h"
1920
#include "swift/SIL/SILBuilder.h"
2021
#include "swift/SIL/SILArgument.h"
@@ -380,3 +381,10 @@ bool SILBasicBlock::isTrampoline() const {
380381
bool SILBasicBlock::isLegalToHoistInto() const {
381382
return true;
382383
}
384+
385+
const SILDebugScope *SILBasicBlock::getScopeOfFirstNonMetaInstruction() {
386+
for (auto &Inst : *this)
387+
if (Inst.isMetaInstruction())
388+
return Inst.getDebugScope();
389+
return begin()->getDebugScope();
390+
}

lib/SIL/SILInstruction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,20 @@ bool SILInstruction::mayTrap() const {
11881188
}
11891189
}
11901190

1191+
bool SILInstruction::isMetaInstruction() const {
1192+
// Every instruction that implements getVarInfo() should be in this list.
1193+
switch (getKind()) {
1194+
case SILInstructionKind::AllocBoxInst:
1195+
case SILInstructionKind::AllocStackInst:
1196+
case SILInstructionKind::DebugValueInst:
1197+
case SILInstructionKind::DebugValueAddrInst:
1198+
return true;
1199+
default:
1200+
return false;
1201+
}
1202+
llvm_unreachable("Instruction not handled in isMetaInstruction()!");
1203+
}
1204+
11911205
//===----------------------------------------------------------------------===//
11921206
// Utilities
11931207
//===----------------------------------------------------------------------===//

lib/SIL/SILVerifier.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4545,14 +4545,14 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
45454545

45464546
const SILDebugScope *LastSeenScope = nullptr;
45474547
for (SILInstruction &SI : *BB) {
4548-
if (isMaintenanceInst(&SI))
4548+
if (SI.isMetaInstruction())
45494549
continue;
45504550
LastSeenScope = SI.getDebugScope();
45514551
AlreadySeenScopes.insert(LastSeenScope);
45524552
break;
45534553
}
45544554
for (SILInstruction &SI : *BB) {
4555-
if (isMaintenanceInst(&SI))
4555+
if (SI.isMetaInstruction())
45564556
continue;
45574557

45584558
// If we haven't seen this debug scope yet, update the
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Check we don't crash when verifying debug info.
2+
// Ideally this should print the output after loadable by address runs
3+
// but there's no way of doing this in SIL (for IRGen passes).
4+
// RUN: %target-swift-frontend -emit-sil %s -Onone \
5+
// RUN: -sil-verify-all -Xllvm -verify-di-holes -emit-ir \
6+
// RUN: -Xllvm -sil-print-debuginfo -g -o - | %FileCheck %s
7+
8+
struct m {
9+
let major: Int
10+
let minor: Int
11+
let n: Int
12+
let o: [String]
13+
let p: [String]
14+
init(major: Int, minor: Int, n: Int, o: [String], p: [String]) {
15+
self.major = major
16+
self.minor = minor
17+
self.n = n
18+
self.o = o
19+
self.p = p
20+
}
21+
}
22+
23+
enum a {
24+
case any
25+
case b(m)
26+
}
27+
28+
struct c<e> {
29+
enum f {
30+
case g(a)
31+
}
32+
}
33+
34+
struct h<i>{
35+
typealias j = i
36+
typealias d = j
37+
typealias f = c<d>.f
38+
subscript(identifier: d) -> f {
39+
return .g(.any)
40+
}
41+
func k(l: f, identifier: d) -> h {
42+
switch (l, self[identifier]) {
43+
default:
44+
return self
45+
}
46+
}
47+
}
48+
49+
// CHECK: define linkonce_odr hidden %swift.opaque* @"$S4main1mVwCP"

0 commit comments

Comments
 (0)