Skip to content

Commit 0b8fc3d

Browse files
committed
Remove Resilience workarounds and describe fixed-size global resilient
variables in DWARF. rdar://problem/48409386
1 parent c7a6f8a commit 0b8fc3d

File tree

7 files changed

+50
-60
lines changed

7 files changed

+50
-60
lines changed

include/swift/Remote/FailureKinds.def

+3
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@ FAILURE(TypeHasNoSuchMember,
2727
FAILURE(CouldNotResolveTypeDecl,
2828
"could not resolve a type with mangled name '%0'", (String))
2929

30+
FAILURE(NotFixedLayout,
31+
"query cannot be determined for a type with no fixed layout", ())
32+
3033
#undef FAILURE

lib/IRGen/GenDecl.cpp

+4-34
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ bool LinkInfo::isUsed(IRLinkage IRL) {
16371637
llvm::GlobalVariable *swift::irgen::createVariable(
16381638
IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *storageType,
16391639
Alignment alignment, DebugTypeInfo DbgTy, Optional<SILLocation> DebugLoc,
1640-
StringRef DebugName, bool inFixedBuffer, bool indirectForDebugInfo) {
1640+
StringRef DebugName, bool inFixedBuffer) {
16411641
auto name = linkInfo.getName();
16421642
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
16431643
if (existingValue) {
@@ -1671,7 +1671,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(
16711671
if (IGM.DebugInfo && !DbgTy.isNull() && linkInfo.isForDefinition())
16721672
IGM.DebugInfo->emitGlobalVariableDeclaration(
16731673
var, DebugName.empty() ? name : DebugName, name, DbgTy,
1674-
var->hasInternalLinkage(), indirectForDebugInfo, DebugLoc);
1674+
var->hasInternalLinkage(), inFixedBuffer, DebugLoc);
16751675

16761676
return var;
16771677
}
@@ -1809,13 +1809,6 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
18091809
Size fixedSize;
18101810
Alignment fixedAlignment;
18111811
bool inFixedBuffer = false;
1812-
bool indirectForDebugInfo = false;
1813-
1814-
// FIXME: Remove this once LLDB has proper support for resilience.
1815-
bool isREPLVar = false;
1816-
if (auto *decl = var->getDecl())
1817-
if (decl->isREPLVar())
1818-
isREPLVar = true;
18191812

18201813
if (var->isInitializedObject()) {
18211814
assert(ti.isFixedSize(expansion));
@@ -1837,7 +1830,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
18371830
fixedAlignment = Layout->getAlignment();
18381831
castStorageToType = cast<FixedTypeInfo>(ti).getStorageType();
18391832
assert(fixedAlignment >= TargetInfo.HeapObjectAlignment);
1840-
} else if (isREPLVar || ti.isFixedSize(expansion)) {
1833+
} else if (ti.isFixedSize(expansion)) {
18411834
// Allocate static storage.
18421835
auto &fixedTI = cast<FixedTypeInfo>(ti);
18431836
storageType = fixedTI.getStorageType();
@@ -1850,28 +1843,6 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
18501843
storageType = getFixedBufferTy();
18511844
fixedSize = Size(DataLayout.getTypeAllocSize(storageType));
18521845
fixedAlignment = Alignment(DataLayout.getABITypeAlignment(storageType));
1853-
1854-
// DebugInfo is not resilient for now, so disable resilience to figure out
1855-
// if lldb needs to dereference the global variable or not.
1856-
//
1857-
// FIXME: Once lldb can make use of remote mirrors to calculate layouts
1858-
// at runtime, this should be removed.
1859-
{
1860-
LoweringModeScope Scope(*this, TypeConverter::Mode::CompletelyFragile);
1861-
1862-
SILType loweredTy = var->getLoweredType();
1863-
auto &nonResilientTI = cast<FixedTypeInfo>(getTypeInfo(loweredTy));
1864-
auto packing = nonResilientTI.getFixedPacking(*this);
1865-
switch (packing) {
1866-
case FixedPacking::OffsetZero:
1867-
break;
1868-
case FixedPacking::Allocate:
1869-
indirectForDebugInfo = true;
1870-
break;
1871-
default:
1872-
llvm_unreachable("Bad packing");
1873-
}
1874-
}
18751846
}
18761847

18771848
// Check whether we've created the global variable already.
@@ -1913,8 +1884,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
19131884
auto DbgTy = DebugTypeInfo::getGlobal(var, storageTypeWithContainer,
19141885
fixedSize, fixedAlignment);
19151886
gvar = createVariable(*this, link, storageTypeWithContainer,
1916-
fixedAlignment, DbgTy, loc, name, inFixedBuffer,
1917-
indirectForDebugInfo);
1887+
fixedAlignment, DbgTy, loc, name, inFixedBuffer);
19181888
}
19191889
/// Add a zero initializer.
19201890
if (forDefinition)

lib/IRGen/GenDecl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ 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,
54-
bool indirectForDebugInfo = false);
53+
StringRef DebugName = StringRef(), bool heapAllocated = false);
5554

