Skip to content

Commit c4da432

Browse files
authored
Merge pull request #41294 from gottesmm/pr-b518620ad3fad5dc8776648c3e8f560064c80668
[debug-info] Add DIExpr to SILDebugVariable::operator== for real.
2 parents b3220f7 + 0e3bca9 commit c4da432

File tree

3 files changed

+95
-54
lines changed

3 files changed

+95
-54
lines changed

include/swift/SIL/SILDebugVariable.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//===--- SILDebugVariable.h -----------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_SIL_SILDEBUGVARIABLE_H
14+
#define SWIFT_SIL_SILDEBUGVARIABLE_H
15+
16+
#include "swift/Basic/LLVM.h"
17+
#include "swift/SIL/SILDebugInfoExpression.h"
18+
#include "swift/SIL/SILLocation.h"
19+
#include "swift/SIL/SILType.h"
20+
#include "llvm/ADT/StringRef.h"
21+
22+
namespace swift {
23+
24+
class AllocationInst;
25+
26+
/// Holds common debug information about local variables and function
27+
/// arguments that are needed by DebugValueInst, AllocStackInst,
28+
/// and AllocBoxInst.
29+
struct SILDebugVariable {
30+
StringRef Name;
31+
unsigned ArgNo : 16;
32+
unsigned Constant : 1;
33+
unsigned Implicit : 1;
34+
Optional<SILType> Type;
35+
Optional<SILLocation> Loc;
36+
const SILDebugScope *Scope;
37+
SILDebugInfoExpression DIExpr;
38+
39+
// Use vanilla copy ctor / operator
40+
SILDebugVariable(const SILDebugVariable &) = default;
41+
SILDebugVariable &operator=(const SILDebugVariable &) = default;
42+
43+
SILDebugVariable()
44+
: ArgNo(0), Constant(false), Implicit(false), Scope(nullptr) {}
45+
SILDebugVariable(bool Constant, uint16_t ArgNo)
46+
: ArgNo(ArgNo), Constant(Constant), Implicit(false), Scope(nullptr) {}
47+
SILDebugVariable(StringRef Name, bool Constant, unsigned ArgNo,
48+
bool IsImplicit = false, Optional<SILType> AuxType = {},
49+
Optional<SILLocation> DeclLoc = {},
50+
const SILDebugScope *DeclScope = nullptr,
51+
llvm::ArrayRef<SILDIExprElement> ExprElements = {})
52+
: Name(Name), ArgNo(ArgNo), Constant(Constant), Implicit(IsImplicit),
53+
Type(AuxType), Loc(DeclLoc), Scope(DeclScope), DIExpr(ExprElements) {}
54+
55+
/// Created from either AllocStack or AllocBox instruction
56+
static Optional<SILDebugVariable>
57+
createFromAllocation(const AllocationInst *AI);
58+
59+
bool operator==(const SILDebugVariable &V) {
60+
return ArgNo == V.ArgNo && Constant == V.Constant && Name == V.Name &&
61+
Implicit == V.Implicit && Type == V.Type && Loc == V.Loc &&
62+
Scope == V.Scope && DIExpr == V.DIExpr;
63+
}
64+
65+
SILDebugVariable withoutDIExpr() const {
66+
auto result = *this;
67+
result.DIExpr = {};
68+
return result;
69+
}
70+
71+
bool isLet() const { return Name.size() && Constant; }
72+
73+
bool isVar() const { return Name.size() && !Constant; }
74+
};
75+
76+
} // namespace swift
77+
78+
#endif

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/SIL/SILAllocated.h"
3434
#include "swift/SIL/SILArgumentArrayRef.h"
3535
#include "swift/SIL/SILDebugInfoExpression.h"
36+
#include "swift/SIL/SILDebugVariable.h"
3637
#include "swift/SIL/SILDeclRef.h"
3738
#include "swift/SIL/SILFunctionConventions.h"
3839
#include "swift/SIL/SILLocation.h"
@@ -1775,50 +1776,6 @@ class UnaryInstructionWithTypeDependentOperandsBase
17751776
}
17761777
};
17771778

