Skip to content

Commit 06a5443

Browse files
committed
Sink isFixedBuffer into DebugTypeInfo and ensure the enclosed types aren't
emitted with the size of the fixed buffer.
1 parent 7731ee9 commit 06a5443

File tree

9 files changed

+130
-88
lines changed

9 files changed

+130
-88
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "DebugTypeInfo.h"
1919
#include "FixedTypeInfo.h"
20+
#include "IRGenModule.h"
2021
#include "swift/SIL/SILGlobalVariable.h"
2122
#include "llvm/Support/Debug.h"
2223
#include "llvm/Support/raw_ostream.h"
@@ -27,23 +28,12 @@ using namespace irgen;
2728
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
2829
Optional<Size::int_type> SizeInBits,
2930
Alignment Align, bool HasDefaultAlignment,
30-
bool IsMetadata, bool SizeIsFragmentSize)
31+
bool IsMetadata, bool SizeIsFragmentSize,
32+
bool IsFixedBuffer)
3133
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
3234
SizeInBits(SizeInBits), Align(Align),
3335
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata),
34-
SizeIsFragmentSize(SizeIsFragmentSize) {
35-
assert(Align.getValue() != 0);
36-
}
37-
38-
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
39-
Optional<Size> SizeInBytes, Alignment Align,
40-
bool HasDefaultAlignment, bool IsMetadata,
41-
bool SizeIsFragmentSize)
42-
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
43-
Align(Align), DefaultAlignment(HasDefaultAlignment),
44-
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize) {
45-
if (SizeInBytes)
46-
SizeInBits = SizeInBytes->getValue() * 8;
36+
SizeIsFragmentSize(SizeIsFragmentSize), IsFixedBuffer(IsFixedBuffer) {
4737
assert(Align.getValue() != 0);
4838
}
4939

@@ -56,17 +46,20 @@ static bool hasDefaultAlignment(swift::Type Ty) {
5646
return true;
5747
}
5848

59-
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty,
60-
const TypeInfo &Info,
49+
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
6150
bool IsFragmentTypeInfo) {
62-
Optional<Size> size;
63-
if (Info.isFixedSize()) {
64-
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
65-
size = FixTy.getFixedSize();
51+
Optional<Size::int_type> SizeInBits;
52+
llvm::Type *StorageType = TI.getStorageType();
53+
if (TI.isFixedSize()) {
54+
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&TI);
55+
Size::int_type Size = FixTy.getFixedSize().getValue() * 8;
56+
//if (!StorageType->isPointerTy())
57+
// Size -= FixTy.getSpareBits().asAPInt().countTrailingOnes();
58+
SizeInBits = Size;
6659
}
67-
assert(Info.getStorageType() && "StorageType is a nullptr");
68-
return DebugTypeInfo(Ty.getPointer(), Info.getStorageType(), size,
69-
Info.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
60+
assert(TI.getStorageType() && "StorageType is a nullptr");
61+
return DebugTypeInfo(Ty.getPointer(), StorageType, SizeInBits,
62+
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
7063
false, IsFragmentTypeInfo);
7164
}
7265

@@ -92,8 +85,8 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
9285
DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
9386
llvm::Type *StorageTy, Size size,
9487
Alignment align) {
95-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
96-
align, true, false, false);
88+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
89+
true, false, false);
9790
assert(StorageTy && "StorageType is a nullptr");
9891
assert(!DbgTy.isContextArchetype() &&
9992
"type metadata cannot contain an archetype");
@@ -103,8 +96,8 @@ DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
10396
DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
10497
llvm::Type *StorageTy, Size size,
10598
Alignment align) {
106-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
107-
align, true, true, false);
99+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
100+
true, true, false);
108101
assert(StorageTy && "StorageType is a nullptr");
109102
assert(!DbgTy.isContextArchetype() &&
110103
"type metadata cannot contain an archetype");
@@ -117,8 +110,29 @@ DebugTypeInfo DebugTypeInfo::getForwardDecl(swift::Type Ty) {
117110
}
118111

