Skip to content

Commit b52cb82

Browse files
authored
ZJIT: Format negative offsets as small negative hex values (ruby#16248)
Before: Optimized HIR: fn block in <main>@benchmarks/setivar.rb:40: bb1(): EntryPoint interpreter v1:BasicObject = LoadSelf Jump bb3(v1) bb2(): EntryPoint JIT(0) v4:BasicObject = LoadArg :self@0 Jump bb3(v4) bb3(v6:BasicObject): v10:CPtr = GetEP 1 v11:BasicObject = LoadField v10, :obj@0xffffffffffffffe8 <= PatchPoint NoSingletonClass(TheClass@0x1037633c0) PatchPoint MethodRedefined(TheClass@0x1037633c0, set_value_loop@0xf5f1, cme:0x103a14650) v21:HeapObject[class_exact:TheClass] = GuardType v11, HeapObject[class_exact:TheClass] v22:BasicObject = SendDirect v21, 0x16cf41630, :set_value_loop (0x16cf41670) CheckInterrupts Return v22 After: Optimized HIR: fn block in <main>@benchmarks/setivar.rb:40: bb1(): EntryPoint interpreter v1:BasicObject = LoadSelf Jump bb3(v1) bb2(): EntryPoint JIT(0) v4:BasicObject = LoadArg :self@0 Jump bb3(v4) bb3(v6:BasicObject): v10:CPtr = GetEP 1 v11:BasicObject = LoadField v10, :obj@-0x18 <= PatchPoint NoSingletonClass(TheClass@0x102bc3420) PatchPoint MethodRedefined(TheClass@0x102bc3420, set_value_loop@0xf5f1, cme:0x102e945f0) v21:HeapObject[class_exact:TheClass] = GuardType v11, HeapObject[class_exact:TheClass] v22:BasicObject = SendDirect v21, 0x16dadd630, :set_value_loop (0x16dadd670) CheckInterrupts Return v22
1 parent 0271319 commit b52cb82

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

zjit/src/hir.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ impl PtrPrintMap {
431431
}
432432
}
433433

434+
struct Offset(i32);
435+
436+
impl std::fmt::LowerHex for Offset {
437+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
438+
let prefix = if f.alternate() { "0x" } else { "" };
439+
let bare_hex = format!("{:x}", self.0.abs());
440+
f.pad_integral(self.0 >= 0, prefix, &bare_hex)
441+
}
442+
}
443+
434444
impl PtrPrintMap {
435445
/// Map a pointer for printing
436446
pub fn map_ptr<T>(&self, ptr: *const T) -> *const T {
@@ -467,8 +477,8 @@ impl PtrPrintMap {
467477
self.map_ptr(id as *const c_void)
468478
}
469479

470-
fn map_offset(&self, id: i32) -> *const c_void {
471-
self.map_ptr(id as *const c_void)
480+
fn map_offset(&self, id: i32) -> Offset {
481+
Offset(self.map_ptr(id as *const c_void) as i32)
472482
}
473483

474484
/// Map shape ID into a pointer for printing
@@ -1644,8 +1654,8 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
16441654
&Insn::GetEP { level } => write!(f, "GetEP {level}"),
16451655
Insn::GetLEP => write!(f, "GetLEP"),
16461656
Insn::LoadSelf => write!(f, "LoadSelf"),
1647-
&Insn::LoadField { recv, id, offset, return_type: _ } => write!(f, "LoadField {recv}, :{}@{:p}", id.contents_lossy(), self.ptr_map.map_offset(offset)),
1648-
&Insn::StoreField { recv, id, offset, val } => write!(f, "StoreField {recv}, :{}@{:p}, {val}", id.contents_lossy(), self.ptr_map.map_offset(offset)),
1657+
&Insn::LoadField { recv, id, offset, return_type: _ } => write!(f, "LoadField {recv}, :{}@{:#x}", id.contents_lossy(), self.ptr_map.map_offset(offset)),
1658+
&Insn::StoreField { recv, id, offset, val } => write!(f, "StoreField {recv}, :{}@{:#x}, {val}", id.contents_lossy(), self.ptr_map.map_offset(offset)),
16491659
&Insn::WriteBarrier { recv, val } => write!(f, "WriteBarrier {recv}, {val}"),
16501660
Insn::SetIvar { self_val, id, val, .. } => write!(f, "SetIvar {self_val}, :{}, {val}", id.contents_lossy()),
16511661
Insn::GetGlobal { id, .. } => write!(f, "GetGlobal :{}", id.contents_lossy()),

0 commit comments

Comments
 (0)