mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Most of the work of this patch is just propagating metadata states throughout the system, especially local-type-data caching and metadata-path resolution. It took a few design revisions to get both DynamicMetadataRequest and MetadataResponse to a shape that felt right and seemed to make everything easier. The design is laid out pretty clearly (I hope) in the comments on DynamicMetadataRequest and MetadataResponse, so I'm not going to belabor it again here. Instead, I'll list out the work that's still outstanding: - I'm sure there are places we're asking for complete metadata where we could be asking for something weaker. - I need to actually test the runtime behavior to verify that it's breaking the cycles it's supposed to, instead of just not regressing anything else. - I need to add something to the runtime to actually force all the generic arguments of a generic type to be complete before reporting completion. I think we can get away with this for now because all existing types construct themselves completely on the first request, but there might be a race condition there if another asks for the type argument, gets an abstract metadata, and constructs a type with it without ever needing it to be completed. - Non-generic resilient types need to be switched over to an IRGen pattern that supports initialization suspension. - We should probably space out the MetadataStates so that there's some space between Abstract and Complete. - The runtime just calmly sits there, never making progress and permanently blocking any waiting threads, if you actually form an unresolvable metadata dependency cycle. It is possible to set up such a thing in a way that Sema can't diagnose, and we should detect it at runtime. I've set up some infrastructure so that it should be straightforward to diagnose this, but I haven't actually implemented the diagnostic yet. - It's not clear to me that swift_checkMetadataState is really cheap enough that it doesn't make sense to use a cache for type-fulfilled metadata in associated type access functions. Fortunately this is not ABI-affecting, so we can evaluate it anytime. - Type layout really seems like a lot of code now that we sometimes need to call swift_checkMetadataState for generic arguments. Maybe we can have the runtime do this by marking low bits or something, so that a TypeLayoutRef is actually either (1) a TypeLayout, (2) a known layout-complete metadata, or (3) a metadata of unknown state. We could do that later with a flag, but we'll need to at least future-proof by allowing the runtime functions to return a MetadataDependency.
166 lines
5.7 KiB
C++
166 lines
5.7 KiB
C++
//===--- Fulfillment.h - Deriving type/conformance metadata -----*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines interfaces for deriving type metadata and protocol
|
|
// witness tables from various sources.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_IRGEN_FULFILLMENT_H
|
|
#define SWIFT_IRGEN_FULFILLMENT_H
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
#include "swift/AST/Types.h"
|
|
#include "swift/AST/GenericSignature.h"
|
|
#include "MetadataPath.h"
|
|
|
|
namespace swift {
|
|
namespace irgen {
|
|
class IRGenModule;
|
|
enum IsExact_t : bool;
|
|
|
|
/// The metadata value can be fulfilled by following the given metadata
|
|
/// path from the given source.
|
|
struct Fulfillment {
|
|
Fulfillment() = default;
|
|
Fulfillment(unsigned sourceIndex, MetadataPath &&path, MetadataState state)
|
|
: SourceIndex(sourceIndex), State(unsigned(state)), Path(std::move(path)) {}
|
|
|
|
/// The source index.
|
|
unsigned SourceIndex : 30;
|
|
|
|
/// The state of the metadata at the fulfillment.
|
|
unsigned State : 2;
|
|
|
|
/// The path from the source metadata.
|
|
MetadataPath Path;
|
|
|
|
MetadataState getState() const { return MetadataState(State); }
|
|
};
|
|
|
|
class FulfillmentMap {
|
|
using FulfillmentKey = std::pair<Type, ProtocolDecl*>;
|
|
|
|
llvm::DenseMap<FulfillmentKey, Fulfillment> Fulfillments;
|
|
|
|
public:
|
|
struct InterestingKeysCallback {
|
|
/// Is the given type something that we should add fulfillments for?
|
|
virtual bool isInterestingType(CanType type) const = 0;
|
|
|
|
/// Is the given type expressed in terms of types that we should add
|
|
/// fulfillments for?
|
|
///
|
|
/// It's okay to conservatively return true here.
|
|
virtual bool hasInterestingType(CanType type) const = 0;
|
|
|
|
/// Are we only interested in a subset of the conformances for a
|
|
/// given type?
|
|
virtual bool hasLimitedInterestingConformances(CanType type) const = 0;
|
|
|
|
/// Return the limited interesting conformances for an interesting type.
|
|
virtual GenericSignature::ConformsToArray
|
|
getInterestingConformances(CanType type) const = 0;
|
|
|
|
/// Return the limited interesting conformances for an interesting type.
|
|
virtual CanType getSuperclassBound(CanType type) const = 0;
|
|
|
|
virtual ~InterestingKeysCallback() = default;
|
|
};
|
|
|
|
FulfillmentMap() = default;
|
|
|
|
using iterator = decltype(Fulfillments)::iterator;
|
|
iterator begin() { return Fulfillments.begin(); }
|
|
iterator end() { return Fulfillments.end(); }
|
|
|
|
/// Is it even theoretically possible that we might find a fulfillment
|
|
/// in the given type?
|
|
static bool isInterestingTypeForFulfillments(CanType type) {
|
|
// Some day, if we ever record fulfillments for concrete types, this
|
|
// optimization will probably no longer be useful.
|
|
return type->hasTypeParameter();
|
|
}
|
|
|
|
/// Search the given type metadata for useful fulfillments.
|
|
///
|
|
/// \return true if any fulfillments were added by this search.
|
|
bool searchTypeMetadata(IRGenModule &IGM, CanType type, IsExact_t isExact,
|
|
MetadataState metadataState,
|
|
unsigned sourceIndex, MetadataPath &&path,
|
|
const InterestingKeysCallback &interestingKeys);
|
|
|
|
bool searchConformance(IRGenModule &IGM,
|
|
const ProtocolConformance *conformance,
|
|
unsigned sourceIndex, MetadataPath &&path,
|
|
const InterestingKeysCallback &interestingKeys);
|
|
|
|
/// Search the given witness table for useful fulfillments.
|
|
///
|
|
/// \return true if any fulfillments were added by this search.
|
|
bool searchWitnessTable(IRGenModule &IGM, CanType type, ProtocolDecl *protocol,
|
|
unsigned sourceIndex, MetadataPath &&path,
|
|
const InterestingKeysCallback &interestingKeys);
|
|
|
|
/// Register a fulfillment for the given key.
|
|
///
|
|
/// \return true if the fulfillment was added, which won't happen if there's
|
|
/// already a fulfillment that was at least as good
|
|
bool addFulfillment(FulfillmentKey key, unsigned source,
|
|
MetadataPath &&path, MetadataState state);
|
|
|
|
const Fulfillment *getTypeMetadata(CanType type) const {
|
|
auto it = Fulfillments.find({type, nullptr});
|
|
if (it != Fulfillments.end()) {
|
|
return &it->second;
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
const Fulfillment *getWitnessTable(CanType type, ProtocolDecl *proto) const {
|
|
auto it = Fulfillments.find({type, proto});
|
|
if (it != Fulfillments.end()) {
|
|
return &it->second;
|
|
} else {
|
|
return nullptr;
|
|
}
|
|
}
|
|
|
|
void dump() const;
|
|
void print(llvm::raw_ostream &out) const;
|
|
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &out,
|
|
const FulfillmentMap &map) {
|
|
map.print(out);
|
|
return out;
|
|
}
|
|
|
|
private:
|
|
bool searchNominalTypeMetadata(IRGenModule &IGM, CanType type,
|
|
MetadataState metadataState, unsigned source,
|
|
MetadataPath &&path,
|
|
const InterestingKeysCallback &keys);
|
|
|
|
/// Search the given witness table for useful fulfillments.
|
|
///
|
|
/// \return true if any fulfillments were added by this search.
|
|
bool searchWitnessTable(
|
|
IRGenModule &IGM, CanType type, ProtocolDecl *protocol, unsigned source,
|
|
MetadataPath &&path, const InterestingKeysCallback &keys,
|
|
llvm::SmallPtrSetImpl<ProtocolDecl *> *interestingConformances);
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
#endif
|