Commit 386ff6e5 authored by Mike Stanton's avatar Mike Stanton Committed by Commit Bot

[TurboFan] Serializing context information

In TurboFan, context specialization is an optimization that tries to
either replace the load of a value from the context with a constant,
or if that can't be achieved, at least reduce the hops up the
context chain by starting a walk to the required depth from the
first constant context that it can reach.

Currently, this optimization is performed by looking into the
heap during a reducer pass. With fully concurrent TurboFan, we
need to instead gather information about contexts we may want
to perform this optimization on during serialization.

This CL adds functionality to the serializer to recognize and
model operations that affect the context register. We add to the
hinting structure already used by the serializer. There is
a new type of hint: a VirtualContext. This is a tuple consisting
of a handle to a Context, and a distance field that indicates how
far away in a to-be-realized chain this VirtualContext sits from
the context in the handle. For example:

bytecode stream:
...
CreateBlockContext
...

After a block context is created, the accumulator now contains
a VirtualContext Hint with a distance of 1 from any context hints
that we are keeping track of in the current context register.

More details in the design doc here:
https://docs.google.com/document/d/1Y0LKKCEenLWyAZTetoAIpKTZRCxaNdkYV8X1GaCax2A/edit?usp=sharing

Change-Id: I63732ebd106cc138fb1e9789d0676ece63e15d27
Bug: v8:7790
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1605941
Commit-Queue: Michael Stanton <mvstanton@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarMaya Lekova <mslekova@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62370}
parent 5de719d4
......@@ -144,9 +144,10 @@ Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) {
// Now walk up the concrete context chain for the remaining depth.
ContextRef concrete = maybe_concrete.value();
concrete.SerializeContextChain(); // TODO(neis): Remove later.
for (; depth > 0; --depth) {
concrete = concrete.previous();
concrete = concrete.previous(&depth);
if (depth > 0) {
TRACE_BROKER_MISSING(broker(), "previous value for context " << concrete);
return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth);
}
if (!access.immutable()) {
......@@ -157,8 +158,6 @@ Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) {
// This will hold the final value, if we can figure it out.
base::Optional<ObjectRef> maybe_value;
concrete.SerializeSlot(static_cast<int>(access.index()));
maybe_value = concrete.get(static_cast<int>(access.index()));
if (maybe_value.has_value() && !maybe_value->IsSmi()) {
// Even though the context slot is immutable, the context might have escaped
......@@ -174,6 +173,9 @@ Reduction JSContextSpecialization::ReduceJSLoadContext(Node* node) {
}
if (!maybe_value.has_value()) {
TRACE_BROKER_MISSING(broker(), "slot value " << access.index()
<< " for context "
<< concrete);
return SimplifyJSLoadContext(node, jsgraph()->Constant(concrete), depth);
}
......@@ -207,9 +209,10 @@ Reduction JSContextSpecialization::ReduceJSStoreContext(Node* node) {
// Now walk up the concrete context chain for the remaining depth.
ContextRef concrete = maybe_concrete.value();
concrete.SerializeContextChain(); // TODO(neis): Remove later.
for (; depth > 0; --depth) {
concrete = concrete.previous();
concrete = concrete.previous(&depth);
if (depth > 0) {
TRACE_BROKER_MISSING(broker(), "previous value for context " << concrete);
return SimplifyJSStoreContext(node, jsgraph()->Constant(concrete), depth);
}
return SimplifyJSStoreContext(node, jsgraph()->Constant(concrete), depth);
......
......@@ -531,7 +531,6 @@ class ContextData : public HeapObjectData {
void SerializeContextChain(JSHeapBroker* broker);
ContextData* previous() const {
CHECK(serialized_context_chain_);
return previous_;
}
......@@ -539,16 +538,17 @@ class ContextData : public HeapObjectData {
ObjectData* GetSlot(int index) {
auto search = slots_.find(index);
CHECK(search != slots_.end());
if (search != slots_.end()) {
return search->second;
}
return nullptr;
}
void SerializeScopeInfo(JSHeapBroker* broker);
ScopeInfoData* scope_info() const { return scope_info_; }
private:
ZoneMap<int, ObjectData*> slots_;
bool serialized_context_chain_ = false;
ContextData* previous_ = nullptr;
ScopeInfoData* scope_info_ = nullptr;
};
......@@ -558,15 +558,12 @@ ContextData::ContextData(JSHeapBroker* broker, ObjectData** storage,
: HeapObjectData(broker, storage, object), slots_(broker->zone()) {}
void ContextData::SerializeContextChain(JSHeapBroker* broker) {
if (serialized_context_chain_) return;
serialized_context_chain_ = true;
if (previous_ != nullptr) return;
TraceScope tracer(broker, this, "ContextData::SerializeContextChain");
Handle<Context> context = Handle<Context>::cast(object());
DCHECK_NULL(previous_);
// Context::previous DCHECK-fails when called on the native context.
if (!context->IsNativeContext()) {
TraceScope tracer(broker, this, "ContextData::SerializeContextChain");
previous_ = broker->GetOrCreateData(context->previous())->AsContext();
previous_->SerializeContextChain(broker);
}
......@@ -574,7 +571,7 @@ void ContextData::SerializeContextChain(JSHeapBroker* broker) {
void ContextData::SerializeSlot(JSHeapBroker* broker, int index) {
TraceScope tracer(broker, this, "ContextData::SerializeSlot");
TRACE(broker, "Serializing script context slot " << index);
TRACE(broker, "Serializing context slot " << index);
Handle<Context> context = Handle<Context>::cast(object());
CHECK(index >= 0 && index < context->length());
ObjectData* odata = broker->GetOrCreateData(context->get(index));
......@@ -1896,25 +1893,39 @@ bool ObjectRef::equals(const ObjectRef& other) const {
Isolate* ObjectRef::isolate() const { return broker()->isolate(); }
ContextRef ContextRef::previous() const {
ContextRef ContextRef::previous(size_t* depth) const {
DCHECK_NOT_NULL(depth);
DCHECK_GE(*depth, 0);
if (broker()->mode() == JSHeapBroker::kDisabled) {
AllowHandleAllocation handle_allocation;
AllowHandleDereference handle_dereference;
return ContextRef(broker(),
handle(object()->previous(), broker()->isolate()));
Context current = *object();
while (*depth != 0) {
current = current.previous();
(*depth)--;
}
return ContextRef(broker(), handle(current, broker()->isolate()));
}
ContextData* current = this->data()->AsContext();
while (*depth != 0 && current->previous() != nullptr) {
current = current->previous();
(*depth)--;
}
return ContextRef(broker(), data()->AsContext()->previous());
return ContextRef(broker(), current);
}
// Not needed for TypedLowering.
ObjectRef ContextRef::get(int index) const {
base::Optional<ObjectRef> ContextRef::get(int index) const {
if (broker()->mode() == JSHeapBroker::kDisabled) {
AllowHandleAllocation handle_allocation;
AllowHandleDereference handle_dereference;
Handle<Object> value(object()->get(index), broker()->isolate());
return ObjectRef(broker(), value);
}
return ObjectRef(broker(), data()->AsContext()->GetSlot(index));
ObjectData* optional_slot = data()->AsContext()->GetSlot(index);
if (optional_slot != nullptr) {
return ObjectRef(broker(), optional_slot);
}
return base::nullopt;
}
JSHeapBroker::JSHeapBroker(Isolate* isolate, Zone* broker_zone,
......@@ -2459,6 +2470,11 @@ bool AllocationSiteRef::IsFastLiteral() const {
return data()->AsAllocationSite()->IsFastLiteral();
}
void JSObjectRef::SerializeElements() {
CHECK_EQ(broker()->mode(), JSHeapBroker::kSerializing);
data()->AsJSObject()->SerializeElements(broker());
}
void JSObjectRef::EnsureElementsTenured() {
if (broker()->mode() == JSHeapBroker::kDisabled) {
AllowHandleAllocation allow_handle_allocation;
......@@ -2909,7 +2925,7 @@ MapRef NativeContextRef::GetFunctionMapFromIndex(int index) const {
DCHECK_GE(index, Context::FIRST_FUNCTION_MAP_INDEX);
DCHECK_LE(index, Context::LAST_FUNCTION_MAP_INDEX);
if (broker()->mode() == JSHeapBroker::kDisabled) {
return get(index).AsMap();
return get(index).value().AsMap();
}
return MapRef(broker(), data()->AsNativeContext()->function_maps().at(
index - Context::FIRST_FUNCTION_MAP_INDEX));
......
......@@ -229,6 +229,7 @@ class JSObjectRef : public HeapObjectRef {
ObjectRef RawFastPropertyAt(FieldIndex index) const;
FixedArrayBaseRef elements() const;
void SerializeElements();
void EnsureElementsTenured();
ElementsKind GetElementsKind() const;
......@@ -317,10 +318,14 @@ class ContextRef : public HeapObjectRef {
Handle<Context> object() const;
void SerializeContextChain();
ContextRef previous() const;
// {previous} decrements n by 1 for each previous link successfully
// followed. If depth != 0 on function return, then it only got
// partway to the desired depth.
ContextRef previous(size_t* depth) const;
void SerializeSlot(int index);
ObjectRef get(int index) const;
base::Optional<ObjectRef> get(int index) const;
// We only serialize the ScopeInfo if certain Promise
// builtins are called.
......
......@@ -31,17 +31,9 @@ class Zone;
namespace compiler {
#define CLEAR_ENVIRONMENT_LIST(V) \
V(CallRuntime) \
V(CallRuntimeForPair) \
V(CreateBlockContext) \
V(CreateEvalContext) \
V(CreateFunctionContext) \
V(Debugger) \
V(PopContext) \
V(PushContext) \
V(ResumeGenerator) \
V(StaContextSlot) \
V(StaCurrentContextSlot) \
V(SuspendGenerator) \
V(SwitchOnGeneratorState)
......@@ -60,6 +52,7 @@ namespace compiler {
V(BitwiseOrSmi) \
V(BitwiseXor) \
V(BitwiseXorSmi) \
V(CallRuntime) \
V(CloneObject) \
V(CreateArrayFromIterable) \
V(CreateArrayLiteral) \
......@@ -67,6 +60,7 @@ namespace compiler {
V(CreateEmptyObjectLiteral) \
V(CreateMappedArguments) \
V(CreateObjectLiteral) \
V(CreateRegExpLiteral) \
V(CreateRestParameter) \
V(CreateUnmappedArguments) \
V(Dec) \
......@@ -82,10 +76,8 @@ namespace compiler {
V(ForInStep) \
V(GetTemplateObject) \
V(Inc) \
V(LdaContextSlot) \
V(LdaCurrentContextSlot) \
V(LdaImmutableContextSlot) \
V(LdaImmutableCurrentContextSlot) \
V(LdaLookupSlot) \
V(LdaLookupSlotInsideTypeof) \
V(LogicalNot) \
V(Mod) \
V(ModSmi) \
......@@ -99,6 +91,7 @@ namespace compiler {
V(ShiftRightLogical) \
V(ShiftRightLogicalSmi) \
V(ShiftRightSmi) \
V(StaLookupSlot) \
V(Sub) \
V(SubSmi) \
V(TestEqual) \
......@@ -147,6 +140,7 @@ namespace compiler {
#define IGNORED_BYTECODE_LIST(V) \
V(CallNoFeedback) \
V(IncBlockCounter) \
V(LdaNamedPropertyNoFeedback) \
V(StackCheck) \
V(StaNamedPropertyNoFeedback) \
......@@ -161,6 +155,7 @@ namespace compiler {
#define SUPPORTED_BYTECODE_LIST(V) \
V(CallAnyReceiver) \
V(CallJSRuntime) \
V(CallProperty) \
V(CallProperty0) \
V(CallProperty1) \
......@@ -172,13 +167,25 @@ namespace compiler {
V(CallWithSpread) \
V(Construct) \
V(ConstructWithSpread) \
V(CreateBlockContext) \
V(CreateCatchContext) \
V(CreateClosure) \
V(CreateEvalContext) \
V(CreateFunctionContext) \
V(CreateWithContext) \
V(GetSuperConstructor) \
V(LdaConstant) \
V(LdaContextSlot) \
V(LdaCurrentContextSlot) \
V(LdaImmutableContextSlot) \
V(LdaImmutableCurrentContextSlot) \
V(LdaModuleVariable) \
V(LdaFalse) \
V(LdaGlobal) \
V(LdaGlobalInsideTypeof) \
V(LdaKeyedProperty) \
V(LdaLookupContextSlot) \
V(LdaLookupContextSlotInsideTypeof) \
V(LdaLookupGlobalSlot) \
V(LdaLookupGlobalSlotInsideTypeof) \
V(LdaNamedProperty) \
......@@ -190,10 +197,15 @@ namespace compiler {
V(LdaUndefined) \
V(LdaZero) \
V(Mov) \
V(PopContext) \
V(PushContext) \
V(Return) \
V(StaContextSlot) \
V(StaCurrentContextSlot) \
V(StaGlobal) \
V(StaInArrayLiteral) \
V(StaKeyedProperty) \
V(StaModuleVariable) \
V(StaNamedOwnProperty) \
V(StaNamedProperty) \
V(Star) \
......@@ -216,35 +228,23 @@ struct HandleComparator {
}
};
struct FunctionBlueprint {
Handle<SharedFunctionInfo> shared;
Handle<FeedbackVector> feedback_vector;
struct VirtualContext {
unsigned int distance;
Handle<Context> context;
bool operator<(const FunctionBlueprint& other) const {
// A feedback vector is never used for more than one SFI, so it can
// be used for strict ordering of blueprints.
DCHECK_IMPLIES(feedback_vector.equals(other.feedback_vector),
shared.equals(other.shared));
return HandleComparator<FeedbackVector>()(feedback_vector,
other.feedback_vector);
VirtualContext(unsigned int distance_in, Handle<Context> context_in)
: distance(distance_in), context(context_in) {
CHECK_GT(distance, 0);
}
bool operator<(const VirtualContext& other) const {
return HandleComparator<Context>()(context, other.context) &&
distance < other.distance;
}
};
class CompilationSubject {
public:
explicit CompilationSubject(FunctionBlueprint blueprint)
: blueprint_(blueprint) {}
CompilationSubject(Handle<JSFunction> closure, Isolate* isolate);
FunctionBlueprint blueprint() const { return blueprint_; }
MaybeHandle<JSFunction> closure() const { return closure_; }
private:
FunctionBlueprint blueprint_;
MaybeHandle<JSFunction> closure_;
};
class FunctionBlueprint;
using ConstantsSet = ZoneSet<Handle<Object>, HandleComparator<Object>>;
using VirtualContextsSet = ZoneSet<VirtualContext>;
using MapsSet = ZoneSet<Handle<Map>, HandleComparator<Map>>;
using BlueprintsSet = ZoneSet<FunctionBlueprint>;
......@@ -255,10 +255,12 @@ class Hints {
const ConstantsSet& constants() const;
const MapsSet& maps() const;
const BlueprintsSet& function_blueprints() const;
const VirtualContextsSet& virtual_contexts() const;
void AddConstant(Handle<Object> constant);
void AddMap(Handle<Map> map);
void AddFunctionBlueprint(FunctionBlueprint function_blueprint);
void AddVirtualContext(VirtualContext virtual_context);
void Add(const Hints& other);
......@@ -271,6 +273,7 @@ class Hints {
#endif
private:
VirtualContextsSet virtual_contexts_;
ConstantsSet constants_;
MapsSet maps_;
BlueprintsSet function_blueprints_;
......@@ -285,6 +288,50 @@ enum class SerializerForBackgroundCompilationFlag : uint8_t {
using SerializerForBackgroundCompilationFlags =
base::Flags<SerializerForBackgroundCompilationFlag>;
class FunctionBlueprint {
public:
FunctionBlueprint(Handle<JSFunction> function, Isolate* isolate, Zone* zone);
FunctionBlueprint(Handle<SharedFunctionInfo> shared,
Handle<FeedbackVector> feedback_vector,
const Hints& context_hints);
Handle<SharedFunctionInfo> shared() const { return shared_; }
Handle<FeedbackVector> feedback_vector() const { return feedback_vector_; }
const Hints& context_hints() const { return context_hints_; }
bool operator<(const FunctionBlueprint& other) const {
// A feedback vector is never used for more than one SFI, so it can
// be used for strict ordering of blueprints.
DCHECK_IMPLIES(feedback_vector_.equals(other.feedback_vector_),
shared_.equals(other.shared_));
return HandleComparator<FeedbackVector>()(feedback_vector_,
other.feedback_vector_);
}
private:
Handle<SharedFunctionInfo> shared_;
Handle<FeedbackVector> feedback_vector_;
Hints context_hints_;
};
class CompilationSubject {
public:
explicit CompilationSubject(FunctionBlueprint blueprint)
: blueprint_(blueprint) {}
// The zone parameter is to correctly initialize the blueprint,
// which contains zone-allocated context information.
CompilationSubject(Handle<JSFunction> closure, Isolate* isolate, Zone* zone);
const FunctionBlueprint& blueprint() const { return blueprint_; }
MaybeHandle<JSFunction> closure() const { return closure_; }
private:
FunctionBlueprint blueprint_;
MaybeHandle<JSFunction> closure_;
};
// The SerializerForBackgroundCompilation makes sure that the relevant function
// data such as bytecode, SharedFunctionInfo and FeedbackVector, used by later
// optimizations in the compiler, is copied to the heap broker.
......@@ -347,6 +394,25 @@ class SerializerForBackgroundCompilation {
base::Optional<NameRef> static_name);
void ProcessMapForNamedPropertyAccess(MapRef const& map, NameRef const& name);
void ProcessCreateContext();
enum ContextProcessingMode {
kIgnoreSlot,
kSerializeSlot,
kSerializeSlotAndAddToAccumulator
};
void ProcessContextAccess(const Hints& context_hints, int slot, int depth,
ContextProcessingMode mode);
void ProcessImmutableLoad(ContextRef& context, int slot,
ContextProcessingMode mode);
void ProcessLdaLookupGlobalSlot(interpreter::BytecodeArrayIterator* iterator);
void ProcessLdaLookupContextSlot(
interpreter::BytecodeArrayIterator* iterator);
// Performs extension lookups for [0, depth) like
// BytecodeGraphBuilder::CheckContextExtensions().
void ProcessCheckContextExtensions(int depth);
Hints RunChildSerializer(CompilationSubject function,
base::Optional<Hints> new_target,
const HintsVector& arguments, bool with_spread);
......
......@@ -264,7 +264,7 @@ TEST(SerializeUnconditionalJump) {
"function f() {"
" function p() {};"
" function q() {};"
" if (a) g(q);"
" if (a) q();"
" else g(p);"
" return p;"
"};"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment