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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/decisions/runtime-region-contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions runtime/src/heap_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/perf_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions runtime/src/perf_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions runtime/src/value_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/core/core-ir-loops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
1 change: 1 addition & 0 deletions src/compiler/core/core-native-instruction-facts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ const pairInt32Inputs = Object.freeze([
Object.freeze([COMPILER_INT32_RANGE, COMPILER_INT32_RANGE]),
]);
const boundBinaryConsumers: ReadonlySet<string> = new Set([
"*",
"%",
"<",
"<=",
Expand Down
48 changes: 48 additions & 0 deletions src/compiler/target/lower-native-fast-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,7 @@ export function numericLoopHasTerminalStore(
fn: BytecodeFunction,
header: number,
order: ReadonlyArray<number>,
arithmetic: "number" | "checked-integer" = "number",
): boolean {
const stores = order.filter((ip) => fn.instructions[ip]!.opcode === "STORE_PROPERTY");
if (stores.length !== 1) return false;
Expand All @@ -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(
Expand All @@ -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<number>,
): 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,
Expand Down
Loading
Loading