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

- Loop-version stable counted iteration over packed `Array` and `Array` subclasses, with fallback-free direct reads and mutation-safe current-index side exits.
62 changes: 31 additions & 31 deletions crates/perry-codegen/src/codegen/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 @@ -369,37 +370,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,
)
.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 @@ -427,6 +420,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
false,
false,
)
.with_context(|| format!("lowering method '{}::{}'", class.name, method.name))?;
// Representation-selection Phase 5a: the additive `internal`
Expand Down Expand Up @@ -464,6 +458,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
Some(fact.clone()),
None,
false,
false,
)
.with_context(|| {
format!(
Expand Down Expand Up @@ -504,6 +499,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
false,
Some(fact.clone()),
None,
false,
true,
)
.with_context(|| {
Expand Down Expand Up @@ -544,6 +540,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
false,
false,
)
.with_context(|| {
format!(
Expand Down Expand Up @@ -611,6 +608,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
false,
false,
)
.with_context(|| format!("lowering getter '{}::{}'", class.name, prop))?;
}
Expand Down Expand Up @@ -666,6 +664,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
false,
false,
)
.with_context(|| format!("lowering setter '{}::{}'", class.name, prop))?;
}
Expand Down Expand Up @@ -763,6 +762,7 @@ pub(super) fn emit_module_artifacts(c: ModuleArtifactsCtx<'_>) -> Result<()> {
None,
None,
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
53 changes: 53 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,59 @@ 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
}

pub(super) fn emit_public_generic_method_forwarder(
llmod: &mut LlModule,
method: &perry_hir::Function,
public_name: &str,
generic_body_name: &str,
) {
let mut params: Vec<(crate::types::LlvmType, String)> =
Vec::with_capacity(method.params.len() + 1);
params.push((DOUBLE, "%this_arg".to_string()));
for p in &method.params {
params.push((DOUBLE, format!("%arg{}", p.id)));
}
let wf = llmod.define_function(public_name, DOUBLE, params);
let _ = wf.create_block("entry");
let mut arg_names: Vec<String> = Vec::with_capacity(method.params.len() + 1);
arg_names.push("%this_arg".to_string());
for p in &method.params {
arg_names.push(format!("%arg{}", p.id));
}
let call_args: Vec<(crate::types::LlvmType, &str)> =
arg_names.iter().map(|arg| (DOUBLE, arg.as_str())).collect();
let value = wf
.block_mut(0)
.unwrap()
.call(DOUBLE, generic_body_name, &call_args);
wf.block_mut(0).unwrap().ret(DOUBLE, &value);
}

/// 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