Skip to content

Commit 7857eb3

Browse files
devsnekV8 LUCI CQ
authored and
V8 LUCI CQ
committed
Reland^2 "Add ContinuationPreservedEmbedderData builtins to extras binding"
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 <snek@chromium.org> > > Reviewed-by: Darius Mercadier <dmercadier@chromium.org> > > Reviewed-by: Leszek Swirski <leszeks@chromium.org> > > Reviewed-by: Nico Hartmann <nicohartmann@chromium.org> > > 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 <syg@chromium.org> > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > 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 <dmercadier@chromium.org> Reviewed-by: Leszek Swirski <leszeks@chromium.org> Commit-Queue: snek <snek@chromium.org> Cr-Commit-Position: refs/heads/main@{#94866}
1 parent c755c09 commit 7857eb3

28 files changed

+637
-73
lines changed

src/builtins/base.tq

-1
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,6 @@ extern macro ChangeUint32ToWord(uint32): uintptr; // Doesn't sign-extend.
13171317
extern macro ChangeInt32ToInt64(int32): int64; // Sign-extends.
13181318
extern macro ChangeUint32ToUint64(uint32): uint64; // Doesn't sign-extend.
13191319
extern macro LoadNativeContext(Context): NativeContext;
1320-
extern macro GetContinuationPreservedEmbedderData(): Object;
13211320
extern macro TruncateFloat64ToFloat16(float64): float16;
13221321
extern macro TruncateFloat32ToFloat16(float32): float16;
13231322
extern macro TruncateFloat64ToFloat32(float64): float32;

src/builtins/promise-misc.tq

+23-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ extern macro PromiseBuiltinsAssembler::IsIsolatePromiseHookEnabled(uint32):
3030

3131
extern macro PromiseBuiltinsAssembler::PromiseHookFlags(): uint32;
3232

33+
namespace macros {
34+
extern macro GetContinuationPreservedEmbedderData(): Object;
35+
extern macro SetContinuationPreservedEmbedderData(Object): void;
36+
}
37+
3338
namespace promise {
3439
extern macro IsFunctionWithPrototypeSlotMap(Map): bool;
3540

@@ -78,7 +83,7 @@ macro NewPromiseFulfillReactionJobTask(
7883
return new PromiseFulfillReactionJobTask{
7984
map: PromiseFulfillReactionJobTaskMapConstant(),
8085
continuation_preserved_embedder_data:
81-
GetContinuationPreservedEmbedderData(),
86+
macros::GetContinuationPreservedEmbedderData(),
8287
argument,
8388
context: handlerContext,
8489
handler,
@@ -106,7 +111,7 @@ macro NewPromiseRejectReactionJobTask(
106111
return new PromiseRejectReactionJobTask{
107112
map: PromiseRejectReactionJobTaskMapConstant(),
108113
continuation_preserved_embedder_data:
109-
GetContinuationPreservedEmbedderData(),
114+
macros::GetContinuationPreservedEmbedderData(),
110115
argument,
111116
context: handlerContext,
112117
handler,
@@ -301,7 +306,7 @@ macro NewPromiseReaction(
301306
return new PromiseReaction{
302307
map: PromiseReactionMapConstant(),
303308
continuation_preserved_embedder_data:
304-
GetContinuationPreservedEmbedderData(),
309+
macros::GetContinuationPreservedEmbedderData(),
305310
next: next,
306311
reject_handler: rejectHandler,
307312
fulfill_handler: fulfillHandler,
@@ -345,7 +350,7 @@ macro NewPromiseResolveThenableJobTask(
345350
return new PromiseResolveThenableJobTask{
346351
map: PromiseResolveThenableJobTaskMapConstant(),
347352
continuation_preserved_embedder_data:
348-
GetContinuationPreservedEmbedderData(),
353+
macros::GetContinuationPreservedEmbedderData(),
349354
context: nativeContext,
350355
promise_to_resolve: promiseToResolve,
351356
thenable,
@@ -450,4 +455,18 @@ transitioning macro BranchIfAccessCheckFailed(
450455
}
451456
} label HasAccess {}
452457
}
458+
459+
@if(V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA)
460+
transitioning javascript builtin GetContinuationPreservedEmbedderData(
461+
js-implicit context: Context, receiver: JSAny)(): JSAny {
462+
return UnsafeCast<JSAny>(macros::GetContinuationPreservedEmbedderData());
463+
}
464+
465+
@if(V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA)
466+
transitioning javascript builtin SetContinuationPreservedEmbedderData(
467+
js-implicit context: Context, receiver: JSAny)(data: Object): Undefined {
468+
macros::SetContinuationPreservedEmbedderData(data);
469+
return Undefined;
470+
}
471+
453472
}

src/compiler/js-call-reducer.cc

+39
Original file line numberDiff line numberDiff line change
@@ -5189,6 +5189,12 @@ Reduction JSCallReducer::ReduceJSCall(Node* node,
51895189
case Builtin::kBigIntAsIntN:
51905190
case Builtin::kBigIntAsUintN:
51915191
return ReduceBigIntAsN(node, builtin);
5192+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
5193+
case Builtin::kGetContinuationPreservedEmbedderData:
5194+
return ReduceGetContinuationPreservedEmbedderData(node);
5195+
case Builtin::kSetContinuationPreservedEmbedderData:
5196+
return ReduceSetContinuationPreservedEmbedderData(node);
5197+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
51925198
default:
51935199
break;
51945200
}
@@ -8852,6 +8858,39 @@ Reduction JSCallReducer::ReduceJSCallMathMinMaxWithArrayLike(Node* node,
88528858
return ReplaceWithSubgraph(&a, subgraph);
88538859
}
88548860

8861+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
8862+
Reduction JSCallReducer::ReduceGetContinuationPreservedEmbedderData(
8863+
Node* node) {
8864+
JSCallNode n(node);
8865+
Effect effect = n.effect();
8866+
Control control = n.control();
8867+
8868+
Node* value = effect = graph()->NewNode(
8869+
simplified()->GetContinuationPreservedEmbedderData(), effect);
8870+
8871+
ReplaceWithValue(node, value, effect, control);
8872+
return Replace(node);
8873+
}
8874+
8875+
Reduction JSCallReducer::ReduceSetContinuationPreservedEmbedderData(
8876+
Node* node) {
8877+
JSCallNode n(node);
8878+
Effect effect = n.effect();
8879+
Control control = n.control();
8880+
8881+
if (n.ArgumentCount() == 0) return NoChange();
8882+
8883+
effect =
8884+
graph()->NewNode(simplified()->SetContinuationPreservedEmbedderData(),
8885+
n.Argument(0), effect);
8886+
8887+
Node* value = jsgraph()->UndefinedConstant();
8888+
8889+
ReplaceWithValue(node, value, effect, control);
8890+
return Replace(node);
8891+
}
8892+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
8893+
88558894
CompilationDependencies* JSCallReducer::dependencies() const {
88568895
return broker()->dependencies();
88578896
}

src/compiler/js-call-reducer.h

+5
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ class V8_EXPORT_PRIVATE JSCallReducer final : public AdvancedReducer {
239239
base::Optional<Reduction> TryReduceJSCallMathMinMaxWithArrayLike(Node* node);
240240
Reduction ReduceJSCallMathMinMaxWithArrayLike(Node* node, Builtin builtin);
241241

242+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
243+
Reduction ReduceGetContinuationPreservedEmbedderData(Node* node);
244+
Reduction ReduceSetContinuationPreservedEmbedderData(Node* node);
245+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
246+
242247
// The pendant to ReplaceWithValue when using GraphAssembler-based reductions.
243248
Reduction ReplaceWithSubgraph(JSCallReducerAssembler* gasm, Node* subgraph);
244249
std::pair<Node*, Node*> ReleaseEffectAndControlFromAssembler(

src/compiler/opcodes.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@
429429

430430
#define SIMPLIFIED_SPECULATIVE_NUMBER_UNOP_LIST(V) V(SpeculativeToNumber)
431431

432+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
433+
#define SIMPLIFIED_CPED_OP_LIST(V) \
434+
V(GetContinuationPreservedEmbedderData) \
435+
V(SetContinuationPreservedEmbedderData)
436+
#else
437+
#define SIMPLIFIED_CPED_OP_LIST(V)
438+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
439+
432440
#define SIMPLIFIED_OTHER_OP_LIST(V) \
433441
V(Allocate) \
434442
V(AllocateRaw) \
@@ -534,7 +542,8 @@
534542
V(TransitionElementsKind) \
535543
V(TypeOf) \
536544
V(Unsigned32Divide) \
537-
V(VerifyType)
545+
V(VerifyType) \
546+
SIMPLIFIED_CPED_OP_LIST(V)
538547

539548
#define SIMPLIFIED_SPECULATIVE_BIGINT_BINOP_LIST(V) \
540549
V(SpeculativeBigIntAdd) \

src/compiler/simplified-lowering.cc

+11
Original file line numberDiff line numberDiff line change
@@ -4652,6 +4652,17 @@ class RepresentationSelector {
46524652
SetOutput<T>(node, LoadRepresentationOf(node->op()).representation());
46534653
return;
46544654

4655+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4656+
case IrOpcode::kGetContinuationPreservedEmbedderData:
4657+
SetOutput<T>(node, MachineRepresentation::kTagged);
4658+
return;
4659+
4660+
case IrOpcode::kSetContinuationPreservedEmbedderData:
4661+
ProcessInput<T>(node, 0, UseInfo::AnyTagged());
4662+
SetOutput<T>(node, MachineRepresentation::kNone);
4663+
return;
4664+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4665+
46554666
default:
46564667
FATAL(
46574668
"Representation inference: unsupported opcode %i (%s), node #%i\n.",

src/compiler/simplified-operator.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,26 @@ struct SimplifiedOperatorGlobalCache final {
13411341
kSpeculativeToBigIntBigInt64Operator;
13421342
SpeculativeToBigIntOperator<BigIntOperationHint::kBigInt>
13431343
kSpeculativeToBigIntBigIntOperator;
1344+
1345+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
1346+
struct GetContinuationPreservedEmbedderDataOperator : public Operator {
1347+
GetContinuationPreservedEmbedderDataOperator()
1348+
: Operator(IrOpcode::kGetContinuationPreservedEmbedderData,
1349+
Operator::kNoThrow | Operator::kNoDeopt | Operator::kNoWrite,
1350+
"GetContinuationPreservedEmbedderData", 0, 1, 0, 1, 1, 0) {}
1351+
};
1352+
GetContinuationPreservedEmbedderDataOperator
1353+
kGetContinuationPreservedEmbedderData;
1354+
1355+
struct SetContinuationPreservedEmbedderDataOperator : public Operator {
1356+
SetContinuationPreservedEmbedderDataOperator()
1357+
: Operator(IrOpcode::kSetContinuationPreservedEmbedderData,
1358+
Operator::kNoThrow | Operator::kNoDeopt | Operator::kNoRead,
1359+
"SetContinuationPreservedEmbedderData", 1, 1, 0, 0, 1, 0) {}
1360+
};
1361+
SetContinuationPreservedEmbedderDataOperator
1362+
kSetContinuationPreservedEmbedderData;
1363+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
13441364
};
13451365

13461366
namespace {
@@ -2200,6 +2220,18 @@ const Operator* SimplifiedOperatorBuilder::StoreField(
22002220
2, 1, 1, 0, 1, 0, store_access);
22012221
}
22022222

2223+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
2224+
const Operator*
2225+
SimplifiedOperatorBuilder::GetContinuationPreservedEmbedderData() {
2226+
return &cache_.kGetContinuationPreservedEmbedderData;
2227+
}
2228+
2229+
const Operator*
2230+
SimplifiedOperatorBuilder::SetContinuationPreservedEmbedderData() {
2231+
return &cache_.kSetContinuationPreservedEmbedderData;
2232+
}
2233+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
2234+
22032235
const Operator* SimplifiedOperatorBuilder::LoadMessage() {
22042236
return zone()->New<Operator>(IrOpcode::kLoadMessage, Operator::kEliminatable,
22052237
"LoadMessage", 1, 1, 1, 1, 1, 0);

src/compiler/simplified-operator.h

+5
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,11 @@ class V8_EXPORT_PRIVATE SimplifiedOperatorBuilder final
12181218
const FastApiCallFunctionVector& c_candidate_functions,
12191219
FeedbackSource const& feedback, CallDescriptor* descriptor);
12201220

1221+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
1222+
const Operator* GetContinuationPreservedEmbedderData();
1223+
const Operator* SetContinuationPreservedEmbedderData();
1224+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
1225+
12211226
private:
12221227
Zone* zone() const { return zone_; }
12231228

src/compiler/turboshaft/assembler.h

+10
Original file line numberDiff line numberDiff line change
@@ -4477,6 +4477,16 @@ class TurboshaftAssemblerOpInterface
44774477
}
44784478
#endif // V8_ENABLE_WEBASSEMBLY
44794479

4480+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4481+
V<Object> GetContinuationPreservedEmbedderData() {
4482+
return ReduceIfReachableGetContinuationPreservedEmbedderData();
4483+
}
4484+
4485+
void SetContinuationPreservedEmbedderData(V<Object> data) {
4486+
ReduceIfReachableSetContinuationPreservedEmbedderData(data);
4487+
}
4488+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4489+
44804490
template <typename Rep>
44814491
V<Rep> resolve(const V<Rep>& v) {
44824492
return v;

src/compiler/turboshaft/graph-builder.cc

+8
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,14 @@ OpIndex GraphBuilder::Process(
23982398
kind);
23992399
}
24002400

2401+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
2402+
case IrOpcode::kGetContinuationPreservedEmbedderData:
2403+
return __ GetContinuationPreservedEmbedderData();
2404+
case IrOpcode::kSetContinuationPreservedEmbedderData:
2405+
__ SetContinuationPreservedEmbedderData(Map(node->InputAt(0)));
2406+
return OpIndex::Invalid();
2407+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
2408+
24012409
default:
24022410
std::cerr << "unsupported node type: " << *node->op() << "\n";
24032411
node->Print(std::cerr);

src/compiler/turboshaft/machine-lowering-reducer-inl.h

+19
Original file line numberDiff line numberDiff line change
@@ -3290,6 +3290,25 @@ class MachineLoweringReducer : public Next {
32903290
return string;
32913291
}
32923292

3293+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
3294+
V<Object> REDUCE(GetContinuationPreservedEmbedderData)() {
3295+
return __ Load(
3296+
__ ExternalConstant(
3297+
ExternalReference::continuation_preserved_embedder_data(isolate_)),
3298+
LoadOp::Kind::RawAligned(), MemoryRepresentation::TaggedPointer());
3299+
}
3300+
3301+
V<None> REDUCE(SetContinuationPreservedEmbedderData)(V<Object> data) {
3302+
__ Store(
3303+
__ ExternalConstant(
3304+
ExternalReference::continuation_preserved_embedder_data(isolate_)),
3305+
data, StoreOp::Kind::RawAligned(),
3306+
MemoryRepresentation::TaggedPointer(),
3307+
WriteBarrierKind::kNoWriteBarrier);
3308+
return V<None>::Invalid();
3309+
}
3310+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
3311+
32933312
private:
32943313
V<Word32> BuildUint32Mod(V<Word32> left, V<Word32> right) {
32953314
Label<Word32> done(this);

src/compiler/turboshaft/maglev-graph-building-phase.cc

+18
Original file line numberDiff line numberDiff line change
@@ -3452,6 +3452,24 @@ class GraphBuilder {
34523452
return maglev::ProcessResult::kContinue;
34533453
}
34543454

3455+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
3456+
maglev::ProcessResult Process(
3457+
maglev::GetContinuationPreservedEmbedderData* node,
3458+
const maglev::ProcessingState&) {
3459+
V<Object> data = __ GetContinuationPreservedEmbedderData();
3460+
SetMap(node, data);
3461+
return maglev::ProcessResult::kContinue;
3462+
}
3463+
3464+
maglev::ProcessResult Process(
3465+
maglev::SetContinuationPreservedEmbedderData* node,
3466+
const maglev::ProcessingState&) {
3467+
V<Object> data = Map(node->input(0));
3468+
__ SetContinuationPreservedEmbedderData(data);
3469+
return maglev::ProcessResult::kContinue;
3470+
}
3471+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
3472+
34553473
template <typename NodeT>
34563474
maglev::ProcessResult Process(NodeT* node,
34573475
const maglev::ProcessingState& state) {

src/compiler/turboshaft/operations.h

+50
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,19 @@ using Variable = SnapshotTable<OpIndex, VariableData>::Key;
203203
V(Switch) \
204204
V(Deoptimize)
205205

206+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
207+
#define TURBOSHAFT_CPED_OPERATION_LIST(V) \
208+
V(GetContinuationPreservedEmbedderData) \
209+
V(SetContinuationPreservedEmbedderData)
210+
#else
211+
#define TURBOSHAFT_CPED_OPERATION_LIST(V)
212+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
213+
206214
// These operations should be lowered to Machine operations during
207215
// MachineLoweringPhase.
208216
#define TURBOSHAFT_SIMPLIFIED_OPERATION_LIST(V) \
209217
TURBOSHAFT_INTL_OPERATION_LIST(V) \
218+
TURBOSHAFT_CPED_OPERATION_LIST(V) \
210219
V(ArgumentsLength) \
211220
V(BigIntBinop) \
212221
V(BigIntComparison) \
@@ -8600,6 +8609,47 @@ struct SetStackPointerOp : FixedArityOperationT<1, SetStackPointerOp> {
86008609

86018610
#endif // V8_ENABLE_WEBASSEMBLY
86028611

8612+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
8613+
struct GetContinuationPreservedEmbedderDataOp
8614+
: FixedArityOperationT<0, GetContinuationPreservedEmbedderDataOp> {
8615+
static constexpr OpEffects effects = OpEffects().CanReadOffHeapMemory();
8616+
8617+
base::Vector<const RegisterRepresentation> outputs_rep() const {
8618+
return RepVector<RegisterRepresentation::Tagged()>();
8619+
}
8620+
8621+
base::Vector<const MaybeRegisterRepresentation> inputs_rep(
8622+
ZoneVector<MaybeRegisterRepresentation>& storage) const {
8623+
return {};
8624+
}
8625+
8626+
GetContinuationPreservedEmbedderDataOp() : Base() {}
8627+
8628+
void Validate(const Graph& graph) const {}
8629+
8630+
auto options() const { return std::tuple{}; }
8631+
};
8632+
8633+
struct SetContinuationPreservedEmbedderDataOp
8634+
: FixedArityOperationT<1, SetContinuationPreservedEmbedderDataOp> {
8635+
static constexpr OpEffects effects = OpEffects().CanWriteOffHeapMemory();
8636+
8637+
base::Vector<const RegisterRepresentation> outputs_rep() const { return {}; }
8638+
8639+
base::Vector<const MaybeRegisterRepresentation> inputs_rep(
8640+
ZoneVector<MaybeRegisterRepresentation>& storage) const {
8641+
return MaybeRepVector<MaybeRegisterRepresentation::Tagged()>();
8642+
}
8643+
8644+
explicit SetContinuationPreservedEmbedderDataOp(V<Object> value)
8645+
: Base(value) {}
8646+
8647+
void Validate(const Graph& graph) const {}
8648+
8649+
auto options() const { return std::tuple{}; }
8650+
};
8651+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
8652+
86038653
#define OPERATION_EFFECTS_CASE(Name) Name##Op::EffectsIfStatic(),
86048654
static constexpr base::Optional<OpEffects>
86058655
kOperationEffectsTable[kNumberOfOpcodes] = {

0 commit comments

Comments
 (0)