5655
void disableAddressSanitizer(IRGenModule &IGM, llvm::GlobalVariable *var);
5756
}

lib/IRGen/IRGenDebugInfo.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,20 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
10661066
llvm::dwarf::DW_LANG_Swift, nullptr, MangledName);
10671067
}
10681068

1069+
llvm::DIType *createFixedValueBufferStruct(llvm::DIType *PointeeTy) {
1070+
unsigned Line = 0;
1071+
unsigned PtrSize = CI.getTargetInfo().getPointerWidth(0);
1072+
llvm::DINode::DIFlags Flags = llvm::DINode::FlagArtificial;
1073+
llvm::DIFile *File = MainFile;
1074+
llvm::DIScope *Scope = TheCU;
1075+
llvm::Metadata *Elements[] = {DBuilder.createMemberType(
1076+
Scope, "contents", File, 0, PtrSize, 0, 0, Flags, PointeeTy)};
1077+
return DBuilder.createStructType(
1078+
Scope, "$swift.fixedbuffer", File, Line, 3 * PtrSize, 0, Flags,
1079+
/* DerivedFrom */ nullptr, DBuilder.getOrCreateArray(Elements),
1080+
llvm::dwarf::DW_LANG_Swift, nullptr);
1081+
}
1082+
10691083
llvm::DIType *createFunctionPointer(DebugTypeInfo DbgTy, llvm::DIScope *Scope,
10701084
unsigned SizeInBits, unsigned AlignInBits,
10711085
llvm::DINode::DIFlags Flags,
@@ -2227,7 +2241,7 @@ void IRGenDebugInfoImpl::emitDbgIntrinsic(
22272241

22282242
void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
22292243
llvm::GlobalVariable *Var, StringRef Name, StringRef LinkageName,
2230-
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool Indirect,
2244+
DebugTypeInfo DbgTy, bool IsLocalToUnit, bool InFixedBuffer,
22312245
Optional<SILLocation> Loc) {
22322246
if (Opts.DebugInfoLevel <= IRGenDebugInfoLevel::LineTables)
22332247
return;
@@ -2240,16 +2254,16 @@ void IRGenDebugInfoImpl::emitGlobalVariableDeclaration(
22402254
// would confuse both the user and LLDB.
22412255
return;
22422256

2257+
if (InFixedBuffer)
2258+
Ty = createFixedValueBufferStruct(Ty);
2259+
22432260
auto L = getStartLocation(Loc);
22442261
auto File = getOrCreateFile(L.Filename);
22452262

22462263
// Emit it as global variable of the current module.
22472264
llvm::DIExpression *Expr = nullptr;
22482265
if (!Var)
22492266
Expr = DBuilder.createConstantValueExpression(0);
2250-
else if (Indirect)
2251-
Expr =
2252-
DBuilder.createExpression(ArrayRef<uint64_t>(llvm::dwarf::DW_OP_deref));
22532267
auto *GV = DBuilder.createGlobalVariableExpression(
22542268
MainModule, Name, LinkageName, File, L.Line, Ty, IsLocalToUnit, Expr);
22552269
if (Var)
@@ -2388,10 +2402,10 @@ void IRGenDebugInfo::emitDbgIntrinsic(IRBuilder &Builder, llvm::Value *Storage,
23882402

23892403
void IRGenDebugInfo::emitGlobalVariableDeclaration(
23902404
llvm::GlobalVariable *Storage, StringRef Name, StringRef LinkageName,
2391-
DebugTypeInfo DebugType, bool IsLocalToUnit, bool Indirect,
2405+
DebugTypeInfo DebugType, bool IsLocalToUnit, bool InFixedBuffer,
23922406
Optional<SILLocation> Loc) {
23932407
static_cast<IRGenDebugInfoImpl *>(this)->emitGlobalVariableDeclaration(
2394-
Storage, Name, LinkageName, DebugType, IsLocalToUnit, Indirect, Loc);
2408+
Storage, Name, LinkageName, DebugType, IsLocalToUnit, InFixedBuffer, Loc);
23952409
}
23962410

23972411
void IRGenDebugInfo::emitTypeMetadata(IRGenFunction &IGF, llvm::Value *Metadata,

lib/IRGen/IRGenSIL.cpp

+2-14
Original file line numberDiff line numberDiff line change
@@ -1884,15 +1884,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {
18841884

18851885
auto expansion = IGM.getResilienceExpansionForLayout(var);
18861886

1887-
// FIXME: Remove this once LLDB has proper support for resilience.
1888-
bool isREPLVar = false;
1889-
if (auto *decl = var->getDecl())
1890-
if (decl->isREPLVar())
1891-
isREPLVar = true;
1892-
18931887
// If the global is fixed-size in all resilience domains that can see it,
18941888
// we allocated storage for it statically, and there's nothing to do.
1895-
if (isREPLVar || ti.isFixedSize(expansion))
1889+
if (ti.isFixedSize(expansion))
18961890
return;
18971891

18981892
// Otherwise, the static storage for the global consists of a fixed-size
@@ -1920,15 +1914,9 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
19201914
Address addr = IGM.getAddrOfSILGlobalVariable(var, ti,
19211915
NotForDefinition);
19221916

1923-
// FIXME: Remove this once LLDB has proper support for resilience.
1924-
bool isREPLVar = false;
1925-
if (auto *decl = var->getDecl())
1926-
if (decl->isREPLVar())
1927-
isREPLVar = true;
1928-
19291917
// If the global is fixed-size in all resilience domains that can see it,
19301918
// we allocated storage for it statically, and there's nothing to do.
1931-
if (isREPLVar || ti.isFixedSize(expansion)) {
1919+
if (ti.isFixedSize(expansion)) {
19321920
setLoweredAddress(i, addr);
19331921
return;
19341922
}

lib/RemoteAST/RemoteAST.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,21 @@ class RemoteASTContextImpl {
327327
return fail<uint64_t>(Failure::TypeHasNoSuchMember, memberName);
328328

329329
// Fast path: element 0 is always at offset 0.
330-
if (targetIndex == 0) return uint64_t(0);
330+
if (targetIndex == 0)
331+
return uint64_t(0);
331332

332333
// Create an IRGen instance.
333334
auto irgen = getIRGen();
334-
if (!irgen) return Result<uint64_t>::emplaceFailure(Failure::Unknown);
335+
if (!irgen)
336+
return Result<uint64_t>::emplaceFailure(Failure::Unknown);
335337
auto &IGM = irgen->IGM;
336-
337338
SILType loweredTy = IGM.getLoweredType(type);
338339

340+
// Only the runtime metadata knows the offsets of resilient members.
341+
auto &typeInfo = IGM.getTypeInfo(loweredTy);
342+
if (!isa<irgen::FixedTypeInfo>(&typeInfo))
343+
return Result<uint64_t>::emplaceFailure(Failure::NotFixedLayout);
344+
339345
// If the type has a statically fixed offset, return that.
340346
if (auto offset =
341347
irgen::getFixedTupleElementOffset(IGM, loweredTy, targetIndex))

test/DebugInfo/global_resilience.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ let large = Rectangle(p: Point(x: 1, y: 2), s: Size(w: 3, h: 4), color: 5)
2222
// CHECK-SAME: !dbg ![[LARGE:[0-9]+]]
2323

2424
// CHECK: ![[SMALL]] = !DIGlobalVariableExpression(
25+
// CHECK-SAME: var: ![[VAR_SMALL:[0-9]+]]
2526
// CHECK-SAME: expr: !DIExpression())
27+
// CHECK: ![[VAR_SMALL]] = distinct !DIGlobalVariable(
28+
// CHECK-SAME: type: ![[SMALL_TY:[0-9]+]]
29+
// CHECK: ![[SMALL_TY]] = !DICompositeType(tag: DW_TAG_structure_type,
30+
// CHECK-SAME: name: "$swift.fixedbuffer",
2631
// CHECK: ![[LARGE]] = !DIGlobalVariableExpression(
27-
// CHECK-SAME: expr: !DIExpression(DW_OP_deref))
32+
// CHECK-SAME: var: ![[VAR_LARGE:[0-9]+]]
33+
// CHECK-SAME: expr: !DIExpression())
34+
// CHECK: ![[VAR_LARGE]] = distinct !DIGlobalVariable(
35+
// CHECK-SAME: type: ![[LARGE_TY:[0-9]+]]
36+
// CHECK: ![[LARGE_TY]] = !DICompositeType(tag: DW_TAG_structure_type,
37+
// CHECK-SAME: name: "$swift.fixedbuffer",

0 commit comments

Comments
 (0)