Skip to content

Commit daff78c

Browse files
committed
Make BitCodeAbbrev ownership explicit using shared_ptr rather than IntrusiveRefCntPtr
If this is a problem for anyone (shared_ptr is two pointers in size, whereas IntrusiveRefCntPtr is 1 - and the ref count control block that make_shared adds is probably larger than the one int in RefCountedBase) I'd prefer to address this by adding a lower-overhead version of shared_ptr (possibly refactoring IntrusiveRefCntPtr into such a thing) to avoid the intrusiveness - this allows memory ownership to remain orthogonal to types and at least to me, seems to make code easier to understand (since no implicit ownership acquisition can happen). llvm-svn: 291006
1 parent 2ff1858 commit daff78c

File tree

7 files changed

+119
-127
lines changed

7 files changed

+119
-127
lines changed

llvm/include/llvm/Bitcode/BitCodes.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,8 @@ template <> struct isPodLike<BitCodeAbbrevOp> { static const bool value=true; };
166166
/// BitCodeAbbrev - This class represents an abbreviation record. An
167167
/// abbreviation allows a complex record that has redundancy to be stored in a
168168
/// specialized format instead of the fully-general, fully-vbr, format.
169-
class BitCodeAbbrev : public RefCountedBase<BitCodeAbbrev> {
169+
class BitCodeAbbrev {
170170
SmallVector<BitCodeAbbrevOp, 32> OperandList;
171-
// Only RefCountedBase is allowed to delete.
172-
~BitCodeAbbrev() = default;
173-
friend class RefCountedBase<BitCodeAbbrev>;
174171

175172
public:
176173
unsigned getNumOperandInfos() const {

llvm/include/llvm/Bitcode/BitstreamReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BitstreamBlockInfo {
4242
/// describe abbreviations that all blocks of the specified ID inherit.
4343
struct BlockInfo {
4444
unsigned BlockID;
45-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
45+
std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
4646
std::string Name;
4747
std::vector<std::pair<unsigned, std::string> > RecordNames;
4848
};
@@ -316,11 +316,11 @@ class BitstreamCursor : SimpleBitstreamCursor {
316316
unsigned CurCodeSize = 2;
317317

318318
/// Abbrevs installed at in this block.
319-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
319+
std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
320320

321321
struct Block {
322322
unsigned PrevCodeSize;
323-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
323+
std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
324324

325325
explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
326326
};

llvm/include/llvm/Bitcode/BitstreamWriter.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ class BitstreamWriter {
4343
unsigned BlockInfoCurBID;
4444

4545
/// CurAbbrevs - Abbrevs installed at in this block.
46-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> CurAbbrevs;
46+
std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs;
4747

4848
struct Block {
4949
unsigned PrevCodeSize;
5050
size_t StartSizeWord;
51-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> PrevAbbrevs;
51+
std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs;
5252
Block(unsigned PCS, size_t SSW) : PrevCodeSize(PCS), StartSizeWord(SSW) {}
5353
};
5454

@@ -59,7 +59,7 @@ class BitstreamWriter {
5959
/// These describe abbreviations that all blocks of the specified ID inherit.
6060
struct BlockInfo {
6161
unsigned BlockID;
62-
std::vector<IntrusiveRefCntPtr<BitCodeAbbrev>> Abbrevs;
62+
std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs;
6363
};
6464
std::vector<BlockInfo> BlockInfoRecords;
6565

@@ -469,12 +469,12 @@ class BitstreamWriter {
469469

470470
private:
471471
// Emit the abbreviation as a DEFINE_ABBREV record.
472-
void EncodeAbbrev(BitCodeAbbrev *Abbv) {
472+
void EncodeAbbrev(const BitCodeAbbrev &Abbv) {
473473
EmitCode(bitc::DEFINE_ABBREV);
474-
EmitVBR(Abbv->getNumOperandInfos(), 5);
475-
for (unsigned i = 0, e = static_cast<unsigned>(Abbv->getNumOperandInfos());
474+
EmitVBR(Abbv.getNumOperandInfos(), 5);
475+
for (unsigned i = 0, e = static_cast<unsigned>(Abbv.getNumOperandInfos());
476476
i != e; ++i) {
477-
const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
477+
const BitCodeAbbrevOp &Op = Abbv.getOperandInfo(i);
478478
Emit(Op.isLiteral(), 1);
479479
if (Op.isLiteral()) {
480480
EmitVBR64(Op.getLiteralValue(), 8);
@@ -489,10 +489,10 @@ class BitstreamWriter {
489489

490490
/// EmitAbbrev - This emits an abbreviation to the stream. Note that this
491491
/// method takes ownership of the specified abbrev.
492-
unsigned EmitAbbrev(BitCodeAbbrev *Abbv) {
492+
unsigned EmitAbbrev(std::shared_ptr<BitCodeAbbrev> Abbv) {
493493
// Emit the abbreviation as a record.
494-
EncodeAbbrev(Abbv);
495-
CurAbbrevs.push_back(Abbv);
494+
EncodeAbbrev(*Abbv);
495+
CurAbbrevs.push_back(std::move(Abbv));
496496
return static_cast<unsigned>(CurAbbrevs.size())-1 +
497497
bitc::FIRST_APPLICATION_ABBREV;
498498
}
@@ -532,13 +532,13 @@ class BitstreamWriter {
532532

533533
/// EmitBlockInfoAbbrev - Emit a DEFINE_ABBREV record for the specified
534534
/// BlockID.
535-
unsigned EmitBlockInfoAbbrev(unsigned BlockID, BitCodeAbbrev *Abbv) {
535+
unsigned EmitBlockInfoAbbrev(unsigned BlockID, std::shared_ptr<BitCodeAbbrev> Abbv) {
536536
SwitchToBlockID(BlockID);
537-
EncodeAbbrev(Abbv);
537+
EncodeAbbrev(*Abbv);
538538

539539
// Add the abbrev to the specified block record.
540540
BlockInfo &Info = getOrCreateBlockInfo(BlockID);
541-
Info.Abbrevs.push_back(Abbv);
541+
Info.Abbrevs.push_back(std::move(Abbv));
542542

543543
return Info.Abbrevs.size()-1+bitc::FIRST_APPLICATION_ABBREV;
544544
}

llvm/include/llvm/Support/FileSystem.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -829,28 +829,23 @@ class directory_iterator {
829829
};
830830

831831
namespace detail {
832-
/// RecDirIterState - Keeps state for the recursive_directory_iterator. It is
833-
/// reference counted in order to preserve InputIterator semantics on copy.
834-
struct RecDirIterState : public RefCountedBase<RecDirIterState> {
835-
RecDirIterState()
836-
: Level(0)
837-
, HasNoPushRequest(false) {}
838-
832+
/// Keeps state for the recursive_directory_iterator.
833+
struct RecDirIterState {
839834
std::stack<directory_iterator, std::vector<directory_iterator>> Stack;
840-
uint16_t Level;
841-
bool HasNoPushRequest;
835+
uint16_t Level = 0;
836+
bool HasNoPushRequest = false;
842837
};
843838
} // end namespace detail
844839

845840
/// recursive_directory_iterator - Same as directory_iterator except for it
846841
/// recurses down into child directories.
847842
class recursive_directory_iterator {
848-
IntrusiveRefCntPtr<detail::RecDirIterState> State;
843+
std::shared_ptr<detail::RecDirIterState> State;
849844

850845
public:
851846
recursive_directory_iterator() = default;
852847
explicit recursive_directory_iterator(const Twine &path, std::error_code &ec)
853-
: State(new detail::RecDirIterState) {
848+
: State(std::make_shared<detail::RecDirIterState>()) {
854849
State->Stack.push(directory_iterator(path, ec));
855850
if (State->Stack.top() == directory_iterator())
856851
State.reset();

llvm/lib/Bitcode/Reader/BitstreamReader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ unsigned BitstreamCursor::readRecord(unsigned AbbrevID,
273273
}
274274

275275
void BitstreamCursor::ReadAbbrevRecord() {
276-
BitCodeAbbrev *Abbv = new BitCodeAbbrev();
276+
auto Abbv = std::make_shared<BitCodeAbbrev>();
277277
unsigned NumOpInfo = ReadVBR(5);
278278
for (unsigned i = 0; i != NumOpInfo; ++i) {
279279
bool IsLiteral = Read(1);
@@ -307,7 +307,7 @@ void BitstreamCursor::ReadAbbrevRecord() {
307307

308308
if (Abbv->getNumOperandInfos() == 0)
309309
report_fatal_error("Abbrev record with no operands");
310-
CurAbbrevs.push_back(Abbv);
310+
CurAbbrevs.push_back(std::move(Abbv));
311311
}
312312

313313
Optional<BitstreamBlockInfo>

0 commit comments

Comments
 (0)