diff --git a/TODO.md b/TODO.md index 87015e8e7..8a630526c 100644 --- a/TODO.md +++ b/TODO.md @@ -700,11 +700,6 @@ bounded and consumer-driven as planned below. marking are now young and reclaimed at remark, so a long cycle costs memory held until then rather than promoted garbage; find what bounds progress per step. -- [ ] Speculate int32 loop state that enters as a double. A direct entry with a - Number parameter keeps `seed = (Math.imul(seed, a) + b) | 0` in a double - phi, so every iteration converts to int32 and back: `serial-compute` runs - 995 ms against Node's 204. Versioning the loop on an int32 entry value would - keep the chain in integers; a boxed seed already decodes int-first. - [ ] Keep int32 elements integral in dense-array loop regions. The region types every element as a double, so `(sum + values[i]) | 0` over small integers costs 5 ns per element against Node's 0.5. Restarting into the generic loop @@ -1166,10 +1161,9 @@ only reproducible input. opaque callee that returns its strict `arguments` costs 340 ns per call against Node's 6.6 (`call-arguments-escape`). Dense storage needs define, delete, freeze, and own-key order to understand it, and a move to the table once an - index gets non-default attributes. In V8's EarleyBoyer, creating arguments - objects takes 19% of the main thread's samples: its `var a = arguments` - loops read `a[i]` with a dynamic index, which stays virtual only under - locked primordials, and the suite needs mutable ones. + index gets non-default attributes. Unmapped in-range numeric reads can use + the incoming slice under either primordial policy; escaping or materialized + arguments objects still need dense indexed storage. - [ ] Deterministically release nonescaping RegExp and ICU handles at compiled scope end once ownership analysis proves their lifetime. Retain finalization for every diff --git a/docs/decisions/runtime-region-contracts.md b/docs/decisions/runtime-region-contracts.md index 5ba2eacec..6d6e64c5e 100644 --- a/docs/decisions/runtime-region-contracts.md +++ b/docs/decisions/runtime-region-contracts.md @@ -103,6 +103,13 @@ the same materialization contract. The cached heap object becomes authoritative before invoking either observer, including when the builtin changes descriptors or returns the same object. Callee and receiver positions require separate proof. +An unmapped arguments binding can read an in-range numeric index from its incoming +slice while the binding remains unmaterialized. The invocation retains and roots +the entire slice, including extra arguments, through its last read. Other keys and +materialized bindings use the arguments object's property semantics. This read +belongs to its activation, so transformations cannot move it into another function +or use an entry that retains only declared parameters. + Fixed TypedArray regions preserve memory-operation order for overlapping views. Read-only reductions retain the same storage guards without requiring writable buffers. Every view contributes its complete visited index window, including all @@ -123,6 +130,18 @@ store. Collecting initializers retain the owner as an ordinary rooted value and use per-store barriers; no backing pointer survives their calls. A handler that could observe partially changed Array length prevents that admission. +Safe-integer regions also retain certified `Math.imul` and `Math.clz32` calls in +integer locals. Entry guards reject non-integer inputs and negative zero before +the original loop performs any coercion. Integer arithmetic failure restores the +iteration's starting values, while polling publishes completed iterations. + +Dense Array loops may overwrite a present own element when the storage has uniform +writable descriptors. An overwrite must be the iteration's sole store, followed +only by local work that cannot restart it. Independent indices check each access +before the store commits; holes, accessors, coercions and unsupported descriptors +resume the original iteration. Replacing a heap value preserves the deletion +barrier. Borrowed storage ends at a poll, after publishing the completed iteration. + DataView accessor calls join fixed storage regions as byte views. Admission uses the conditions that make `byteLength` the intrinsic getter: the watched-prototype protector, the intrinsic prototype, an empty own layout, and a fixed view over an diff --git a/runtime/src/generated/bytecode_operations.inc b/runtime/src/generated/bytecode_operations.inc index 1f8108951..c193a5d32 100644 --- a/runtime/src/generated/bytecode_operations.inc +++ b/runtime/src/generated/bytecode_operations.inc @@ -127,3 +127,4 @@ BYTECODE_OPERATION(PREPARED_STRING_COMPARE) BYTECODE_OPERATION(PRECISE_NUMBER_SUM) BYTECODE_OPERATION(STRING_CONCAT_LENGTH) BYTECODE_OPERATION(CONCAT_STRINGS) +BYTECODE_OPERATION(LOAD_DYNAMIC_ARGUMENT) diff --git a/runtime/src/perf_stats.c b/runtime/src/perf_stats.c index 11b9bbc1b..2bedd4424 100644 --- a/runtime/src/perf_stats.c +++ b/runtime/src/perf_stats.c @@ -1046,8 +1046,9 @@ static void mal_perf_stats_print(void) { (unsigned long long) mal_perf_stats.fixed_typed_array_region_views[3], (unsigned long long) mal_perf_stats.fixed_typed_array_region_views[4], (unsigned long long) mal_perf_stats.fixed_typed_array_affine_regions); - fprintf(stderr, "[perf-dense-array-region-stats] admissions=%llu\n", - (unsigned long long) mal_perf_stats.dense_array_number_region_admissions); + fprintf(stderr, "[perf-dense-array-region-stats] admissions=%llu element-stores=%llu\n", + (unsigned long long) mal_perf_stats.dense_array_number_region_admissions, + (unsigned long long) mal_perf_stats.dense_array_number_region_stores); fprintf(stderr, "[perf-typed-array-stride-stats] admissions=%llu\n", (unsigned long long) mal_perf_stats.fixed_typed_array_stride_regions); fprintf(stderr, "[perf-data-view-region-stats] views=%llu\n", diff --git a/runtime/src/perf_stats.h b/runtime/src/perf_stats.h index 02d1ee402..15c3daadd 100644 --- a/runtime/src/perf_stats.h +++ b/runtime/src/perf_stats.h @@ -241,6 +241,7 @@ typedef struct MalPerfStats { u64 fixed_typed_array_stride_regions; u64 fixed_data_view_region_views; u64 dense_array_number_region_admissions; + u64 dense_array_number_region_stores; u64 record_iteration_region_admissions; u64 record_columns_admissions; u64 record_columns_flushes; diff --git a/runtime/src/vm.c b/runtime/src/vm.c index d9dea3548..60a75916a 100644 --- a/runtime/src/vm.c +++ b/runtime/src/vm.c @@ -2739,6 +2739,12 @@ static void mal_vm_run_until_frame_count( MAL_VM_INTERPRETER_DIRECT_LEAF(); continue; } + MAL_VM_CASE(LOAD_DYNAMIC_ARGUMENT) + registers[instruction->as.load_dynamic_argument.dst] = + mal_vm_load_dynamic_argument(frame->arguments, frame->argument_count, + registers[instruction->as.load_dynamic_argument.key]); + MAL_VM_INTERPRETER_DIRECT_LEAF(); + continue; MAL_VM_CASE(LOAD_STATIC_ARGUMENT) { i32 index = instruction->as.load_static_argument.index; if (index < frame->argument_count) { diff --git a/runtime/src/vm.h b/runtime/src/vm.h index 6213f9ccd..7b24560fc 100644 --- a/runtime/src/vm.h +++ b/runtime/src/vm.h @@ -358,6 +358,10 @@ typedef struct MalInstruction { i32 dst, index; } load_argument; + struct { + i32 dst, key; + } load_dynamic_argument; + struct { i32 dst, direct, fallback, index; } load_static_argument; diff --git a/runtime/src/vm_load.c b/runtime/src/vm_load.c index 96dcd9d15..58cce6bd3 100644 --- a/runtime/src/vm_load.c +++ b/runtime/src/vm_load.c @@ -17,7 +17,7 @@ */ #define WIRE_MAGIC 0x574c414du // "MALW" little-endian -#define WIRE_VERSION 84u +#define WIRE_VERSION 85u #define WIRE_FLAG_HAS_DEBUG 1u typedef enum WireOp { @@ -777,6 +777,11 @@ static void rd_instruction_operands(Rd *r, MalInstruction *o, I32Builder *side_d r->ok = false; } return; + case WIRE_LOAD_DYNAMIC_ARGUMENT: + o->opcode = MAL_OP_LOAD_DYNAMIC_ARGUMENT; + o->as.load_dynamic_argument.dst = rd_i32(r); + o->as.load_dynamic_argument.key = rd_i32(r); + return; case WIRE_LOAD_STATIC_ARGUMENT: o->opcode = MAL_OP_LOAD_STATIC_ARGUMENT; o->as.load_static_argument.dst = rd_i32(r); @@ -1628,6 +1633,7 @@ static bool mal_loaded_instruction_writes_register( MAL_WRITES_DST(MAL_OP_CREATE_ARGUMENTS_OBJECT, create_arguments_object); MAL_WRITES_DST(MAL_OP_LOAD_ARGUMENT_COUNT, load_argument_count); MAL_WRITES_DST(MAL_OP_LOAD_ARGUMENT, load_argument); + MAL_WRITES_DST(MAL_OP_LOAD_DYNAMIC_ARGUMENT, load_dynamic_argument); case MAL_OP_LOAD_STATIC_ARGUMENT: return instruction->as.load_static_argument.dst == target_register || instruction->as.load_static_argument.fallback == target_register; @@ -1791,7 +1797,8 @@ static i32 argument_retention_limit(const MalFunction *fn) { if (instruction->opcode == MAL_OP_CREATE_ARGUMENTS_OBJECT || instruction->opcode == MAL_OP_CREATE_REST_ARGUMENTS || instruction->opcode == MAL_OP_CALL_REST_ARGUMENTS || - instruction->opcode == MAL_OP_LOAD_ARGUMENT) { + instruction->opcode == MAL_OP_LOAD_ARGUMENT || + instruction->opcode == MAL_OP_LOAD_DYNAMIC_ARGUMENT) { return INT32_MAX; } if (instruction->opcode == MAL_OP_LOAD_STATIC_ARGUMENT && @@ -2924,6 +2931,11 @@ MalLoadedRuntimeImage *mal_runtime_image_load_with_host_resolver( r.ok = false; } } + } else if (instruction->opcode == MAL_OP_LOAD_DYNAMIC_ARGUMENT) { + i32 dst = instruction->as.load_dynamic_argument.dst; + i32 key = instruction->as.load_dynamic_argument.key; + if (dst < 0 || dst >= fn->register_count || + key < 0 || key >= fn->register_count) r.ok = false; } else if (instruction->opcode == MAL_OP_GUARD_SERVICE_EPOCH) { if (instruction->as.guard_service_epoch.dst < 0 || instruction->as.guard_service_epoch.dst >= fn->register_count || instruction->as.guard_service_epoch.epoch < 0 || instruction->as.guard_service_epoch.epoch >= fn->register_count) r.ok = false; } else if (instruction->opcode == MAL_OP_READ_SERVICE_EPOCH) { diff --git a/runtime/src/vm_ops.h b/runtime/src/vm_ops.h index 2ed4f7c4b..029449480 100644 --- a/runtime/src/vm_ops.h +++ b/runtime/src/vm_ops.h @@ -500,6 +500,17 @@ void mal_op_load_argument_count(MalCallable *callable, const MalInstruction *ins void mal_op_load_argument(MalCallable *callable, const MalInstruction *instruction); void mal_op_load_static_argument(MalCallable *callable, const MalInstruction *instruction); +/** Read an incoming value without observing an arguments object's properties. */ +static inline MalValue mal_vm_load_dynamic_argument( + const MalValue *arguments, i32 count, MalValue key +) { + if (!mal_ops_is_number(key)) return MAL_VALUE_UNDEFINED; + f64 number = mal_ops_number_as_f64(key); + if (!(number >= 0.0 && number < (f64) count)) return MAL_VALUE_UNDEFINED; + u32 index = (u32) number; + return number == (f64) index ? arguments[index] : MAL_VALUE_UNDEFINED; +} + // Build an arguments object over `args`; a non-empty map aliases indexed // properties to captured formal bindings. Shared by both execution backends. MalValue mal_create_arguments_object( @@ -2723,19 +2734,21 @@ static inline bool mal_vm_fixed_numeric_storage( } /** - * A dense Array's elements below `count`, borrowed until a poll like typed-array - * bytes. A hole or non-Number element is not an own Number, so its read declines. + * Dense Array storage borrowed until a poll. Reads establish their own value kind; + * writable borrows overwrite only present elements and preserve the deletion barrier. */ typedef struct MalDenseNumberSpan { - const MalValue *elements; + MalValue *elements; u32 length; u32 count; } MalDenseNumberSpan; -static inline bool mal_vm_dense_number_storage(MalValue receiver, MalDenseNumberSpan *span) { +static inline bool mal_vm_dense_number_storage( + MalValue receiver, bool writable, MalDenseNumberSpan *span +) { if (!mal_value_is_heap_type(receiver, MAL_HEAP_ARRAY_OBJECT)) return false; const MalArrayObject *array = (const MalArrayObject *) mal_value_to_heap(receiver); - if (array->dense_deopted) return false; + if (array->dense_deopted || (writable && !array->dense_elements_writable)) return false; *span = (MalDenseNumberSpan) { .elements = array->elements, .length = array->length, @@ -2745,6 +2758,20 @@ static inline bool mal_vm_dense_number_storage(MalValue receiver, MalDenseNumber return true; } +static inline bool mal_vm_dense_number_store( + const MalDenseNumberSpan *span, u32 index, f64 value +) { + if (index >= span->count) return false; + MalValue *slot = &span->elements[index]; + MalValue previous = *slot; + if (mal_value_is_array_hole(previous)) return false; + // Number stores introduce no young edge, but replaced heap values still need SATB. + mal_gc_write_barrier(previous); + *slot = mal_ops_number_value(value); + MAL_PERF_COUNT(dense_array_number_region_stores); + return true; +} + // Each width scales the index by a constant, which keeps a multiply by the span's // element size out of every element address. static inline f64 mal_vm_fixed_numeric_load(const MalTypedArraySpan *span, u32 index) { diff --git a/src/compiler/core/core-cross-call-transforms.ts b/src/compiler/core/core-cross-call-transforms.ts index fe0428f48..322bc3c7e 100644 --- a/src/compiler/core/core-cross-call-transforms.ts +++ b/src/compiler/core/core-cross-call-transforms.ts @@ -332,6 +332,7 @@ const INLINE_ENVIRONMENT_OPCODES: ReadonlySet = new Set([ ]); const INLINE_UNSUPPORTED_OPCODES = new Set([ + "loadDynamicArgument", "createArgumentsObject", "createRestArguments", "createRestArgumentsState", @@ -2244,6 +2245,7 @@ function canBridgeInlineResult( } const CALLBACK_SPECIALIZATION_EXCLUDED_OPCODES: ReadonlySet = new Set([ + "loadDynamicArgument", "loadCallee", "loadArgument", "loadArgumentCount", diff --git a/src/compiler/core/core-ir-opcodes.ts b/src/compiler/core/core-ir-opcodes.ts index 5910e16db..eb484e33c 100644 --- a/src/compiler/core/core-ir-opcodes.ts +++ b/src/compiler/core/core-ir-opcodes.ts @@ -96,6 +96,7 @@ export const CORE_OPCODES = [ "iteratorStep", "loadArgument", "loadArgumentCount", + "loadDynamicArgument", "loadCallee", "loadCaptured", "loadGlobalIndex", @@ -194,6 +195,7 @@ const TWO_OUTPUTS = new Set([ ]); const GC_FREE = new Set([ + "loadDynamicArgument", "createRestArgumentsState", "guardServiceEpoch", "readServiceEpoch", @@ -714,6 +716,7 @@ const INPUT_ARITIES = { iteratorNext: [2, 2], iteratorStep: [2, 2], loadArgument: [0, 0], + loadDynamicArgument: [1, 1], loadArgumentCount: [0, 0], loadCallee: [0, 0], loadCaptured: [0, 0], diff --git a/src/compiler/core/core-local-capture-arguments.ts b/src/compiler/core/core-local-capture-arguments.ts index a19f34c47..d8a4b542a 100644 --- a/src/compiler/core/core-local-capture-arguments.ts +++ b/src/compiler/core/core-local-capture-arguments.ts @@ -71,6 +71,7 @@ function helperBody( opcode === "loadCallee" || opcode === "loadArgumentCount" || opcode === "loadArgument" || + opcode === "loadDynamicArgument" || opcode === "loadStaticArgument" || opcode === "createFunction" || opcode === "createArgumentsObject" || diff --git a/src/compiler/core/core-native-entry-analysis.ts b/src/compiler/core/core-native-entry-analysis.ts index d4b7f0ff4..f171c58f6 100644 --- a/src/compiler/core/core-native-entry-analysis.ts +++ b/src/compiler/core/core-native-entry-analysis.ts @@ -533,7 +533,11 @@ export function coreArgumentObservation(fn: CoreFunctionStore): CoreArgumentObse const header = fn.instructionHeader(instruction); if (typeof header === "string") continue; const opcode = header.opcode; - if (opcode === "createArgumentsObject" || opcode === "callRestArguments") + if ( + opcode === "createArgumentsObject" || + opcode === "callRestArguments" || + opcode === "loadDynamicArgument" + ) return { kind: "general" }; if (opcode === "createRestArguments") { const startIndex = fn.instructionAttributes(instruction).startIndex; diff --git a/src/compiler/core/core-native-field-analysis.ts b/src/compiler/core/core-native-field-analysis.ts index f7bf56d5c..3be1b1332 100644 --- a/src/compiler/core/core-native-field-analysis.ts +++ b/src/compiler/core/core-native-field-analysis.ts @@ -46,6 +46,7 @@ const argumentProofs = new WeakMap< // These read the parameter through the activation rather than as an operand. const PARAMETER_ALIAS_OPCODES = new Set([ + "loadDynamicArgument", "loadArgument", "loadStaticArgument", "createArgumentsObject", diff --git a/src/compiler/core/core-private-field-elements.ts b/src/compiler/core/core-private-field-elements.ts index 9a259996a..34c238892 100644 --- a/src/compiler/core/core-private-field-elements.ts +++ b/src/compiler/core/core-private-field-elements.ts @@ -70,6 +70,7 @@ const CALLBACK_METHODS: ReadonlyMap = new Map([ // Operations through which a function can reach arguments it declares no parameter for. const ARGUMENT_READERS: ReadonlySet = new Set([ + "loadDynamicArgument", "callRestArguments", "createArgumentsObject", "createRestArguments", diff --git a/src/compiler/core/core-static-value-calls.ts b/src/compiler/core/core-static-value-calls.ts index e71ca05fa..328e11296 100644 --- a/src/compiler/core/core-static-value-calls.ts +++ b/src/compiler/core/core-static-value-calls.ts @@ -166,6 +166,7 @@ function staticParameterPlan( "loadCallee", "loadNewTarget", "loadArgument", + "loadDynamicArgument", "loadArgumentCount", "loadStaticArgument", "createArgumentsObject", diff --git a/src/compiler/core/semantic-lowering.ts b/src/compiler/core/semantic-lowering.ts index 202df070b..ec9c16611 100644 --- a/src/compiler/core/semantic-lowering.ts +++ b/src/compiler/core/semantic-lowering.ts @@ -522,6 +522,7 @@ interface CoreFrontendFunction { staticArgumentsFallbackRegister?: number; /** Incoming arguments as a packed rest view for in-range element reads. */ argumentsElementRegister?: number; + directArgumentElements?: true; /** * Locked primordials pin the builtin slice, so this function's `arguments` copies * read the frame and its reads use `argumentsCopyAccesses`; no object exists. @@ -5767,6 +5768,14 @@ function compileFunctionParams( }); } (fn.staticArgumentsRegisters ??= new Map()).set(usage, argumentCountRegister); + if ( + !fn.isGenerator && + !fn.isAsync && + !fn.mappedArgumentSlots?.some((slot) => slot >= 0) + ) { + fn.directArgumentElements = true; + continue; + } // Without these facts the view could not stay virtual and would allocate per call. if ( fn.argumentsElementRegister === undefined && @@ -13438,7 +13447,7 @@ function compileArgumentsElementRead( const count = fn.staticArgumentsRegisters?.get(usage); const objectJumps: Array> = []; let viewDone: Extract | undefined; - if (view !== undefined && count !== undefined) { + if ((fn.directArgumentElements || view !== undefined) && count !== undefined) { const current = loadRegisterFromLocation( fn, cursor.block, @@ -13492,7 +13501,11 @@ function compileArgumentsElementRead( operator: "<", }); guard(inRange); - cursor.block.emitter.emit({ type: "loadProperty", registers: [result, view, index] }); + cursor.block.emitter.emit( + fn.directArgumentElements + ? { type: "loadDynamicArgument", registers: [result, index] } + : { type: "loadProperty", registers: [result, view!, index] }, + ); viewDone = { type: "jump", blocks: [-1] }; cursor.block.emitter.emit(viewDone); const objectIndex = fn.blocks.push({ emitter: unboundCoreEmitter }) - 1; diff --git a/src/compiler/shared/compiler-instruction.ts b/src/compiler/shared/compiler-instruction.ts index e0dd8d387..a8b58221e 100644 --- a/src/compiler/shared/compiler-instruction.ts +++ b/src/compiler/shared/compiler-instruction.ts @@ -347,6 +347,11 @@ type CompilerOperation = registers: [number]; index: number; } + | { + /** Read the retained incoming slice; invalid indices produce undefined without coercion. */ + type: "loadDynamicArgument"; + registers: [number, number]; + } | { // Direct static index read. Supplied indexes use the optional live mapped // value; missing indexes lazily materialize and cache the arguments object. diff --git a/src/compiler/target/bytecode-operation-spec.ts b/src/compiler/target/bytecode-operation-spec.ts index b90209a6a..97f05aab9 100644 --- a/src/compiler/target/bytecode-operation-spec.ts +++ b/src/compiler/target/bytecode-operation-spec.ts @@ -137,6 +137,7 @@ export const BYTECODE_OPERATIONS = [ "PRECISE_NUMBER_SUM", "STRING_CONCAT_LENGTH", "CONCAT_STRINGS", + "LOAD_DYNAMIC_ARGUMENT", ] as const; export type BytecodeOperation = (typeof BYTECODE_OPERATIONS)[number]; diff --git a/src/compiler/target/compiler-artifact-codec.ts b/src/compiler/target/compiler-artifact-codec.ts index 09100adc3..921ba1709 100644 --- a/src/compiler/target/compiler-artifact-codec.ts +++ b/src/compiler/target/compiler-artifact-codec.ts @@ -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 = 370; +export const COMPILER_ARTIFACT_VERSION = 371; /** Direct entries of C library bindings, as `binding-unit.ts` names them. */ const FOREIGN_CALL_SYMBOL = /^mal_ffi_direct\d+_\w+$/; diff --git a/src/compiler/target/emit-program-image.ts b/src/compiler/target/emit-program-image.ts index 5505f95f8..df4c37588 100644 --- a/src/compiler/target/emit-program-image.ts +++ b/src/compiler/target/emit-program-image.ts @@ -3275,6 +3275,8 @@ function emitInstruction(instruction: BytecodeInstruction, dataOffset?: number) return `{ .opcode = MAL_OP_LOAD_ARGUMENT_COUNT, .as.load_argument_count = { .dst = ${instruction.dst} } }`; case "LOAD_ARGUMENT": return `{ .opcode = MAL_OP_LOAD_ARGUMENT, .as.load_argument = { .dst = ${instruction.dst}, .index = ${instruction.index} } }`; + case "LOAD_DYNAMIC_ARGUMENT": + return `{ .opcode = MAL_OP_LOAD_DYNAMIC_ARGUMENT, .as.load_dynamic_argument = { .dst = ${instruction.dst}, .key = ${instruction.key} } }`; case "LOAD_STATIC_ARGUMENT": return `{ .opcode = MAL_OP_LOAD_STATIC_ARGUMENT, .as.load_static_argument = { .dst = ${instruction.dst}, .direct = ${instruction.direct}, .fallback = ${instruction.fallback}, .index = ${instruction.index} } }`; case "LOAD_THIS": diff --git a/src/compiler/target/lower-core-target.ts b/src/compiler/target/lower-core-target.ts index d5cc27363..0922f4003 100644 --- a/src/compiler/target/lower-core-target.ts +++ b/src/compiler/target/lower-core-target.ts @@ -183,6 +183,7 @@ const REGISTERLESS_CORE_OPERATIONS: ReadonlySet = new Set([ ]); const RAW_ARGUMENT_OPCODES: ReadonlySet = new Set([ + "loadDynamicArgument", "loadArgumentCount", "loadArgument", "loadStaticArgument", diff --git a/src/compiler/target/lower-native-fast-paths.ts b/src/compiler/target/lower-native-fast-paths.ts index 04c8d433b..ec84e24ae 100644 --- a/src/compiler/target/lower-native-fast-paths.ts +++ b/src/compiler/target/lower-native-fast-paths.ts @@ -1314,6 +1314,7 @@ export function numberRecordDataflow( break; } case "LOAD_PROPERTY": + case "TO_PROPERTY_KEY": admitted = indexed && read(instruction.key, "number") && @@ -1693,11 +1694,53 @@ function integerLoopInstruction(instruction: BytecodeInstruction): boolean { ); case "UNARY": return NUMBER_RECORD_UNARY_OPERATORS.has(instruction.operator); + case "CALL_KNOWN": + case "MATH_UNARY_NUMBER": + case "MATH_BINARY_NUMBER": { + const operation = nativeNumericBuiltinInstruction(instruction)?.operation; + return operation === "Math.imul" || operation === "Math.clz32"; + } default: return false; } } +/** A sole store followed only by local work, so a guard can never replay it. */ +export function numericLoopHasTerminalStore( + fn: BytecodeFunction, + header: number, + order: ReadonlyArray, +): boolean { + const stores = order.filter((ip) => fn.instructions[ip]!.opcode === "STORE_PROPERTY"); + if (stores.length !== 1) return false; + if ( + order.some((ip) => + numberRecordSuccessors(fn.instructions[ip]!, ip).some( + (next) => next <= ip && next !== header, + ), + ) + ) + return false; + const members = new Set(order); + const seen = new Set(); + const pending = numberRecordSuccessors(fn.instructions[stores[0]!]!, stores[0]!); + while (pending.length > 0) { + const ip = pending.pop()!; + if (ip === header || !members.has(ip) || seen.has(ip)) continue; + seen.add(ip); + const op = fn.instructions[ip]!; + if ( + !integerLoopInstruction(op) && + !["LOAD_GLOBAL", "STORE_GLOBAL", "LOAD_CAPTURED", "THROW_IF_TDZ"].includes( + op.opcode, + ) + ) + return false; + pending.push(...numberRecordSuccessors(op, ip)); + } + return true; +} + function bytecodePredecessors( fn: BytecodeFunction, body: NativeBodyFacts, @@ -2058,6 +2101,7 @@ function numericLoopIndexFacts( (op.opcode === "UNARY" && (op.operator === "tonumeric" || op.operator === "+")) ) result = facts.get(op.src); + else if (op.opcode === "TO_PROPERTY_KEY") result = facts.get(op.key); else if ( op.opcode === "UNARY" && (op.operator === "increment" || op.operator === "decrement") @@ -2806,7 +2850,17 @@ function fixedNumericLoopStorage( memory.length > FIXED_NUMERIC_LOOP_ACCESS_LIMIT ) return undefined; - const counted = fixedNumericLoopCounter(fn, shape, body, lengthLoads, memory); + const provenCounter = fixedNumericLoopCounter(fn, shape, body, lengthLoads, memory); + const checkedStore = memory.some( + ({ ip, op, data }) => + op.opcode === "STORE_PROPERTY" && + !(data === undefined + ? provenCounter?.offsets.has(ip) + : provenCounter?.windows.has(ip)), + ); + const terminalStore = numericLoopHasTerminalStore(fn, shape.order[0]!, shape.order); + if (checkedStore && !terminalStore) return undefined; + const counted = checkedStore ? undefined : provenCounter; const offsets = counted?.offsets ?? new Map(); const windows = counted?.windows ?? new Map(); @@ -2862,12 +2916,10 @@ function fixedNumericLoopStorage( }); continue; } - // Without a counter, every access is a read that checks its own key. Stores stay - // at counted keys, whose range the entry guards prove even in a checked view. + // A terminal checked store validates its own key before the iteration commits. if (offset === undefined) { - if (op.opcode !== "LOAD_PROPERTY") return undefined; views.set(op.object, { - writable: previous?.writable === true, + writable: previous?.writable === true || op.opcode === "STORE_PROPERTY", minOffset: previous?.minOffset ?? 0, maxOffset: previous?.maxOffset ?? 0, checked: true, @@ -2974,12 +3026,13 @@ function fixedNumericLoopStorage( (lengthLoad !== undefined && views.get(lengthLoad.object)?.byteScale !== undefined) ) return undefined; - // Stores admit only TypedArray views, whose length no store changes; records and - // a known Array length need Array views, which stay read-only so stores keep their kinds. + // Record guards can restart an iteration; a known Array length permits only the + // terminal-store alternative, whose overwrite cannot change that length. if ( (records.size > 0 || - fn.instructions[lengthLoad?.ip ?? -1]?.opcode === - "LOAD_PROPERTY_STATIC_ARRAY_LENGTH") && + (fn.instructions[lengthLoad?.ip ?? -1]?.opcode === + "LOAD_PROPERTY_STATIC_ARRAY_LENGTH" && + !terminalStore)) && [...views.values()].some((view) => view.writable) ) return undefined; @@ -3028,6 +3081,26 @@ function fixedNumericLoopStorage( ) ) return undefined; + if (op.opcode === "TO_PROPERTY_KEY") { + // An admitted receiver is coercible; the Number key remains private to its accesses. + if ( + !views.has(op.object) || + shape.order.some((other) => { + const use = fn.instructions[other]!; + return ( + vmInstructionReadRegisters(use).includes(op.dst) && + !( + (use.opcode === "LOAD_PROPERTY" || use.opcode === "STORE_PROPERTY") && + use.key === op.dst && + use.object !== op.dst && + (use.opcode !== "STORE_PROPERTY" || use.value !== op.dst) + ) + ); + }) + ) + return undefined; + continue; + } if (op.opcode === "LOAD_PROPERTY" || op.opcode === "STORE_PROPERTY") continue; if ( (op.opcode !== "CREATE_NUMBER" && @@ -3279,8 +3352,9 @@ function lowerNumericLoopRegion( !shape.order.some((ip) => { const instruction = fn.instructions[ip]!; return ( - instruction.opcode === "BINARY" && - INTEGER_LOOP_PROFITABLE_OPERATORS.has(instruction.operator) + (instruction.opcode === "BINARY" && + INTEGER_LOOP_PROFITABLE_OPERATORS.has(instruction.operator)) || + nativeNumericBuiltinInstruction(instruction) !== undefined ); })) ) @@ -3339,6 +3413,16 @@ function lowerNumericLoopRegion( } // A record is an iteration's borrowed element, never a Number to pass in or out. const records = new Set(storage?.records ?? []); + const preparedKeys = shape.order.flatMap((ip) => { + const op = fn.instructions[ip]!; + return op.opcode === "TO_PROPERTY_KEY" ? [op.dst] : []; + }); + if ( + preparedKeys.some( + (key) => inputs.has(key) || exits.some((exit) => exit.outputs.includes(key)), + ) + ) + return undefined; if ( [...inputs].some((register) => records.has(register)) || exits.some((exit) => exit.outputs.some((register) => records.has(register))) diff --git a/src/compiler/target/program-image-codec.ts b/src/compiler/target/program-image-codec.ts index 80ac2ab6a..babe31278 100644 --- a/src/compiler/target/program-image-codec.ts +++ b/src/compiler/target/program-image-codec.ts @@ -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 = 84; +export const WIRE_VERSION = 85; /** Canonical wire tags generated into the C runtime from the same registry. */ export const WIRE_OPCODES = BYTECODE_OPERATIONS; @@ -869,6 +869,10 @@ function writeInstructionOperands(w: Writer, i: BytecodeInstruction): void { w.i32(i.capturedIndices.length); w.i32Array(i.capturedIndices); return; + case "LOAD_DYNAMIC_ARGUMENT": + w.i32(i.dst); + w.i32(i.key); + return; case "LOAD_ARGUMENT": if (i.index < 0) throw new RangeError("program-image-codec: negative argument index"); @@ -1735,6 +1739,8 @@ function readInstructionOperands(r: Reader): BytecodeInstruction { return { opcode, dst: r.i32() }; case "LOAD_ARGUMENT_COUNT": return { opcode, dst: r.i32() }; + case "LOAD_DYNAMIC_ARGUMENT": + return { opcode, dst: r.i32(), key: r.i32() }; case "LOAD_ARGUMENT": { const dst = r.i32(); const index = r.i32(); diff --git a/src/compiler/target/program-image.ts b/src/compiler/target/program-image.ts index 871f9d34b..e1bceefec 100644 --- a/src/compiler/target/program-image.ts +++ b/src/compiler/target/program-image.ts @@ -1341,6 +1341,7 @@ export function nativeInstructionEffects( case "MOVE": case "LOAD_CALLEE": case "LOAD_ARGUMENT": + case "LOAD_DYNAMIC_ARGUMENT": case "LOAD_ARGUMENT_COUNT": case "CREATE_UNDEFINED": case "CREATE_NULL": @@ -1934,6 +1935,7 @@ export function validateNativeDirectEntry( throw new RangeError("Native direct entry has an incomplete rest view"); if ( instruction.opcode === "CREATE_ARGUMENTS_OBJECT" || + instruction.opcode === "LOAD_DYNAMIC_ARGUMENT" || instruction.opcode === "CALL_REST_ARGUMENTS" ) throw new RangeError("Native direct entry observes the general argument slice"); @@ -2983,6 +2985,7 @@ export function vmNativeInstructionMayCaptureStack( case "CREATE_BIGINT": case "LOAD_ARGUMENT_COUNT": case "LOAD_ARGUMENT": + case "LOAD_DYNAMIC_ARGUMENT": case "LOAD_NEW_TARGET": case "LOAD_THIS": case "LOAD_CALLEE": diff --git a/src/compiler/target/render-native-c.ts b/src/compiler/target/render-native-c.ts index d40fcda74..137f5d930 100644 --- a/src/compiler/target/render-native-c.ts +++ b/src/compiler/target/render-native-c.ts @@ -90,6 +90,7 @@ import { ffiIntegerArgumentFits, nativeDataViewAccessor, numericLoopCallMethods, + numericLoopHasTerminalStore, numericLoopOrder, nativeNumericBuiltinInstruction, lowerNativeFastPaths, @@ -1503,6 +1504,8 @@ function integralLoopRegisters( return Object.is(op.value, -0) ? undefined : safe(op.value, op.value); case "MOVE": return rangeOf(op.src); + case "TO_PROPERTY_KEY": + return rangeOf(op.key); case "LOAD_PROPERTY": return elementLoads; case "CALL": @@ -1682,7 +1685,7 @@ function emitRecordIterationRegion( }; const guards: Array = [ `${cursor}.active`, - `mal_vm_dense_number_storage(r${plan.iterator}, &${id}_array)`, + `mal_vm_dense_number_storage(r${plan.iterator}, false, &${id}_array)`, ]; const lines: Array = ["{", ` MalDenseNumberSpan ${id}_array = {0};`]; const declined = `${id}_declined`; @@ -2217,12 +2220,12 @@ function emitNumericLoopRegion( ); else guards.push(`mal_safe_integer_admit(${value}, &${local(register)})`); } - // Only element views have a dense Array alternative, and only while nothing stores. - const readOnly = + // An Array overwrite may commit only after the iteration's last restarting operation. + const arrayAlternative = plan.kind === "fixed-numeric" && - plan.views.every( - (view) => !view.writable && view.byteScale === undefined && view.lend === undefined, - ); + plan.views.every((view) => view.byteScale === undefined && view.lend === undefined) && + (plan.views.every((view) => !view.writable) || + numericLoopHasTerminalStore(fn, plan.id, order)); const records = new Set(plan.kind !== "integer" ? (plan.records ?? []) : []); const checkedViews = new Set( plan.kind === "fixed-numeric" @@ -2348,11 +2351,11 @@ function emitNumericLoopRegion( ? [` MalTypedArrayObject *${id}_lt = nullptr;`, ` u32 ${id}_ll = 0;`] : []), ` bool ${id}_typed = ${typedAdmissions.join(" && ")};`, - ` bool ${id}_array = ${readOnly ? `!${id}_typed && ${elementViews.map((view) => `mal_vm_dense_number_storage(r${view.object}, &${id}_a${view.object})`).join(" && ")}` : "false"};`, + ` bool ${id}_array = ${arrayAlternative ? `!${id}_typed && ${elementViews.map((view) => `mal_vm_dense_number_storage(r${view.object}, ${view.writable}, &${id}_a${view.object})`).join(" && ")}` : "false"};`, ); if (counter !== undefined && boundObject !== undefined && !textBound) rangePrelude.push( - ` ${local(counter.bound)} = ${typedPossible ? `${id}_typed ? (f64) ${id}_v${boundObject}.length : ` : ""}${readOnly ? `${id}_array ? (f64) ${id}_a${boundObject}.length : ` : ""}0.0;`, + ` ${local(counter.bound)} = ${typedPossible ? `${id}_typed ? (f64) ${id}_v${boundObject}.length : ` : ""}${arrayAlternative ? `${id}_array ? (f64) ${id}_a${boundObject}.length : ` : ""}0.0;`, ); guards.unshift(`(${id}_typed || ${id}_array)`); if (textViews.length > 0) guards.unshift(`${id}_text`); @@ -2492,13 +2495,15 @@ function emitNumericLoopRegion( case "CALL_KNOWN": case "MATH_UNARY_NUMBER": case "MATH_BINARY_NUMBER": { - const builtin = floating ? nativeNumericBuiltinInstruction(op) : undefined; + const builtin = nativeNumericBuiltinInstruction(op); if (builtin === undefined) throw new Error("Invalid numeric loop builtin"); - if (integral.has(op.dst)) { + if (!floating || integral.has(op.dst)) { const [first, second] = builtin.arguments.map((argument) => argument.kind === "number" ? `(i32) ${argument.value | 0}` - : intOf(argument.register, ip), + : floating + ? intOf(argument.register, ip) + : int32(argument.register), ); return builtin.operation === "Math.imul" ? `${local(op.dst)} = (i64) (i32) ((u32) ${first} * (u32) ${second});` @@ -2651,8 +2656,15 @@ function emitNumericLoopRegion( return `{ ${bound}${local(op.dst)} = ${integral.has(op.dst) ? "(i64) " : ""}mal_vm_fixed_numeric_load(&${id}_v${op.object}, (u32) ${key}); }`; } case "STORE_PROPERTY": { - if (mode === "array") throw new Error("Invalid dense Array loop store"); const key = integral.has(op.key) ? local(op.key) : num(op.key); + const length = `${id}_${mode === "array" ? "a" : "v"}${op.object}.${mode === "array" ? "count" : "length"}`; + const bound = !checkedViews.has(op.object) + ? "" + : integral.has(op.key) + ? `if (__builtin_expect(!(${key} >= 0 && ${key} < (i64) ${length}), 0)) ${restart} ` + : `if (__builtin_expect(!(${key} >= 0.0 && ${key} < (f64) ${length}) || ${key} != trunc(${key}), 0)) ${restart} `; + if (mode === "array") + return `{ ${bound}if (__builtin_expect(!mal_vm_dense_number_store(&${id}_a${op.object}, (u32) ${key}, ${num(op.value)}), 0)) ${restart} }`; const access = nativeInstructions[ip]; if ( mode !== "f64" && @@ -2668,11 +2680,11 @@ function emitNumericLoopRegion( : access.elementKind === "Uint8ClampedArray" ? `mal_typed_array_to_uint8_clamp(${num(op.value)})` : `(u${width * 8}) mal_ops_number_to_uint32(${num(op.value)})`; - return `mal_scalar_store_native_u${width * 8}(${id}_v${op.object}.data + (usize)(u32) ${key} * ${width}, ${bits});`; + return `{ ${bound}mal_scalar_store_native_u${width * 8}(${id}_v${op.object}.data + (usize)(u32) ${key} * ${width}, ${bits}); }`; } return mode === "f64" - ? `mal_scalar_store_native_u64(${id}_v${op.object}.data + (usize)(u32) ${key} * 8, mal_scalar_f64_to_bits(${num(op.value)}));` - : `mal_vm_fixed_numeric_store(&${id}_v${op.object}, (u32) ${key}, ${num(op.value)});`; + ? `{ ${bound}mal_scalar_store_native_u64(${id}_v${op.object}.data + (usize)(u32) ${key} * 8, mal_scalar_f64_to_bits(${num(op.value)})); }` + : `{ ${bound}mal_vm_fixed_numeric_store(&${id}_v${op.object}, (u32) ${key}, ${num(op.value)}); }`; } case "REQUIRE_COERCIBLE": // The element load already restarted the iteration for anything but an object. @@ -2699,6 +2711,8 @@ function emitNumericLoopRegion( return `${local(op.dst)} = ${op.value};`; case "MOVE": return `${local(op.dst)} = ${integral.has(op.dst) ? local(op.src) : num(op.src)};`; + case "TO_PROPERTY_KEY": + return `${local(op.dst)} = ${integral.has(op.dst) ? local(op.key) : num(op.key)};`; case "BINARY": { const compared = compilerIntegerRangeComparison( op.operator, @@ -2967,7 +2981,7 @@ function emitNumericLoopRegion( ? ` MalValue ${local(register)} = MAL_VALUE_UNDEFINED;` : ` ${cType(register)} ${local(register)} = ${initial.get(register) ?? (kind === "boolean" ? "false" : "0")};`, ), - ` if (${[`${cursor}.active`, `mal_vm_dense_number_storage(r${plan.iterator}, &${id}_a)`, ...guards].join(" && ")}) {`, + ` if (${[`${cursor}.active`, `mal_vm_dense_number_storage(r${plan.iterator}, false, &${id}_a)`, ...guards].join(" && ")}) {`, ...arrayBody(), " }", "}", @@ -2979,7 +2993,7 @@ function emitNumericLoopRegion( ...textViews.map((view) => ` MalStringUnitSpan ${id}_s${view.object} = {0};`), ...elementViews.flatMap((view) => [ ...(lengthBound ? [] : [` MalTypedArraySpan ${id}_v${view.object} = {0};`]), - ...(readOnly ? [` MalDenseNumberSpan ${id}_a${view.object} = {0};`] : []), + ...(arrayAlternative ? [` MalDenseNumberSpan ${id}_a${view.object} = {0};`] : []), ]), ...dataViews.map((view) => ` MalDataViewSpan ${id}_d${view.object} = {0};`), ...lentViews.flatMap((view) => [ @@ -2997,7 +3011,7 @@ function emitNumericLoopRegion( : [ ` if (${[...guards, ...typedViewGuards].join(" && ") || "true"}) {`, ...typedBodies, - ...(readOnly && !textual + ...(arrayAlternative && !textual ? [ ` } else if (${[...guards, ...arrayViewGuards].join(" && ")}) {`, ...arrayBody(), @@ -3936,13 +3950,15 @@ function emitCompiledVariant( capturesEnv || hasWith || analysis.captures.copiedValues.length > 0); - const retainsForwardedArguments = fn.instructions.some( - (instruction) => instruction.opcode === "CALL_REST_ARGUMENTS", + const retainsIncomingArguments = fn.instructions.some( + (instruction) => + instruction.opcode === "CALL_REST_ARGUMENTS" || + instruction.opcode === "LOAD_DYNAMIC_ARGUMENT", ); - const deferredRestRootIp = retainsForwardedArguments + const deferredRestRootIp = retainsIncomingArguments ? undefined : deferredRestArgumentRootIp(nativeContract); - const retainsArguments = retainsForwardedArguments || deferredRestRootIp !== undefined; + const retainsArguments = retainsIncomingArguments || deferredRestRootIp !== undefined; const restArgumentRootReleaseIp = fn.instructions.some( (instruction) => instruction.opcode === "LOAD_ARGUMENT" || @@ -15408,6 +15424,12 @@ class NativeInstructionEmission { return [ `r${instruction.dst} = arg_count > ${instruction.index} ? args[${instruction.index}] : MAL_VALUE_UNDEFINED;`, ]; + case "LOAD_DYNAMIC_ARGUMENT": { + if (this.context.directArgumentRepresentations !== undefined) + throw new Error("Direct entry observes the general argument slice"); + const value = `mal_vm_load_dynamic_argument(args, arg_count, ${this.boxed(instruction.key)})`; + return [`r${instruction.dst} = ${this.callValue(instruction.dst, value)};`]; + } case "LOAD_STATIC_ARGUMENT": { if (this.context.directArgumentRepresentations !== undefined) { const representation = diff --git a/src/compiler/target/runtime-image.ts b/src/compiler/target/runtime-image.ts index 17465b64e..9a8fd36e9 100644 --- a/src/compiler/target/runtime-image.ts +++ b/src/compiler/target/runtime-image.ts @@ -623,6 +623,7 @@ export function computeArgumentRetentionLimit( case "CREATE_REST_ARGUMENTS": case "CALL_REST_ARGUMENTS": case "LOAD_ARGUMENT": + case "LOAD_DYNAMIC_ARGUMENT": return 0x7fffffff; case "LOAD_STATIC_ARGUMENT": maximum = Math.max(maximum, instruction.index); @@ -881,6 +882,11 @@ export type BytecodeInstruction = dst: number; index: number; } + | { + opcode: "LOAD_DYNAMIC_ARGUMENT"; + dst: number; + key: number; + } | { opcode: "LOAD_STATIC_ARGUMENT"; dst: number; @@ -3979,6 +3985,12 @@ function lowerInstructionToBytecodeInstruction( dst: instruction.registers[0], index: instruction.index, }; + case "loadDynamicArgument": + return { + opcode: "LOAD_DYNAMIC_ARGUMENT", + dst: instruction.registers[0], + key: instruction.registers[1], + }; case "loadStaticArgument": return { opcode: "LOAD_STATIC_ARGUMENT", diff --git a/src/test262/program-image-merge.ts b/src/test262/program-image-merge.ts index 7dd237434..43a187c5a 100644 --- a/src/test262/program-image-merge.ts +++ b/src/test262/program-image-merge.ts @@ -537,6 +537,7 @@ function cloneInstruction( case "CREATE_ARGUMENTS_OBJECT": case "LOAD_ARGUMENT_COUNT": case "LOAD_ARGUMENT": + case "LOAD_DYNAMIC_ARGUMENT": case "LOAD_STATIC_ARGUMENT": case "LOAD_THIS": case "LOAD_NEW_TARGET": diff --git a/tests/dynamic-arguments.test.ts b/tests/dynamic-arguments.test.ts new file mode 100644 index 000000000..cefb47751 --- /dev/null +++ b/tests/dynamic-arguments.test.ts @@ -0,0 +1,93 @@ +import { describe, expect, it } from "vitest"; +import { resolveBuildConfig } from "../src/build-config.ts"; +import { parseScript } from "../src/compiler/frontend/parser.ts"; +import { analyzeSourceAndRunSemanticAnalysis } from "../src/compiler/frontend/semantic-analysis.ts"; +import { compileSemanticProgramToProgramImage } from "../src/compiler/pipeline/compile-core.ts"; +import { compilerProgramFactsFromConfig } from "../src/compiler/shared/compiler-facts.ts"; +import { + deserializeCompilerArtifact, + serializeCompilerArtifact, +} from "../src/compiler/target/compiler-artifact-codec.ts"; +import { validateNativeDirectEntry } from "../src/compiler/target/program-image.ts"; +import { computeArgumentRetentionLimit } from "../src/compiler/target/runtime-image.ts"; + +function compile(source: string, primordials: "mutable" | "locked" = "mutable") { + const text = `globalThis.read = ${source};`; + return compileSemanticProgramToProgramImage( + analyzeSourceAndRunSemanticAnalysis( + text, + "/arguments.js", + parseScript(text, { strict: false }), + ), + { + facts: compilerProgramFactsFromConfig( + resolveBuildConfig({ engine: { primordials } }), + ), + }, + ); +} + +const read = 'function read(key) { "use strict"; return arguments[key]; }'; + +describe.each(["mutable", "locked"] as const)( + "incoming argument reads with %s primordials", + (primordials) => { + it.each([ + [read, true], + [ + "function read() { let value; const args = arguments; for(let i=0;i { + const image = compile(source, primordials); + expect( + image.runtime.functions[1]!.instructions.some( + (op) => op.opcode === "LOAD_DYNAMIC_ARGUMENT", + ), + ).toBe(eligible); + }); + }, +); + +it("retains the incoming slice through a dynamic read's artifact roundtrip", () => { + const image = deserializeCompilerArtifact(serializeCompilerArtifact(compile(read))); + const owner = image.runtime.functions[1]!; + expect(owner.instructions.some((op) => op.opcode === "LOAD_DYNAMIC_ARGUMENT")).toBe( + true, + ); + expect(owner.needsArguments).toBe(true); + expect(computeArgumentRetentionLimit(owner)).toBe(0x7fffffff); + expect( + owner.instructions + .slice(0, owner.argumentSnapshotCount) + .some((op) => op.opcode === "LOAD_DYNAMIC_ARGUMENT"), + ).toBe(false); +}); + +it("rejects a direct entry without the dynamic read's incoming slice", () => { + const native = compile(read).native.functions[1]!; + const op = native.body.instructions.find((op) => op.opcode === "LOAD_DYNAMIC_ARGUMENT"); + if (op === undefined) throw new Error("Missing dynamic argument read"); + expect(() => + validateNativeDirectEntry( + { ...native.body, instructions: [op] }, + { + id: 0, + parameterRepresentations: Array.from( + { length: native.body.parameterCount }, + () => "boxed" as const, + ), + argumentRepresentations: Array.from( + { length: native.body.parameterCount }, + () => "boxed" as const, + ), + resultRepresentation: "boxed", + registerRepresentations: native.registerRepresentations, + gc: native.gc, + }, + ), + ).toThrow(/general argument slice/); +}); diff --git a/tests/helpers/native-preparations.ts b/tests/helpers/native-preparations.ts index 35c9dcd07..4b2372d47 100644 --- a/tests/helpers/native-preparations.ts +++ b/tests/helpers/native-preparations.ts @@ -39,7 +39,8 @@ export function nativePreparations(root: string, file: string): Array ({ value })); + if (padding.length !== 4) throw new Error("Missing allocation pressure"); +} +retained.returned.value++; +console.log("retained", retained.returned === retained.original, retained.original.value); + +function collect() { + const args = arguments; + let value = ""; + for (let index = args.length - 1; index >= 0; index--) { + const padding = [index, index + 1].map((value) => ({ value })); + value += args[index].name + padding.length; + } + return value; +} +console.log("alias", collect({ name: "a" }, { name: "b" }, { name: "c" })); + +let conversions = 0; +const key = { + toString() { + conversions++; + return "1"; + }, +}; +console.log("keys", Object.is(pick(-0, "own"), -0), pick(key, "own"), conversions); +console.log("invalid", pick(-1), pick(0.5), pick(NaN), pick(Infinity)); + +function materialized(key) { + arguments[1] = "replaced"; + return arguments[key]; +} +console.log("materialized", materialized(1, "own")); diff --git a/tests/local/arguments-inherited-index.cjs b/tests/local/arguments-inherited-index.cjs new file mode 100644 index 000000000..54b9d85a4 --- /dev/null +++ b/tests/local/arguments-inherited-index.cjs @@ -0,0 +1,22 @@ +"use strict"; + +function read(key) { + return arguments[key]; +} + +const key = 4096; +const previous = Object.getOwnPropertyDescriptor(Object.prototype, String(key)); +let calls = 0; +try { + Object.defineProperty(Object.prototype, String(key), { + configurable: true, + get() { + calls++; + return "inherited"; + }, + }); + console.log(read(0, "own"), read(1, "own"), read(key, "own"), calls); +} finally { + if (previous) Object.defineProperty(Object.prototype, String(key), previous); + else delete Object.prototype[key]; +} diff --git a/tests/local/dense-array-number-region.js b/tests/local/dense-array-number-region.js index 2509ac8c9..92bfbd5c3 100644 --- a/tests/local/dense-array-number-region.js +++ b/tests/local/dense-array-number-region.js @@ -1,5 +1,18 @@ -// Dense Array element sums run in an unboxed loop region; every other element -// shape must decline before it is read and finish in the original loop. +function scatterNumbers(values, output, source, target, count) { + while (--count >= 0) output[target++] = values[source++] * 2; + return [source, target, count]; +} + +function incrementPresent(values) { + for (let index = 0; index < values.length; index++) values[index] += 0.5; + return values; +} + +function addAt(values, key, count) { + while (--count >= 0) values[key] += 0.5; + return values; +} + function sumReloaded(values) { let total = 0; for (let index = 0; index < values.length; index++) total += values[index]; @@ -324,6 +337,178 @@ try { lines.push(`for-of destructured null ${error instanceof TypeError}`); } lines.push(`for-of break ${sumUntilNegative([1, 2, 3, -1, 100])}`); +function recordScatter(label, values, output, source, target, count) { + let state; + try { + state = scatterNumbers(values, output, source, target, count); + } catch (error) { + state = error.name; + } + lines.push( + JSON.stringify([ + label, + state, + Object.keys(output), + Array.from(output, (value) => (Object.is(value, -0) ? "-0" : String(value))), + output["1.5"], + output["2.5"], + output.NaN, + ]), + ); +} + +recordScatter("independent indices", [1, 2, 3, 4], [0, 0, 0, 0, 0], 1, 2, 3); +const aliased = [1, 2, 3, 4, 5]; +recordScatter("aliased forward", aliased, aliased, 0, 1, 4); +const reverseAlias = [1, 2, 3, 4, 5]; +recordScatter("aliased backward", reverseAlias, reverseAlias, 1, 0, 4); +const filledBackwards = new Array(8); +for (let index = filledBackwards.length - 1; index >= 0; index--) + filledBackwards[index] = 0; +recordScatter("filled holes", [1, 2, 3], filledBackwards, 0, 1, 3); +recordScatter( + "non-finite values", + [-0, NaN, Infinity, -Infinity, 2 ** 53], + [0, 0, 0, 0, 0], + 0, + 0, + 5, +); +recordScatter("past source", [1, 2], [0, 0, 0, 0], 0, 0, 4); +recordScatter("extending target", [1, 2, 3, 4], [0, 0], 0, 0, 4); +recordScatter("sealed", [1, 2, 3], Object.seal([0, 0, 0]), 0, 0, 3); +recordScatter("nonextensible", [1, 2, 3], Object.preventExtensions([0, 0, 0]), 0, 0, 3); +recordScatter("frozen", [1, 2, 3], Object.freeze([0, 0, 0]), 0, 0, 3); +recordScatter("frozen zero-trip", [1], Object.freeze([0]), 0, 0, 0); +const fixedLength = [0, 0, 0]; +Object.defineProperty(fixedLength, "length", { writable: false }); +recordScatter("fixed length", [1, 2, 3], fixedLength, 0, 0, 3); +const readonlyElement = [0, 0, 0]; +Object.defineProperty(readonlyElement, "1", { writable: false }); +recordScatter("readonly element", [1, 2, 3], readonlyElement, 0, 0, 3); +const fractionalKeys = [1, 2, 3]; +fractionalKeys["0.5"] = 7; +fractionalKeys["1.5"] = 9; +recordScatter("fractional source", fractionalKeys, [0, 0], 0.5, 0, 2); +recordScatter("fractional target", [1, 2], [0, 0, 0], 0, 1.5, 2); +recordScatter("negative target", [1, 2, 3], [0, 0], 0, -1, 3); +recordScatter("negative zero key", [1, 2], [0, 0], -0, -0, 2); +const notANumber = [1]; +notANumber.NaN = 7; +recordScatter("NaN source", notANumber, [0, 0], NaN, 0, 2); +recordScatter("NaN target", [1, 2], [0, 0], 0, NaN, 2); +const storeEvents = []; +const getterPrototype = Object.create(Array.prototype); +Object.defineProperty(getterPrototype, "1", { + get() { + storeEvents.push("source getter"); + return 5; + }, +}); +const inheritedSource = [1, , 3]; +Object.setPrototypeOf(inheritedSource, getterPrototype); +recordScatter("inherited source", inheritedSource, [0, 0, 0], 0, 0, 3); +const setterPrototype = Object.create(Array.prototype); +Object.defineProperty(setterPrototype, "1", { + set(value) { + storeEvents.push(`target setter ${value}`); + }, +}); +const inheritedTarget = [0, , 0]; +Object.setPrototypeOf(inheritedTarget, setterPrototype); +recordScatter("inherited target", [1, 2, 3], inheritedTarget, 0, 0, 3); +const accessorTarget = [0, 0, 0]; +Object.defineProperty(accessorTarget, "1", { + set(value) { + storeEvents.push(`own setter ${value}`); + }, + get() { + return 13; + }, +}); +recordScatter("own target accessor", [1, 2, 3], accessorTarget, 0, 0, 3); +const shrinkingTarget = [0, 0, 0]; +recordScatter( + "coercion resizes storage", + [ + 1, + { + valueOf() { + storeEvents.push("source coercion"); + shrinkingTarget.length = 0; + return 7; + }, + }, + 3, + ], + shrinkingTarget, + 0, + 0, + 3, +); +const failedTarget = [0, 0, 0]; +recordScatter( + "coercion throws", + [ + 1, + { + valueOf() { + storeEvents.push("throwing coercion"); + throw new RangeError("fixture"); + }, + }, + 3, + ], + failedTarget, + 0, + 0, + 3, +); +const proxyTarget = new Proxy([0, 0, 0], { + set(target, key, value, receiver) { + storeEvents.push(`proxy set ${key}`); + return Reflect.set(target, key, value, receiver); + }, +}); +recordScatter("proxy target", [1, 2, 3], proxyTarget, 0, 0, 3); +const untouchedProxy = new Proxy([], { + get() { + throw new Error("zero-trip receiver was observed"); + }, + set() { + throw new Error("zero-trip receiver was written"); + }, +}); +lines.push(`zero-trip proxy ${scatterNumbers(untouchedProxy, untouchedProxy, 0, 0, 0)}`); +const oldHeapValues = [{ value: 1 }, "old", [3]]; +recordScatter("overwritten heap values", [1, 2, 3], oldHeapValues, 0, 0, 3); +for (const Type of [Float64Array, Int32Array, Uint8Array]) + recordScatter(Type.name, new Type([1, 2, 3, 4]), new Type(4), 1, 0, 3); +const overlappingBuffer = new ArrayBuffer(6 * 8); +const overlappingSource = new Float64Array(overlappingBuffer, 0, 5); +overlappingSource.set([1, 2, 3, 4, 5]); +recordScatter( + "overlapping views", + overlappingSource, + new Float64Array(overlappingBuffer, 8, 5), + 0, + 0, + 4, +); +const manyWrites = Array.from({ length: 2048 }, (_, index) => index * 0.25); +recordScatter( + "polling", + manyWrites, + new Array(manyWrites.length).fill(0), + 0, + 0, + manyWrites.length, +); +lines.push(`increment present ${incrementPresent([1, 2.5, -0])}`); +for (const key of [-0, -1, 0.5, NaN, Infinity, "0"]) + lines.push(`compound key ${String(key)} ${addAt([1, 2], key, 2)[key]}`); +lines.push(`store events ${storeEvents.join(",")}`); + if (process.env.DENSE_ARRAY_PROTOTYPE_HOLE === "1") { Array.prototype[1] = 100; show("prototype hole", sumReloaded([1, , 3])); diff --git a/tests/local/native-integer-loop-regions.js b/tests/local/native-integer-loop-regions.js index e943bf211..6fcdb4e43 100644 --- a/tests/local/native-integer-loop-regions.js +++ b/tests/local/native-integer-loop-regions.js @@ -99,6 +99,19 @@ function hashRange(count) { return hash; } +function multiplyState(start, multiplier, limit) { + let value = start; + for (let index = 0; index < limit; index++) + value = (Math.imul(value, multiplier) + 1013904223) | 0; + return value; +} + +function leadingZeroState(start, limit) { + let value = start; + for (let index = 0; index < limit; index++) value = Math.clz32(value); + return value; +} + function smallestFactor(value) { for (let divisor = 2; divisor * divisor <= value; divisor++) if (value % divisor === 0) return divisor; @@ -207,6 +220,85 @@ function reference(name, values) { check(reference("collatz", [1, 27, 3, 77031].map(collatzSteps)), "collatz"); check(reference("hash", [0, 1000, 77777].map(hashRange)), "hash"); +const intrinsicCases = [ + [0, -1426500812, 27], + [-0, -1426500812, 27], + [1, -1267516731, 27], + [-1, -1585484893, 26], + [1.5, -1267516731, 27], + [NaN, -1426500812, 27], + [Infinity, -1426500812, 27], + [-Infinity, -1426500812, 27], + [2147483648, 720982836, 26], + [4294967295, -1585484893, 26], + [Number.MAX_SAFE_INTEGER, -1585484893, 26], + [-Number.MAX_SAFE_INTEGER, -1267516731, 27], + [2 ** 53, -1426500812, 27], + ["3", -949548569, 27], + [null, -1426500812, 27], + [undefined, -1426500812, 27], +]; +for (const [seed, product, zeros] of intrinsicCases) { + check(multiplyState(seed, 1664525, 4) === product, "integer multiply recurrence"); + check(leadingZeroState(seed, 3) === zeros, "leading zero recurrence"); +} +for (const seed of [ + ...intrinsicCases.map(([seed]) => seed), + { marker: "unobserved seed" }, + 1n, + Symbol("seed"), +]) { + check(Object.is(multiplyState(seed, 3, 0), seed), "multiply zero-trip identity"); + check(Object.is(leadingZeroState(seed, 0), seed), "leading zero zero-trip identity"); +} +let multiplierCoercions = 0; +check( + multiplyState( + 2, + { + valueOf() { + multiplierCoercions++; + return 3; + }, + }, + 3, + ) === 295853065 && multiplierCoercions === 3, + "multiply coerces its multiplier on every fallback iteration", +); +let seedCoercions = 0; +check( + leadingZeroState( + { + valueOf() { + seedCoercions++; + return 1; + }, + }, + 3, + ) === 27 && seedCoercions === 1, + "leading zero coerces its initial seed once", +); +const intrinsicFailure = { marker: "intrinsic failure" }; +let throwingCoercions = 0; +try { + multiplyState( + 2, + { + valueOf() { + if (++throwingCoercions === 2) throw intrinsicFailure; + return 3; + }, + }, + 3, + ); + check(false, "multiply coercion must throw"); +} catch (error) { + check( + error === intrinsicFailure && throwingCoercions === 2, + "multiply abrupt coercion", + ); +} +check(multiplyState(7, 1664525, 100000) === -517394777, "polled multiply recurrence"); check( reference("factor", [97, 2187, 7919 * 7919, 1 << 20].map(smallestFactor)), "factor", diff --git a/tests/native-integer-loop-region-plan.test.ts b/tests/native-integer-loop-region-plan.test.ts index bcd445a4d..5777e07aa 100644 --- a/tests/native-integer-loop-region-plan.test.ts +++ b/tests/native-integer-loop-region-plan.test.ts @@ -1,6 +1,8 @@ import { describe, expect, it } from "vitest"; +import { resolveBuildConfig } from "../src/build-config.ts"; import { analyzeSourceAndRunSemanticAnalysis } from "../src/compiler/frontend/semantic-analysis.ts"; import { compileSemanticProgramToProgramImage } from "../src/compiler/pipeline/compile-core.ts"; +import { compilerProgramFactsFromConfig } from "../src/compiler/shared/compiler-facts.ts"; import { deserializeCompilerArtifact, serializeCompilerArtifact, @@ -13,13 +15,18 @@ import type { ProgramImage } from "../src/compiler/target/program-image.ts"; import type { BytecodeInstruction } from "../src/compiler/target/runtime-image.ts"; import { mergeProgramImages } from "../src/test262/program-image-merge.ts"; -function compile(body: string, profile = false) { +function compile(body: string, profile = false, locked = false) { return compileSemanticProgramToProgramImage( analyzeSourceAndRunSemanticAnalysis( `globalThis.step = function step(start, limit) { ${body} };`, "/integer-loop-plan.js", ), - { profile }, + { + profile, + ...(locked + ? { facts: compilerProgramFactsFromConfig(resolveBuildConfig({})) } + : {}), + }, ); } @@ -38,6 +45,69 @@ function regions(image: ProgramImage): ReadonlyArray { + it("admits a compound element update with a terminal store", () => { + const image = compile(` + for (let index = 0; index < start.length; index++) start[index] += 0.5; + return start; + `); + expect( + regions(image).some( + (plan) => + plan.kind === "fixed-numeric" && plan.views.some((view) => view.writable), + ), + ).toBe(true); + expect( + regions(deserializeCompilerArtifact(serializeCompilerArtifact(image))), + ).toEqual(regions(image)); + }); + + it("checks independent element indices before a terminal loop store", () => { + const image = compile(` + const left = start.left, right = start.right; + let source = 1, target = 2, count = limit; + while (--count >= 0) right[target++] = left[source++] * 2 + 1; + return target; + `); + const [plan] = regions(image); + expect(plan?.kind).toBe("fixed-numeric"); + if (plan?.kind !== "fixed-numeric") throw new Error("Missing numeric storage loop"); + expect(plan.counter).toBeUndefined(); + expect(plan.views.some((view) => view.writable && view.checked)).toBe(true); + expect( + regions(deserializeCompilerArtifact(serializeCompilerArtifact(image))), + ).toEqual(regions(image)); + }); + + it.each([ + "right[target++] = left[source++]; right[target++] = 1;", + "right[target++] = left[source++]; total += left[source++];", + ])("declines iteration replay after an independent store: %s", (body) => { + const image = compile(` + const left = start.left, right = start.right; + let source = 1, target = 2, count = limit, total = 0; + while (--count >= 0) { ${body} } + return total + target; + `); + expect(regions(image)).toEqual([]); + }); + + it.each(["Math.imul(value, 1664525)", "Math.clz32(value)"])( + "keeps the integer builtin recurrence %s in a region", + (expression) => { + const image = compile( + ` + let value = start; + for (let index = 0; index < limit; index++) value = ${expression}; + return value; + `, + false, + true, + ); + expect(regions(image).map((region) => region.kind)).toEqual(["integer"]); + expect(() => serializeCompilerArtifact(image)).not.toThrow(); + }, + ); + it.each([false, true])( "preserves contained fusion admission and profile boundaries with profiling %s", (profile) => { diff --git a/tests/native-preparation.test.ts b/tests/native-preparation.test.ts index ec91f478c..996fdb131 100644 --- a/tests/native-preparation.test.ts +++ b/tests/native-preparation.test.ts @@ -130,19 +130,22 @@ describe("native runtime preparation", () => { expect(ensureNativeArtifacts).toHaveBeenCalledWith(contexts.at(-1)); }); - it("prepares locked instrumentation before arguments forwarding hooks", () => { - setup(selectedProject("arguments-forwarding.test.ts")); - const locked = preparedContexts().filter( - (context) => context.features?.primordialsLocked === true, - ); - expect(locked).toHaveLength(1); - expect(locked[0]?.features).toMatchObject({ - nodeEnabled: false, - webPlatformEnabled: true, - }); - expect(perfStatsDefines(locked[0]?.environment)).toEqual(["-DMAL_PERF_STATS=1"]); - expect(ensureNativeArtifacts).toHaveBeenCalledWith(locked[0]); - }); + it.each(["arguments-forwarding.test.ts", "arguments-alias.test.ts"])( + "prepares locked instrumentation before %s hooks", + (file) => { + setup(selectedProject(file)); + const locked = preparedContexts().filter( + (context) => context.features?.primordialsLocked === true, + ); + expect(locked).toHaveLength(1); + expect(locked[0]?.features).toMatchObject({ + nodeEnabled: false, + webPlatformEnabled: true, + }); + expect(perfStatsDefines(locked[0]?.environment)).toEqual(["-DMAL_PERF_STATS=1"]); + expect(ensureNativeArtifacts).toHaveBeenCalledWith(locked[0]); + }, + ); it.each([ "gc.test.ts", diff --git a/tests/native-typed-array-loop-plan.test.ts b/tests/native-typed-array-loop-plan.test.ts index d16d09d91..73bb42351 100644 --- a/tests/native-typed-array-loop-plan.test.ts +++ b/tests/native-typed-array-loop-plan.test.ts @@ -176,13 +176,12 @@ describe("fixed TypedArray loop storage", () => { expect(deserializeCompilerArtifact(serializeCompilerArtifact(image))).toEqual(image); expect(emitCompiledFunction(native, 1, "", false)).not.toBeNull(); }); - it("declines a store whose key is not the loop index", () => { + it("checks a terminal store whose key is not the loop index", () => { const image = compile("for(let i=0;i plan.kind === "fixed-numeric", - ), - ).toEqual([]); + const [plan] = image.native.functions[1]!.storage!.numericLoopRegions; + if (plan?.kind !== "fixed-numeric") throw new Error("Missing checked storage region"); + expect(plan.counter).toBeUndefined(); + expect(plan.views.every((view) => view.checked === true)).toBe(true); }); it("retains all eight owners for a seven-input numeric kernel", () => { const image = compile( @@ -539,7 +538,7 @@ describe("fixed TypedArray loop storage", () => { ).toEqual([]); }); - it("kills a hoisted constant overwritten by a memory result in the loop", () => { + it("checks indices when a memory result overwrites a hoisted offset", () => { const image = compile("for(let i=0;i { }), }, }; - expect(analyzeNativeFastPaths(changed).plans.numericLoopRegions).toEqual([]); + const [plan] = analyzeNativeFastPaths(changed).plans.numericLoopRegions; + if (plan?.kind !== "fixed-numeric") throw new Error("Missing checked storage region"); + expect(plan.counter).toBeUndefined(); + expect(plan.views.every((view) => view.checked === true)).toBe(true); }); it("requires the induction update to consume this iteration's increment", () => { const native = compile( @@ -603,12 +605,16 @@ describe("fixed TypedArray loop storage", () => { ], }, }).plans.numericLoopRegions; - expect(selected.map((region) => region.kind)).toEqual( - earlyUpdate ? [] : ["fixed-numeric"], - ); + expect(selected.map((region) => region.kind)).toEqual(["fixed-numeric"]); + if (selected[0]?.kind !== "fixed-numeric") + throw new Error("Missing storage region"); + if (earlyUpdate) { + expect(selected[0].counter).toBeUndefined(); + expect(selected[0].views.every((view) => view.checked === true)).toBe(true); + } else expect(selected[0].counter).toBeDefined(); } }); - it("rejects a strided update that reads the previous iteration's arithmetic result", () => { + it("checks indices when a strided update reads the previous iteration's result", () => { const native = compile("for(let i=0;i { ); const step = original[stepIp]!; const instructions = original.with(stepIp, update).with(updateIp, step); - expect( - analyzeNativeFastPaths({ - ...native, - body: { ...native.body, instructions }, - instructions: instructions.map(() => undefined), - }).plans.numericLoopRegions, - ).toEqual([]); + const [checked] = analyzeNativeFastPaths({ + ...native, + body: { ...native.body, instructions }, + instructions: instructions.map(() => undefined), + }).plans.numericLoopRegions; + if (checked?.kind !== "fixed-numeric") + throw new Error("Missing checked storage region"); + expect(checked.counter).toBeUndefined(); + expect(checked.views.every((view) => view.checked === true)).toBe(true); }); it("retains one range and three ordered view accesses through the artifact", () => { const image = compile(body), @@ -660,7 +668,6 @@ describe("fixed TypedArray loop storage", () => { const kindGuards = [...source.matchAll(/\.kind == MAL_TA_FLOAT64/g)]; expect(kindGuards).toHaveLength(plan.views.length); expect(kindGuards.at(-1)!.index).toBeLessThan(source.indexOf("for (;;)")); - expect(source.match(/for \(;;\)/g)).toHaveLength(2); expect(source.match(/mal_vm_fixed_numeric_storage\(/g)).toHaveLength( plan.views.length, ); @@ -700,15 +707,12 @@ describe("fixed TypedArray loop storage", () => { expect(deserializeCompilerArtifact(serializeCompilerArtifact(image))).toEqual(image); }); it.each([ - "for(let i=0;i=0;i++){output[i]=left[i]+right[i];}", @@ -719,11 +723,24 @@ describe("fixed TypedArray loop storage", () => { "for(let i=0;i { + const image = compile(source); + const [plan] = image.native.functions[1]!.storage!.numericLoopRegions; + expect(plan?.kind).toBe("fixed-numeric"); + if (plan?.kind !== "fixed-numeric") throw new Error("Missing checked storage region"); + expect(plan.counter).toBeUndefined(); + expect(plan.views.every((view) => view.checked === true)).toBe(true); + expect(deserializeCompilerArtifact(serializeCompilerArtifact(image))).toEqual(image); + }); + it.each([ + "for(let i=0;i { expect( diff --git a/tests/native/arguments-alias.test.ts b/tests/native/arguments-alias.test.ts index a8e041c86..02c50d62e 100644 --- a/tests/native/arguments-alias.test.ts +++ b/tests/native/arguments-alias.test.ts @@ -29,30 +29,32 @@ function argumentObjects(binary: string): number { return Number(line?.match(/(?:^|\s)objects=([0-9]+)/)?.[1]); } -describe("local arguments aliases", () => { - let compiled: string; - let interpreted: string; - let expected: string; - beforeAll(() => { - ({ compiled, interpreted } = buildBackendPairFromOneProgramImage({ - fixture, - name: "arguments-alias", - outDir, - config: preparedNativeEvalConfig({ primordials: "locked" }), - environment: { ...process.env, MAL_PERF_STATS: "1" }, - })); - expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); - }, 600_000); +describe.each(["mutable", "locked"] as const)( + "local arguments aliases (%s primordials)", + (primordials) => { + let compiled: string; + let interpreted: string; + let expected: string; + beforeAll(() => { + ({ compiled, interpreted } = buildBackendPairFromOneProgramImage({ + fixture, + name: `arguments-alias-${primordials}`, + outDir, + config: preparedNativeEvalConfig({ primordials }), + environment: { ...process.env, MAL_PERF_STATS: "1" }, + })); + expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + }, 600_000); - it("read the same values and object as `arguments`", () => { - for (const binary of [compiled, interpreted]) { - expect(runToStdout(binary)).toBe(expected); - expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); - } - }); + it("read the same values and object as `arguments`", () => { + for (const binary of [compiled, interpreted]) { + expect(runToStdout(binary)).toBe(expected); + expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); + } + }); - it("read the frame without building an arguments object", () => { - // 4000 reading calls; the identity, write, reassignment and escape cases build a few. - expect(argumentObjects(compiled)).toBeLessThan(30); - }); -}); + it("read the frame without building an arguments object", () => { + expect(argumentObjects(compiled)).toBeLessThan(30); + }); + }, +); diff --git a/tests/native/arguments-element-reads.test.ts b/tests/native/arguments-element-reads.test.ts index fbd60bfd6..c9db6f790 100644 --- a/tests/native/arguments-element-reads.test.ts +++ b/tests/native/arguments-element-reads.test.ts @@ -53,12 +53,8 @@ describe.each(["mutable", "locked"] as const)( } }); - it.runIf(primordials === "locked")( - "copy incoming arguments without building an arguments object", - () => { - // 2500 copy-loop calls; the fixture's other cases build fewer than 20 objects. - expect(argumentObjects(compiled)).toBeLessThan(40); - }, - ); + it("copy incoming arguments without building an arguments object", () => { + expect(argumentObjects(compiled)).toBeLessThan(40); + }); }, ); diff --git a/tests/native/dense-array-number-region.test.ts b/tests/native/dense-array-number-region.test.ts index e1064b7f8..0740fe5b0 100644 --- a/tests/native/dense-array-number-region.test.ts +++ b/tests/native/dense-array-number-region.test.ts @@ -103,6 +103,14 @@ describe("dense Array number loop regions", () => { MAL_HOST_GC: "1", DENSE_ARRAY_PROTOTYPE_HOLE: primordials === "mutable" ? "1" : "0", }; + for (const name of ["scatterNumbers", "incrementPresent", "addAt"]) + expect( + regionPlans(pair, name).some( + (plan) => + plan.kind === "fixed-numeric" && plan.views.some((view) => view.writable), + ), + name, + ).toBe(true); const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8", env: { ...process.env, ...env }, @@ -121,6 +129,13 @@ describe("dense Array number loop regions", () => { )?.[1] ?? 0, ), ).toBeGreaterThan(0); + expect( + Number( + measured.stderr.match( + /\[perf-dense-array-region-stats\].* element-stores=(\d+)/, + )?.[1] ?? 0, + ), + ).toBeGreaterThan(0); for (const binary of [pair.compiled, pair.interpreted]) expect( runToStdout(binary, { env: { ...env, ...STRESS_ENV }, timeoutMs: 60000 }), diff --git a/tests/native/dynamic-arguments.test.ts b/tests/native/dynamic-arguments.test.ts new file mode 100644 index 000000000..8f3e68c38 --- /dev/null +++ b/tests/native/dynamic-arguments.test.ts @@ -0,0 +1,46 @@ +import { execFileSync } from "node:child_process"; +import { mkdtempSync, rmSync } from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { afterAll, expect, it } from "vitest"; +import { resolveBuildConfig } from "../../src/build-config.ts"; +import { + buildBackendPairFromOneProgramImage, + runToStdout, + STRESS_ENV, +} from "../../src/test-harness.ts"; + +const outDir = mkdtempSync(path.join(os.tmpdir(), "mal-dynamic-arguments-")); +afterAll(() => rmSync(outDir, { recursive: true, force: true })); + +it.each(["mutable", "locked"] as const)( + "preserves incoming argument identity across nested calls and collections with %s primordials", + (primordials) => { + const fixture = "tests/local/arguments-incoming.cjs"; + const pair = buildBackendPairFromOneProgramImage({ + fixture, + name: `arguments-incoming-${primordials}`, + outDir, + config: resolveBuildConfig({ engine: { primordials, eval: false, realms: false } }), + }); + const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + for (const binary of [pair.compiled, pair.interpreted]) + expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); + }, + 300000, +); + +it("observes inherited out-of-range argument indices", () => { + const fixture = "tests/local/arguments-inherited-index.cjs"; + const pair = buildBackendPairFromOneProgramImage({ + fixture, + name: "arguments-inherited-index", + outDir, + config: resolveBuildConfig({ + engine: { primordials: "mutable", eval: false, realms: false }, + }), + }); + const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + for (const binary of [pair.compiled, pair.interpreted]) + expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); +}, 300000); diff --git a/tests/native/native-integer-loop-regions.test.ts b/tests/native/native-integer-loop-regions.test.ts index 09a4daea0..6a415a939 100644 --- a/tests/native/native-integer-loop-regions.test.ts +++ b/tests/native/native-integer-loop-regions.test.ts @@ -1,4 +1,5 @@ import { beforeAll, describe, expect, it } from "vitest"; +import { resolveBuildConfig } from "../../src/build-config.ts"; import { emitCompiledFunction } from "../../src/compiler/target/render-native-c.ts"; import { assertExactLines, @@ -22,48 +23,61 @@ const kernels = [ "scaledSum", "capturedSum", ]; +const builtinKernels = ["multiplyState", "leadingZeroState"]; const expected = ["native-integer-loop-regions PASS"]; -describe("native safe-integer loop regions", () => { - let compiled: string; - let interpreted: string; - const emittedKernels = new Map(); +describe.each(["mutable", "locked"] as const)( + "native safe-integer loop regions with %s primordials", + (primordials) => { + let compiled: string; + let interpreted: string; + const emittedKernels = new Map(); - beforeAll(() => { - const pair = buildBackendPairFromOneProgramImage({ - fixture: "tests/local/native-integer-loop-regions.js", - name: "native-integer-loop-regions", + beforeAll(() => { + const pair = buildBackendPairFromOneProgramImage({ + fixture: "tests/local/native-integer-loop-regions.js", + name: `native-integer-loop-regions-${primordials}`, + config: resolveBuildConfig({ engine: { primordials } }), + }); + ({ compiled, interpreted } = pair); + const image = pair.programImage; + for (const [index, fn] of image.runtime.functions.entries()) { + const name = String.fromCharCode( + ...(image.runtime.stringConstants[fn.nameStringIndex] ?? []), + ); + if (!kernels.includes(name) && !builtinKernels.includes(name)) continue; + const native = image.native.functions[index]!; + expect(native.storage!.numericLoopRegions).toHaveLength( + kernels.includes(name) || primordials === "locked" ? 1 : 0, + ); + emittedKernels.set(name, emitCompiledFunction(native, index, "", false)!.source); + } + }, 600_000); + + it.each(kernels)("admits the loop of %s once per entry", (name) => { + expect(emittedKernels.get(name)!).toContain("mal_safe_integer_admit("); }); - ({ compiled, interpreted } = pair); - const image = pair.programImage; - for (const [index, fn] of image.runtime.functions.entries()) { - const name = String.fromCharCode( - ...(image.runtime.stringConstants[fn.nameStringIndex] ?? []), - ); - if (!kernels.includes(name)) continue; - const native = image.native.functions[index]!; - expect(native.storage!.numericLoopRegions).toHaveLength(1); - emittedKernels.set(name, emitCompiledFunction(native, index, "", false)!.source); - } - }, 600_000); - it.each(kernels)("admits the loop of %s once per entry", (name) => { - expect(emittedKernels.get(name)!).toContain("mal_safe_integer_admit("); - }); + it.each(builtinKernels)("uses the certified builtin identity in %s", (name) => { + expect(emittedKernels.get(name)?.includes("mal_safe_integer_admit(") ?? false).toBe( + primordials === "locked", + ); + }); - it("matches double semantics through every guard", () => { - assertExactLines(runToStdout(compiled), expected); - }); + it("matches double semantics through every guard", () => { + assertExactLines(runToStdout(compiled), expected); + }); - it( - "returns polled iterations to the original loop under collection stress", - () => { - assertExactLines(runToStdout(compiled, { env: STRESS_ENV }), expected); - }, - scaledNativeRunTimeoutMs(undefined, { ...process.env, ...STRESS_ENV }) + 10_000, - ); + it( + "returns polled iterations to the original loop under collection stress", + () => { + assertExactLines(runToStdout(compiled, { env: STRESS_ENV }), expected); + }, + scaledNativeRunTimeoutMs(undefined, { ...process.env, ...STRESS_ENV }) + 10_000, + ); - it("matches the interpreted semantics", () => { - assertExactLines(runToStdout(interpreted), expected); - }); -}); + it("matches the interpreted semantics", () => { + assertExactLines(runToStdout(interpreted), expected); + }); + }, +); diff --git a/tests/program-image-codec.test.ts b/tests/program-image-codec.test.ts index 5e381e888..94820e800 100644 --- a/tests/program-image-codec.test.ts +++ b/tests/program-image-codec.test.ts @@ -211,6 +211,7 @@ const instructions: Array = [ { opcode: "SET_PROTOTYPE", object: 10, prototype: 13, literal: true }, { opcode: "LOAD_ARGUMENT_COUNT", dst: 14 }, { opcode: "LOAD_ARGUMENT", dst: 14, index: 2 }, + { opcode: "LOAD_DYNAMIC_ARGUMENT", dst: 14, key: 1 }, { opcode: "LOAD_STATIC_ARGUMENT", dst: 14, @@ -1013,7 +1014,7 @@ describe("program-image-codec", () => { it("covers every opcode in the wire table", () => { expect(new Set(WIRE_OPCODES).size).toBe(WIRE_OPCODES.length); - expect(WIRE_OPCODES.slice(-23)).toEqual([ + const appended = [ "INIT_GLOBAL_VARS", "CREATE_PRIVATE_NAMES", "INIT_PRIVATE_FIELDS", @@ -1037,7 +1038,9 @@ describe("program-image-codec", () => { "PRECISE_NUMBER_SUM", "STRING_CONCAT_LENGTH", "CONCAT_STRINGS", - ]); + "LOAD_DYNAMIC_ARGUMENT", + ]; + expect(WIRE_OPCODES.slice(-appended.length)).toEqual(appended); }); it("round-trips template construction and rejects invalid bounds and registers", () => { diff --git a/tests/test-suite-native-sanitizer.txt b/tests/test-suite-native-sanitizer.txt index 33c56fb9c..59672f2f4 100644 --- a/tests/test-suite-native-sanitizer.txt +++ b/tests/test-suite-native-sanitizer.txt @@ -131,6 +131,7 @@ tests/native/data-view-region.test.ts tests/native/typed-array-length-bound.test.ts tests/native/string-scan-region.test.ts tests/native/dense-array-number-region.test.ts +tests/native/dynamic-arguments.test.ts tests/native/static-property-projection.test.ts tests/native/writable-stream.test.ts diff --git a/tests/test-suite-test262-check.txt b/tests/test-suite-test262-check.txt index a8d93e6f4..3f77a0760 100644 --- a/tests/test-suite-test262-check.txt +++ b/tests/test-suite-test262-check.txt @@ -212,7 +212,7 @@ test/language/statements/await-using/initializer-Symbol.dispose-called-at-end-of test/language/statements/using/initializer-disposed-at-end-of-forstatement.js test/language/statements/using/throws-suppressederror-if-multiple-errors-during-disposal.js -# Binding, coercion, and eval environments (49) +# Binding, coercion, and eval environments test/built-ins/JSON/stringify/replacer-function-object-deleted-property.js test/built-ins/Object/create/properties-arg-to-object-bigint.js test/built-ins/eval/no-construct.js @@ -237,9 +237,11 @@ test/language/expressions/assignment/dstr/obj-prop-put-let.js test/language/expressions/class/dstr/private-gen-meth-static-ary-ptrn-elem-obj-prop-id-init.js test/language/expressions/class/elements/new-no-sc-line-method-rs-privatename-identifier.js test/language/expressions/compound-assignment/11.13.2-37-s.js +test/language/expressions/compound-assignment/S11.13.2_A7.1_T4.js test/language/expressions/compound-assignment/compound-assignment-operator-calls-putvalue-lref--v-.js test/language/expressions/less-than-or-equal/S11.8.3_A3.1_T1.2.js test/language/expressions/postfix-decrement/bigint.js +test/language/expressions/postfix-increment/S11.3.1_A6_T3.js test/language/expressions/prefix-increment/bigint.js test/language/expressions/prefix-increment/operator-prefix-increment-x-calls-putvalue-lhs-newvalue-.js test/language/function-code/10.4.3-1-54gs.js