Skip to content
Closed
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
25 changes: 25 additions & 0 deletions benchmarks/compiler_output/fixtures/scalar_replacement_literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,29 @@ function scalarReplacementChecksum(): number {
return values[0] + values[1] + values[2] + values.length;
}

class Position {}
class Velocity {}

let aggregateChecksum = 0;

function consumeAggregate(initializers: { component: unknown }[]): void {
for (let i = 0; i < initializers.length; i++) {
const initializer = initializers[i];
if (initializer.component === Position) aggregateChecksum += 1;
if (initializer.component === Velocity) aggregateChecksum += 2;
}
}

function scalarAggregateCallChecksum(): number {
aggregateChecksum = 0;
const iterations = 500_000;
for (let i = 0; i < iterations; i++) {
consumeAggregate([{ component: Position }, { component: Velocity }]);
consumeAggregate([{ component: Position }]);
consumeAggregate([{ component: Velocity }]);
}
return aggregateChecksum;
}

console.log(scalarReplacementChecksum());
console.log(scalarAggregateCallChecksum());
30 changes: 28 additions & 2 deletions benchmarks/compiler_output/workloads.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1349,50 +1349,76 @@ function_contains = "scalarReplacementChecksum"
regex_none = ["@js_object_get_field", "@js_object_set_field", "@js_array_get", "@js_array_set"]
detail = "scalar-replaced literals do not use runtime property or array access helpers"

[[workloads.scalar_replacement_literals.ir_checks]]
name = "known_aggregate_call_no_object_or_array_heap_alloc"
function_contains = "scalarAggregateCallChecksum"
regex_none = ["@js_object_alloc", "@js_object_alloc_with_shape", "@js_array_alloc"]
detail = "known fixed-aggregate consumers scalar-replace descriptor objects and carrier arrays"

[[workloads.scalar_replacement_literals.ir_checks]]
name = "known_aggregate_call_no_property_or_array_runtime_access"
function_contains = "scalarAggregateCallChecksum"
regex_none = ["@js_object_get_field", "@js_object_set_field", "@js_array_get", "@js_array_set"]
detail = "known fixed-aggregate consumers read scalar fields without runtime aggregate helpers"

[[workloads.scalar_replacement_literals.stdout_checks]]
name = "scalar_replacement_checksum"
equals = "17\n"
equals = "17\n3000000\n"
detail = "scalar-replacement fixture stdout checksum"

[workloads.scalar_replacement_literals.native_rep_checks]
function_contains = "scalarReplacementChecksum"
allow_materialization_reasons = ["runtime_api"]

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "scalar_object_literal_store"
source_function = "scalarReplacementChecksum"
expr_kind = "ScalarObjectLiteralInit"
consumer = "scalar_object_field_store"
native_rep_name = "js_value"
access_mode = "none"

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "scalar_object_field_get"
source_function = "scalarReplacementChecksum"
expr_kind = "ScalarObjectFieldGet"
consumer = "scalar_object_field_load"
native_rep_name = "js_value"
access_mode = "none"

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "scalar_object_field_set"
source_function = "scalarReplacementChecksum"
expr_kind = "ScalarObjectFieldSet"
consumer = "scalar_object_field_store"
native_rep_name = "js_value"
access_mode = "none"

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "scalar_array_literal_store"
source_function = "scalarReplacementChecksum"
expr_kind = "ScalarArrayLiteralInit"
consumer = "scalar_array_element_store"
native_rep_name = "js_value"
access_mode = "none"

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "scalar_array_index_get"
source_function = "scalarReplacementChecksum"
expr_kind = "ScalarArrayIndexGet"
consumer = "scalar_array_element_load"
native_rep_name = "js_value"
access_mode = "none"

[[workloads.scalar_replacement_literals.native_rep_checks.require_records]]
name = "known_aggregate_call_scalar_fields"
source_function = "scalarAggregateCallChecksum"
expr_kind = "ScalarAggregateFieldInit"
consumer = "scalar_object_field_store"
native_rep_name = "js_value"
access_mode = "none"
notes_contains = "carrier_array=elided"
min = 4

[workloads.width_aware_buffer_kernels]
source = "benchmarks/compiler_output/fixtures/width_aware_buffer_kernels.ts"
kind = "width_aware_buffer_kernels"
Expand Down
7 changes: 7 additions & 0 deletions changelog.d/8703-scalar-aggregate-calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Performance

- Scalar-replace short arrays of non-escaping object literals passed to known,
bounded aggregate consumers. Their carrier arrays, descriptor objects,
property/index accesses, and write barriers are now eliminated after
conservative inlining and loop unrolling; identity-observing and otherwise
escaping uses continue to materialize normally.
2 changes: 2 additions & 0 deletions crates/perry-codegen/src/stmt/let_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::let_buffer_views::{math_min_length_buffer_ids, register_noalias_buffe
use super::let_stmt_facts::{
buffer_local_alias_source, collect_scalar_class_data, native_i32_alias_source,
note_ptr_shape_scalar_replaced, pod_view_count_source, record_pod_rejection,
record_scalar_aggregate_field,
};
use super::unused_expr::lower_unused_expr;
use crate::expr::{
Expand Down Expand Up @@ -1901,6 +1902,7 @@ pub(crate) fn lower_let(
}
}
}
record_scalar_aggregate_field(ctx, id, name, &v);
v
} else {
String::new() // unused below; cleanup blocks check used_i32_init
Expand Down
34 changes: 34 additions & 0 deletions crates/perry-codegen/src/stmt/let_stmt_facts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ use super::*;

use crate::native_value::BufferAccessMode;

/// #8691: the aggregate scalar-replacement transform erases the carrier array
/// and object literals before codegen, leaving one synthetic local per field.
/// Preserve lowering evidence for those eliminated allocations so the result
/// remains visible to `--explain-lowering`.
pub(super) fn record_scalar_aggregate_field(ctx: &mut FnCtx<'_>, id: u32, name: &str, value: &str) {
if !name.starts_with("__perry_scalar_aggregate_") {
return;
}
let lowered = crate::native_value::LoweredValue {
semantic: crate::native_value::SemanticKind::JsValue,
rep: crate::native_value::NativeRep::JsValue,
llvm_ty: DOUBLE,
value: value.to_string(),
};
ctx.record_lowered_value_with_access_mode(
"ScalarAggregateFieldInit",
Some(id),
"scalar_object_field_store",
&lowered,
None,
None,
None,
None,
false,
false,
vec![
format!("local={name}"),
"carrier_array=elided".to_string(),
"carrier_object=elided".to_string(),
"write_barrier=0".to_string(),
],
);
}

pub(super) fn pod_view_count_source(ctx: &FnCtx<'_>, expr: &perry_hir::Expr) -> String {
match expr {
perry_hir::Expr::Integer(n) => format!("constant:{n}"),
Expand Down
Loading
Loading