Skip to content

Commit 9eb3adb

Browse files
authored
Merge pull request #62591 from adrian-prantl/fixed-buffer
Sink isFixedBuffer into DebugTypeInfo and ensure the enclosed types
2 parents 4e01ced + 06a5443 commit 9eb3adb

File tree

9 files changed

+189
-119
lines changed

9 files changed

+189
-119
lines changed

lib/IRGen/DebugTypeInfo.cpp

Lines changed: 68 additions & 39 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"
@@ -25,13 +26,15 @@ using namespace swift;
2526
using namespace irgen;
2627

2728
DebugTypeInfo::DebugTypeInfo(swift::Type Ty, llvm::Type *FragmentStorageTy,
28-
Optional<Size> size, Alignment align,
29-
bool HasDefaultAlignment, bool IsMetadata,
30-
bool SizeIsFragmentSize)
31-
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy), size(size),
32-
align(align), DefaultAlignment(HasDefaultAlignment),
33-
IsMetadataType(IsMetadata), SizeIsFragmentSize(SizeIsFragmentSize) {
34-
assert(align.getValue() != 0);
29+
Optional<Size::int_type> SizeInBits,
30+
Alignment Align, bool HasDefaultAlignment,
31+
bool IsMetadata, bool SizeIsFragmentSize,
32+
bool IsFixedBuffer)
33+
: Type(Ty.getPointer()), FragmentStorageType(FragmentStorageTy),
34+
SizeInBits(SizeInBits), Align(Align),
35+
DefaultAlignment(HasDefaultAlignment), IsMetadataType(IsMetadata),
36+
SizeIsFragmentSize(SizeIsFragmentSize), IsFixedBuffer(IsFixedBuffer) {
37+
assert(Align.getValue() != 0);
3538
}
3639

3740
/// Determine whether this type has a custom @_alignment attribute.
@@ -43,17 +46,20 @@ static bool hasDefaultAlignment(swift::Type Ty) {
4346
return true;
4447
}
4548

46-
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty,
47-
const TypeInfo &Info,
49+
DebugTypeInfo DebugTypeInfo::getFromTypeInfo(swift::Type Ty, const TypeInfo &TI,
4850
bool IsFragmentTypeInfo) {
49-
Optional<Size> size;
50-
if (Info.isFixedSize()) {
51-
const FixedTypeInfo &FixTy = *cast<const FixedTypeInfo>(&Info);
52-
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;
5359
}
54-
assert(Info.getStorageType() && "StorageType is a nullptr");
55-
return DebugTypeInfo(Ty.getPointer(), Info.getStorageType(), size,
56-
Info.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
60+
assert(TI.getStorageType() && "StorageType is a nullptr");
61+
return DebugTypeInfo(Ty.getPointer(), StorageType, SizeInBits,
62+
TI.getBestKnownAlignment(), ::hasDefaultAlignment(Ty),
5763
false, IsFragmentTypeInfo);
5864
}
5965

@@ -79,8 +85,8 @@ DebugTypeInfo DebugTypeInfo::getLocalVariable(VarDecl *Decl, swift::Type Ty,
7985
DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
8086
llvm::Type *StorageTy, Size size,
8187
Alignment align) {
82-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
83-
align, true, false, false);
88+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
89+
true, false, false);
8490
assert(StorageTy && "StorageType is a nullptr");
8591
assert(!DbgTy.isContextArchetype() &&
8692
"type metadata cannot contain an archetype");
@@ -90,23 +96,22 @@ DebugTypeInfo DebugTypeInfo::getGlobalMetadata(swift::Type Ty,
9096
DebugTypeInfo DebugTypeInfo::getTypeMetadata(swift::Type Ty,
9197
llvm::Type *StorageTy, Size size,
9298
Alignment align) {
93-
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size,
94-
align, true, true, false);
99+
DebugTypeInfo DbgTy(Ty.getPointer(), StorageTy, size.getValue() * 8, align,
100+
true, true, false);
95101
assert(StorageTy && "StorageType is a nullptr");
96102
assert(!DbgTy.isContextArchetype() &&
97103
"type metadata cannot contain an archetype");
98104
return DbgTy;
99105
}
100106

101107
DebugTypeInfo DebugTypeInfo::getForwardDecl(swift::Type Ty) {
102-
DebugTypeInfo DbgTy(Ty.getPointer(), nullptr, {}, Alignment(1), true,
103-
false, false);
108+
DebugTypeInfo DbgTy(Ty.getPointer());
104109
return DbgTy;
105110
}
106111

