Skip to content

Commit b1aa0b0

Browse files
authored
[DataLayout] Remove clear and reset methods (NFC) (#102993)
`clear` was never necessary as it is always called on a fresh instance of the class or just before freeing an instance's memory. `reset` is effectively the same as the constructor. Pull Reuquest: #102993
1 parent f0ef1d3 commit b1aa0b0

File tree

4 files changed

+54
-61
lines changed

4 files changed

+54
-61
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,10 @@ class DataLayout {
185185
/// if the string is malformed.
186186
Error parseSpecifier(StringRef Desc);
187187

188-
// Free all internal data structures.
189-
void clear();
190-
191188
public:
192-
/// Constructs a DataLayout from a specification string. See reset().
193-
explicit DataLayout(StringRef LayoutDescription) {
194-
reset(LayoutDescription);
195-
}
189+
/// Constructs a DataLayout from a specification string.
190+
/// WARNING: Aborts execution if the string is malformed. Use parse() instead.
191+
explicit DataLayout(StringRef LayoutString);
196192

197193
DataLayout(const DataLayout &DL) { *this = DL; }
198194

@@ -203,9 +199,6 @@ class DataLayout {
203199
bool operator==(const DataLayout &Other) const;
204200
bool operator!=(const DataLayout &Other) const { return !(*this == Other); }
205201

206-
/// Parse a data layout string (with fallback to default values).
207-
void reset(StringRef LayoutDescription);
208-
209202
/// Parse a data layout string and return the layout. Return an error
210203
/// description on failure.
211204
static Expected<DataLayout> parse(StringRef LayoutDescription);

llvm/lib/IR/DataLayout.cpp

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ unsigned StructLayout::getElementContainingOffset(uint64_t FixedOffset) const {
117117
return SI - MemberOffsets.begin();
118118
}
119119

120+
namespace {
121+
122+
class StructLayoutMap {
123+
using LayoutInfoTy = DenseMap<StructType *, StructLayout *>;
124+
LayoutInfoTy LayoutInfo;
125+
126+
public:
127+
~StructLayoutMap() {
128+
// Remove any layouts.
129+
for (const auto &I : LayoutInfo) {
130+
StructLayout *Value = I.second;
131+
Value->~StructLayout();
132+
free(Value);
133+
}
134+
}
135+
136+
StructLayout *&operator[](StructType *STy) { return LayoutInfo[STy]; }
137+
};
138+
139+
} // end anonymous namespace
140+
120141
//===----------------------------------------------------------------------===//
121142
// LayoutAlignElem, LayoutAlign support
122143
//===----------------------------------------------------------------------===//
@@ -191,36 +212,31 @@ static const std::pair<AlignTypeEnum, LayoutAlignElem> DefaultAlignments[] = {
191212
{VECTOR_ALIGN, {128, Align(16), Align(16)}}, // v16i8, v8i16, v4i32, ...
192213
};
193214

194-
void DataLayout::reset(StringRef Desc) {
195-
clear();
196-
197-
LayoutMap = nullptr;
215+
DataLayout::DataLayout(StringRef LayoutString) {
198216
BigEndian = false;
199217
AllocaAddrSpace = 0;
200-
StackNaturalAlign.reset();
201218
ProgramAddrSpace = 0;
202219
DefaultGlobalsAddrSpace = 0;
203-
FunctionPtrAlign.reset();
204220
TheFunctionPtrAlignType = FunctionPtrAlignType::Independent;
205221
ManglingMode = MM_None;
206-
NonIntegralAddressSpaces.clear();
207222
StructAlignment = LayoutAlignElem::get(Align(1), Align(8), 0);
208223

209224
// Default alignments
210225
for (const auto &[Kind, Layout] : DefaultAlignments) {
211226
if (Error Err = setAlignment(Kind, Layout.ABIAlign, Layout.PrefAlign,
212227
Layout.TypeBitWidth))
213-
return report_fatal_error(std::move(Err));
228+
report_fatal_error(std::move(Err));
214229
}
215230
if (Error Err = setPointerAlignmentInBits(0, Align(8), Align(8), 64, 64))
216-
return report_fatal_error(std::move(Err));
231+
report_fatal_error(std::move(Err));
217232

218-
if (Error Err = parseSpecifier(Desc))
219-
return report_fatal_error(std::move(Err));
233+
if (Error Err = parseSpecifier(LayoutString))
234+
report_fatal_error(std::move(Err));
220235
}
221236

222237
DataLayout &DataLayout::operator=(const DataLayout &Other) {
223-
clear();
238+
delete static_cast<StructLayoutMap *>(LayoutMap);
239+
LayoutMap = nullptr;
224240
StringRepresentation = Other.StringRepresentation;
225241
BigEndian = Other.BigEndian;
226242
AllocaAddrSpace = Other.AllocaAddrSpace;
@@ -693,42 +709,7 @@ Align DataLayout::getIntegerAlignment(uint32_t BitWidth,
693709
return abi_or_pref ? I->ABIAlign : I->PrefAlign;
694710
}
695711

696-
namespace {
697-
698-
class StructLayoutMap {
699-
using LayoutInfoTy = DenseMap<StructType*, StructLayout*>;
700-
LayoutInfoTy LayoutInfo;
701-
702-
public:
703-
~StructLayoutMap() {
704-
// Remove any layouts.
705-
for (const auto &I : LayoutInfo) {
706-
StructLayout *Value = I.second;
707-
Value->~StructLayout();
708-
free(Value);
709-
}
710-
}
711-
712-
StructLayout *&operator[](StructType *STy) {
713-
return LayoutInfo[STy];
714-
}
715-
};
716-
717-
} // end anonymous namespace
718-
719-
void DataLayout::clear() {
720-
LegalIntWidths.clear();
721-
IntAlignments.clear();
722-
FloatAlignments.clear();
723-
VectorAlignments.clear();
724-
Pointers.clear();
725-
delete static_cast<StructLayoutMap *>(LayoutMap);
726-
LayoutMap = nullptr;
727-
}
728-
729-
DataLayout::~DataLayout() {
730-
clear();
731-
}
712+
DataLayout::~DataLayout() { delete static_cast<StructLayoutMap *>(LayoutMap); }
732713

733714
const StructLayout *DataLayout::getStructLayout(StructType *Ty) const {
734715
if (!LayoutMap)

llvm/lib/IR/Module.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ void Module::setModuleFlag(ModFlagBehavior Behavior, StringRef Key,
388388
setModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
389389
}
390390

391-
void Module::setDataLayout(StringRef Desc) {
392-
DL.reset(Desc);
393-
}
391+
void Module::setDataLayout(StringRef Desc) { DL = DataLayout(Desc); }
394392

395393
void Module::setDataLayout(const DataLayout &Other) { DL = Other; }
396394

llvm/unittests/IR/DataLayoutTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@ using namespace llvm;
1919

2020
namespace {
2121

22+
TEST(DataLayoutTest, CopyAssignmentInvalidatesStructLayout) {
23+
DataLayout DL1 = cantFail(DataLayout::parse("p:32:32"));
24+
DataLayout DL2 = cantFail(DataLayout::parse("p:64:64"));
25+
26+
LLVMContext Ctx;
27+
StructType *Ty = StructType::get(PointerType::getUnqual(Ctx));
28+
29+
// Initialize struct layout caches.
30+
EXPECT_EQ(DL1.getStructLayout(Ty)->getSizeInBits(), 32U);
31+
EXPECT_EQ(DL1.getStructLayout(Ty)->getAlignment(), Align(4));
32+
EXPECT_EQ(DL2.getStructLayout(Ty)->getSizeInBits(), 64U);
33+
EXPECT_EQ(DL2.getStructLayout(Ty)->getAlignment(), Align(8));
34+
35+
// The copy should invalidate DL1's cache.
36+
DL1 = DL2;
37+
EXPECT_EQ(DL1.getStructLayout(Ty)->getSizeInBits(), 64U);
38+
EXPECT_EQ(DL1.getStructLayout(Ty)->getAlignment(), Align(8));
39+
EXPECT_EQ(DL2.getStructLayout(Ty)->getSizeInBits(), 64U);
40+
EXPECT_EQ(DL2.getStructLayout(Ty)->getAlignment(), Align(8));
41+
}
42+
2243
TEST(DataLayoutTest, FunctionPtrAlign) {
2344
EXPECT_EQ(MaybeAlign(0), DataLayout("").getFunctionPtrAlign());
2445
EXPECT_EQ(MaybeAlign(1), DataLayout("Fi8").getFunctionPtrAlign());

0 commit comments

Comments
 (0)