diff --git a/docs/decisions/runtime-region-contracts.md b/docs/decisions/runtime-region-contracts.md index 6d6e64c5e..46144f976 100644 --- a/docs/decisions/runtime-region-contracts.md +++ b/docs/decisions/runtime-region-contracts.md @@ -141,6 +141,12 @@ 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. +Integer-heavy Array bodies first try safe-integer locals. Entry inputs fit the signed +or unsigned 32-bit range; loaded elements and intermediate arithmetic remain exact +safe integers other than negative zero. Invariant input guards also expose known +bounds to the C compiler. A failed guard restores the current iteration's start and +enters the Number body. Arithmetic that could restart after a store excludes this +specialization. DataView accessor calls join fixed storage regions as byte views. Admission uses the conditions that make `byteLength` the intrinsic getter: the watched-prototype diff --git a/runtime/src/heap_string.c b/runtime/src/heap_string.c index 491b62308..42649a935 100644 --- a/runtime/src/heap_string.c +++ b/runtime/src/heap_string.c @@ -1098,9 +1098,11 @@ static bool mal_string_try_append( left->parent->length == left->length) { buffer = left->parent; if (buffer->latin1 && !right->latin1) return false; - } else if (left->storage != MAL_STRING_STORAGE_CONS || right->length > MAL_STRING_APPEND_PIECE_MAX) { - // Only a rope that an earlier append produced starts a buffer, so a single - // concatenation stays one constant-size cons node. + } else if (left->storage != MAL_STRING_STORAGE_CONS || right->length > MAL_STRING_APPEND_PIECE_MAX || + (left->left->storage != MAL_STRING_STORAGE_CONS && + left->right->storage != MAL_STRING_STORAGE_CONS)) { + // A second rope link distinguishes a growing chain from a short + // concatenation before paying for a backing buffer and dependent prefix. return false; } bool latin1 = buffer != nullptr ? buffer->latin1 : left->latin1 && right->latin1; diff --git a/runtime/src/perf_stats.c b/runtime/src/perf_stats.c index 2bedd4424..2328edd2f 100644 --- a/runtime/src/perf_stats.c +++ b/runtime/src/perf_stats.c @@ -1046,9 +1046,10 @@ 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 element-stores=%llu\n", + fprintf(stderr, "[perf-dense-array-region-stats] admissions=%llu element-stores=%llu integer-back-edges=%llu\n", (unsigned long long) mal_perf_stats.dense_array_number_region_admissions, - (unsigned long long) mal_perf_stats.dense_array_number_region_stores); + (unsigned long long) mal_perf_stats.dense_array_number_region_stores, + (unsigned long long) mal_perf_stats.dense_array_integer_region_back_edges); 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 15c3daadd..a0c6c40dc 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_integer_region_back_edges; u64 dense_array_number_region_stores; u64 record_iteration_region_admissions; u64 record_columns_admissions; diff --git a/runtime/src/value_ops.h b/runtime/src/value_ops.h index 22fad1399..3a09ec018 100644 --- a/runtime/src/value_ops.h +++ b/runtime/src/value_ops.h @@ -154,6 +154,16 @@ static inline bool mal_safe_integer_admit(f64 value, i64 *out) { return true; } +/** Admit without coercion, retaining an int32 payload instead of widening it first. */ +static inline bool mal_safe_integer_value_admit(MalValue value, i64 *out) { + if (value >= MAL_VALUE_TAG_BASE) { + if (value >= MAL_VALUE_STATIC) return false; + *out = mal_value_to_i32(value); + return true; + } + return mal_safe_integer_admit(mal_value_to_f64(value), out); +} + static inline bool mal_safe_integer_fits(i64 value, i64 *out) { if (value > MAL_SAFE_INTEGER_MAX || value < -MAL_SAFE_INTEGER_MAX) return false; *out = value; diff --git a/src/compiler/core/core-ir-loops.ts b/src/compiler/core/core-ir-loops.ts index 30881adf5..7c57722e3 100644 --- a/src/compiler/core/core-ir-loops.ts +++ b/src/compiler/core/core-ir-loops.ts @@ -631,6 +631,9 @@ export function analyzeCoreLoopInductions( 0, constant === undefined ? 0xffff_ffff : 0xffff_ffff >>> (constant & 31), ); + } else if (operator === ">>" && constant !== undefined) { + const shift = constant & 31; + range = numericRange(-0x8000_0000 >> shift, 0x7fff_ffff >> shift); } else if ( operator === "&" && constant !== undefined && diff --git a/src/compiler/core/core-native-instruction-facts.ts b/src/compiler/core/core-native-instruction-facts.ts index 2d8249064..3fdf5f94f 100644 --- a/src/compiler/core/core-native-instruction-facts.ts +++ b/src/compiler/core/core-native-instruction-facts.ts @@ -153,6 +153,7 @@ const pairInt32Inputs = Object.freeze([ Object.freeze([COMPILER_INT32_RANGE, COMPILER_INT32_RANGE]), ]); const boundBinaryConsumers: ReadonlySet = new Set([ + "*", "%", "<", "<=", diff --git a/src/compiler/target/lower-native-fast-paths.ts b/src/compiler/target/lower-native-fast-paths.ts index ec84e24ae..3d064c55a 100644 --- a/src/compiler/target/lower-native-fast-paths.ts +++ b/src/compiler/target/lower-native-fast-paths.ts @@ -1710,6 +1710,7 @@ export function numericLoopHasTerminalStore( fn: BytecodeFunction, header: number, order: ReadonlyArray, + arithmetic: "number" | "checked-integer" = "number", ): boolean { const stores = order.filter((ip) => fn.instructions[ip]!.opcode === "STORE_PROPERTY"); if (stores.length !== 1) return false; @@ -1729,6 +1730,12 @@ export function numericLoopHasTerminalStore( if (ip === header || !members.has(ip) || seen.has(ip)) continue; seen.add(ip); const op = fn.instructions[ip]!; + if ( + arithmetic === "checked-integer" && + ((op.opcode === "BINARY" && ["+", "-", "*", "/", "%"].includes(op.operator)) || + (op.opcode === "UNARY" && ["-", "increment", "decrement"].includes(op.operator))) + ) + return false; if ( !integerLoopInstruction(op) && !["LOAD_GLOBAL", "STORE_GLOBAL", "LOAD_CAPTURED", "THROW_IF_TDZ"].includes( @@ -1741,6 +1748,47 @@ export function numericLoopHasTerminalStore( return true; } +/** Checked arithmetic must finish before a store, so a failure can resume its iteration. */ +export function numericLoopSupportsCheckedIntegers( + fn: BytecodeFunction, + header: number, + order: ReadonlyArray, +): boolean { + if ( + !order.some((ip) => { + const op = fn.instructions[ip]!; + return ( + (op.opcode === "BINARY" && INTEGER_LOOP_PROFITABLE_OPERATORS.has(op.operator)) || + (op.opcode === "UNARY" && op.operator === "~") + ); + }) + ) + return false; + if ( + !order.every((ip) => { + const op = fn.instructions[ip]!; + return ( + integerLoopInstruction(op) || + [ + "LOAD_PROPERTY", + "STORE_PROPERTY", + "TO_PROPERTY_KEY", + "LOAD_PROPERTY_STATIC", + "LOAD_PROPERTY_STATIC_ARRAY_LENGTH", + "LOAD_GLOBAL", + "LOAD_CAPTURED", + "THROW_IF_TDZ", + ].includes(op.opcode) + ); + }) + ) + return false; + return ( + !order.some((ip) => fn.instructions[ip]!.opcode === "STORE_PROPERTY") || + numericLoopHasTerminalStore(fn, header, order, "checked-integer") + ); +} + function bytecodePredecessors( fn: BytecodeFunction, body: NativeBodyFacts, diff --git a/src/compiler/target/render-native-c.ts b/src/compiler/target/render-native-c.ts index 137f5d930..6522d1e39 100644 --- a/src/compiler/target/render-native-c.ts +++ b/src/compiler/target/render-native-c.ts @@ -91,6 +91,7 @@ import { nativeDataViewAccessor, numericLoopCallMethods, numericLoopHasTerminalStore, + numericLoopSupportsCheckedIntegers, numericLoopOrder, nativeNumericBuiltinInstruction, lowerNativeFastPaths, @@ -2019,6 +2020,7 @@ function emitNumericLoopRegion( facts: NativeFunctionPlan["facts"], nativeInstructions: NativeFunctionPlan["instructions"], poll: (ip: number, spill: string, isolate: string) => string, + arrayArithmetic?: "number" | "checked-integer", ): ReadonlyArray { if (plan.kind === "array-iteration" && plan.recordGraph !== undefined) return emitRecordIterationRegion(fn, plan, reps, propertyCache, poll); @@ -2084,7 +2086,8 @@ function emitNumericLoopRegion( ]); if (order === undefined || flow === undefined) throw new Error("Invalid native integer loop region"); - const floating = plan.kind !== "integer"; + const floating = plan.kind !== "integer" && arrayArithmetic !== "checked-integer"; + const arrayNumberFallback = `__array_number_fallback_${plan.id}`; // The deferred cursor's index is the iteration state a restart must rewind. const cursor = plan.kind === "array-iteration" ? `__iter_deferred_${plan.initializeIp}` : undefined; @@ -2200,6 +2203,10 @@ function emitNumericLoopRegion( initial.set(register, `(i64) r${register}`); continue; } + if (representation === "boxed" && arrayArithmetic === "checked-integer") { + guards.push(`mal_safe_integer_value_admit(r${register}, &${local(register)})`); + continue; + } const value = representation === "boxed" ? `mal_ops_number_as_f64(r${register})` @@ -2220,6 +2227,35 @@ function emitNumericLoopRegion( ); else guards.push(`mal_safe_integer_admit(${value}, &${local(register)})`); } + if (arrayArithmetic === "checked-integer") { + const written = new Set( + order.flatMap((ip) => vmInstructionWriteRegisters(fn.instructions[ip]!)), + ); + for (const register of plan.inputs) { + if (kindOf(register) === "boolean") continue; + const ranges = written.has(register) + ? [] + : order.flatMap((ip) => { + const range = instructionInputIntegerRange(facts?.[ip], register); + return range === undefined ? [] : [range]; + }); + const low = Math.max( + ADMITTED_INPUT_RANGE.low, + ranges.length === 0 + ? -Infinity + : Math.min(...ranges.map((range) => range.minimum)), + ); + const high = Math.min( + ADMITTED_INPUT_RANGE.high, + ranges.length === 0 + ? Infinity + : Math.max(...ranges.map((range) => range.maximum)), + ); + guards.push( + `(${local(register)} >= INT64_C(${low}) && ${local(register)} <= INT64_C(${high}))`, + ); + } + } // An Array overwrite may commit only after the iteration's last restarting operation. const arrayAlternative = plan.kind === "fixed-numeric" && @@ -2227,13 +2263,39 @@ function emitNumericLoopRegion( (plan.views.every((view) => !view.writable) || numericLoopHasTerminalStore(fn, plan.id, order)); const records = new Set(plan.kind !== "integer" ? (plan.records ?? []) : []); + if ( + arrayArithmetic === undefined && + plan.kind === "fixed-numeric" && + arrayAlternative && + records.size === 0 && + globals.length === 0 && + plan.views.every((view) => view.text !== true && view.lentOnly !== true) && + numericLoopSupportsCheckedIntegers(fn, plan.id, order) + ) { + const emit = (arithmetic: "number" | "checked-integer") => + emitNumericLoopRegion( + fn, + plan, + reps, + propertyCache, + bindingLoad, + globalSlot, + facts, + nativeInstructions, + poll, + arithmetic, + ); + return [...emit("checked-integer"), `${arrayNumberFallback}:;`, ...emit("number")]; + } const checkedViews = new Set( plan.kind === "fixed-numeric" ? plan.views.flatMap((view) => (view.checked === true ? [view.object] : [])) : [], ); // Records exist only for Array views. - const arrayOnly = plan.kind === "fixed-numeric" && records.size > 0; + const arrayOnly = + plan.kind === "fixed-numeric" && + (records.size > 0 || arrayArithmetic === "checked-integer"); const views = plan.kind === "fixed-numeric" ? plan.views : []; const textViews = views.filter((view) => view.text === true); const elementViews = views.filter( @@ -2467,7 +2529,7 @@ function emitNumericLoopRegion( ...stored.map((index) => `${slotStart(index)} = ${slot(index)};`), ].join( " ", - )}${cursor === undefined ? "" : ` ${id}_si = ${id}_ci;`} if (mal_gc_poll_requested_in(${id}_isolate)) ${restart} continue;`; + )}${cursor === undefined ? "" : ` ${id}_si = ${id}_ci;`}${arrayArithmetic === "checked-integer" ? " MAL_PERF_COUNT(dense_array_integer_region_back_edges);" : ""} if (mal_gc_poll_requested_in(${id}_isolate)) ${restart} continue;`; if (innerHeaders.has(ip)) return `${position.get(ip)! <= position.get(from)! ? `if (mal_gc_poll_requested_in(${id}_isolate)) { ${id}_exit = ${exitIndex.get(ip)}; break; } ` : ""}goto ${label(mode, ip)};`; const exit = exitIndex.get(ip); @@ -2620,16 +2682,19 @@ function emitNumericLoopRegion( return `{ ${bound}${local(op.dst)} = ${integral.has(op.dst) ? "(i64)" : "(f64)"} mal_string_unit_span_at(&${id}_s${op.thisValue}, (u32) ${key}); }`; } case "LOAD_PROPERTY": { - const key = integral.has(op.key) ? local(op.key) : num(op.key); + const integerKey = !floating || integral.has(op.key); + const key = integerKey ? local(op.key) : num(op.key); const length = `${id}_${mode === "array" ? "a" : "v"}${op.object}.${mode === "array" ? "count" : "length"}`; // Any other key leaves the view to the generic load, which handles holes and non-indices. const bound = !checkedViews.has(op.object) ? "" - : integral.has(op.key) + : integerKey ? `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" && records.has(op.dst)) return `{ ${bound}MalValue ${id}_e = ${id}_a${op.object}.elements[(u32) ${key}]; if (__builtin_expect(!mal_value_is_heap_type(${id}_e, MAL_HEAP_OBJECT), 0)) ${restart} ${local(op.dst)} = ${id}_e; }`; + if (mode === "array" && arrayArithmetic === "checked-integer") + return `{ ${bound}MalValue ${id}_e = ${id}_a${op.object}.elements[(u32) ${key}]; if (__builtin_expect(!mal_safe_integer_value_admit(${id}_e, &${local(op.dst)}), 0)) ${restart} }`; if (mode === "array") return `{ ${bound}MalValue ${id}_e = ${id}_a${op.object}.elements[(u32) ${key}]; if (__builtin_expect(!mal_ops_is_number(${id}_e), 0)) ${restart} ${local(op.dst)} = mal_ops_number_as_f64(${id}_e); }`; if (mode === "f64") @@ -2656,11 +2721,12 @@ 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": { - const key = integral.has(op.key) ? local(op.key) : num(op.key); + const integerKey = !floating || integral.has(op.key); + const key = integerKey ? 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) + : integerKey ? `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") @@ -2927,46 +2993,50 @@ function emitNumericLoopRegion( for (const index of stored) lines.push(` ${globalSlot(index)} = ${boxSlot(slotStart(index))};`); if (cursor !== undefined) lines.push(` ${cursor}.index = ${id}_si;`); - lines.push(` goto L${plan.id};`); + lines.push( + ` goto ${arrayArithmetic === "checked-integer" ? arrayNumberFallback : `L${plan.id}`};`, + ); integral = baseIntegral; return lines; }; - const typedBodies = textual - ? [" MAL_PERF_COUNT(fixed_typed_array_region_admissions);", ...loopBody("text")] - : plan.kind === "fixed-numeric" - ? [ - " MAL_PERF_COUNT(fixed_typed_array_region_admissions);", - ...((counter?.stride ?? 1) > 1 - ? [" MAL_PERF_COUNT(fixed_typed_array_stride_regions);"] - : []), - ...(views.some((view) => view.minOffset !== 0 || view.maxOffset !== 0) - ? [" MAL_PERF_COUNT(fixed_typed_array_affine_regions);"] - : []), - ...elementViews.map( - (view) => - ` MAL_PERF_COUNT(fixed_typed_array_region_views[${id}_v${view.object}.kind]);`, - ), - ...dataViews.map(() => " MAL_PERF_COUNT(fixed_data_view_region_views);"), - // DataView accesses name their own element types, so only element views select a body. - ...(elementViews.length === 0 - ? loopBody("typed") - : [ - // One bounded specialization keeps kind dispatch out of the established Float64 loop. - ` if (${elementViews.map((view) => `${id}_v${view.object}.kind == MAL_TA_FLOAT64`).join(" && ")}) {`, - " MAL_PERF_COUNT(fixed_typed_array_region_f64_admissions);", - ...loopBody("f64").map((line) => ` ${line}`), - ...(!integerBody - ? [] - : [ - ` } else if (${elementViews.map((view) => `${id}_v${view.object}.kind < MAL_TA_FLOAT32`).join(" && ")}) {`, - ...loopBody("integer").map((line) => ` ${line}`), - ]), - " } else {", - ...loopBody("typed").map((line) => ` ${line}`), - " }", - ]), - ] - : loopBody("typed"); + const typedBodies = arrayOnly + ? [] + : textual + ? [" MAL_PERF_COUNT(fixed_typed_array_region_admissions);", ...loopBody("text")] + : plan.kind === "fixed-numeric" + ? [ + " MAL_PERF_COUNT(fixed_typed_array_region_admissions);", + ...((counter?.stride ?? 1) > 1 + ? [" MAL_PERF_COUNT(fixed_typed_array_stride_regions);"] + : []), + ...(views.some((view) => view.minOffset !== 0 || view.maxOffset !== 0) + ? [" MAL_PERF_COUNT(fixed_typed_array_affine_regions);"] + : []), + ...elementViews.map( + (view) => + ` MAL_PERF_COUNT(fixed_typed_array_region_views[${id}_v${view.object}.kind]);`, + ), + ...dataViews.map(() => " MAL_PERF_COUNT(fixed_data_view_region_views);"), + // DataView accesses name their own element types, so only element views select a body. + ...(elementViews.length === 0 + ? loopBody("typed") + : [ + // One bounded specialization keeps kind dispatch out of the established Float64 loop. + ` if (${elementViews.map((view) => `${id}_v${view.object}.kind == MAL_TA_FLOAT64`).join(" && ")}) {`, + " MAL_PERF_COUNT(fixed_typed_array_region_f64_admissions);", + ...loopBody("f64").map((line) => ` ${line}`), + ...(!integerBody + ? [] + : [ + ` } else if (${elementViews.map((view) => `${id}_v${view.object}.kind < MAL_TA_FLOAT32`).join(" && ")}) {`, + ...loopBody("integer").map((line) => ` ${line}`), + ]), + " } else {", + ...loopBody("typed").map((line) => ` ${line}`), + " }", + ]), + ] + : loopBody("typed"); const arrayBody = (): ReadonlyArray => [ " MAL_PERF_COUNT(dense_array_number_region_admissions);", ...loopBody("array"), diff --git a/tests/local/checked-array-integers.cjs b/tests/local/checked-array-integers.cjs new file mode 100644 index 000000000..a04194f99 --- /dev/null +++ b/tests/local/checked-array-integers.cjs @@ -0,0 +1,79 @@ +"use strict"; + +function scatter(source, target, factor, carry) { + let index = 0; + let destination = 0; + let count = source.length; + while (--count >= 0) { + carry = source[index++] * factor + carry; + target[destination++] = carry | 0; + } + return [target, carry, index, destination]; +} + +function lastProduct(source, factor) { + let index = 0; + let result = 1; + let bits = 0; + while (index < source.length) { + bits ^= index; + result = source[index++] * factor; + } + return [Object.is(result, -0), result, bits, Number.isNaN(result), result === Infinity]; +} + +function divide(source, divisor, carry) { + let index = 0; + let bits = 0; + while (index < source.length) { + carry = carry / divisor + source[index++]; + bits ^= index; + } + return [carry, bits]; +} + +function storeThenMultiply(array, value) { + let count = 1; + while (count-- > 0) { + array[0] = (array[0] + 1) | 0; + value = value * 3; + } + return [array[0], value]; +} + +function shiftedBigInt(input, shift) { + const value = input >> shift; + return value < 0n ? value * -3n : value * 2n; +} + +console.log("integer", JSON.stringify(scatter([1, 2, 3], [0, 0, 0], 3, 7))); +console.log("negative", JSON.stringify(lastProduct([-2], 3))); +console.log("fraction", JSON.stringify(scatter([1, 2.5, 3], [0, 0, 0], 3, 7))); +console.log("wide", JSON.stringify(lastProduct([Number.MAX_SAFE_INTEGER], 3))); +console.log("unsigned", JSON.stringify(lastProduct([0xffffffff], 3))); +console.log("past unsigned", JSON.stringify(lastProduct([2 ** 32], 3))); +console.log("wide carry", JSON.stringify(scatter([1, 2, 3], [0, 0, 0], 3, 2 ** 40))); +console.log("negative zero", JSON.stringify(lastProduct([0], -1))); +console.log("input zero", JSON.stringify(lastProduct([-0], 1))); +console.log("infinity", JSON.stringify(lastProduct([Infinity], 1))); +console.log("nan", JSON.stringify(lastProduct([NaN], 1))); +console.log("fractional division", JSON.stringify(divide([1, 2, 3], 2, 7))); +console.log("division by zero", JSON.stringify(divide([1, 2], 0, 7))); +console.log( + "post-store", + JSON.stringify(storeThenMultiply([0], Number.MAX_SAFE_INTEGER)), +); +console.log("bigint", String(shiftedBigInt(16n, 2n)), String(shiftedBigInt(-16n, 1n))); + +let conversions = 0; +const boxed = { + valueOf() { + conversions++; + return 2.5; + }, +}; +console.log( + "coercion", + JSON.stringify(scatter([1, boxed, 3], [0, 0, 0], 3, 7)), + conversions, +); diff --git a/tests/local/string-append-buffer.js b/tests/local/string-append-buffer.js index ea7110479..74d735c17 100644 --- a/tests/local/string-append-buffer.js +++ b/tests/local/string-append-buffer.js @@ -20,12 +20,12 @@ lines.push( `slice ${frozen === base} ${frozen.length} ${extended.endsWith("-more")} ${frozen.endsWith(",")}`, ); -const wide = grow("latin:", 40, (index) => (index === 20 ? "☃" : "x")); +const wide = grow("latin:", 180, (index) => (index === 140 ? "☃" : "x")); lines.push( - `encoding ${wide.length} ${wide.indexOf("☃")} ${wide.charCodeAt(26)} ${wide.at(-1)}`, + `encoding ${wide.length} ${wide.indexOf("☃")} ${wide.charCodeAt(146)} ${wide.at(-1)}`, ); -const widened = grow("w:", 60, (index) => String.fromCharCode(97 + (index % 26))); +const widened = grow("w:", 180, (index) => String.fromCharCode(97 + (index % 26))); const unit = widened.charCodeAt(30); const afterWiden = widened + "é" + "tail"; lines.push(`widen ${unit} ${afterWiden.slice(-5)} ${afterWiden.length}`); diff --git a/tests/native-integer-loop-region-plan.test.ts b/tests/native-integer-loop-region-plan.test.ts index 5777e07aa..c422612df 100644 --- a/tests/native-integer-loop-region-plan.test.ts +++ b/tests/native-integer-loop-region-plan.test.ts @@ -8,7 +8,11 @@ import { serializeCompilerArtifact, } from "../src/compiler/target/compiler-artifact-codec.ts"; import type { NativeNumericLoopRegionPlan } from "../src/compiler/target/lower-native-fast-paths.ts"; -import { numericLoopShape } from "../src/compiler/target/lower-native-fast-paths.ts"; +import { + numericLoopOrder, + numericLoopShape, + numericLoopSupportsCheckedIntegers, +} from "../src/compiler/target/lower-native-fast-paths.ts"; import { validateNativeProgramStorage } from "../src/compiler/target/lower-native-storage.ts"; import { analyzeNativeBodyFacts } from "../src/compiler/target/native-body-facts.ts"; import type { ProgramImage } from "../src/compiler/target/program-image.ts"; @@ -45,6 +49,35 @@ function regions(image: ProgramImage): ReadonlyArray { + it.each([ + ["limit = limit | 0", true], + ["limit = ~limit", true], + ["limit = limit + 1", false], + ["limit = limit - 1", false], + ["limit = limit * 3", false], + ["limit = limit / 3", false], + ["limit = limit % 3", false], + ["limit = -limit", false], + ["limit++", false], + ] as const)("keeps the store commit boundary before %s", (suffix, admitted) => { + const image = compile(` + let count = 1; + while (count-- > 0) { start[0] = (start[0] + 1) | 0; ${suffix}; } + return limit; + `); + const fn = image.native.functions[1]!; + const plan = regions(image).find((candidate) => candidate.kind === "fixed-numeric"); + if (plan === undefined) throw new Error("Missing Array number region"); + const order = numericLoopOrder( + fn.body, + plan.id, + new Set(plan.claimedIps.filter((ip) => ip !== plan.entryIp)), + true, + ); + if (order === undefined) throw new Error("Missing region instruction order"); + expect(numericLoopSupportsCheckedIntegers(fn.body, plan.id, order)).toBe(admitted); + }); + 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; diff --git a/tests/native-integer-range-facts.test.ts b/tests/native-integer-range-facts.test.ts index 4e457fc66..08f335d20 100644 --- a/tests/native-integer-range-facts.test.ts +++ b/tests/native-integer-range-facts.test.ts @@ -20,6 +20,56 @@ import { nativeInt32ArithmeticExpression } from "../src/compiler/target/native-s import { inspectStaticValueFunction } from "./helpers/static-values.ts"; describe("native integer ranges", () => { + it("leaves dynamic BigInt shifts outside Number bounds", () => { + const out = inspectStaticValueFunction( + "function compute(input,shift,gate){const n=input>>shift;gate(n);return n*2n;}globalThis.compute=compute;", + "compute", + ); + const ip = out.native.body.instructions.findIndex( + (op) => op.opcode === "BINARY" && op.operator === "*", + ); + const op = out.native.body.instructions[ip]!; + if (op.opcode !== "BINARY") throw new Error("Missing BigInt multiply"); + expect(instructionInputIntegerRange(out.native.facts?.[ip], op.left)).toBeUndefined(); + }); + + it.each([0, 1, 14, 31, 32, 33, -1, -32, 1.5])( + "retains a signed shift's bounds at multiplication for shift %s", + (shift) => { + const out = inspectStaticValueFunction( + `function compute(input,other,gate){const n=input>>${shift};gate(n);return n*(other|0);}globalThis.compute=compute;`, + "compute", + ); + const ip = out.native.body.instructions.findIndex( + (op) => op.opcode === "BINARY" && op.operator === "*", + ); + const op = out.native.body.instructions[ip]!; + if (op.opcode !== "BINARY") throw new Error("Missing multiply consumer"); + expect(instructionInputIntegerRange(out.native.facts?.[ip], op.left)).toEqual({ + ...COMPILER_INT32_RANGE, + minimum: -0x8000_0000 >> shift, + maximum: 0x7fff_ffff >> shift, + }); + }, + ); + + it("retains a constant mask's bounds at multiplication", () => { + const out = inspectStaticValueFunction( + "function compute(input,other,gate){const n=input&16383;gate(n);return n*(other|0);}globalThis.compute=compute;", + "compute", + ); + const ip = out.native.body.instructions.findIndex( + (op) => op.opcode === "BINARY" && op.operator === "*", + ); + const op = out.native.body.instructions[ip]!; + if (op.opcode !== "BINARY") throw new Error("Missing multiply consumer"); + expect(instructionInputIntegerRange(out.native.facts?.[ip], op.left)).toEqual({ + ...COMPILER_INT32_RANGE, + minimum: 0, + maximum: 16383, + }); + }); + it("uses the external operand's bounds in a numeric fusion's integer finish", () => { const out = inspectStaticValueFunction( "function compute(left,right,gate){const n=(right&255)+1;gate(n);return (left+2)^n;}globalThis.compute=compute;", diff --git a/tests/native/checked-array-integers.test.ts b/tests/native/checked-array-integers.test.ts new file mode 100644 index 000000000..db0dc8c66 --- /dev/null +++ b/tests/native/checked-array-integers.test.ts @@ -0,0 +1,42 @@ +import { execFileSync, spawnSync } 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-checked-array-integers-")); +afterAll(() => rmSync(outDir, { recursive: true, force: true })); + +it.each(["mutable", "locked"] as const)( + "preserves Array writes and Number fallback with %s primordials", + (primordials) => { + const fixture = "tests/local/checked-array-integers.cjs"; + const pair = buildBackendPairFromOneProgramImage({ + fixture, + name: `checked-array-integers-${primordials}`, + outDir, + config: resolveBuildConfig({ engine: { primordials } }), + environment: { ...process.env, MAL_PERF_STATS: "1" }, + }); + const expected = execFileSync(process.execPath, [fixture], { encoding: "utf8" }); + const measured = spawnSync(pair.compiled, [], { + encoding: "utf8", + timeout: 60000, + env: { ...process.env, MAL_PERF_STATS: "1" }, + }); + expect(measured.status, measured.stderr).toBe(0); + expect(measured.stdout).toBe(expected); + expect( + Number(measured.stderr.match(/integer-back-edges=(\d+)/)?.[1] ?? 0), + ).toBeGreaterThan(0); + for (const binary of [pair.compiled, pair.interpreted]) + expect(runToStdout(binary, { env: STRESS_ENV, timeoutMs: 60000 })).toBe(expected); + }, + 300000, +); diff --git a/tests/test-suite-native-sanitizer.txt b/tests/test-suite-native-sanitizer.txt index 59672f2f4..dd91de755 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/checked-array-integers.test.ts tests/native/dynamic-arguments.test.ts tests/native/static-property-projection.test.ts tests/native/writable-stream.test.ts @@ -204,6 +205,7 @@ tests/native/map-storage.test.ts tests/native/weak-storage.test.ts tests/native/string-rope-followups.test.ts +tests/native/string-append-buffer.test.ts tests/native/string-cursor-frontier.test.ts tests/native/string-search-followups.test.ts