Skip to content

Commit d4149db

Browse files
Merge pull request swiftlang#23425 from adrian-prantl/48409386
Remove Resilience workarounds and describe fixed-size global reslient
2 parents 7c7f60a + 0b8fc3d commit d4149db

File tree

7 files changed

+50
-60
lines changed

7 files changed

+50
-60
lines changed

include/swift/Remote/FailureKinds.def

Lines changed: 3 additions & 0 deletions
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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,7 +1640,7 @@ bool LinkInfo::isUsed(IRLinkage IRL) {
16401640
llvm::GlobalVariable *swift::irgen::createVariable(
16411641
IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *storageType,
16421642
Alignment alignment, DebugTypeInfo DbgTy, Optional<SILLocation> DebugLoc,
1643-
StringRef DebugName, bool inFixedBuffer, bool indirectForDebugInfo) {
1643+
StringRef DebugName, bool inFixedBuffer) {
16441644
auto name = linkInfo.getName();
16451645
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
16461646
if (existingValue) {
@@ -1674,7 +1674,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(
16741674
if (IGM.DebugInfo && !DbgTy.isNull() && linkInfo.isForDefinition())
16751675
IGM.DebugInfo->emitGlobalVariableDeclaration(
16761676
var, DebugName.empty() ? name : DebugName, name, DbgTy,
1677-
var->hasInternalLinkage(), indirectForDebugInfo, DebugLoc);
1677+
var->hasInternalLinkage(), inFixedBuffer, DebugLoc);
16781678

16791679
return var;
16801680
}
@@ -1812,13 +1812,6 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
18121812
Size fixedSize;
18131813
Alignment fixedAlignment;
18141814
bool inFixedBuffer = false;
1815-
bool indirectForDebugInfo = false;
1816-
1817-
// FIXME: Remove this once LLDB has proper support for resilience.
1818-
bool isREPLVar = false;
1819-
if (auto *decl = var->getDecl())
1820-
if (decl->isREPLVar())
1821-
isREPLVar = true;
18221815

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

18801851
// Check whether we've created the global variable already.
@@ -1916,8 +1887,7 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
19161887
auto DbgTy = DebugTypeInfo::getGlobal(var, storageTypeWithContainer,
19171888
fixedSize, fixedAlignment);
19181889
gvar = createVariable(*this, link, storageTypeWithContainer,
1919-
fixedAlignment, DbgTy, loc, name, inFixedBuffer,
1920-
indirectForDebugInfo);
1890+
fixedAlignment, DbgTy, loc, name, inFixedBuffer);
19211891
}
19221892
/// Add a zero initializer.
19231893
if (forDefinition)

lib/IRGen/GenDecl.h

Lines changed: 1 addition & 2 deletions
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

Lines changed: 20 additions & 6 deletions
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

Lines changed: 2 additions & 14 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 11 additions & 1 deletion
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)