Skip to content

Commit

Permalink
Reland^2 "Add ContinuationPreservedEmbedderData builtins to extras bi…
Browse files Browse the repository at this point in the history
…nding"

This reverts commit cb1277e.

> Original change's description:
> > Add ContinuationPreservedEmbedderData builtins to extras binding
> >
> > Node.js and Deno wish to use CPED for AsyncLocalStorage and APM, which
> > needs a high performance implementation. These builtins allow JavaScript
> > to handle CPED performantly.
> >
> > Change-Id: I7577be80818524baa52791dfce57d442d7c0c933
> > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5638129
> > Commit-Queue: snek <[email protected]>
> > Reviewed-by: Darius Mercadier <[email protected]>
> > Reviewed-by: Leszek Swirski <[email protected]>
> > Reviewed-by: Nico Hartmann <[email protected]>
> > Cr-Commit-Position: refs/heads/main@{#94607}
>
> Change-Id: Ief390f0b99891c8de83b4c794180440f91cbaf1f
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5649024
> Auto-Submit: Shu-yu Guo <[email protected]>
> Bot-Commit: Rubber Stamper <[email protected]>
> Commit-Queue: Rubber Stamper <[email protected]>
> Cr-Commit-Position: refs/heads/main@{#94608}

Change-Id: I4943071ffe192084e83bfe3113cfe9c92ef31465
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/5677045
Reviewed-by: Darius Mercadier <[email protected]>
Reviewed-by: Leszek Swirski <[email protected]>
Commit-Queue: snek <[email protected]>
Cr-Commit-Position: refs/heads/main@{#94866}
  • Loading branch information
devsnek authored and V8 LUCI CQ committed Jul 5, 2024
1 parent c755c09 commit 7857eb3
Show file tree
Hide file tree
Showing 28 changed files with 637 additions and 73 deletions.
1 change: 0 additions & 1 deletion src/builtins/base.tq
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,6 @@ extern macro ChangeUint32ToWord(uint32): uintptr; // Doesn't sign-extend.
extern macro ChangeInt32ToInt64(int32): int64; // Sign-extends.
extern macro ChangeUint32ToUint64(uint32): uint64; // Doesn't sign-extend.
extern macro LoadNativeContext(Context): NativeContext;
extern macro GetContinuationPreservedEmbedderData(): Object;
extern macro TruncateFloat64ToFloat16(float64): float16;
extern macro TruncateFloat32ToFloat16(float32): float16;
extern macro TruncateFloat64ToFloat32(float64): float32;
Expand Down
27 changes: 23 additions & 4 deletions src/builtins/promise-misc.tq
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ extern macro PromiseBuiltinsAssembler::IsIsolatePromiseHookEnabled(uint32):

extern macro PromiseBuiltinsAssembler::PromiseHookFlags(): uint32;

namespace macros {
extern macro GetContinuationPreservedEmbedderData(): Object;
extern macro SetContinuationPreservedEmbedderData(Object): void;
}

namespace promise {
extern macro IsFunctionWithPrototypeSlotMap(Map): bool;

Expand Down Expand Up @@ -78,7 +83,7 @@ macro NewPromiseFulfillReactionJobTask(
return new PromiseFulfillReactionJobTask{
map: PromiseFulfillReactionJobTaskMapConstant(),
continuation_preserved_embedder_data:
GetContinuationPreservedEmbedderData(),
macros::GetContinuationPreservedEmbedderData(),
argument,
context: handlerContext,
handler,
Expand Down Expand Up @@ -106,7 +111,7 @@ macro NewPromiseRejectReactionJobTask(
return new PromiseRejectReactionJobTask{
map: PromiseRejectReactionJobTaskMapConstant(),
continuation_preserved_embedder_data:
GetContinuationPreservedEmbedderData(),
macros::GetContinuationPreservedEmbedderData(),
argument,
context: handlerContext,
handler,
Expand Down Expand Up @@ -301,7 +306,7 @@ macro NewPromiseReaction(
return new PromiseReaction{
map: PromiseReactionMapConstant(),
continuation_preserved_embedder_data:
GetContinuationPreservedEmbedderData(),
macros::GetContinuationPreservedEmbedderData(),
next: next,
reject_handler: rejectHandler,
fulfill_handler: fulfillHandler,
Expand Down Expand Up @@ -345,7 +350,7 @@ macro NewPromiseResolveThenableJobTask(
return new PromiseResolveThenableJobTask{
map: PromiseResolveThenableJobTaskMapConstant(),
continuation_preserved_embedder_data:
GetContinuationPreservedEmbedderData(),
macros::GetContinuationPreservedEmbedderData(),
context: nativeContext,
promise_to_resolve: promiseToResolve,
thenable,
Expand Down Expand Up @@ -450,4 +455,18 @@ transitioning macro BranchIfAccessCheckFailed(
}
} label HasAccess {}
}

@if(V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA)
transitioning javascript builtin GetContinuationPreservedEmbedderData(
js-implicit context: Context, receiver: JSAny)(): JSAny {
return UnsafeCast<JSAny>(macros::GetContinuationPreservedEmbedderData());
}

@if(V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA)
transitioning javascript builtin SetContinuationPreservedEmbedderData(
js-implicit context: Context, receiver: JSAny)(data: Object): Undefined {
macros::SetContinuationPreservedEmbedderData(data);
return Undefined;
}

}
39 changes: 39 additions & 0 deletions src/compiler/js-call-reducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5189,6 +5189,12 @@ Reduction JSCallReducer::ReduceJSCall(Node* node,
case Builtin::kBigIntAsIntN:
case Builtin::kBigIntAsUintN:
return ReduceBigIntAsN(node, builtin);
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
case Builtin::kGetContinuationPreservedEmbedderData:
return ReduceGetContinuationPreservedEmbedderData(node);
case Builtin::kSetContinuationPreservedEmbedderData:
return ReduceSetContinuationPreservedEmbedderData(node);
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
default:
break;
}
Expand Down Expand Up @@ -8852,6 +8858,39 @@ Reduction JSCallReducer::ReduceJSCallMathMinMaxWithArrayLike(Node* node,
return ReplaceWithSubgraph(&a, subgraph);
}

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
Reduction JSCallReducer::ReduceGetContinuationPreservedEmbedderData(
Node* node) {
JSCallNode n(node);
Effect effect = n.effect();
Control control = n.control();

Node* value = effect = graph()->NewNode(
simplified()->GetContinuationPreservedEmbedderData(), effect);

ReplaceWithValue(node, value, effect, control);
return Replace(node);
}

Reduction JSCallReducer::ReduceSetContinuationPreservedEmbedderData(
Node* node) {
JSCallNode n(node);
Effect effect = n.effect();
Control control = n.control();

if (n.ArgumentCount() == 0) return NoChange();

effect =
graph()->NewNode(simplified()->SetContinuationPreservedEmbedderData(),
n.Argument(0), effect);

Node* value = jsgraph()->UndefinedConstant();

ReplaceWithValue(node, value, effect, control);
return Replace(node);
}
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

CompilationDependencies* JSCallReducer::dependencies() const {
return broker()->dependencies();
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/js-call-reducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ class V8_EXPORT_PRIVATE JSCallReducer final : public AdvancedReducer {
base::Optional<Reduction> TryReduceJSCallMathMinMaxWithArrayLike(Node* node);
Reduction ReduceJSCallMathMinMaxWithArrayLike(Node* node, Builtin builtin);

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
Reduction ReduceGetContinuationPreservedEmbedderData(Node* node);
Reduction ReduceSetContinuationPreservedEmbedderData(Node* node);
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

// The pendant to ReplaceWithValue when using GraphAssembler-based reductions.
Reduction ReplaceWithSubgraph(JSCallReducerAssembler* gasm, Node* subgraph);
std::pair<Node*, Node*> ReleaseEffectAndControlFromAssembler(
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@

#define SIMPLIFIED_SPECULATIVE_NUMBER_UNOP_LIST(V) V(SpeculativeToNumber)

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
#define SIMPLIFIED_CPED_OP_LIST(V) \
V(GetContinuationPreservedEmbedderData) \
V(SetContinuationPreservedEmbedderData)
#else
#define SIMPLIFIED_CPED_OP_LIST(V)
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

#define SIMPLIFIED_OTHER_OP_LIST(V) \
V(Allocate) \
V(AllocateRaw) \
Expand Down Expand Up @@ -534,7 +542,8 @@
V(TransitionElementsKind) \
V(TypeOf) \
V(Unsigned32Divide) \
V(VerifyType)
V(VerifyType) \
SIMPLIFIED_CPED_OP_LIST(V)

#define SIMPLIFIED_SPECULATIVE_BIGINT_BINOP_LIST(V) \
V(SpeculativeBigIntAdd) \
Expand Down
11 changes: 11 additions & 0 deletions src/compiler/simplified-lowering.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4652,6 +4652,17 @@ class RepresentationSelector {
SetOutput<T>(node, LoadRepresentationOf(node->op()).representation());
return;

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
case IrOpcode::kGetContinuationPreservedEmbedderData:
SetOutput<T>(node, MachineRepresentation::kTagged);
return;

case IrOpcode::kSetContinuationPreservedEmbedderData:
ProcessInput<T>(node, 0, UseInfo::AnyTagged());
SetOutput<T>(node, MachineRepresentation::kNone);
return;
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

default:
FATAL(
"Representation inference: unsupported opcode %i (%s), node #%i\n.",
Expand Down
32 changes: 32 additions & 0 deletions src/compiler/simplified-operator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,26 @@ struct SimplifiedOperatorGlobalCache final {
kSpeculativeToBigIntBigInt64Operator;
SpeculativeToBigIntOperator<BigIntOperationHint::kBigInt>
kSpeculativeToBigIntBigIntOperator;

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
struct GetContinuationPreservedEmbedderDataOperator : public Operator {
GetContinuationPreservedEmbedderDataOperator()
: Operator(IrOpcode::kGetContinuationPreservedEmbedderData,
Operator::kNoThrow | Operator::kNoDeopt | Operator::kNoWrite,
"GetContinuationPreservedEmbedderData", 0, 1, 0, 1, 1, 0) {}
};
GetContinuationPreservedEmbedderDataOperator
kGetContinuationPreservedEmbedderData;

struct SetContinuationPreservedEmbedderDataOperator : public Operator {
SetContinuationPreservedEmbedderDataOperator()
: Operator(IrOpcode::kSetContinuationPreservedEmbedderData,
Operator::kNoThrow | Operator::kNoDeopt | Operator::kNoRead,
"SetContinuationPreservedEmbedderData", 1, 1, 0, 0, 1, 0) {}
};
SetContinuationPreservedEmbedderDataOperator
kSetContinuationPreservedEmbedderData;
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
};

namespace {
Expand Down Expand Up @@ -2200,6 +2220,18 @@ const Operator* SimplifiedOperatorBuilder::StoreField(
2, 1, 1, 0, 1, 0, store_access);
}

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
const Operator*
SimplifiedOperatorBuilder::GetContinuationPreservedEmbedderData() {
return &cache_.kGetContinuationPreservedEmbedderData;
}

const Operator*
SimplifiedOperatorBuilder::SetContinuationPreservedEmbedderData() {
return &cache_.kSetContinuationPreservedEmbedderData;
}
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

const Operator* SimplifiedOperatorBuilder::LoadMessage() {
return zone()->New<Operator>(IrOpcode::kLoadMessage, Operator::kEliminatable,
"LoadMessage", 1, 1, 1, 1, 1, 0);
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/simplified-operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,11 @@ class V8_EXPORT_PRIVATE SimplifiedOperatorBuilder final
const FastApiCallFunctionVector& c_candidate_functions,
FeedbackSource const& feedback, CallDescriptor* descriptor);

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
const Operator* GetContinuationPreservedEmbedderData();
const Operator* SetContinuationPreservedEmbedderData();
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

private:
Zone* zone() const { return zone_; }

Expand Down
10 changes: 10 additions & 0 deletions src/compiler/turboshaft/assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4477,6 +4477,16 @@ class TurboshaftAssemblerOpInterface
}
#endif // V8_ENABLE_WEBASSEMBLY

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
V<Object> GetContinuationPreservedEmbedderData() {
return ReduceIfReachableGetContinuationPreservedEmbedderData();
}

void SetContinuationPreservedEmbedderData(V<Object> data) {
ReduceIfReachableSetContinuationPreservedEmbedderData(data);
}
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

template <typename Rep>
V<Rep> resolve(const V<Rep>& v) {
return v;
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/turboshaft/graph-builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,14 @@ OpIndex GraphBuilder::Process(
kind);
}

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
case IrOpcode::kGetContinuationPreservedEmbedderData:
return __ GetContinuationPreservedEmbedderData();
case IrOpcode::kSetContinuationPreservedEmbedderData:
__ SetContinuationPreservedEmbedderData(Map(node->InputAt(0)));
return OpIndex::Invalid();
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

default:
std::cerr << "unsupported node type: " << *node->op() << "\n";
node->Print(std::cerr);
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/turboshaft/machine-lowering-reducer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3290,6 +3290,25 @@ class MachineLoweringReducer : public Next {
return string;
}

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
V<Object> REDUCE(GetContinuationPreservedEmbedderData)() {
return __ Load(
__ ExternalConstant(
ExternalReference::continuation_preserved_embedder_data(isolate_)),
LoadOp::Kind::RawAligned(), MemoryRepresentation::TaggedPointer());
}

V<None> REDUCE(SetContinuationPreservedEmbedderData)(V<Object> data) {
__ Store(
__ ExternalConstant(
ExternalReference::continuation_preserved_embedder_data(isolate_)),
data, StoreOp::Kind::RawAligned(),
MemoryRepresentation::TaggedPointer(),
WriteBarrierKind::kNoWriteBarrier);
return V<None>::Invalid();
}
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

private:
V<Word32> BuildUint32Mod(V<Word32> left, V<Word32> right) {
Label<Word32> done(this);
Expand Down
18 changes: 18 additions & 0 deletions src/compiler/turboshaft/maglev-graph-building-phase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3452,6 +3452,24 @@ class GraphBuilder {
return maglev::ProcessResult::kContinue;
}

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
maglev::ProcessResult Process(
maglev::GetContinuationPreservedEmbedderData* node,
const maglev::ProcessingState&) {
V<Object> data = __ GetContinuationPreservedEmbedderData();
SetMap(node, data);
return maglev::ProcessResult::kContinue;
}

maglev::ProcessResult Process(
maglev::SetContinuationPreservedEmbedderData* node,
const maglev::ProcessingState&) {
V<Object> data = Map(node->input(0));
__ SetContinuationPreservedEmbedderData(data);
return maglev::ProcessResult::kContinue;
}
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

template <typename NodeT>
maglev::ProcessResult Process(NodeT* node,
const maglev::ProcessingState& state) {
Expand Down
50 changes: 50 additions & 0 deletions src/compiler/turboshaft/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,19 @@ using Variable = SnapshotTable<OpIndex, VariableData>::Key;
V(Switch) \
V(Deoptimize)

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
#define TURBOSHAFT_CPED_OPERATION_LIST(V) \
V(GetContinuationPreservedEmbedderData) \
V(SetContinuationPreservedEmbedderData)
#else
#define TURBOSHAFT_CPED_OPERATION_LIST(V)
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

// These operations should be lowered to Machine operations during
// MachineLoweringPhase.
#define TURBOSHAFT_SIMPLIFIED_OPERATION_LIST(V) \
TURBOSHAFT_INTL_OPERATION_LIST(V) \
TURBOSHAFT_CPED_OPERATION_LIST(V) \
V(ArgumentsLength) \
V(BigIntBinop) \
V(BigIntComparison) \
Expand Down Expand Up @@ -8600,6 +8609,47 @@ struct SetStackPointerOp : FixedArityOperationT<1, SetStackPointerOp> {

#endif // V8_ENABLE_WEBASSEMBLY

#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
struct GetContinuationPreservedEmbedderDataOp
: FixedArityOperationT<0, GetContinuationPreservedEmbedderDataOp> {
static constexpr OpEffects effects = OpEffects().CanReadOffHeapMemory();

base::Vector<const RegisterRepresentation> outputs_rep() const {
return RepVector<RegisterRepresentation::Tagged()>();
}

base::Vector<const MaybeRegisterRepresentation> inputs_rep(
ZoneVector<MaybeRegisterRepresentation>& storage) const {
return {};
}

GetContinuationPreservedEmbedderDataOp() : Base() {}

void Validate(const Graph& graph) const {}

auto options() const { return std::tuple{}; }
};

struct SetContinuationPreservedEmbedderDataOp
: FixedArityOperationT<1, SetContinuationPreservedEmbedderDataOp> {
static constexpr OpEffects effects = OpEffects().CanWriteOffHeapMemory();

base::Vector<const RegisterRepresentation> outputs_rep() const { return {}; }

base::Vector<const MaybeRegisterRepresentation> inputs_rep(
ZoneVector<MaybeRegisterRepresentation>& storage) const {
return MaybeRepVector<MaybeRegisterRepresentation::Tagged()>();
}

explicit SetContinuationPreservedEmbedderDataOp(V<Object> value)
: Base(value) {}

void Validate(const Graph& graph) const {}

auto options() const { return std::tuple{}; }
};
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA

#define OPERATION_EFFECTS_CASE(Name) Name##Op::EffectsIfStatic(),
static constexpr base::Optional<OpEffects>
kOperationEffectsTable[kNumberOfOpcodes] = {
Expand Down
Loading

0 comments on commit 7857eb3

Please sign in to comment.