119112
DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
120-
llvm::Type *StorageTy, Size size,
121-
Alignment align) {
113+
llvm::Type *FragmentStorageType,
114+
IRGenModule &IGM) {
115+
// Prefer the original, potentially sugared version of the type if
116+
// the type hasn't been mucked with by an optimization pass.
117+
auto LowTy = GV->getLoweredType().getASTType();
118+
auto *Type = LowTy.getPointer();
119+
if (auto *Decl = GV->getDecl()) {
120+
auto DeclType = Decl->getType();
121+
if (DeclType->isEqual(LowTy))
122+
Type = DeclType.getPointer();
123+
}
124+
auto &TI = IGM.getTypeInfoForUnlowered(Type);
125+
DebugTypeInfo DbgTy = getFromTypeInfo(Type, TI, false);
126+
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
127+
assert(!DbgTy.isContextArchetype() &&
128+
"type of global variable cannot be an archetype");
129+
return DbgTy;
130+
}
131+
132+
DebugTypeInfo
133+
DebugTypeInfo::getGlobalFixedBuffer(SILGlobalVariable *GV,
134+
llvm::Type *FragmentStorageType,
135+
Size SizeInBytes, Alignment Align) {
122136
// Prefer the original, potentially sugared version of the type if
123137
// the type hasn't been mucked with by an optimization pass.
124138
auto LowTy = GV->getLoweredType().getASTType();
@@ -128,31 +142,34 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
128142
if (DeclType->isEqual(LowTy))
129143
Type = DeclType.getPointer();
130144
}
131-
DebugTypeInfo DbgTy(Type, StorageTy, size, align, ::hasDefaultAlignment(Type),
132-
false, false);
133-
assert(StorageTy && "FragmentStorageType is a nullptr");
145+
DebugTypeInfo DbgTy(Type, FragmentStorageType, SizeInBytes.getValue() * 8,
146+
Align, ::hasDefaultAlignment(Type), false, false, true);
147+
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
134148
assert(!DbgTy.isContextArchetype() &&
135149
"type of global variable cannot be an archetype");
136-
assert(align.getValue() != 0);
137150
return DbgTy;
138151
}
139152

140153
DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
141154
llvm::Type *FragmentStorageType,
142-
Size size, Alignment align) {
155+
Size SizeInBytes, Alignment align) {
143156
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(),
144-
FragmentStorageType, size, align, true, false, false);
157+
FragmentStorageType, SizeInBytes.getValue() * 8, align,
158+
true, false, false);
145159
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
146160
assert(!DbgTy.isContextArchetype() &&
147161
"type of objc class cannot be an archetype");
148162
return DbgTy;
149163
}
150164

151165
DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
152-
llvm::Type *StorageType, Size size,
153-
Alignment align) {
166+
llvm::Type *StorageType,
167+
IRGenModule &IGM) {
168+
auto &TI = IGM.getTypeInfoForUnlowered(Ty);
169+
assert(TI.getStorageType() == StorageType);
170+
DebugTypeInfo DbgTy = getFromTypeInfo(Ty, TI, false);
154171
assert(StorageType && "FragmentStorageType is a nullptr");
155-
return {Ty, StorageType, size, align, true, false, false};
172+
return DbgTy;
156173
}
157174

158175
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {

lib/IRGen/DebugTypeInfo.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SILGlobalVariable;
3232

3333
namespace irgen {
3434
class TypeInfo;
35+
class IRGenModule;
3536

3637
/// This data structure holds everything needed to emit debug info
3738
/// for a type.
@@ -47,19 +48,16 @@ class DebugTypeInfo {
4748
Alignment Align;
4849
bool DefaultAlignment = true;
4950
bool IsMetadataType = false;
50-
bool SizeIsFragmentSize;
51+
bool SizeIsFragmentSize = false;
52+
bool IsFixedBuffer = false;
5153

5254
public:
5355
DebugTypeInfo() = default;
5456
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
5557
Optional<Size::int_type> SizeInBits = {},
5658
Alignment AlignInBytes = Alignment(1),
5759
bool HasDefaultAlignment = true, bool IsMetadataType = false,
58-
bool IsFragmentTypeInfo = false);
59-
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
60-
Optional<Size> SizeInBytes, Alignment AlignInBytes,
61-
bool HasDefaultAlignment, bool IsMetadataType,
62-
bool IsFragmentTypeInfo);
60+
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false);
6361

6462
/// Create type for a local variable.
6563
static DebugTypeInfo getLocalVariable(VarDecl *Decl, swift::Type Ty,
@@ -80,15 +78,17 @@ class DebugTypeInfo {
8078
bool IsFragmentTypeInfo);
8179
/// Global variables.
8280
static DebugTypeInfo getGlobal(SILGlobalVariable *GV,
83-
llvm::Type *StorageType, Size size,
84-
Alignment align);
81+
llvm::Type *StorageType, IRGenModule &IGM);
82+
static DebugTypeInfo getGlobalFixedBuffer(SILGlobalVariable *GV,
83+
llvm::Type *StorageType,
84+
Size SizeInBytes, Alignment align);
8585
/// ObjC classes.
8686
static DebugTypeInfo getObjCClass(ClassDecl *theClass,
8787
llvm::Type *StorageType, Size size,
8888
Alignment align);
8989
/// Error type.
9090
static DebugTypeInfo getErrorResult(swift::Type Ty, llvm::Type *StorageType,
91-
Size size, Alignment align);
91+
IRGenModule &IGM);
9292

