3b414a277c
Relands165c583d57[VM] Introduction of type testing stubs - Part 1 This CL: * Adds a field to [RawAbstractType] which will always hold a pointer to the entrypoint of a type testing stub * Makes this new field be initialized to a default stub whenever a instances are created (e.g. via Type::New(), snapshot reader, ...) * Makes the clustered snapshotter write a reference to the corresponding [RawInstructions] object when writing the field and do the reverse when reading it. * Makes us call the type testing stub for performing assert-assignable checks. To reduce unnecessary loads on callsites, we store the entrypoint of the type testing stubs directly in the type objects. This means that the caller of type testing stubs can simply branch there without populating a code object first. This also means that the type testing stubs themselves have no access to a pool and we therefore also don't hold on to the [Code] object, only the [Instruction] object is necessary. The type testing stubs do not setup a frame themselves and also have no safepoint. In the case when the type testing stubs could not determine a positive answer they will tail-call a general-purpose stub. The general-purpose stub sets up a stub frame, tries to consult a [SubtypeTestCache] and bails out to runtime if this was unsuccessful. This CL is just the the first, for ease of reviewing. The actual type-specialized type testing stubs will be generated in later CLs. Reviewed-on: https://dart-review.googlesource.com/44787 Relandsf226c22424[VM] Introduction of type testing stubs - Part 2 This CL starts building type testing stubs specialzed for [Type] objects we test against. More specifically, it adds support for: * Handling obvious fast cases on the call sites (while still having a call to stub for negative case) * Handling type tests against type parameters, by loading the value of the type parameter on the call sites and invoking it's type testing stub. * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subtype-checks. ==> e.g. String/List<dynamic> * Specialzed type testing stubs for instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the type arguments. ==> e.g. Widget<State>, where we know [Widget] is only extended and not implemented. * Specialzed type testing stubs for certain non-instantiated types where we can do [CidRange]-based subclass-checks for the class and [CidRange]-based subtype-checks for the instantiated type arguments and cid based comparisons for type parameters. (Note that this fast-case migth result in some false-negatives!) ==> e.g. _HashMapEntry<K, V>, where we know [_HashMapEntry] is only extended and not implemented. This optimizes cases where the caller uses `new HashMap<A, B>()` and only uses `A` and `B` as key/values (and not subclasses of it). The false-negative can occur when subtypes of A or B are used. In such cases we fall back to the [SubtypeTestCache]-based imlementation. Reviewed-on: https://dart-review.googlesource.com/44788 Relands25f98bcc75[VM] Introduction of type testing stubs - Part 3 The changes include: * Make AssertAssignableInstr no longer have a call-summary, which helps methods with several parameter checks by not having to re-load/re-initialize type arguments registers * Lazily create SubtypeTestCaches: We already go to runtime to warm up the caches, so we now also create the caches on the first runtime call and patch the pool entries. * No longer load the destination name into a register: We only need the name when we throw an exception, so it is not on the hot path. Instead we let the runtime look at the call site, decoding a pool index from the instructions stream. The destination name will be available in the pool, at a consecutive index to the subtype cache. * Remove the fall-through to N=1 case for probing subtypeing tests, since those will always be handled by the optimized stubs. * Do not generate optimized stubs for FutureOr<T> (so far it just falled-through to TTS). We can make optimzed version of that later, but it requires special subtyping rules. * Local code quality improvement in the type-testing-stubs: Avoid extra jump at last case of cid-class-range checks. There are still a number of optimization opportunities we can do in future changes. Reviewed-on: https://dart-review.googlesource.com/46984 Relands2c52480ec8[VM] Introduction of type testing stubs - Part 4 In order to avoid generating type testing stubs for too many types in the system - and thereby potentially cause an increase in code size - this change introduces a smarter way to decide for which types we should generate optimized type testing stubs. The precompiler creates a [TypeUsageInfo] which we use to collect information. More specifically: a) We collect the destination types for all type checks we emit (we do this inside AssertAssignableInstr::EmitNativeCode). -> These are types we might want to generate optimized type testing stubs for. b) We collect type argument vectors used in instance creations (we do this inside AllocateObjectInstr::EmitNativeCode) and keep a set of of used type argument vectors for each class. After the precompiler has finished compiling normal code we scan the set of destination types collected in a) for uninstantiated types (or more specifically, type parameter types). We then propagate the type argument vectors used on object allocation sites, which were collected in b), in order to find out what kind of types are flowing into those type parameters. This allows us to extend the set of types which we test against, by adding the types that flow into type parameters. We use this final augmented set of destination types as a "filter" when making the decision whether to generate an optimized type testing stub for a given type. Reviewed-on: https://dart-review.googlesource.com/48640 Issue https://github.com/dart-lang/sdk/issues/32603 Closes https://github.com/dart-lang/sdk/issues/32852 Change-Id: Ib79fbe7f043aa88f32bddad62d7656c638914b44 Reviewed-on: https://dart-review.googlesource.com/50944 Commit-Queue: Martin Kustermann <kustermann@google.com> Reviewed-by: Régis Crelier <regis@google.com>
225 lines
10 KiB
C++
225 lines
10 KiB
C++
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#ifndef RUNTIME_VM_STUB_CODE_H_
|
|
#define RUNTIME_VM_STUB_CODE_H_
|
|
|
|
#include "vm/allocation.h"
|
|
#include "vm/compiler/assembler/assembler.h"
|
|
|
|
namespace dart {
|
|
|
|
// Forward declarations.
|
|
class Code;
|
|
class Isolate;
|
|
class ObjectPointerVisitor;
|
|
class RawCode;
|
|
class SnapshotReader;
|
|
class SnapshotWriter;
|
|
|
|
// List of stubs created in the VM isolate, these stubs are shared by different
|
|
// isolates running in this dart process.
|
|
#if !defined(TARGET_ARCH_DBC)
|
|
#define VM_STUB_CODE_LIST(V) \
|
|
V(GetCStackPointer) \
|
|
V(JumpToFrame) \
|
|
V(RunExceptionHandler) \
|
|
V(DeoptForRewind) \
|
|
V(UpdateStoreBuffer) \
|
|
V(PrintStopMessage) \
|
|
V(CallToRuntime) \
|
|
V(LazyCompile) \
|
|
V(CallBootstrapNative) \
|
|
V(CallNoScopeNative) \
|
|
V(CallAutoScopeNative) \
|
|
V(FixCallersTarget) \
|
|
V(CallStaticFunction) \
|
|
V(OptimizeFunction) \
|
|
V(InvokeDartCode) \
|
|
V(DebugStepCheck) \
|
|
V(UnlinkedCall) \
|
|
V(MonomorphicMiss) \
|
|
V(SingleTargetCall) \
|
|
V(ICCallThroughFunction) \
|
|
V(ICCallThroughCode) \
|
|
V(MegamorphicCall) \
|
|
V(FixAllocationStubTarget) \
|
|
V(Deoptimize) \
|
|
V(DeoptimizeLazyFromReturn) \
|
|
V(DeoptimizeLazyFromThrow) \
|
|
V(UnoptimizedIdenticalWithNumberCheck) \
|
|
V(OptimizedIdenticalWithNumberCheck) \
|
|
V(ICCallBreakpoint) \
|
|
V(RuntimeCallBreakpoint) \
|
|
V(AllocateArray) \
|
|
V(AllocateContext) \
|
|
V(OneArgCheckInlineCache) \
|
|
V(TwoArgsCheckInlineCache) \
|
|
V(SmiAddInlineCache) \
|
|
V(SmiSubInlineCache) \
|
|
V(SmiEqualInlineCache) \
|
|
V(OneArgOptimizedCheckInlineCache) \
|
|
V(TwoArgsOptimizedCheckInlineCache) \
|
|
V(ZeroArgsUnoptimizedStaticCall) \
|
|
V(OneArgUnoptimizedStaticCall) \
|
|
V(TwoArgsUnoptimizedStaticCall) \
|
|
V(Subtype1TestCache) \
|
|
V(Subtype2TestCache) \
|
|
V(Subtype4TestCache) \
|
|
V(DefaultTypeTest) \
|
|
V(TopTypeTypeTest) \
|
|
V(TypeRefTypeTest) \
|
|
V(UnreachableTypeTest) \
|
|
V(SlowTypeTest) \
|
|
V(CallClosureNoSuchMethod) \
|
|
V(FrameAwaitingMaterialization) \
|
|
V(AsynchronousGapMarker)
|
|
|
|
#else
|
|
#define VM_STUB_CODE_LIST(V) \
|
|
V(LazyCompile) \
|
|
V(OptimizeFunction) \
|
|
V(CallClosureNoSuchMethod) \
|
|
V(RunExceptionHandler) \
|
|
V(DeoptForRewind) \
|
|
V(FixCallersTarget) \
|
|
V(Deoptimize) \
|
|
V(DeoptimizeLazyFromReturn) \
|
|
V(DeoptimizeLazyFromThrow) \
|
|
V(DefaultTypeTest) \
|
|
V(TopTypeTypeTest) \
|
|
V(TypeRefTypeTest) \
|
|
V(UnreachableTypeTest) \
|
|
V(SlowTypeTest) \
|
|
V(FrameAwaitingMaterialization) \
|
|
V(AsynchronousGapMarker)
|
|
|
|
#endif // !defined(TARGET_ARCH_DBC)
|
|
|
|
// Is it permitted for the stubs above to refer to Object::null(), which is
|
|
// allocated in the VM isolate and shared across all isolates.
|
|
// However, in cases where a simple GC-safe placeholder is needed on the stack,
|
|
// using Smi 0 instead of Object::null() is slightly more efficient, since a Smi
|
|
// does not require relocation.
|
|
|
|
// class StubEntry is used to describe stub methods generated in dart to
|
|
// abstract out common code executed from generated dart code.
|
|
class StubEntry {
|
|
public:
|
|
explicit StubEntry(const Code& code);
|
|
~StubEntry() {}
|
|
|
|
const ExternalLabel& label() const { return label_; }
|
|
uword EntryPoint() const { return entry_point_; }
|
|
uword CheckedEntryPoint() const { return checked_entry_point_; }
|
|
RawCode* code() const { return code_; }
|
|
intptr_t Size() const { return size_; }
|
|
|
|
// Visit all object pointers.
|
|
void VisitObjectPointers(ObjectPointerVisitor* visitor);
|
|
|
|
private:
|
|
RawCode* code_;
|
|
uword entry_point_;
|
|
uword checked_entry_point_;
|
|
intptr_t size_;
|
|
ExternalLabel label_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(StubEntry);
|
|
};
|
|
|
|
// class StubCode is used to maintain the lifecycle of stubs.
|
|
class StubCode : public AllStatic {
|
|
public:
|
|
// Generate all stubs which are shared across all isolates, this is done
|
|
// only once and the stub code resides in the vm_isolate heap.
|
|
static void InitOnce();
|
|
|
|
static void VisitObjectPointers(ObjectPointerVisitor* visitor);
|
|
|
|
// Returns true if stub code has been initialized.
|
|
static bool HasBeenInitialized();
|
|
|
|
// Check if specified pc is in the dart invocation stub used for
|
|
// transitioning into dart code.
|
|
static bool InInvocationStub(uword pc);
|
|
|
|
// Check if the specified pc is in the jump to frame stub.
|
|
static bool InJumpToFrameStub(uword pc);
|
|
|
|
// Returns NULL if no stub found.
|
|
static const char* NameOfStub(uword entry_point);
|
|
|
|
// Define the shared stub code accessors.
|
|
#define STUB_CODE_ACCESSOR(name) \
|
|
static const StubEntry* name##_entry() { return entries_[k##name##Index]; } \
|
|
static intptr_t name##Size() { return name##_entry()->Size(); }
|
|
VM_STUB_CODE_LIST(STUB_CODE_ACCESSOR);
|
|
#undef STUB_CODE_ACCESSOR
|
|
|
|
static RawCode* GetAllocationStubForClass(const Class& cls);
|
|
|
|
static const StubEntry* UnoptimizedStaticCallEntry(intptr_t num_args_tested);
|
|
|
|
static const intptr_t kNoInstantiator = 0;
|
|
static const intptr_t kInstantiationSizeInWords = 3;
|
|
|
|
static StubEntry* EntryAt(intptr_t index) { return entries_[index]; }
|
|
static void EntryAtPut(intptr_t index, StubEntry* entry) {
|
|
entries_[index] = entry;
|
|
}
|
|
static intptr_t NumEntries() { return kNumStubEntries; }
|
|
|
|
private:
|
|
friend class MegamorphicCacheTable;
|
|
|
|
static const intptr_t kStubCodeSize = 4 * KB;
|
|
|
|
enum {
|
|
#define STUB_CODE_ENTRY(name) k##name##Index,
|
|
VM_STUB_CODE_LIST(STUB_CODE_ENTRY)
|
|
#undef STUB_CODE_ENTRY
|
|
kNumStubEntries
|
|
};
|
|
|
|
static StubEntry* entries_[kNumStubEntries];
|
|
|
|
#if !defined(DART_PRECOMPILED_RUNTIME)
|
|
#define STUB_CODE_GENERATE(name) \
|
|
static void Generate##name##Stub(Assembler* assembler);
|
|
VM_STUB_CODE_LIST(STUB_CODE_GENERATE)
|
|
#undef STUB_CODE_GENERATE
|
|
|
|
// Generate the stub and finalize the generated code into the stub
|
|
// code executable area.
|
|
static RawCode* Generate(const char* name,
|
|
void (*GenerateStub)(Assembler* assembler));
|
|
|
|
static void GenerateMegamorphicMissStub(Assembler* assembler);
|
|
static void GenerateAllocationStubForClass(Assembler* assembler,
|
|
const Class& cls);
|
|
static void GenerateNArgsCheckInlineCacheStub(
|
|
Assembler* assembler,
|
|
intptr_t num_args,
|
|
const RuntimeEntry& handle_ic_miss,
|
|
Token::Kind kind,
|
|
bool optimized = false);
|
|
static void GenerateUsageCounterIncrement(Assembler* assembler,
|
|
Register temp_reg);
|
|
static void GenerateOptimizedUsageCounterIncrement(Assembler* assembler);
|
|
#endif // !defined(DART_PRECOMPILED_RUNTIME)
|
|
};
|
|
|
|
enum DeoptStubKind { kLazyDeoptFromReturn, kLazyDeoptFromThrow, kEagerDeopt };
|
|
|
|
// Zap value used to indicate unused CODE_REG in deopt.
|
|
static const uword kZapCodeReg = 0xf1f1f1f1;
|
|
|
|
// Zap value used to indicate unused return address in deopt.
|
|
static const uword kZapReturnAddress = 0xe1e1e1e1;
|
|
|
|
} // namespace dart
|
|
|
|
#endif // RUNTIME_VM_STUB_CODE_H_
|