Skip to content

Commit cb013b5

Browse files
committed
DebugInfo: Bypass resilience to calculate if a global is indirect or not
Progress on <rdar://problem/39722386>.
1 parent 2d71cae commit cb013b5

File tree

5 files changed

+66
-36
lines changed

5 files changed

+66
-36
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,7 @@ bool LinkInfo::isUsed(llvm::GlobalValue::LinkageTypes Linkage,
19651965
llvm::GlobalVariable *swift::irgen::createVariable(
19661966
IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *storageType,
19671967
Alignment alignment, DebugTypeInfo DbgTy, Optional<SILLocation> DebugLoc,
1968-
StringRef DebugName, bool inFixedBuffer) {
1968+
StringRef DebugName, bool inFixedBuffer, bool indirectForDebugInfo) {
19691969
auto name = linkInfo.getName();
19701970
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
19711971
if (existingValue) {
@@ -1997,7 +1997,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(
19971997
if (IGM.DebugInfo && !DbgTy.isNull() && linkInfo.isForDefinition())
19981998
IGM.DebugInfo->emitGlobalVariableDeclaration(
19991999
var, DebugName.empty() ? name : DebugName, name, DbgTy,
2000-
var->hasInternalLinkage(), inFixedBuffer, DebugLoc);
2000+
var->hasInternalLinkage(), indirectForDebugInfo, DebugLoc);
20012001

20022002
return var;
20032003
}
@@ -2134,6 +2134,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21342134
Size fixedSize;
21352135
Alignment fixedAlignment;
21362136
bool inFixedBuffer = false;
2137+
bool indirectForDebugInfo = false;
21372138

21382139
if (var->isInitializedObject()) {
21392140
assert(ti.isFixedSize(expansion));
@@ -2167,6 +2168,28 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21672168
storageType = getFixedBufferTy();
21682169
fixedSize = Size(DataLayout.getTypeAllocSize(storageType));
21692170
fixedAlignment = Alignment(DataLayout.getABITypeAlignment(storageType));
2171+
2172+
// DebugInfo is not resilient for now, so disable resilience to figure out
2173+
// if lldb needs to dereference the global variable or not.
2174+
//
2175+
// FIXME: Once lldb can make use of remote mirrors to calculate layouts
2176+
// at runtime, this should be removed.
2177+
{
2178+
CompletelyFragileScope Scope(*this);
2179+
2180+
SILType loweredTy = var->getLoweredType();
2181+
auto &nonResilientTI = cast<FixedTypeInfo>(getTypeInfo(loweredTy));
2182+
auto packing = nonResilientTI.getFixedPacking(*this);
2183+
switch (packing) {
2184+
case FixedPacking::OffsetZero:
2185+
break;
2186+
case FixedPacking::Allocate:
2187+
indirectForDebugInfo = true;
2188+
break;
2189+
default:
2190+
llvm_unreachable("Bad packing");
2191+
}
2192+
}
21702193
}
21712194

21722195
// Check whether we've created the global variable already.
@@ -2208,7 +2231,8 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
22082231
auto DbgTy = DebugTypeInfo::getGlobal(var, storageTypeWithContainer,
22092232
fixedSize, fixedAlignment);
22102233
gvar = createVariable(*this, link, storageTypeWithContainer,
2211-
fixedAlignment, DbgTy, loc, name, inFixedBuffer);
2234+
fixedAlignment, DbgTy, loc, name, inFixedBuffer,
2235+
indirectForDebugInfo);
22122236
}
22132237
/// Add a zero initializer.
22142238
if (forDefinition)

lib/IRGen/GenDecl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ namespace irgen {
5050
createVariable(IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *objectType,
5151
Alignment alignment, DebugTypeInfo DebugType = DebugTypeInfo(),
5252
Optional<SILLocation> DebugLoc = None,
53-
StringRef DebugName = StringRef(), bool heapAllocated = false);
53+
StringRef DebugName = StringRef(), bool heapAllocated = false,
54+
bool indirectForDebugInfo = false);
5455

5556
void disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariable *var);
5657
}

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
19891989