1778-
/// Holds common debug information about local variables and function
1779-
/// arguments that are needed by DebugValueInst, AllocStackInst,
1780-
/// and AllocBoxInst.
1781-
struct SILDebugVariable {
1782-
StringRef Name;
1783-
unsigned ArgNo : 16;
1784-
unsigned Constant : 1;
1785-
unsigned Implicit : 1;
1786-
Optional<SILType> Type;
1787-
Optional<SILLocation> Loc;
1788-
const SILDebugScope *Scope;
1789-
SILDebugInfoExpression DIExpr;
1790-
1791-
// Use vanilla copy ctor / operator
1792-
SILDebugVariable(const SILDebugVariable &) = default;
1793-
SILDebugVariable &operator=(const SILDebugVariable &) = default;
1794-
1795-
SILDebugVariable()
1796-
: ArgNo(0), Constant(false), Implicit(false), Scope(nullptr) {}
1797-
SILDebugVariable(bool Constant, uint16_t ArgNo)
1798-
: ArgNo(ArgNo), Constant(Constant), Implicit(false), Scope(nullptr) {}
1799-
SILDebugVariable(StringRef Name, bool Constant, unsigned ArgNo,
1800-
bool IsImplicit = false, Optional<SILType> AuxType = {},
1801-
Optional<SILLocation> DeclLoc = {},
1802-
const SILDebugScope *DeclScope = nullptr,
1803-
llvm::ArrayRef<SILDIExprElement> ExprElements = {})
1804-
: Name(Name), ArgNo(ArgNo), Constant(Constant), Implicit(IsImplicit),
1805-
Type(AuxType), Loc(DeclLoc), Scope(DeclScope), DIExpr(ExprElements) {}
1806-
1807-
/// Created from either AllocStack or AllocBox instruction
1808-
static Optional<SILDebugVariable>
1809-
createFromAllocation(const AllocationInst *AI);
1810-
1811-
bool operator==(const SILDebugVariable &V) {
1812-
return ArgNo == V.ArgNo && Constant == V.Constant && Name == V.Name &&
1813-
Implicit == V.Implicit && Type == V.Type && Loc == V.Loc &&
1814-
Scope == V.Scope && DIExpr == DIExpr;
1815-
}
1816-
1817-
bool isLet() const { return Name.size() && Constant; }
1818-
1819-
bool isVar() const { return Name.size() && !Constant; }
1820-
};
1821-
18221779
/// A DebugVariable where storage for the strings has been
18231780
/// tail-allocated following the parent SILInstruction.
18241781
class TailAllocatedDebugVariable {

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,36 @@ static void promoteDebugValueAddr(DebugValueInst *dvai, SILValue value,
167167
assert(dvai->getOperand()->getType().isLoadable(*dvai->getFunction()) &&
168168
"Unexpected promotion of address-only type!");
169169
assert(value && "Expected valid value");
170+
170171
// Avoid inserting the same debug_value twice.
172+
//
173+
// We remove the di expression when comparing since:
174+
//
175+
// 1. dvai is on will always have the deref diexpr since it is on addresses.
176+
//
177+
// 2. We are only trying to delete debug_var that are on values... values will
178+
// never have an op_deref meaning that the comparison will always fail and
179+
// not serve out purpose here.
180+
auto dvaiWithoutDIExpr = dvai->getVarInfo()->withoutDIExpr();
171181
for (auto *use : value->getUses()) {
172182
if (auto *dvi = dyn_cast<DebugValueInst>(use->getUser())) {
173-
// Since we're not comparing di-expression in
174-
// SILDebugVariable::operator==(), it's necessary to distinguish
175-
// debug_value w/ normal values from that with address-type values.
176-
if (!dvi->hasAddrVal() &&
177-
*dvi->getVarInfo() == *dvai->getVarInfo()) {
183+
if (!dvi->hasAddrVal() && *dvi->getVarInfo() == dvaiWithoutDIExpr) {
178184
deleter.forceDelete(dvai);
179185
return;
180186
}
181187
}
182188
}
183189

184-
auto VarInfo = *dvai->getVarInfo();
185190
// Drop op_deref if dvai is actually a debug_value instruction
191+
auto varInfo = *dvai->getVarInfo();
186192
if (isa<DebugValueInst>(dvai)) {
187-
auto &DIExpr = VarInfo.DIExpr;
188-
if (DIExpr)
189-
DIExpr.eraseElement(DIExpr.element_begin());
193+
auto &diExpr = varInfo.DIExpr;
194+
if (diExpr)
195+
diExpr.eraseElement(diExpr.element_begin());
190196
}
191197

192198
SILBuilderWithScope b(dvai, ctx);
193-
b.createDebugValue(dvai->getLoc(), value, std::move(VarInfo));
199+
b.createDebugValue(dvai->getLoc(), value, std::move(varInfo));
194200
deleter.forceDelete(dvai);
195201
}
196202

0 commit comments

Comments
 (0)