Skip to content

Commit 13203bf

Browse files
committed
Fix a crash when building the swift stdlib caused by the debug info
for a alloc_stack, debug_value, and debug_value_addr disagreeing on the type of the same variable. For -Onone, this commit is NFC. A testcase for generic specialization will follow as soon as SIL debug info serialization efforts are complete. <rdar://problem/24785336>
1 parent 95fe01d commit 13203bf

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,29 @@ DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, llvm::Type *StorageTy,
102102
}
103103

104104
DebugTypeInfo::DebugTypeInfo(ValueDecl *Decl, swift::Type Ty,
105-
const TypeInfo &Info)
105+
const TypeInfo &Info, bool Unwrap)
106106
: DeclOrContext(Decl) {
107107
// Prefer the original, potentially sugared version of the type if
108108
// the type hasn't been mucked with by an optimization pass.
109-
if (Decl->getType().getCanonicalTypeOrNull() == Ty.getCanonicalTypeOrNull())
110-
Type = Decl->getType().getPointer();
109+
CanType DeclType = Decl->getType()->getCanonicalType();
110+
CanType RealType = Ty.getCanonicalTypeOrNull();
111+
if (Unwrap) {
112+
DeclType = DeclType.getLValueOrInOutObjectType();
113+
RealType = RealType.getLValueOrInOutObjectType();
114+
}
115+
116+
// Desugar for comparison.
117+
DeclType = DeclType->getDesugaredType()->getCanonicalType();
118+
// DynamicSelfType is also sugar as far as debug info is concerned.
119+
if (auto DynSelfTy = dyn_cast<DynamicSelfType>(DeclType))
120+
DeclType = DynSelfTy->getSelfType()->getCanonicalType();
121+
RealType = RealType->getDesugaredType()->getCanonicalType();
122+
123+
if ((DeclType == RealType) || isa<AnyFunctionType>(DeclType))
124+
Type = Unwrap ? Decl->getType()->getLValueOrInOutObjectType().getPointer()
125+
: Decl->getType().getPointer();
111126
else
112-
Type = Ty.getPointer();
127+
Type = RealType.getPointer();
113128

114129
initFromTypeInfo(size, align, StorageType, Info);
115130
}

