Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/decisions/runtime-region-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ operation. Neither a property name nor a receiver constructor establishes method
identity. Realm checks also protect delayed materialization, whose prototype must
belong to the original operation's realm.

RegExp exec projections retain the ordinary input conversion, lastIndex access
and legacy match state. When no capture is observed, the call is the sole region
anchor and matching can omit the result Array. A null comparison still observes
the same success or failure; escaping match objects retain the original call.

Constructor capacity hints reserve storage while property presence follows the
original stores. Ordinary constructors retain inherited setters, callbacks and
returned-object behavior; publishing a complete initial shape requires a separate
initialization proof.

Exact callback contexts carry the callee, creation environment, canonical compiled
entry, and stable function index. Runtime image growth can replace the function
table, so contexts resolve rows through that index. Dynamically scoped contexts
Expand Down
37 changes: 37 additions & 0 deletions runtime/src/builtin_regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3814,4 +3814,41 @@ int mal_regexp_try_exact_iterator_step(

#endif // MAL_REGEXP

MalValue mal_regexp_exec_result_projection(
MalVm *vm, MalValue callee, MalValue regexp, MalValue string
) {
MalValue args[1] = {string};
#if MAL_REGEXP
// The captured exec only observes the RegExp brand and lastIndex; unrelated
// prototype mutations do not change its authority or matching algorithm.
if (mal_value_is_native_function_object(callee) &&
mal_native_function_object_callback(mal_value_to_native_function_object(callee)) == regexp_proto_exec &&
mal_value_is_regexp_object(regexp)
#if MAL_REALMS
&& mal_vm_callee_realm(vm, callee) == vm->current_realm
#endif
) {
MalCalleeRoots roots;
mal_gc_callee_roots_begin(
&roots, regexp, mal_value_new_undefined(), callee, args, 1);
vm->gc_native_frames++;
MalValue result = mal_value_new_undefined();
MalString *subject;
if (mal_vm_to_string(vm, args[0], &subject)) {
args[0] = mal_value_from_string(subject);
result = regexp_builtin_exec(
vm, mal_value_to_regexp_object(regexp), regexp, subject,
REGEXP_BUILTIN_EXEC_MATCH_ONLY, false, nullptr);
if (!regexp_threw(vm) && mal_value_is_boolean(result)) {
MAL_PERF_COUNT(regexp_exec_result_elisions);
}
}
vm->gc_native_frames--;
mal_gc_callee_roots_end(&roots);
return result;
}
#endif
return mal_vm_call_cached_value(vm, callee, regexp, args, 1);
}

#include "generated/known_native_builtin_regexp_c.inc"
5 changes: 5 additions & 0 deletions runtime/src/builtin_regexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ bool mal_regexp_exec_capture_projection(
MalValue *result_out
);

/** Preserve the captured call while projecting a result observed only as null. */
MalValue mal_regexp_exec_result_projection(
MalVm *vm, MalValue callee, MalValue regexp, MalValue string
);

/**
* Locked-world form for a fresh unaliased RegExp literal. Its property lookup,
* callback identity, and Realm are compiler-proven; arbitrary input coercion is
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/perf_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ static void mal_perf_stats_print(void) {
stderr,
"[perf-regexp-stats] exec_calls=%llu fast_exec_calls=%llu ascii_exec_calls=%llu "
"ascii_cache_hits=%llu ascii_cache_fills=%llu utf16_exec_calls=%llu "
"utf16_cache_hits=%llu utf16_cache_fills=%llu latin1_exec_calls=%llu\n",
"utf16_cache_hits=%llu utf16_cache_fills=%llu latin1_exec_calls=%llu result_elisions=%llu\n",
(unsigned long long) mal_perf_stats.regexp_exec_calls,
(unsigned long long) mal_perf_stats.regexp_fast_exec_calls,
(unsigned long long) mal_perf_stats.regexp_ascii_exec_calls,
Expand All @@ -618,7 +618,8 @@ static void mal_perf_stats_print(void) {
(unsigned long long) mal_perf_stats.regexp_utf16_exec_calls,
(unsigned long long) mal_perf_stats.regexp_utf16_cache_hits,
(unsigned long long) mal_perf_stats.regexp_utf16_cache_fills,
(unsigned long long) mal_perf_stats.regexp_latin1_exec_calls
(unsigned long long) mal_perf_stats.regexp_latin1_exec_calls,
(unsigned long long) mal_perf_stats.regexp_exec_result_elisions
);
fprintf(
stderr,
Expand Down
1 change: 1 addition & 0 deletions runtime/src/perf_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ typedef struct MalPerfStats {
u64 regexp_utf16_cache_hits;
u64 regexp_utf16_cache_fills;
u64 regexp_latin1_exec_calls;
u64 regexp_exec_result_elisions;

u64 intrinsic_ascii_calls;
u64 intrinsic_ascii_bytes;
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/vm_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

#define WIRE_MAGIC 0x574c414du // "MALW" little-endian
#define WIRE_VERSION 85u
#define WIRE_VERSION 86u
#define WIRE_FLAG_HAS_DEBUG 1u

typedef enum WireOp {
Expand Down Expand Up @@ -1867,7 +1867,8 @@ static void rd_function(MalLoadedRuntimeImage *L, Rd *r, MalFunction *fn, bool d
fn->captured_count < 0 || fn->mapped_argument_count > fn->parameter_count ||
fn->constructor_slot_reserve > MAL_SHAPE_MAX_INLINE_SLOTS ||
(fn->constructor_slot_reserve != 0 &&
(!fn->is_class_constructor || fn->is_derived_constructor)) ||
(fn->is_derived_constructor || fn->kind != MAL_FUNCTION_KIND_NORMAL ||
(!fn->is_class_constructor && !fn->has_prototype))) ||
(!fn->mapped_arguments && fn->mapped_argument_count != 0) ||
(fn->mapped_arguments && fn->strict)) {
r->ok = false;
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/core/core-constructor-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ function rootDefinition(

/** Capacity only: these properties are not considered present until their stores execute. */
export function coreConstructorSlotReserve(fn: CoreFunctionStore): number {
if (!fn.metadata.isClassConstructor || fn.metadata.isDerivedConstructor) return 0;
if (
fn.metadata.isDerivedConstructor ||
fn.isGenerator ||
fn.isAsync ||
(!fn.metadata.isClassConstructor && !fn.metadata.hasPrototype)
)
return 0;
const keys = new Set<number>();
// Private elements share the instance's shape slots; each install names a distinct element.
let privateElements = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/core/core-ir-provenance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4430,7 +4430,7 @@ function regexpExecProjectionCandidates(
safe = false;
break;
}
if (!safe || loads.length === 0 || loads.length > 8) continue;
if (!safe || loads.length > 8) continue;

for (const load of loads) {
const capture = root(instructionResult(fn, load.instruction, 0)!);
Expand Down
15 changes: 12 additions & 3 deletions src/compiler/core/core-ir-region-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,14 @@ function pendingLocalCandidate(
closes === undefined
? instructions
: instructions.filter((instruction) => !closes.includes(instruction));
const fusedExecResult =
candidate.kind === "regexp-exec-projection" && candidate.loads.length === 0;
const cost = costModel.forRegion(costed, {
genericTwins: 1,
genericTwins: fusedExecResult ? 0 : 1,
...(candidate.kind === "stack-object" ||
candidate.kind === "string-split-projection" ||
candidate.kind === "string-split-cursor" ||
candidate.kind === "regexp-exec-projection" ||
(candidate.kind === "regexp-exec-projection" && !fusedExecResult) ||
candidate.kind === "regexp-iterator-projection" ||
candidate.kind === "iterator-result-virtualization" ||
candidate.kind === "iterator-entry-pair-virtualization"
Expand Down Expand Up @@ -631,7 +633,12 @@ function pendingLocalCandidate(
candidate.consumers[0]!.instruction,
])
: candidate.kind === "regexp-exec-projection"
? Object.freeze([candidate.call, candidate.loads[0]!.instruction])
? Object.freeze([
candidate.call,
...candidate.loads
.slice(0, 1)
.map((load) => load.instruction),
])
: candidate.kind === "regexp-iterator-projection"
? Object.freeze([
candidate.step,
Expand Down Expand Up @@ -1087,7 +1094,9 @@ function pendingLocalCandidate(
),
generatedCodeCost: cost.estimatedCStatements,
compilerWorkCost: cost.compileScore,
// Empty exec projections keep their fallback inside one runtime call.
expansive:
!fusedExecResult &&
candidate.kind !== "builtin-collection-call-chain" &&
candidate.kind !== "string-char-code-at-chain" &&
candidate.kind !== "iterator-entry-pair-virtualization",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/core/core-ir-region-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ function verifySpecialization(
});
if (
candidate?.kind !== "regexp-exec-projection" ||
selection.anchors.length !== 2 ||
selection.anchors.length !== (candidate.loads.length === 0 ? 1 : 2) ||
selection.anchors[0] !== candidate.call ||
selection.anchors[1] !== candidate.loads[0]?.instruction ||
regexp.property !== candidate.property ||
Expand Down
9 changes: 5 additions & 4 deletions src/compiler/core/core-ir-regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,10 +833,11 @@ export interface CoreAllocatedRegExpExecProjectionRegion extends CoreAllocatedRe
"regexp-exec-projection",
"regexp-capture-spans",
"whole-region",
readonly [
Extract<CompilerInstruction, { type: "call" | "callKnown" }>,
Extract<CompilerInstruction, { type: "loadProperty" }>,
]
| readonly [Extract<CompilerInstruction, { type: "call" | "callKnown" }>]
| readonly [
Extract<CompilerInstruction, { type: "call" | "callKnown" }>,
Extract<CompilerInstruction, { type: "loadProperty" }>,
]
> {
readonly property?: Extract<CompilerInstruction, { type: "loadPropertyStatic" }>;
readonly propertyPlacement: CorePropertyPlacement;
Expand Down
5 changes: 2 additions & 3 deletions src/compiler/target/compiler-artifact-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ import type {
/** Host-compiler cache format. This metadata never reaches the VM loader. */
export const COMPILER_ARTIFACT_MAGIC = 0x434c414d; // "MALC" little-endian
// Internal artifacts are hard cut-overs: stale cache entries rebuild.
export const COMPILER_ARTIFACT_VERSION = 371;
export const COMPILER_ARTIFACT_VERSION = 373;

/** Direct entries of C library bindings, as `binding-unit.ts` names them. */
const FOREIGN_CALL_SYMBOL = /^mal_ffi_direct\d+_\w+$/;
Expand Down Expand Up @@ -4174,7 +4174,7 @@ function validateRegExpExecProjectionRegion(
let valid =
region.representation === "regexp-capture-spans" &&
region.lastIndexEffect === "retained-call-twin" &&
region.anchors.length === 2 &&
region.anchors.length === (region.loads.length === 0 ? 1 : 2) &&
region.anchors[0] === region.callIp &&
region.anchors[1] === region.loads[0]?.ip &&
(call?.opcode === "CALL" || call?.opcode === "CALL_KNOWN") &&
Expand Down Expand Up @@ -4213,7 +4213,6 @@ function validateRegExpExecProjectionRegion(
(register) =>
Number.isInteger(register) && register >= 0 && register < fn.registerCount,
) &&
region.loads.length > 0 &&
region.loads.length <= 8;
for (const check of region.nullChecks) {
const comparison = fn.instructions[check.comparisonIp];
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/target/lower-core-target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ function lowerCoreSpecializations(
});
regions.push({
...envelope(row),
anchors: [call, loads[0]!.instruction],
anchors: loads.length === 0 ? [call] : [call, loads[0]!.instruction],
kind: "regexp-exec-projection",
license: {
guard: regexp.guard,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/target/program-image-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import type {

export const WIRE_MAGIC = 0x574c414d; // "MALW" little-endian
// Runtime wires are hard cut-overs: stale cached buffers must rebuild.
export const WIRE_VERSION = 85;
export const WIRE_VERSION = 86;

/** Canonical wire tags generated into the C runtime from the same registry. */
export const WIRE_OPCODES = BYTECODE_OPERATIONS;
Expand Down
3 changes: 1 addition & 2 deletions src/compiler/target/program-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4698,7 +4698,7 @@ function lowerExecutionFunctionToNativePlan(
!guard.obligations.includes("fallback") ||
!guard.obligations.includes("materialize") ||
region.lastIndexEffect !== "retained-call-twin" ||
resolvedAnchors.length !== 2 ||
resolvedAnchors.length !== (loads.length === 0 ? 1 : 2) ||
region.resultRegisters.length === 0
) {
throw coreRegionError(region.kind, "projection metadata");
Expand Down Expand Up @@ -4928,7 +4928,6 @@ function lowerExecutionFunctionToNativePlan(
(register) =>
!Number.isInteger(register) || register < 0 || register >= fn.registerCount,
) ||
resolvedLoads.length === 0 ||
resolvedLoads.length > 8 ||
new Set(resolvedLoads.map((load) => load.captureIndex)).size !==
resolvedLoads.length ||
Expand Down
28 changes: 26 additions & 2 deletions src/compiler/target/render-native-c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3889,7 +3889,7 @@ function emitCompiledVariant(
0,
),
});
nextStackSlot += loads.length + 1;
if (loads.length > 0) nextStackSlot += loads.length + 1;
}
const regexpIteratorProjectionSites = new Map<
number,
Expand Down Expand Up @@ -7039,8 +7039,10 @@ function analyzeNativeRegionTopology(
(left, right) => left.captureIndex - right.captureIndex,
);
if (
loads.length === 0 ||
loads.length > 8 ||
projection.anchors.length !== (loads.length === 0 ? 1 : 2) ||
projection.anchors[0] !== projection.callIp ||
projection.anchors[1] !== projection.loads[0]?.ip ||
seenExecCalls.has(projection.callIp) ||
loads.some(
(load, index) =>
Expand Down Expand Up @@ -7938,9 +7940,12 @@ function emitBody(
);
}
for (const site of regexpExecProjectionSites.values()) {
if (site.loads.length === 0) continue;
lines.push(
`bool __regexp_exec_${site.projection.callIp}_fast = false;`,
`bool __regexp_exec_${site.projection.callIp}_projected = false;`,
);
lines.push(
`i32 __regexp_exec_${site.projection.callIp}_starts[${site.loads.length}];`,
`i32 __regexp_exec_${site.projection.callIp}_ends[${site.loads.length}];`,
);
Expand Down Expand Up @@ -12672,6 +12677,14 @@ class NativeInstructionEmission {
if (this.context.nativeRegExpExecProjectionAction?.role === "call") {
const { site } = this.context.nativeRegExpExecProjectionAction;
const projection = site.projection;
if (site.loads.length === 0) {
const callee = `mal_vm_load_primordial(vm, ${knownOperations()[knownOperationIndex(instruction.operation)!]!.node})`;
return [
`r${instruction.dst} = ${this.profileCall("regexp", `mal_regexp_exec_result_projection(vm, ${callee}, ${this.boxedOperand(instruction.thisValue)}, ${this.boxedOperand(instruction.arguments[0]!)})`)};`,
this.throwCheck(),
this.callPoll(),
];
}
const indices = site.loads.map((load) => load.captureIndex).join(", ");
const outputs = site.loads
.map((_load, index) => `&__gc_slots[${site.slotsOffset + index}]`)
Expand Down Expand Up @@ -13335,6 +13348,17 @@ class NativeInstructionEmission {
if (this.context.nativeRegExpExecProjectionAction?.role === "call") {
const { site, propertyLoad } = this.context.nativeRegExpExecProjectionAction;
const projection = site.projection;
if (site.loads.length === 0) {
const callee =
propertyLoad === undefined
? this.boxedOperand(instruction.callee)
: `mal_vm_load_primordial(vm, ${knownOperations()[knownOperationIndex("RegExp.prototype.exec")!]!.node})`;
return [
`r${instruction.dst} = ${this.profileCall("regexp", `mal_regexp_exec_result_projection(vm, ${callee}, ${this.boxedOperand(instruction.thisValue)}, ${this.boxedOperand(instruction.arguments[0]!)})`)};`,
this.throwCheck(),
this.callPoll(),
];
}
const fast = `__regexp_exec_${projection.callIp}_fast`;
const indices = site.loads.map((load) => load.captureIndex).join(", ");
const outputs = site.loads
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/target/runtime-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2174,7 +2174,10 @@ export function validateRuntimeImageMetadata(definition: RuntimeImage): void {
!isNonnegativeSafeInteger(fn.constructorSlotReserve) ||
fn.constructorSlotReserve > CORE_MAX_SHAPED_OBJECT_SLOTS ||
(fn.constructorSlotReserve !== 0 &&
(!fn.isClassConstructor || fn.isDerivedConstructor))
(fn.isDerivedConstructor ||
fn.isGenerator ||
fn.isAsync ||
(!fn.isClassConstructor && !fn.hasPrototype)))
)
throw new RangeError("invalid constructor slot reserve");
for (const instruction of fn.instructions) {
Expand Down
Loading
Loading