107112
DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
108-
llvm::Type *StorageTy, Size size,
109-
Alignment align) {
113+
llvm::Type *FragmentStorageType,
114+
IRGenModule &IGM) {
110115
// Prefer the original, potentially sugared version of the type if
111116
// the type hasn't been mucked with by an optimization pass.
112117
auto LowTy = GV->getLoweredType().getASTType();
@@ -116,37 +121,61 @@ DebugTypeInfo DebugTypeInfo::getGlobal(SILGlobalVariable *GV,
116121
if (DeclType->isEqual(LowTy))
117122
Type = DeclType.getPointer();
118123
}
119-
DebugTypeInfo DbgTy(Type, StorageTy, size, align, ::hasDefaultAlignment(Type),
120-
false, false);
121-
assert(StorageTy && "FragmentStorageType is a nullptr");
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) {
136+
// Prefer the original, potentially sugared version of the type if
137+
// the type hasn't been mucked with by an optimization pass.
138+
auto LowTy = GV->getLoweredType().getASTType();
139+
auto *Type = LowTy.getPointer();
140+
if (auto *Decl = GV->getDecl()) {
141+
auto DeclType = Decl->getType();
142+
if (DeclType->isEqual(LowTy))
143+
Type = DeclType.getPointer();
144+
}
145+
DebugTypeInfo DbgTy(Type, FragmentStorageType, SizeInBytes.getValue() * 8,
146+
Align, ::hasDefaultAlignment(Type), false, false, true);
147+
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
122148
assert(!DbgTy.isContextArchetype() &&
123149
"type of global variable cannot be an archetype");
124-
assert(align.getValue() != 0);
125150
return DbgTy;
126151
}
127152

128153
DebugTypeInfo DebugTypeInfo::getObjCClass(ClassDecl *theClass,
129154
llvm::Type *FragmentStorageType,
130-
Size size, Alignment align) {
155+
Size SizeInBytes, Alignment align) {
131156
DebugTypeInfo DbgTy(theClass->getInterfaceType().getPointer(),
132-
FragmentStorageType, size, align, true, false, false);
157+
FragmentStorageType, SizeInBytes.getValue() * 8, align,
158+
true, false, false);
133159
assert(FragmentStorageType && "FragmentStorageType is a nullptr");
134160
assert(!DbgTy.isContextArchetype() &&
135161
"type of objc class cannot be an archetype");
136162
return DbgTy;
137163
}
138164

139165
DebugTypeInfo DebugTypeInfo::getErrorResult(swift::Type Ty,
140-
llvm::Type *StorageType, Size size,
141-
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);
142171
assert(StorageType && "FragmentStorageType is a nullptr");
143-
return {Ty, StorageType, size, align, true, false, false};
172+
return DbgTy;
144173
}
145174

146175
bool DebugTypeInfo::operator==(DebugTypeInfo T) const {
147-
return (getType() == T.getType() &&
148-
size == T.size &&
149-
align == T.align);
176+
return getType() == T.getType() &&
177+
SizeInBits == T.SizeInBits &&
178+
Align == T.Align;
150179
}
151180

