Skip to content

[serialization] No transitive type change #92511

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 6 commits into from
Jun 21, 2024
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
48 changes: 36 additions & 12 deletions clang/include/clang/Serialization/ASTBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "clang/Serialization/SourceLocationEncoding.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/Bitstream/BitCodes.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
#include <cstdint>

Expand Down Expand Up @@ -70,41 +71,64 @@ using DeclID = DeclIDBase::DeclID;

/// An ID number that refers to a type in an AST file.
///
/// The ID of a type is partitioned into two parts: the lower
/// three bits are used to store the const/volatile/restrict
/// qualifiers (as with QualType) and the upper bits provide a
/// type index. The type index values are partitioned into two
/// The ID of a type is partitioned into three parts:
/// - the lower three bits are used to store the const/volatile/restrict
/// qualifiers (as with QualType).
/// - the next 29 bits provide a type index in the corresponding
/// module file.
/// - the upper 32 bits provide a module file index.
///
/// The type index values are partitioned into two
/// sets. The values below NUM_PREDEF_TYPE_IDs are predefined type
/// IDs (based on the PREDEF_TYPE_*_ID constants), with 0 as a
/// placeholder for "no type". Values from NUM_PREDEF_TYPE_IDs are
/// other types that have serialized representations.
using TypeID = uint32_t;
/// placeholder for "no type". The module file index for predefined
/// types are always 0 since they don't belong to any modules.
/// Values from NUM_PREDEF_TYPE_IDs are other types that have
/// serialized representations.
using TypeID = uint64_t;
/// Same with TypeID except that the LocalTypeID is only meaningful
/// with the corresponding ModuleFile.
///
/// FIXME: Make TypeID and LocalTypeID a class to improve the type
/// safety.
using LocalTypeID = TypeID;

/// A type index; the type ID with the qualifier bits removed.
/// Keep structure alignment 32-bit since the blob is assumed as 32-bit
/// aligned.
class TypeIdx {
uint32_t ModuleFileIndex = 0;
uint32_t Idx = 0;

public:
TypeIdx() = default;
explicit TypeIdx(uint32_t index) : Idx(index) {}

uint32_t getIndex() const { return Idx; }
explicit TypeIdx(uint32_t ModuleFileIdx, uint32_t Idx)
: ModuleFileIndex(ModuleFileIdx), Idx(Idx) {}

uint32_t getModuleFileIndex() const { return ModuleFileIndex; }

uint64_t getValue() const { return ((uint64_t)ModuleFileIndex << 32) | Idx; }

TypeID asTypeID(unsigned FastQuals) const {
if (Idx == uint32_t(-1))
return TypeID(-1);

return (Idx << Qualifiers::FastWidth) | FastQuals;
unsigned Index = (Idx << Qualifiers::FastWidth) | FastQuals;
return ((uint64_t)ModuleFileIndex << 32) | Index;
}

static TypeIdx fromTypeID(TypeID ID) {
if (ID == TypeID(-1))
return TypeIdx(-1);
return TypeIdx(0, -1);

return TypeIdx(ID >> Qualifiers::FastWidth);
return TypeIdx(ID >> 32, (ID & llvm::maskTrailingOnes<TypeID>(32)) >>
Qualifiers::FastWidth);
}
};

static_assert(alignof(TypeIdx) == 4);

/// A structure for putting "fast"-unqualified QualTypes into a
/// DenseMap. This uses the standard pointer hash function.
struct UnsafeQualTypeDenseMapInfo {
Expand Down
22 changes: 10 additions & 12 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,6 @@ class ASTReader
/// ID = (I + 1) << FastQual::Width has already been loaded
llvm::PagedVector<QualType> TypesLoaded;

using GlobalTypeMapType =
ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>;

/// Mapping from global type IDs to the module in which the
/// type resides along with the offset that should be added to the
/// global type ID to produce a local ID.
GlobalTypeMapType GlobalTypeMap;

/// Declarations that have already been loaded from the chain.
///
/// When the pointer at index I is non-NULL, the declaration with ID
Expand Down Expand Up @@ -1429,8 +1421,8 @@ class ASTReader
RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {}
};

QualType readTypeRecord(unsigned Index);
RecordLocation TypeCursorForIndex(unsigned Index);
QualType readTypeRecord(serialization::TypeID ID);
RecordLocation TypeCursorForIndex(serialization::TypeID ID);
void LoadedDecl(unsigned Index, Decl *D);
Decl *ReadDeclRecord(GlobalDeclID ID);
void markIncompleteDeclChain(Decl *D);
Expand Down Expand Up @@ -1544,6 +1536,11 @@ class ASTReader
std::pair<ModuleFile *, unsigned>
translateIdentifierIDToIndex(serialization::IdentifierID ID) const;

/// Translate an \param TypeID ID to the index of TypesLoaded
/// array and the corresponding module file.
std::pair<ModuleFile *, unsigned>
translateTypeIDToIndex(serialization::TypeID ID) const;

public:
/// Load the AST file and validate its contents against the given
/// Preprocessor.
Expand Down Expand Up @@ -1892,10 +1889,11 @@ class ASTReader
QualType GetType(serialization::TypeID ID);

/// Resolve a local type ID within a given AST file into a type.
QualType getLocalType(ModuleFile &F, unsigned LocalID);
QualType getLocalType(ModuleFile &F, serialization::LocalTypeID LocalID);

/// Map a local type ID within a given AST file into a global type ID.
serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
serialization::TypeID
getGlobalTypeID(ModuleFile &F, serialization::LocalTypeID LocalID) const;

/// Read a type from the current position in the given record, which
/// was read from the given AST file.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Serialization/ASTRecordReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class ASTRecordReader
void readTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);

/// Map a local type ID within a given AST file to a global type ID.
serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
serialization::TypeID getGlobalTypeID(serialization::TypeID LocalID) const {
return Reader->getGlobalTypeID(*F, LocalID);
}

Expand Down
3 changes: 0 additions & 3 deletions clang/include/clang/Serialization/ModuleFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,6 @@ class ModuleFile {
/// the global type ID space.
serialization::TypeID BaseTypeIndex = 0;

/// Remapping table for type IDs in this module.
ContinuousRangeMap<uint32_t, int, 2> TypeRemap;

// === Miscellaneous ===

/// Diagnostic IDs and their mappings that the user changed.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
break;
}

return TypeIdx(ID);
return TypeIdx(0, ID);
}

unsigned serialization::ComputeHash(Selector Sel) {
Expand Down
Loading
Loading