lib/IRGen/DebugTypeInfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ class DebugTypeInfo {
6060
DebugTypeInfo(ValueDecl *Decl, const TypeInfo &Info);
6161
DebugTypeInfo(ValueDecl *Decl, llvm::Type *StorageType, Size size,
6262
Alignment align);
63-
DebugTypeInfo(ValueDecl *Decl, swift::Type Ty, const TypeInfo &Info);
63+
DebugTypeInfo(ValueDecl *Decl, swift::Type Ty, const TypeInfo &Info,
64+
bool Unwrap);
6465
DebugTypeInfo(ValueDecl *Decl, swift::Type Ty,
6566
llvm::Type *StorageType, Size size,
6667
Alignment align);
@@ -81,7 +82,8 @@ class DebugTypeInfo {
8182

8283
// Determine whether this type is an Archetype itself.
8384
bool isArchetype() const {
84-
return Type->getLValueOrInOutObjectType()->getKind() == TypeKind::Archetype;
85+
return Type->getLValueOrInOutObjectType()->getDesugaredType()->getKind() ==
86+
TypeKind::Archetype;
8587
}
8688

8789
/// LValues, inout args, and Archetypes are implicitly indirect by

lib/IRGen/IRGenSIL.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3167,17 +3167,20 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
31673167
StringRef Name = getVarName(i);
31683168
DebugTypeInfo DbgTy;
31693169
SILType SILTy = SILVal->getType();
3170-
if (VarDecl *Decl = i->getDecl())
3171-
DbgTy = DebugTypeInfo(Decl, Decl->getType(), getTypeInfo(SILTy));
3172-
else if (i->getFunction()->isBare() &&
3173-
!SILTy.getSwiftType()->hasArchetype() && !Name.empty())
3174-
// Preliminary support for .sil debug information.
3175-
DbgTy = DebugTypeInfo(SILTy.getSwiftType(), getTypeInfo(SILTy), nullptr);
3176-
else
3177-
return;
31783170
// An inout/lvalue type that is described by a debug value has been
31793171
// promoted by an optimization pass. Unwrap the type.
3180-
DbgTy.unwrapLValueOrInOutType();
3172+
bool Unwrap = true;
3173+
auto RealTy = SILVal->getType().getSwiftType();
3174+
if (VarDecl *Decl = i->getDecl()) {
3175+
DbgTy = DebugTypeInfo(Decl, RealTy, getTypeInfo(SILVal->getType()), Unwrap);
3176+
} else if (i->getFunction()->isBare() &&
3177+
!SILTy.getSwiftType()->hasArchetype() && !Name.empty()) {
3178+
// Preliminary support for .sil debug information.
3179+
DbgTy = DebugTypeInfo(RealTy, getTypeInfo(SILTy), nullptr);
3180+
if (Unwrap)
3181+
DbgTy.unwrapLValueOrInOutType();
3182+
} else
3183+
return;
31813184

31823185
// Put the value into a stack slot at -Onone.
31833186
llvm::SmallVector<llvm::Value *, 8> Copy;
@@ -3200,12 +3203,13 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
32003203

32013204
StringRef Name = getVarName(i);
32023205
auto Addr = getLoweredAddress(SILVal).getAddress();
3203-
DebugTypeInfo DbgTy(Decl, SILVal->getType().getSwiftType(),
3204-
getTypeInfo(SILVal->getType()));
3206+
auto RealType = SILVal->getType().getSwiftType();
32053207
// Unwrap implicitly indirect types and types that are passed by
32063208
// reference only at the SIL level and below.
3207-
if (DbgTy.isArchetype() || i->getVarInfo().Constant)
3208-
DbgTy.unwrapLValueOrInOutType();
3209+
bool Unwrap =
3210+
i->getVarInfo().Constant ||
3211+
RealType->getLValueOrInOutObjectType()->getKind() == TypeKind::Archetype;
3212+
DebugTypeInfo DbgTy(Decl, RealType, getTypeInfo(SILVal->getType()), Unwrap);
32093213
// Put the value's address into a stack slot at -Onone and emit a debug
32103214
// intrinsic.
32113215
unsigned ArgNo = i->getVarInfo().ArgNo;
@@ -3468,11 +3472,12 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
34683472
Pattern->getKind() != PatternKind::OptionalSome)
34693473
return;
34703474

3471-
auto DbgTy = DebugTypeInfo(Decl, type);
34723475
// Discard any inout or lvalue qualifiers. Since the object itself
34733476
// is stored in the alloca, emitting it as a reference type would
34743477
// be wrong.
3475-
DbgTy.unwrapLValueOrInOutType();
3478+
bool Unwrap = true;
3479+
auto RealType = i->getType().getSwiftType().getLValueOrInOutObjectType();
3480+
auto DbgTy = DebugTypeInfo(Decl, RealType, type, Unwrap);
34763481
StringRef Name = getVarName(i);
34773482
if (auto DS = i->getDebugScope())
34783483
emitDebugVariableDeclaration(addr, DbgTy, DS, Name,
@@ -3628,7 +3633,7 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
36283633
if (Name == IGM.Context.Id_self.str())
36293634
return;
36303635

3631-
DebugTypeInfo DbgTy(Decl, i->getElementType().getSwiftType(), type);
3636+
DebugTypeInfo DbgTy(Decl, i->getElementType().getSwiftType(), type, false);
36323637
IGM.DebugInfo->emitVariableDeclaration(
36333638
Builder,
36343639
emitShadowCopy(boxWithAddr.getAddress(), i->getDebugScope(), Name, 0),

0 commit comments

Comments
 (0)