Skip to content

[NFC] [Serializer] Pack information in serializer #69287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class alignas(8) Decl {
/// The kind of ownership a declaration has, for visibility purposes.
/// This enumeration is designed such that higher values represent higher
/// levels of name hiding.
enum class ModuleOwnershipKind : unsigned {
enum class ModuleOwnershipKind : unsigned char {
/// This declaration is not owned by a module.
Unowned,

Expand Down
47 changes: 47 additions & 0 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2407,6 +2407,53 @@ class ASTReader
bool isProcessingUpdateRecords() { return ProcessingUpdateRecords; }
};

/// A simple helper class to unpack an integer to bits and consuming
/// the bits in order.
class BitsUnpacker {
constexpr static uint32_t BitsIndexUpbound = 32;

public:
BitsUnpacker(uint32_t V) { updateValue(V); }
BitsUnpacker(const BitsUnpacker &) = delete;
BitsUnpacker(BitsUnpacker &&) = delete;
BitsUnpacker operator=(const BitsUnpacker &) = delete;
BitsUnpacker operator=(BitsUnpacker &&) = delete;
~BitsUnpacker() {
#ifndef NDEBUG
while (isValid())
assert(!getNextBit() && "There are unprocessed bits!");
#endif
}

void updateValue(uint32_t V) {
Value = V;
CurrentBitsIndex = 0;
}

bool getNextBit() {
assert(isValid());
return Value & (1 << CurrentBitsIndex++);
}

uint32_t getNextBits(uint32_t Width) {
assert(isValid());
assert(Width < BitsIndexUpbound);
uint32_t Ret = (Value >> CurrentBitsIndex) & ((1 << Width) - 1);
CurrentBitsIndex += Width;
return Ret;
}

bool canGetNextNBits(uint32_t Width) const {
return CurrentBitsIndex + Width < BitsIndexUpbound;
}

private:
bool isValid() const { return CurrentBitsIndex < BitsIndexUpbound; }

uint32_t Value;
uint32_t CurrentBitsIndex = ~0;
};

} // namespace clang

#endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H
53 changes: 53 additions & 0 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,59 @@ class PCHGenerator : public SemaConsumer {
bool hasEmittedPCH() const { return Buffer->IsComplete; }
};

/// A simple helper class to pack several bits in order into (a) 32 bit
/// integer(s).
class BitsPacker {
constexpr static uint32_t BitIndexUpbound = 32u;

public:
BitsPacker() = default;
BitsPacker(const BitsPacker &) = delete;
BitsPacker(BitsPacker &&) = delete;
BitsPacker operator=(const BitsPacker &) = delete;
BitsPacker operator=(BitsPacker &&) = delete;
~BitsPacker() {
assert(!hasUnconsumedValues() && "There are unprocessed bits!");
}

void addBit(bool Value) { addBits(Value, 1); }
void addBits(uint32_t Value, uint32_t BitsWidth) {
assert(BitsWidth < BitIndexUpbound);
assert((Value < (1u << BitsWidth)) && "Passing narrower bit width!");

if (CurrentBitIndex + BitsWidth >= BitIndexUpbound) {
Values.push_back(0);
CurrentBitIndex = 0;
}

assert(CurrentBitIndex < BitIndexUpbound);
Values.back() |= Value << CurrentBitIndex;
CurrentBitIndex += BitsWidth;
}

bool hasUnconsumedValues() const {
return ConsumingValueIndex < Values.size();
}
uint32_t getNextValue() {
assert(hasUnconsumedValues());
return Values[ConsumingValueIndex++];
}

// We can convert the packer to an uint32_t if there is only one values.
operator uint32_t() {
assert(Values.size() == 1);
return getNextValue();
}

private:
SmallVector<uint64_t, 4> Values;
uint16_t ConsumingValueIndex = 0;
// Initialize CurrentBitIndex with an invalid value
// to make it easier to update Values. See the implementation
// of `addBits` to see the details.
uint16_t CurrentBitIndex = BitIndexUpbound;
};

} // namespace clang

#endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
Loading