9393
TypeBase *getType() const { return Type; }
9494

@@ -119,6 +119,7 @@ class DebugTypeInfo {
119119
bool isMetadataType() const { return IsMetadataType; }
120120
bool hasDefaultAlignment() const { return DefaultAlignment; }
121121
bool isSizeFragmentSize() const { return SizeIsFragmentSize; }
122+
bool isFixedBuffer() const { return IsFixedBuffer; }
122123

123124
bool operator==(DebugTypeInfo T) const;
124125
bool operator!=(DebugTypeInfo T) const;
@@ -159,8 +160,8 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
159160
}
160161
static swift::irgen::DebugTypeInfo getTombstoneKey() {
161162
return swift::irgen::DebugTypeInfo(
162-
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
163-
swift::irgen::Size(0), swift::irgen::Alignment(), false, false, false);
163+
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr, 0,
164+
swift::irgen::Alignment(), false, false, false);
164165
}
165166
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {
166167
return DenseMapInfo<swift::CanType>::getHashValue(Val.getType());

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ llvm::Function *irgen::createFunction(IRGenModule &IGM, LinkInfo &linkInfo,
23542354
llvm::GlobalVariable *swift::irgen::createVariable(
23552355
IRGenModule &IGM, LinkInfo &linkInfo, llvm::Type *storageType,
23562356
Alignment alignment, DebugTypeInfo DbgTy, Optional<SILLocation> DebugLoc,
2357-
StringRef DebugName, bool inFixedBuffer) {
2357+
StringRef DebugName) {
23582358
auto name = linkInfo.getName();
23592359
llvm::GlobalValue *existingValue = IGM.Module.getNamedGlobal(name);
23602360
if (existingValue) {
@@ -2384,7 +2384,7 @@ llvm::GlobalVariable *swift::irgen::createVariable(
23842384
if (IGM.DebugInfo && !DbgTy.isNull() && linkInfo.isForDefinition())
23852385
IGM.DebugInfo->emitGlobalVariableDeclaration(
23862386
var, DebugName.empty() ? name : DebugName, name, DbgTy,
2387-
var->hasInternalLinkage(), inFixedBuffer, DebugLoc);
2387+
var->hasInternalLinkage(), DebugLoc);
23882388

23892389
return var;
23902390
}
@@ -2666,10 +2666,13 @@ Address IRGenModule::getAddrOfSILGlobalVariable(SILGlobalVariable *var,
26662666
loc = var->getLocation();
26672667
name = var->getName();
26682668
}
2669-
auto DbgTy = DebugTypeInfo::getGlobal(var, storageTypeWithContainer,
2670-
fixedSize, fixedAlignment);
2669+
DebugTypeInfo DbgTy =
2670+
inFixedBuffer
2671+
? DebugTypeInfo::getGlobalFixedBuffer(
2672+
var, storageTypeWithContainer, fixedSize, fixedAlignment)
2673+
: DebugTypeInfo::getGlobal(var, storageTypeWithContainer, *this);
26712674
gvar = createVariable(*this, link, storageTypeWithContainer,
2672-
fixedAlignment, DbgTy, loc, name, inFixedBuffer);
2675+
fixedAlignment, DbgTy, loc, name);
26732676
}
26742677
/// Add a zero initializer.
26752678
if (forDefinition)

lib/IRGen/GenDecl.h

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

5656
llvm::GlobalVariable *
5757
createLinkerDirectiveVariable(IRGenModule &IGM, StringRef Name);

lib/IRGen/GenInit.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ void IRGenModule::emitSILGlobalVariable(SILGlobalVariable *var) {
4343
// variable directly, don't actually emit it; just return undef.
4444
if (ti.isKnownEmpty(expansion)) {
4545
if (DebugInfo && var->getDecl()) {
46-
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, Size(0), Alignment(1));
46+
auto DbgTy = DebugTypeInfo::getGlobal(var, Int8Ty, *this);
4747
DebugInfo->emitGlobalVariableDeclaration(
4848
nullptr, var->getDecl()->getName().str(), "", DbgTy,
49-
var->getLinkage() != SILLinkage::Public,
50-
IRGenDebugInfo::NotHeapAllocated, SILLocation(var->getDecl()));
49+
var->getLinkage() != SILLinkage::Public, SILLocation(var->getDecl()));
5150
}
5251
return;
5352
}

0 commit comments

Comments
 (0)