19901990
void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
19911991
llvm::GlobalVariable *Var, StringRef Name, StringRef LinkageName,
1992-
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool InFixedBuffer,
1992+
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool Indirect,
19931993
Optional<SILLocation> Loc) {
19941994
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
19951995
return;
@@ -2009,11 +2009,7 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
20092009
llvm::DIExpression *Expr = nullptr;
20102010
if (!Var)
20112011
Expr = DBuilder.createConstantValueExpression(0);
2012-
else if (InFixedBuffer)
2013-
// FIXME: This is *not* generally correct, but LLDB at the moment cannot
2014-
// poke to runtime to figure out whether a resilient value has inline
2015-
// storage, so this is assuming that it doesn't to get the majority of
2016-
// resilient Foundation types.
2012+
else if (Indirect)
20172013
Expr =
20182014
DBuilder.createExpression(ArrayRef<uint64_t>(llvm::dwarf::DW_OP_deref));
20192015
auto *GV = DBuilder.createGlobalVariableExpression(
@@ -2147,10 +2143,10 @@ void IRGenDebugInfo::emitDbgIntrinsic(IRBuilder &Builder, llvm::Value *Storage,
21472143

21482144
void IRGenDebugInfo::emitGlobalVariableDeclaration(
21492145
llvm::GlobalVariable *Storage, StringRef Name, StringRef LinkageName,
2150-
DebugTypeInfo DebugType, bool IsLocalToUnit, bool InFixedBuffer,
2146+
DebugTypeInfo DebugType, bool IsLocalToUnit, bool Indirect,
21512147
Optional<SILLocation> Loc) {
21522148
static_cast<IRGenDebugInfoImpl *>(this)->emitGlobalVariableDeclaration(
2153-
Storage, Name, LinkageName, DebugType, IsLocalToUnit, InFixedBuffer, Loc);
2149+
Storage, Name, LinkageName, DebugType, IsLocalToUnit, Indirect, Loc);
21542150
}
21552151

21562152
void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
// RUN: %empty-directory(%t)
3+
//
4+
// Compile the external swift module.
5+
// RUN: %target-swift-frontend -g -emit-module -enable-resilience \
6+
// RUN: -emit-module-path=%t/resilient_struct.swiftmodule \
7+
// RUN: -module-name=resilient_struct %S/../Inputs/resilient_struct.swift
8+
//
9+
// RUN: %target-swift-frontend -g -I %t -emit-ir -enable-resilience %s -o - \
10+
// RUN: | %FileCheck %s
11+
import resilient_struct
12+
13+
// Fits in buffer
14+
let small = Size(w: 1, h: 2)
15+
16+
// Needs out-of-line allocation
17+
let large = Rectangle(p: Point(x: 1, y: 2), s: Size(w: 3, h: 4), color: 5)
18+
19+
// CHECK: @"$S17global_resilience5small16resilient_struct4SizeVvp" =
20+
// CHECK-SAME: !dbg ![[SMALL:[0-9]+]]
21+
// CHECK: @"$S17global_resilience5large16resilient_struct9RectangleVvp" =
22+
// CHECK-SAME: !dbg ![[LARGE:[0-9]+]]
23+
24+
// CHECK: ![[SMALL]] = !DIGlobalVariableExpression(
25+
// CHECK-SAME: expr: !DIExpression())
26+
// CHECK: ![[LARGE]] = !DIGlobalVariableExpression(
27+
// CHECK-SAME: expr: !DIExpression(DW_OP_deref))

test/DebugInfo/resilience.swift renamed to test/DebugInfo/struct_resilience.swift

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,13 @@
1313
// RUN: -enable-resilience-bypass | %FileCheck %s --check-prefix=CHECK-LLDB
1414
import resilient_struct
1515

16-
let fixed = Point(x: 1, y: 2)
17-
let non_fixed = Size(w: 1, h: 2)
18-
let int = ResilientInt(i: 1)
19-
// CHECK: @"$S10resilience5fixed16resilient_struct5PointVvp" =
20-
// CHECK-SAME: !dbg ![[FIXED:[0-9]+]]
21-
// CHECK: @"$S10resilience9non_fixed16resilient_struct4SizeVvp" =
22-
// CHECK-SAME: !dbg ![[NON_FIXED:[0-9]+]]
23-
// CHECK: @"$S10resilience3int16resilient_struct12ResilientIntVvp" =
24-
// CHECK-SAME: !dbg ![[INT:[0-9]+]]
25-
// CHECK-LABEL: define{{.*}}main
26-
27-
// CHECK-LABEL: define{{.*}} swiftcc void @"$S10resilience9takesSizeyy16resilient_struct0C0VF"(%swift.opaque* noalias nocapture)
28-
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$S10resilience9takesSizeyy16resilient_struct0C0VF"(%T16resilient_struct4SizeV* noalias nocapture dereferenceable({{8|16}}))
16+
// CHECK-LABEL: define{{.*}} swiftcc void @"$S17struct_resilience9takesSizeyy010resilient_A00D0VF"(%swift.opaque* noalias nocapture)
17+
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$S17struct_resilience9takesSizeyy010resilient_A00D0VF"(%T16resilient_struct4SizeV* noalias nocapture dereferenceable({{8|16}}))
2918
public func takesSize(_ s: Size) {}
3019

3120

32-
// CHECK-LABEL: define{{.*}} swiftcc void @"$S10resilience1fyyF"()
33-
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$S10resilience1fyyF"()
21+
// CHECK-LABEL: define{{.*}} swiftcc void @"$S17struct_resilience1fyyF"()
22+
// CHECK-LLDB-LABEL: define{{.*}} swiftcc void @"$S17struct_resilience1fyyF"()
3423
func f() {
3524
let s1 = Size(w: 1, h: 2)
3625
takesSize(s1)
@@ -48,19 +37,12 @@ func f() {
4837
}
4938
f()
5039

51-
// Note that these DW_OP_deref are not necessarily correct, but it's the best
52-
// approxmiation we have until LLDB can query the runtime for whether a relient
53-
// type's storage is inline or not.
54-
// CHECK: ![[FIXED]] = !DIGlobalVariableExpression(
55-
// CHECK-SAME: expr: !DIExpression())
56-
// CHECK: ![[NON_FIXED]] = !DIGlobalVariableExpression(
57-
// CHECK-SAME: expr: !DIExpression(DW_OP_deref))
5840
// CHECK: ![[TY:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Size",
59-
// CHECK: ![[INT]] = !DIGlobalVariableExpression(
60-
// CHECK-SAME: expr: !DIExpression(DW_OP_deref))
41+
// CHECK: ![[TY:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Size",
6142

6243
// CHECK: ![[V1]] = !DILocalVariable(name: "s1", {{.*}}type: ![[TY]])
6344

45+
// CHECK-LLDB: ![[TY:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Size",
6446
// CHECK-LLDB: ![[TY:[0-9]+]] = !DICompositeType(tag: DW_TAG_structure_type, name: "Size",
6547
// CHECK-LLDB: ![[V1]] = !DILocalVariable(name: "s1", {{.*}}type: ![[TY]])
6648

0 commit comments

Comments
 (0)