From 438d80040ff4a7d6584f8e21eb27a7a21330d8eb Mon Sep 17 00:00:00 2001 From: Zachary Canann Date: Fri, 4 Dec 2026 05:37:07 +0000 Subject: [PATCH 1/2] backend: shorten fixed x86-64 memory displacements Ordinary scalar loads, stores and address calculations reserve four bytes for every known offset. Emit the shortest displacement that still addresses the selected base and byte offset, using the same encoding as existing AVX2 memory operands. RBP and R13 carry a zero displacement byte when the offset is zero. Gate: native matrix | x86-64 scalar memory instructions carry their shortest fixed displacement Spec: wiki/spec/build/machine_state_evidence.md#native-emission-and-backend-testing Proof: exact bytes and altered operands for scalar forms and displacement boundaries | scoped verification in progress Agent: Codex | Model: unavailable | Effort: unavailable | Tokens: unavailable in (unavailable cached), unavailable out | Steps: unavailable | Time: unavailable | Steers: unavailable --- .../src/selected_form_encoding/emit.rs | 20 +++ .../selected_form_encoding/memory/memory.rs | 9 +- .../packed_integer/avx2_dot.rs | 31 +--- .../src/selected_form_encoding/tests.rs | 132 ++++++++++++++++++ .../tests/abi_call_clobbers/memory_effects.rs | 6 +- .../src/machine_effects/memory.rs | 10 +- 6 files changed, 168 insertions(+), 40 deletions(-) diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/emit.rs b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/emit.rs index 0017467bd2ec..887561178d3c 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/emit.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/emit.rs @@ -14,6 +14,26 @@ pub(crate) const fn modrm(mode: u8, reg: u8, rm: u8) -> u8 { (mode << 6) | ((reg & 7) << 3) | (rm & 7) } +/// RBP and R13 need a displacement even at zero. RSP and R12 need a SIB byte. +pub(crate) fn append_base_displacement(bytes: &mut Vec, reg: u8, base: u8, disp: i32) { + let mode = if disp == 0 && base & 7 != 5 { + 0 + } else if i8::try_from(disp).is_ok() { + 1 + } else { + 2 + }; + bytes.push(modrm(mode, reg, base)); + if base & 7 == 4 { + bytes.push(0x24); + } + match mode { + 0 => {} + 1 => bytes.push(disp as u8), + _ => bytes.extend(disp.to_le_bytes()), + } +} + pub(crate) fn rr64(bytes: &mut Vec, opcode: u8, reg: u8, rm: u8) { bytes.extend([rex64(reg, 0, rm), opcode, modrm(3, reg, rm)]); } diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/memory/memory.rs b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/memory/memory.rs index 7c57e281a376..f9b1a049a0bd 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/memory/memory.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/memory/memory.rs @@ -1,4 +1,4 @@ -use super::emit::{modrm, rex, rex64, rr64}; +use super::emit::{append_base_displacement, modrm, rex, rex64, rr64}; use super::request_validation::{guard_canonical, resolve_registers}; use super::{ MachineAlternativeFamily as Family, MachineAlternativeKey, MachineEncodedControlEffect, @@ -119,11 +119,8 @@ fn ordinary_request( if narrow_load { bytes.push(0x0f); } - bytes.extend([opcode, modrm(2, reg, base)]); - if base & 7 == 4 { - bytes.push(0x24); - } - bytes.extend(disp.to_le_bytes()); + bytes.push(opcode); + append_base_displacement(&mut bytes, reg, base, disp); let catalog = crate::x86_64_register_constraint_catalog_for(physical); let constraint = catalog diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/packed_integer/avx2_dot.rs b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/packed_integer/avx2_dot.rs index 1b57cc550dd6..61dc3880b54b 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/packed_integer/avx2_dot.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/packed_integer/avx2_dot.rs @@ -1,4 +1,4 @@ -use super::super::emit::{modrm, rex64}; +use super::super::emit::{append_base_displacement, modrm, rex64}; use super::super::request_validation::byte_loop_registers as loop_regs; use super::super::{ MachineAlternativeFamily as Family, MachineAlternativeKey, MachineEncodedControlEffect, @@ -28,27 +28,6 @@ fn emit_vex3( bytes.extend([0xc4, byte1, byte2, opcode]); } -fn emit_mem_base_disp(bytes: &mut Vec, reg: u8, base: u8, disp: i32) { - if disp == 0 && (base & 7) != 5 { - bytes.push(modrm(0, reg, base)); - if (base & 7) == 4 { - bytes.push(0x24); - } - } else if let Ok(disp8) = i8::try_from(disp) { - bytes.push(modrm(1, reg, base)); - if (base & 7) == 4 { - bytes.push(0x24); - } - bytes.push(disp8 as u8); - } else { - bytes.push(modrm(2, reg, base)); - if (base & 7) == 4 { - bytes.push(0x24); - } - bytes.extend(disp.to_le_bytes()); - } -} - fn emit_mem_sib(bytes: &mut Vec, reg: u8, base: u8, index: u8, disp: i32) -> Result<(), Error> { if index == 4 { return Err(Error::EncodedFormMismatch); @@ -67,7 +46,7 @@ fn emit_mem_sib(bytes: &mut Vec, reg: u8, base: u8, index: u8, disp: i32) -> fn emit_vmovups_load_ymm(bytes: &mut Vec, dst_ymm: u8, base: u8, disp: i32) { emit_vex3(bytes, 1, 0, false, true, 0, dst_ymm, 0, base, 0x10); - emit_mem_base_disp(bytes, dst_ymm, base, disp); + append_base_displacement(bytes, dst_ymm, base, disp); } fn emit_vmovups_load_ymm_sib( @@ -83,7 +62,7 @@ fn emit_vmovups_load_ymm_sib( fn emit_vmovups_store_xmm(bytes: &mut Vec, base: u8, src_xmm: u8) { emit_vex3(bytes, 1, 0, false, false, 0, src_xmm, 0, base, 0x11); - emit_mem_base_disp(bytes, src_xmm, base, 0); + append_base_displacement(bytes, src_xmm, base, 0); } fn emit_vmovss_load_sib( @@ -99,7 +78,7 @@ fn emit_vmovss_load_sib( fn emit_vmovss_store(bytes: &mut Vec, base: u8, src_xmm: u8) { emit_vex3(bytes, 1, 2, false, false, 0, src_xmm, 0, base, 0x11); - emit_mem_base_disp(bytes, src_xmm, base, 0); + append_base_displacement(bytes, src_xmm, base, 0); } fn emit_vmulps_ymm(bytes: &mut Vec, dst: u8, lhs: u8, rhs: u8) { @@ -144,7 +123,7 @@ fn emit_vzeroupper(bytes: &mut Vec) { fn emit_mov_r64_mem(bytes: &mut Vec, dst: u8, base: u8, disp: i32) { bytes.extend([rex64(dst, 0, base), 0x8b]); - emit_mem_base_disp(bytes, dst, base, disp); + append_base_displacement(bytes, dst, base, disp); } fn emit_clamp_u64_imm32(bytes: &mut Vec, reg: u8, max_val: i32) { diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs index adf77cc5a902..c23a2db38130 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs @@ -23,6 +23,138 @@ fn views(p: &ValidatedPhysicalRegisterModel, names: &[&str]) -> Vec resolved(32, 83), - Store => resolved(7, 9), - AddressOffset | Load32 | Load64 => resolved(7, 8), - Load8 | Load16 => resolved(8, 9), + Store => resolved(3, 9), + AddressOffset | Load32 | Load64 => resolved(3, 8), + Load8 | Load16 => resolved(4, 9), Load8Indexed => MachineSizeKnowledge::ExactBytes(9), // REX, opcode, ModRM and SIB, plus a zero disp8 for an rbp or r13 // base; the 16-bit form adds the 0F escape byte. diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/target-x86_64/src/machine_effects/memory.rs b/source-rust/subsystems/compiler/psi-to-native/targets/target-x86_64/src/machine_effects/memory.rs index 348a70a47f71..360ffe61b0fe 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/target-x86_64/src/machine_effects/memory.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/target-x86_64/src/machine_effects/memory.rs @@ -134,7 +134,7 @@ pub(super) fn declaration( byte_count: u16::from(form.bytes), } }, - crate::encoder_resolved(7, Some(9)), + crate::encoder_resolved(3, Some(9)), ), Semantic::PackedAddI16x8 @@ -551,7 +551,7 @@ pub(super) fn declaration( vec![0, 1], vec![], MachineEncodedMemoryEffect::WritePointerV1 { pointer_operand: 0 }, - crate::encoder_resolved(7, Some(9)), + crate::encoder_resolved(3, Some(9)), ), Semantic::AddressOffset => ( MachineMemoryEffect::NoneV1, @@ -559,7 +559,7 @@ pub(super) fn declaration( vec![0], vec![1], MachineEncodedMemoryEffect::NoneV1, - crate::encoder_resolved(7, Some(8)), + crate::encoder_resolved(3, Some(8)), ), Semantic::Load16Indexed | Semantic::Load32Indexed | Semantic::Load64Indexed => { let element_bytes = semantic.indexed_load_bytes().expect("indexed load width"); @@ -598,7 +598,7 @@ pub(super) fn declaration( pointer_operand: 0, byte_count: if semantic == Semantic::Load8 { 1 } else { 2 }, }, - crate::encoder_resolved(8, Some(9)), + crate::encoder_resolved(4, Some(9)), ), Semantic::Load32 | Semantic::Load64 => ( MachineMemoryEffect::ReadPointerV1, @@ -609,7 +609,7 @@ pub(super) fn declaration( pointer_operand: 0, byte_count: if semantic == Semantic::Load32 { 4 } else { 8 }, }, - crate::encoder_resolved(7, Some(8)), + crate::encoder_resolved(3, Some(8)), ), Semantic::Store64 => ( MachineMemoryEffect::WriteFrameStorageV1, From 04ecc42b4bac5ec6f6af9ccc66f452dd3516cdb3 Mon Sep 17 00:00:00 2001 From: Zachary Canann Date: Fri, 4 Dec 2026 05:47:27 +0000 Subject: [PATCH 2/2] backend: iterate shared alternatives in x86 encoder tests The x86 encoding fixture could not compile after the target catalog moved its alternatives to shared slices. Iterate the slice explicitly so the fixture can compare emitted effects against the selected alternative. Gate: native matrix | x86-64 encoder tests use the current machine-effect catalog Spec: wiki/spec/build/machine_state_evidence.md#native-emission-and-backend-testing Proof: strict all-target Clippy and x86-64 unit tests | fixed a test-only iterator error also present on the base revision Agent: Codex | Model: unavailable | Effort: unavailable | Tokens: unavailable in (unavailable cached), unavailable out | Steps: unavailable | Time: unavailable | Steers: unavailable --- .../targets/isa-x86_64/src/selected_form_encoding/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs index c23a2db38130..91c262ea8836 100644 --- a/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs +++ b/source-rust/subsystems/compiler/psi-to-native/targets/isa-x86_64/src/selected_form_encoding/tests.rs @@ -470,7 +470,7 @@ fn scalar_and_control_golden_vectors_round_trip_and_reject_mutations() { let planned = catalog .declarations .iter() - .flat_map(|declaration| &declaration.alternatives) + .flat_map(|declaration| declaration.alternatives.iter()) .find(|alternative| alternative.key == key) .unwrap(); assert_eq!(