Skip to content

Commit e3ea7f2

Browse files
committed
cranelift: tidy inline array.copy helper after review
Reword `emit_inline_array_copy`'s doc to describe a bitwise copy by element width (it also handles `v128` and copies `f32`/`f64` via integer types), and replace the `elem_size`/`n` parameter shadowing with `stride`/`count`. No codegen change. Assisted-by: Claude Code:claude-opus-4-7
1 parent d7ba3ed commit e3ea7f2

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

crates/cranelift/src/func_environ.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,13 +4435,14 @@ impl FuncEnvironment<'_> {
44354435
}
44364436
}
44374437

4438-
/// Expand a small, statically-sized scalar `array.copy` into inline loads
4439-
/// and stores, avoiding the `memory_copy` libcall.
4438+
/// Expand a small, statically-sized `array.copy` into inline loads then
4439+
/// stores, avoiding the `memory_copy` libcall.
44404440
///
4441-
/// Every element is loaded before any is stored so that overlapping source
4442-
/// and destination ranges (`array.copy` has `memmove` semantics) copy
4443-
/// correctly. The addresses and length have already been bounds-checked by
4444-
/// the caller.
4441+
/// The copy is bitwise: `elem_ty` is an integer or vector type matching the
4442+
/// element width (`f32`/`f64` use `i32`/`i64`, `v128` uses `i8x16`), so any
4443+
/// fixed-width element works. Every element is loaded before any is stored,
4444+
/// so overlapping ranges keep `array.copy`'s `memmove` semantics. The caller
4445+
/// has already bounds-checked the addresses and length.
44454446
fn emit_inline_array_copy(
44464447
&mut self,
44474448
builder: &mut FunctionBuilder<'_>,
@@ -4457,15 +4458,14 @@ impl FuncEnvironment<'_> {
44574458
// GC-heap access flags (trap on corruption, no alignment assumed). Not
44584459
// `GC_MEMFLAGS` directly: that constant is gated to the `gc` feature.
44594460
let flags = ir::MemFlagsData::new().with_trap_code(Some(TRAP_GC_HEAP_CORRUPT));
4460-
let elem_size = i32::try_from(elem_size).unwrap();
4461-
let n = i32::try_from(n).unwrap();
4461+
let stride = i32::try_from(elem_size).unwrap();
4462+
let count = i32::try_from(n).unwrap();
44624463
let mut vals: SmallVec<[ir::Value; 8]> = smallvec![];
4463-
for i in 0..n {
4464-
vals.push(builder.ins().load(elem_ty, flags, src_addr, i * elem_size));
4464+
for i in 0..count {
4465+
vals.push(builder.ins().load(elem_ty, flags, src_addr, i * stride));
44654466
}
4466-
for (i, val) in vals.into_iter().enumerate() {
4467-
let offset = i32::try_from(i).unwrap() * elem_size;
4468-
builder.ins().store(flags, val, dst_addr, offset);
4467+
for (i, val) in (0..count).zip(vals) {
4468+
builder.ins().store(flags, val, dst_addr, i * stride);
44694469
}
44704470
}
44714471

0 commit comments

Comments
 (0)