Skip to content

Commit 1e07724

Browse files
authored
Merge pull request #17437 from slavapestov/more-annoying-lldb-resilience-hacks-4.2
DebugInfo: Bypass resilience to calculate if a global is indirect or not [4.2]
2 parents 1832a97 + 8f6bf36 commit 1e07724

File tree

6 files changed

+71
-38
lines changed

6 files changed

+71
-38
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ bool LinkInfo::isUsed(llvm::GlobalValue::LinkageTypes Linkage,
19521952
llvm::GlobalVariable *swift::irgen::createVariable(
19531953
IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *storageType,
19541954
Alignment alignment, DebugTypeInfo DbgTy, Optional<SILLocation> DebugLoc,
1955-
StringRef DebugName, bool inFixedBuffer) {
1955+
StringRef DebugName, bool inFixedBuffer, bool indirectForDebugInfo) {
19561956
auto name = linkInfo.getName();
19571957
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
19581958
if (existingValue) {
@@ -1984,7 +1984,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(
19841984
if (IGM.DebugInfo && !DbgTy.isNull() && linkInfo.isForDefinition())
19851985
IGM.DebugInfo->emitGlobalVariableDeclaration(
19861986
var, DebugName.empty() ? name : DebugName, name, DbgTy,
1987-
var->hasInternalLinkage(), inFixedBuffer, DebugLoc);
1987+
var->hasInternalLinkage(), indirectForDebugInfo, DebugLoc);
19881988

19891989
return var;
19901990
}
@@ -2121,6 +2121,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21212121
Size fixedSize;
21222122
Alignment fixedAlignment;
21232123
bool inFixedBuffer = false;
2124+
bool indirectForDebugInfo = false;
21242125

21252126
if (var->isInitializedObject()) {
21262127
assert(ti.isFixedSize(expansion));
@@ -2154,6 +2155,28 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21542155
storageType = getFixedBufferTy();
21552156
fixedSize = Size(DataLayout.getTypeAllocSize(storageType));
21562157
fixedAlignment = Alignment(DataLayout.getABITypeAlignment(storageType));
2158+
2159+
// DebugInfo is not resilient for now, so disable resilience to figure out
2160+
// if lldb needs to dereference the global variable or not.
2161+
//
2162+
// FIXME: Once lldb can make use of remote mirrors to calculate layouts
2163+
// at runtime, this should be removed.
2164+
{
2165+
CompletelyFragileScope Scope(*this);
2166+
2167+
SILType loweredTy = var->getLoweredType();
2168+
auto &nonResilientTI = cast<FixedTypeInfo>(getTypeInfo(loweredTy));
2169+
auto packing = nonResilientTI.getFixedPacking(*this);
2170+
switch (packing) {
2171+
case FixedPacking::OffsetZero:
2172+
break;
2173+
case FixedPacking::Allocate:
2174+
indirectForDebugInfo = true;
2175+
break;
2176+
default:
2177+
llvm_unreachable("Bad packing");
2178+
}
2179+
}
21572180
}
21582181

21592182
// Check whether we've created the global variable already.
@@ -2195,7 +2218,8 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
21952218
auto DbgTy = DebugTypeInfo::getGlobal(var, storageTypeWithContainer,
21962219
fixedSize, fixedAlignment);
21972220
gvar = createVariable(*this, link, storageTypeWithContainer,
2198-
fixedAlignment, DbgTy, loc, name, inFixedBuffer);
2221+
fixedAlignment, DbgTy, loc, name, inFixedBuffer,
2222+
indirectForDebugInfo);
21992223
}
22002224
/// Add a zero initializer.
22012225
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
@@ -1994,7 +1994,7 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
19941994

19951995
void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
19961996
llvm::GlobalVariable *Var, StringRef Name, StringRef LinkageName,
1997-
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool InFixedBuffer,
1997+
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool Indirect,
19981998
Optional<SILLocation> Loc) {
19991999
if (Opts.DebugInfoKind <= IRGenDebugInfoKind::LineTables)
20002000
return;
@@ -2014,11 +2014,7 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
20142014
llvm::DIExpression *Expr = nullptr;
20152015
if (!Var)
20162016
Expr = DBuilder.createConstantValueExpression(0);
2017-
else if (InFixedBuffer)
2018-
// FIXME: This is *not* generally correct, but LLDB at the moment cannot
2019-
// poke to runtime to figure out whether a resilient value has inline
2020-
// storage, so this is assuming that it doesn't to get the majority of
2021-
// resilient Foundation types.
2017+
else if (Indirect)
20222018
Expr =
20232019
DBuilder.createExpression(ArrayRef<uint64_t>(llvm::dwarf::DW_OP_deref));
20242020
auto *GV = DBuilder.createGlobalVariableExpression(
@@ -2148,10 +2144,10 @@ void IRGenDebugInfo::emitDbgIntrinsic(IRBuilder &Builder, llvm::Value *Storage,
21482144

21492145
void IRGenDebugInfo::emitGlobalVariableDeclaration(
21502146
llvm::GlobalVariable *Storage, StringRef Name, StringRef LinkageName,
2151-
DebugTypeInfo DebugType, bool IsLocalToUnit, bool InFixedBuffer,
2147+
DebugTypeInfo DebugType, bool IsLocalToUnit, bool Indirect,
21522148
Optional<SILLocation> Loc) {
21532149
static_cast<IRGenDebugInfoImpl *>(this)->emitGlobalVariableDeclaration(
2154-
Storage, Name, LinkageName, DebugType, IsLocalToUnit, InFixedBuffer, Loc);
2150+
Storage, Name, LinkageName, DebugType, IsLocalToUnit, Indirect, Loc);
21552151
}
21562152

21572153
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

test/IRGen/global_resilience.sil

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -I %S/../Inputs -enable-source-import -emit-ir -enable-resilience %s | %FileCheck %s
2-
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -I %S/../Inputs -enable-source-import -emit-ir -enable-resilience -O %s
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %S/../Inputs/resilient_struct.swift -enable-resilience -emit-module -emit-module-path %t/resilient_struct.swiftmodule
4+
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -enable-resilience -I %t -emit-ir %s | %FileCheck %s
5+
// RUN: %target-swift-frontend -assume-parsing-unqualified-ownership-sil -enable-resilience -I %t -emit-ir -O %s
36

47
// CHECK: %swift.type = type { [[INT:i32|i64]] }
58

0 commit comments

Comments
 (0)