Skip to content

NFC: better CanType perf #16553

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 2 commits into from
May 11, 2018
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
47 changes: 31 additions & 16 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
TypeBase(const TypeBase&) = delete;
void operator=(const TypeBase&) = delete;

/// CanonicalType - This field is always set to the ASTContext for canonical
/// types, and is otherwise lazily populated by ASTContext when the canonical
/// form of a non-canonical type is requested.
llvm::PointerUnion<TypeBase *, const ASTContext *> CanonicalType;
/// This union contains to the ASTContext for canonical types, and is
/// otherwise lazily populated by ASTContext when the canonical form of a
/// non-canonical type is requested. The disposition of the union is stored
/// outside of the union for performance. See Bits.TypeBase.IsCanonical.
union {
CanType CanonicalType;
const ASTContext *Context;
};

/// Returns true if the given type is a sugared type.
///
Expand All @@ -275,11 +279,14 @@ class alignas(1 << TypeAlignInBits) TypeBase {
union { uint64_t OpaqueBits;

SWIFT_INLINE_BITFIELD_BASE(TypeBase, bitmax(NumTypeKindBits,8) +
RecursiveTypeProperties::BitWidth,
RecursiveTypeProperties::BitWidth + 1,
/// Kind - The discriminator that indicates what subclass of type this is.
Kind : bitmax(NumTypeKindBits,8),

Properties : RecursiveTypeProperties::BitWidth
Properties : RecursiveTypeProperties::BitWidth,

/// Whether this type is canonical or not.
IsCanonical : 1
);

SWIFT_INLINE_BITFIELD(ErrorType, TypeBase, 1,
Expand Down Expand Up @@ -384,12 +391,15 @@ class alignas(1 << TypeAlignInBits) TypeBase {
protected:
TypeBase(TypeKind kind, const ASTContext *CanTypeCtx,
RecursiveTypeProperties properties)
: CanonicalType((TypeBase*)nullptr) {
: Context(nullptr) {
Bits.OpaqueBits = 0;
Bits.TypeBase.Kind = static_cast<unsigned>(kind);
Bits.TypeBase.IsCanonical = false;
// If this type is canonical, switch the CanonicalType union to ASTContext.
if (CanTypeCtx)
CanonicalType = CanTypeCtx;
if (CanTypeCtx) {
Bits.TypeBase.IsCanonical = true;
Context = CanTypeCtx;
}
setRecursiveProperties(properties);
}

Expand All @@ -403,7 +413,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
TypeKind getKind() const { return static_cast<TypeKind>(Bits.TypeBase.Kind); }

/// isCanonical - Return true if this is a canonical type.
bool isCanonical() const { return CanonicalType.is<const ASTContext *>(); }
bool isCanonical() const { return Bits.TypeBase.IsCanonical; }

/// hasCanonicalTypeComputed - Return true if we've already computed a
/// canonical version of this type.
Expand All @@ -419,7 +429,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
if (isCanonical())
return CanType(const_cast<TypeBase*>(this));
if (hasCanonicalTypeComputed())
return CanType(CanonicalType.get<TypeBase*>());
return CanonicalType;
return const_cast<TypeBase*>(this)->computeCanonicalType();
}

Expand All @@ -434,11 +444,10 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// getASTContext - Return the ASTContext that this type belongs to.
ASTContext &getASTContext() {
// If this type is canonical, it has the ASTContext in it.
if (CanonicalType.is<const ASTContext *>())
return const_cast<ASTContext&>(*CanonicalType.get<const ASTContext *>());
if (isCanonical())
return *const_cast<ASTContext*>(Context);
// If not, canonicalize it to get the Context.
return const_cast<ASTContext&>(*getCanonicalType()->
CanonicalType.get<const ASTContext *>());
return *const_cast<ASTContext*>(getCanonicalType()->Context);
}

/// isEqual - Return true if these two types are equal, ignoring sugar.
Expand Down Expand Up @@ -5262,7 +5271,13 @@ inline CanType CanType::getWithoutSpecifierTypeImpl(CanType type) {
inline CanType CanType::getNominalParent() const {
return cast<NominalOrBoundGenericNominalType>(*this).getParent();
}


inline bool CanType::isActuallyCanonicalOrNull() const {
return getPointer() == nullptr ||
getPointer() == llvm::DenseMapInfo<TypeBase *>::getTombstoneKey() ||
getPointer()->isCanonical();
}

inline Type TupleTypeElt::getVarargBaseTy() const {
TypeBase *T = getType().getPointer();
if (auto *AT = dyn_cast<ArraySliceType>(T))
Expand Down
9 changes: 1 addition & 8 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ void *TypeBase::operator new(size_t bytes, const ASTContext &ctx,
return ctx.Allocate(bytes, alignment, arena);
}

bool CanType::isActuallyCanonicalOrNull() const {
return getPointer() == nullptr ||
getPointer() == llvm::DenseMapInfo<TypeBase *>::getTombstoneKey() ||
getPointer()->isCanonical();
}

NominalTypeDecl *CanType::getAnyNominal() const {
return dyn_cast_or_null<NominalTypeDecl>(getAnyGeneric());
}
Expand Down Expand Up @@ -1249,8 +1243,7 @@ CanType TypeBase::computeCanonicalType() {

// Cache the canonical type for future queries.
assert(Result && "Case not implemented!");
CanonicalType = Result;
return CanType(Result);
return CanonicalType = CanType(Result);
}

CanType TypeBase::getCanonicalType(GenericSignature *sig) {
Expand Down