Skip to content

Commit 5c1dee6

Browse files
devsnekV8 LUCI CQ
authored andcommitted
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}
1 parent 093d4e9 commit 5c1dee6

27 files changed

Lines changed: 644 additions & 73 deletions

src/builtins/base.tq

Lines changed: 0 additions & 1 deletion
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

Lines changed: 23 additions & 4 deletions
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

Lines changed: 39 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 10 additions & 1 deletion
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4650,6 +4650,17 @@ class RepresentationSelector {
46504650
SetOutput<T>(node, LoadRepresentationOf(node->op()).representation());
46514651
return;
46524652

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

src/compiler/simplified-operator.cc

Lines changed: 32 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4375,6 +4375,16 @@ class TurboshaftAssemblerOpInterface
43754375
}
43764376
#endif // V8_ENABLE_WEBASSEMBLY
43774377

4378+
#ifdef V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4379+
V<Object> GetContinuationPreservedEmbedderData() {
4380+
return ReduceIfReachableGetContinuationPreservedEmbedderData();
4381+
}
4382+
4383+
void SetContinuationPreservedEmbedderData(V<Object> data) {
4384+
ReduceIfReachableSetContinuationPreservedEmbedderData(data);
4385+
}
4386+
#endif // V8_ENABLE_CONTINUATION_PRESERVED_EMBEDDER_DATA
4387+
43784388
template <typename Rep>
43794389
V<Rep> resolve(const V<Rep>& v) {
43804390
return v;

src/compiler/turboshaft/graph-builder.cc

Lines changed: 8 additions & 0 deletions
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);

0 commit comments

Comments
 (0)