152181
bool DebugTypeInfo::operator!=(DebugTypeInfo T) const { return !operator==(T); }
@@ -168,9 +197,9 @@ TypeDecl *DebugTypeInfo::getDecl() const {
168197
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
169198
LLVM_DUMP_METHOD void DebugTypeInfo::dump() const {
170199
llvm::errs() << "[";
171-
if (size)
172-
llvm::errs() << "Size " << size->getValue() << " ";
173-
llvm::errs() << "Alignment " << align.getValue() << "] ";
200+
if (SizeInBits)
201+
llvm::errs() << "SizeInBits " << *SizeInBits << " ";
202+
llvm::errs() << "Alignment " << Align.getValue() << "] ";
174203
getType()->dump(llvm::errs());
175204

176205
if (FragmentStorageType) {

lib/IRGen/DebugTypeInfo.h

Lines changed: 26 additions & 20 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.
@@ -43,18 +44,20 @@ class DebugTypeInfo {
4344
/// Needed to determine the size of basic types and to determine
4445
/// the storage type for undefined variables.
4546
llvm::Type *FragmentStorageType = nullptr;
46-
Optional<Size> size;
47-
Alignment align;
47+
Optional<Size::int_type> SizeInBits;
48+
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;
54-
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy,
55-
Optional<Size> SizeInBytes, Alignment AlignInBytes,
56-
bool HasDefaultAlignment, bool IsMetadataType,
57-
bool IsFragmentTypeInfo);
56+
DebugTypeInfo(swift::Type Ty, llvm::Type *StorageTy = nullptr,
57+
Optional<Size::int_type> SizeInBits = {},
58+
Alignment AlignInBytes = Alignment(1),
59+
bool HasDefaultAlignment = true, bool IsMetadataType = false,
60+
bool IsFragmentTypeInfo = false, bool IsFixedBuffer = false);
5861

5962
/// Create type for a local variable.
6063
static DebugTypeInfo getLocalVariable(VarDecl *Decl, swift::Type Ty,
@@ -75,15 +78,17 @@ class DebugTypeInfo {
7578
bool IsFragmentTypeInfo);
7679
/// Global variables.
7780
static DebugTypeInfo getGlobal(SILGlobalVariable *GV,
78-
llvm::Type *StorageType, Size size,
79-
Alignment align);
81+
llvm::Type *StorageType, IRGenModule &IGM);
82+
static DebugTypeInfo getGlobalFixedBuffer(SILGlobalVariable *GV,
83+
llvm::Type *StorageType,
84+
Size SizeInBytes, Alignment align);
8085
/// ObjC classes.
8186
static DebugTypeInfo getObjCClass(ClassDecl *theClass,
8287
llvm::Type *StorageType, Size size,
8388
Alignment align);
8489
/// Error type.
8590
static DebugTypeInfo getErrorResult(swift::Type Ty, llvm::Type *StorageType,
86-
Size size, Alignment align);
91+
IRGenModule &IGM);
8792

8893
TypeBase *getType() const { return Type; }
8994

@@ -99,21 +104,22 @@ class DebugTypeInfo {
99104
}
100105

101106
llvm::Type *getFragmentStorageType() const {
102-
if (size && size->isZero())
107+
if (SizeInBits && *SizeInBits == 0)
103108
assert(FragmentStorageType && "only defined types may have a size");
104109
return FragmentStorageType;
105110
}
106-
Optional<Size> getTypeSize() const {
107-
return SizeIsFragmentSize ? llvm::None : size;
111+
Optional<Size::int_type> getTypeSizeInBits() const {
112+
return SizeIsFragmentSize ? llvm::None : SizeInBits;
108113
}
109-
Optional<Size> getRawSize() const { return size; }
110-
void setSize(Size NewSize) { size = NewSize; }
111-
Alignment getAlignment() const { return align; }
114+
Optional<Size::int_type> getRawSizeInBits() const { return SizeInBits; }
115+
void setSizeInBits(Size::int_type NewSize) { SizeInBits = NewSize; }
116+
Alignment getAlignment() const { return Align; }
112117
bool isNull() const { return Type == nullptr; }
113118
bool isForwardDecl() const { return FragmentStorageType == nullptr; }
114119
bool isMetadataType() const { return IsMetadataType; }
115120
bool hasDefaultAlignment() const { return DefaultAlignment; }
116121
bool isSizeFragmentSize() const { return SizeIsFragmentSize; }
122+
bool isFixedBuffer() const { return IsFixedBuffer; }
117123

118124
bool operator==(DebugTypeInfo T) const;
119125
bool operator!=(DebugTypeInfo T) const;
@@ -128,7 +134,7 @@ class CompletedDebugTypeInfo : public DebugTypeInfo {
128134

129135
public:
130136
static Optional<CompletedDebugTypeInfo> get(DebugTypeInfo DbgTy) {
131-
if (!DbgTy.getRawSize() || DbgTy.isSizeFragmentSize())
137+
if (!DbgTy.getRawSizeInBits() || DbgTy.isSizeFragmentSize())
132138
return {};
133139
return CompletedDebugTypeInfo(DbgTy);
134140
}
@@ -139,7 +145,7 @@ class CompletedDebugTypeInfo : public DebugTypeInfo {
139145
DebugTypeInfo::getFromTypeInfo(Ty, Info, /*IsFragment*/ false));
140146
}
141147

142-
Size::int_type getSizeValue() const { return size->getValue(); }
148+
Size::int_type getSizeInBits() const { return *SizeInBits; }
143149
};
144150

145151
}
@@ -154,8 +160,8 @@ template <> struct DenseMapInfo<swift::irgen::DebugTypeInfo> {
154160
}
155161
static swift::irgen::DebugTypeInfo getTombstoneKey() {
156162
return swift::irgen::DebugTypeInfo(
157-
llvm::DenseMapInfo<swift::TypeBase *>::getTombstoneKey(), nullptr,
158-
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);
159165
}
160166
static unsigned getHashValue(swift::irgen::DebugTypeInfo Val) {
161167
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)