Skip to content

Commit 443423e

Browse files
committed
Move the section name from GlobalObject to the LLVMContext
Summary: Convention wisdom says that bytes in Function are precious, and the vast, vast majority of globals do not live in special sections. Even when they do, they tend to live in the same section. Store the section name on the LLVMContext in a StringSet, and maintain a map from GlobalObject* to section name like we do for metadata, prefix data, etc. The fact that we've survived this long wasting at least three pointers of space in Function suggests that Function bytes are perhaps not as precious as we once thought. Given that most functions have metadata attachments when debug info is enabled, we might consider adding a pointer here to make that access more efficient. Reviewers: jlebar, dexonsmith, mehdi_amini Subscribers: mehdi_amini, aprantl, llvm-commits Differential Revision: https://reviews.llvm.org/D28150 llvm-svn: 291613
1 parent 3f50904 commit 443423e

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

llvm/include/llvm/IR/GlobalObject.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class GlobalObject : public GlobalValue {
3737
setGlobalValueSubClassData(0);
3838
}
3939

40-
std::string Section; // Section to emit this into, empty means default
4140
Comdat *ObjComdat;
4241
enum {
4342
LastAlignmentBit = 4,
4443
HasMetadataHashEntryBit,
44+
HasSectionHashEntryBit,
4545

4646
GlobalObjectBits,
4747
};
@@ -66,8 +66,26 @@ class GlobalObject : public GlobalValue {
6666
unsigned getGlobalObjectSubClassData() const;
6767
void setGlobalObjectSubClassData(unsigned Val);
6868

69-
bool hasSection() const { return !getSection().empty(); }
70-
StringRef getSection() const { return Section; }
69+
/// Check if this global has a custom object file section.
70+
///
71+
/// This is more efficient than calling getSection() and checking for an empty
72+
/// string.
73+
bool hasSection() const {
74+
return getGlobalValueSubClassData() & (1 << HasSectionHashEntryBit);
75+
}
76+
77+
/// Get the custom section of this global if it has one.
78+
///
79+
/// If this global does not have a custom section, this will be empty and the
80+
/// default object file section (.text, .data, etc) will be used.
81+
StringRef getSection() const {
82+
return hasSection() ? getSectionImpl() : StringRef();
83+
}
84+
85+
/// Change the section for this global.
86+
///
87+
/// Setting the section to the empty string tells LLVM to choose an
88+
/// appropriate default object file section.
7189
void setSection(StringRef S);
7290

7391
bool hasComdat() const { return getComdat() != nullptr; }
@@ -134,14 +152,20 @@ class GlobalObject : public GlobalValue {
134152
void clearMetadata();
135153

136154
private:
155+
void setGlobalObjectFlag(unsigned Bit, bool Val) {
156+
unsigned Mask = 1 << Bit;
157+
setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
158+
(Val ? Mask : 0u));
159+
}
160+
137161
bool hasMetadataHashEntry() const {
138162
return getGlobalValueSubClassData() & (1 << HasMetadataHashEntryBit);
139163
}
140164
void setHasMetadataHashEntry(bool HasEntry) {
141-
unsigned Mask = 1 << HasMetadataHashEntryBit;
142-
setGlobalValueSubClassData((~Mask & getGlobalValueSubClassData()) |
143-
(HasEntry ? Mask : 0u));
165+
setGlobalObjectFlag(HasMetadataHashEntryBit, HasEntry);
144166
}
167+
168+
StringRef getSectionImpl() const;
145169
};
146170

147171
} // end namespace llvm

llvm/lib/IR/Globals.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/IR/Operator.h"
2525
#include "llvm/Support/Error.h"
2626
#include "llvm/Support/ErrorHandling.h"
27+
#include "LLVMContextImpl.h"
2728
using namespace llvm;
2829

2930
//===----------------------------------------------------------------------===//
@@ -37,6 +38,10 @@ static_assert(sizeof(GlobalValue) ==
3738
sizeof(Constant) + 2 * sizeof(void *) + 2 * sizeof(unsigned),
3839
"unexpected GlobalValue size growth");
3940

41+
// GlobalObject adds a comdat.
42+
static_assert(sizeof(GlobalObject) == sizeof(GlobalValue) + sizeof(void *),
43+
"unexpected GlobalObject size growth");
44+
4045
bool GlobalValue::isMaterializable() const {
4146
if (const Function *F = dyn_cast<Function>(this))
4247
return F->isMaterializable();
@@ -160,11 +165,24 @@ Comdat *GlobalValue::getComdat() {
160165
return cast<GlobalObject>(this)->getComdat();
161166
}
162167

163-
void GlobalObject::setSection(StringRef S) {
164-
Section = S;
168+
StringRef GlobalObject::getSectionImpl() const {
169+
assert(hasSection());
170+
return getContext().pImpl->GlobalObjectSections[this];
171+
}
165172

166-
// The C api requires this to be null terminated.
167-
Section.c_str();
173+
void GlobalObject::setSection(StringRef S) {
174+
// Do nothing if we're clearing the section and it is already empty.
175+
if (!hasSection() && S.empty())
176+
return;
177+
178+
// Get or create a stable section name string and put it in the table in the
179+
// context.
180+
S = getContext().pImpl->SectionStrings.insert(S).first->first();
181+
getContext().pImpl->GlobalObjectSections[this] = S;
182+
183+
// Update the HasSectionHashEntryBit. Setting the section to the empty string
184+
// means this global no longer has a section.
185+
setGlobalObjectFlag(HasSectionHashEntryBit, !S.empty());
168186
}
169187

170188
bool GlobalValue::isDeclaration() const {

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/Hashing.h"
2727
#include "llvm/ADT/SmallPtrSet.h"
2828
#include "llvm/ADT/StringMap.h"
29+
#include "llvm/ADT/StringSet.h"
2930
#include "llvm/IR/Constants.h"
3031
#include "llvm/IR/DebugInfoMetadata.h"
3132
#include "llvm/IR/DerivedTypes.h"
@@ -1194,6 +1195,12 @@ class LLVMContextImpl {
11941195
/// Collection of per-GlobalObject metadata used in this context.
11951196
DenseMap<const GlobalObject *, MDGlobalAttachmentMap> GlobalObjectMetadata;
11961197

1198+
/// Collection of per-GlobalObject sections used in this context.
1199+
DenseMap<const GlobalObject *, StringRef> GlobalObjectSections;
1200+
1201+
/// Stable collection of section strings.
1202+
StringSet<> SectionStrings;
1203+
11971204
/// DiscriminatorTable - This table maps file:line locations to an
11981205
/// integer representing the next DWARF path discriminator to assign to
11991206
/// instructions in different blocks at the same location.

0 commit comments

Comments
 (0)