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
20 changes: 20 additions & 0 deletions changelog.d/8690-loop-versioned-packed-arraylike.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
### Performance

- Loop-version stable counted iteration over packed `Array` and `Array` subclasses, with fallback-free direct reads and mutation-safe current-index side exits.

### Fixed

- The `x | 0` canonical-ToInt32 store lever no longer re-evaluates its operand
tree as native i32. `expr_produces_canonical_raw_f64` vouches for the RESULT
of `x | 0`, not for `x`'s operands, so consuming it as a licence to call
`lower_expr_native(_, I32)` violated that path's documented precondition
(`can_lower_expr_as_i32_in_current_region`). The i32-chain arm `fptosi`s
every operand it cannot lower natively, which turned
`h ^ recv.charCodeAt(i)` on an `any` receiver into an inline `xor i32` over
whatever that method returned — a BigInt silently produced garbage instead
of the spec's `TypeError`. The lever now lowers through `lower_expr`, which
applies `is_provably_not_bigint` per operand, and takes one trailing
`toint32_fast` to feed the i32 slot; `x | 0` always lowers to `sitofp i32`,
so that pair folds away. A proven tree still gets the inline `xor i32` and
keeps the i32-slot store. Pinned by the pre-existing negative control
`char_code_at_on_an_unproven_receiver_keeps_the_runtime_lowering`.
65 changes: 33 additions & 32 deletions crates/perry-codegen/src/codegen/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::entry::compile_module_entry;
use super::helpers::{
function_body_returns_generator_object, sanitize, scoped_fn_name, unknown_func_wrapper_name,
};
use super::indexed_method_artifacts::{compile_indexed_method_clones, IndexedMethodArtifactsCtx};
use super::method::{
compile_method, compile_static_method, compile_typed_f64_method,
compile_typed_f64_receiver_method, compile_typed_i1_method, compile_typed_i32_method,
Expand Down Expand Up @@ -298,38 +299,29 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
.nonnegative_index_methods
.get(&(class.name.clone(), method.name.clone()))
{
compile_method(
llmod,
class,
method,
func_names,
strings,
class_table,
method_names,
module_globals,
module_global_types,
opts.import_function_prefixes,
enum_table,
static_field_globals,
class_ids,
func_signatures,
func_synthetic_arguments,
module_boxed_vars,
closure_rest_params,
cross_module,
None,
false,
None,
Some(nonnegative_index_params),
false,
false,
)
.with_context(|| {
format!(
"lowering nonnegative-index method clone '{}::{}'",
class.name, method.name
)
})?;
compile_indexed_method_clones(
IndexedMethodArtifactsCtx {
llmod,
class,
method,
func_names,
strings,
classes: class_table,
methods: method_names,
module_globals,
module_global_types,
import_function_prefixes: opts.import_function_prefixes,
enums: enum_table,
static_field_globals,
class_ids,
func_signatures,
func_synthetic_arguments,
module_boxed_vars,
closure_rest_params,
cross_module,
},
nonnegative_index_params,
)?;
}
compile_method(
llmod,
Expand Down Expand Up @@ -358,6 +350,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| format!("lowering method '{}::{}'", class.name, method.name))?;
if cross_module
Expand Down Expand Up @@ -388,6 +381,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
false,
false,
true,
)
.with_context(|| {
Expand Down Expand Up @@ -433,6 +427,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| {
format!(
Expand Down Expand Up @@ -468,6 +463,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
Some(fact.clone()),
None,
false,
false,
true,
)
.with_context(|| {
Expand Down Expand Up @@ -510,6 +506,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
false,
Some(fact.clone()),
None,
false,
true,
false,
)
Expand Down Expand Up @@ -552,6 +549,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| {
format!(
Expand Down Expand Up @@ -620,6 +618,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| format!("lowering getter '{}::{}'", class.name, prop))?;
}
Expand Down Expand Up @@ -676,6 +675,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| format!("lowering setter '{}::{}'", class.name, prop))?;
}
Expand Down Expand Up @@ -774,6 +774,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
false,
false,
false,
)
.with_context(|| format!("lowering constructor for '{}'", class.name))?;
}
Expand Down
4 changes: 4 additions & 0 deletions crates/perry-codegen/src/codegen/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,7 @@ pub(super) fn compile_closure(
class_shape_slots: HashMap::new(),
class_header_images: HashMap::new(),
cached_lengths: HashMap::new(),
array_length_snapshots: HashMap::new(),
bounded_index_pairs: Vec::new(),
packed_f64_loop_facts: Vec::new(),
masked_window_array_facts: Vec::new(),
Expand Down Expand Up @@ -1154,6 +1155,9 @@ pub(super) fn compile_closure(
typed_f64_methods: &cross_module.typed_f64_methods,
pshape_methods: &cross_module.pshape_methods,
nonnegative_index_methods: &cross_module.nonnegative_index_methods,
trusted_array_param_handles: HashMap::new(),
versioned_indexed_loop_facts: Vec::new(),
stable_packed_loop_facts: Vec::new(),
pshape_tower_routable: &cross_module.pshape_tower_routable,
proven_this: None,
typed_i32_methods: &cross_module.typed_i32_methods,
Expand Down
8 changes: 8 additions & 0 deletions crates/perry-codegen/src/codegen/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ pub(super) fn compile_module_entry(
class_shape_slots: HashMap::new(),
class_header_images: HashMap::new(),
cached_lengths: HashMap::new(),
array_length_snapshots: HashMap::new(),
bounded_index_pairs: Vec::new(),
packed_f64_loop_facts: Vec::new(),
masked_window_array_facts: Vec::new(),
Expand Down Expand Up @@ -954,6 +955,9 @@ pub(super) fn compile_module_entry(
typed_f64_methods: &cross_module.typed_f64_methods,
pshape_methods: &cross_module.pshape_methods,
nonnegative_index_methods: &cross_module.nonnegative_index_methods,
trusted_array_param_handles: HashMap::new(),
versioned_indexed_loop_facts: Vec::new(),
stable_packed_loop_facts: Vec::new(),
pshape_tower_routable: &cross_module.pshape_tower_routable,
proven_this: None,
typed_i32_methods: &cross_module.typed_i32_methods,
Expand Down Expand Up @@ -1571,6 +1575,7 @@ pub(super) fn compile_module_entry(
class_shape_slots: HashMap::new(),
class_header_images: HashMap::new(),
cached_lengths: HashMap::new(),
array_length_snapshots: HashMap::new(),
bounded_index_pairs: Vec::new(),
packed_f64_loop_facts: Vec::new(),
masked_window_array_facts: Vec::new(),
Expand Down Expand Up @@ -1653,6 +1658,9 @@ pub(super) fn compile_module_entry(
typed_f64_methods: &cross_module.typed_f64_methods,
pshape_methods: &cross_module.pshape_methods,
nonnegative_index_methods: &cross_module.nonnegative_index_methods,
trusted_array_param_handles: HashMap::new(),
versioned_indexed_loop_facts: Vec::new(),
stable_packed_loop_facts: Vec::new(),
pshape_tower_routable: &cross_module.pshape_tower_routable,
proven_this: None,
typed_i32_methods: &cross_module.typed_i32_methods,
Expand Down
4 changes: 4 additions & 0 deletions crates/perry-codegen/src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ pub(super) fn compile_function(
class_shape_slots: HashMap::new(),
class_header_images: HashMap::new(),
cached_lengths: HashMap::new(),
array_length_snapshots: HashMap::new(),
bounded_index_pairs: Vec::new(),
packed_f64_loop_facts: Vec::new(),
masked_window_array_facts: Vec::new(),
Expand Down Expand Up @@ -1204,6 +1205,9 @@ pub(super) fn compile_function(
typed_f64_methods: &cross_module.typed_f64_methods,
pshape_methods: &cross_module.pshape_methods,
nonnegative_index_methods: &cross_module.nonnegative_index_methods,
trusted_array_param_handles: HashMap::new(),
versioned_indexed_loop_facts: Vec::new(),
stable_packed_loop_facts: Vec::new(),
pshape_tower_routable: &cross_module.pshape_tower_routable,
proven_this: None,
typed_i32_methods: &cross_module.typed_i32_methods,
Expand Down
25 changes: 25 additions & 0 deletions crates/perry-codegen/src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,31 @@ pub(super) fn scoped_static_method_name(
)
}

pub(super) fn node_stream_parent_kind(
classes: &HashMap<String, &perry_hir::Class>,
class: &perry_hir::Class,
) -> Option<&'static str> {
let mut cur = class.extends_name.as_deref();
let mut depth = 0usize;
while let Some(name) = cur {
match name {
"Readable" => return Some("readable"),
"Duplex" => return Some("duplex"),
"Transform" => return Some("transform"),
_ => {}
}
cur = classes
.get(name)
.copied()
.and_then(|parent| parent.extends_name.as_deref());
depth += 1;
if depth > 32 {
break;
}
}
None
}

/// Walk a function body looking for `Return(Some(expr))` shapes that
/// identify the function as a factory returning a class. Sets
/// `*produced` to the resolved class name when the first qualifying
Expand Down
Loading
Loading