Skip to content

Commit 3a0596b

Browse files
ZJIT: Add Shape type to HIR (#15528)
It's just a nicety (they fit fine as CUInt32) but this makes printing look nicer in real execution and also in tests (helps with #15489). Co-authored-by: Randy Stauner <[email protected]>
1 parent bb4a6f3 commit 3a0596b

File tree

6 files changed

+68
-50
lines changed

6 files changed

+68
-50
lines changed

zjit/src/codegen.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
364364
&Insn::Const { val: Const::CInt64(val) } => gen_const_long(val),
365365
&Insn::Const { val: Const::CUInt16(val) } => gen_const_uint16(val),
366366
&Insn::Const { val: Const::CUInt32(val) } => gen_const_uint32(val),
367+
&Insn::Const { val: Const::CShape(val) } => {
368+
assert_eq!(SHAPE_ID_NUM_BITS, 32);
369+
gen_const_uint32(val.0)
370+
}
367371
Insn::Const { .. } => panic!("Unexpected Const in gen_insn: {insn}"),
368372
Insn::NewArray { elements, state } => gen_new_array(asm, opnds!(elements), &function.frame_state(*state)),
369373
Insn::NewHash { elements, state } => gen_new_hash(jit, asm, opnds!(elements), &function.frame_state(*state)),

zjit/src/hir.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ pub enum Const {
303303
CUInt8(u8),
304304
CUInt16(u16),
305305
CUInt32(u32),
306+
CShape(ShapeId),
306307
CUInt64(u64),
307308
CPtr(*const u8),
308309
CDouble(f64),
@@ -389,6 +390,7 @@ impl<'a> std::fmt::Display for ConstPrinter<'a> {
389390
// number than {:?} does and we don't know why.
390391
// We'll have to resolve that first.
391392
Const::CPtr(val) => write!(f, "CPtr({:?})", self.ptr_map.map_ptr(val)),
393+
&Const::CShape(shape_id) => write!(f, "CShape({:p})", self.ptr_map.map_shape(shape_id)),
392394
_ => write!(f, "{:?}", self.inner),
393395
}
394396
}
@@ -468,7 +470,7 @@ impl PtrPrintMap {
468470
}
469471

470472
/// Map shape ID into a pointer for printing
471-
fn map_shape(&self, id: ShapeId) -> *const c_void {
473+
pub fn map_shape(&self, id: ShapeId) -> *const c_void {
472474
self.map_ptr(id.0 as *const c_void)
473475
}
474476
}
@@ -2140,6 +2142,7 @@ impl Function {
21402142
Insn::Const { val: Const::CUInt8(val) } => Type::from_cint(types::CUInt8, *val as i64),
21412143
Insn::Const { val: Const::CUInt16(val) } => Type::from_cint(types::CUInt16, *val as i64),
21422144
Insn::Const { val: Const::CUInt32(val) } => Type::from_cint(types::CUInt32, *val as i64),
2145+
Insn::Const { val: Const::CShape(val) } => Type::from_cint(types::CShape, val.0 as i64),
21432146
Insn::Const { val: Const::CUInt64(val) } => Type::from_cint(types::CUInt64, *val as i64),
21442147
Insn::Const { val: Const::CPtr(val) } => Type::from_cptr(*val),
21452148
Insn::Const { val: Const::CDouble(val) } => Type::from_double(*val),
@@ -3206,8 +3209,7 @@ impl Function {
32063209
self.push_insn(block, Insn::WriteBarrier { recv: self_val, val });
32073210
if next_shape_id != recv_type.shape() {
32083211
// Write the new shape ID
3209-
assert_eq!(SHAPE_ID_NUM_BITS, 32);
3210-
let shape_id = self.push_insn(block, Insn::Const { val: Const::CUInt32(next_shape_id.0) });
3212+
let shape_id = self.push_insn(block, Insn::Const { val: Const::CShape(next_shape_id) });
32113213
let shape_id_offset = unsafe { rb_shape_id_offset() };
32123214
self.push_insn(block, Insn::StoreField { recv: self_val, id: ID!(_shape_id), offset: shape_id_offset, val: shape_id });
32133215
}
@@ -4773,6 +4775,7 @@ impl Function {
47734775
Const::CUInt8(_) => self.assert_subtype(insn_id, val, types::CUInt8),
47744776
Const::CUInt16(_) => self.assert_subtype(insn_id, val, types::CUInt16),
47754777
Const::CUInt32(_) => self.assert_subtype(insn_id, val, types::CUInt32),
4778+
Const::CShape(_) => self.assert_subtype(insn_id, val, types::CShape),
47764779
Const::CUInt64(_) => self.assert_subtype(insn_id, val, types::CUInt64),
47774780
Const::CBool(_) => self.assert_subtype(insn_id, val, types::CBool),
47784781
Const::CDouble(_) => self.assert_subtype(insn_id, val, types::CDouble),

zjit/src/hir/opt_tests.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,8 +3813,8 @@ mod hir_opt_tests {
38133813
v20:HeapBasicObject = GuardShape v19, 0x1000
38143814
StoreField v20, :@foo@0x1001, v10
38153815
WriteBarrier v20, v10
3816-
v23:CUInt32[4194311] = Const CUInt32(4194311)
3817-
StoreField v20, :_shape_id@0x1002, v23
3816+
v23:CShape[0x1002] = Const CShape(0x1002)
3817+
StoreField v20, :_shape_id@0x1003, v23
38183818
CheckInterrupts
38193819
Return v10
38203820
");
@@ -3845,16 +3845,16 @@ mod hir_opt_tests {
38453845
v26:HeapBasicObject = GuardShape v25, 0x1000
38463846
StoreField v26, :@foo@0x1001, v10
38473847
WriteBarrier v26, v10
3848-
v29:CUInt32[4194311] = Const CUInt32(4194311)
3849-
StoreField v26, :_shape_id@0x1002, v29
3848+
v29:CShape[0x1002] = Const CShape(0x1002)
3849+
StoreField v26, :_shape_id@0x1003, v29
38503850
v16:Fixnum[2] = Const Value(2)
38513851
PatchPoint SingleRactorMode
38523852
v31:HeapBasicObject = GuardType v6, HeapBasicObject
3853-
v32:HeapBasicObject = GuardShape v31, 0x1003
3853+
v32:HeapBasicObject = GuardShape v31, 0x1002
38543854
StoreField v32, :@bar@0x1004, v16
38553855
WriteBarrier v32, v16
3856-
v35:CUInt32[4194312] = Const CUInt32(4194312)
3857-
StoreField v32, :_shape_id@0x1002, v35
3856+
v35:CShape[0x1005] = Const CShape(0x1005)
3857+
StoreField v32, :_shape_id@0x1003, v35
38583858
CheckInterrupts
38593859
Return v16
38603860
");
@@ -6095,8 +6095,8 @@ mod hir_opt_tests {
60956095
v29:HeapObject[class_exact:C] = GuardShape v26, 0x1038
60966096
StoreField v29, :@foo@0x1039, v16
60976097
WriteBarrier v29, v16
6098-
v32:CUInt32[4194311] = Const CUInt32(4194311)
6099-
StoreField v29, :_shape_id@0x103a, v32
6098+
v32:CShape[0x103a] = Const CShape(0x103a)
6099+
StoreField v29, :_shape_id@0x103b, v32
61006100
CheckInterrupts
61016101
Return v16
61026102
");
@@ -6130,8 +6130,8 @@ mod hir_opt_tests {
61306130
v29:HeapObject[class_exact:C] = GuardShape v26, 0x1038
61316131
StoreField v29, :@foo@0x1039, v16
61326132
WriteBarrier v29, v16
6133-
v32:CUInt32[4194311] = Const CUInt32(4194311)
6134-
StoreField v29, :_shape_id@0x103a, v32
6133+
v32:CShape[0x103a] = Const CShape(0x103a)
6134+
StoreField v29, :_shape_id@0x103b, v32
61356135
CheckInterrupts
61366136
Return v16
61376137
");
@@ -9514,8 +9514,8 @@ mod hir_opt_tests {
95149514
v57:HeapBasicObject = GuardShape v56, 0x1000
95159515
StoreField v57, :@formatted@0x1001, v39
95169516
WriteBarrier v57, v39
9517-
v60:CUInt32[4194311] = Const CUInt32(4194311)
9518-
StoreField v57, :_shape_id@0x1002, v60
9517+
v60:CShape[0x1002] = Const CShape(0x1002)
9518+
StoreField v57, :_shape_id@0x1003, v60
95199519
v45:Class[VMFrozenCore] = Const Value(VALUE(0x1008))
95209520
PatchPoint MethodRedefined(Class@0x1010, lambda@0x1018, cme:0x1020)
95219521
PatchPoint NoSingletonClass(Class@0x1010)

zjit/src/hir_type/gen_hir_type.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def final_type name, base: $object, c_name: nil
139139
signed.subtype "CInt#{width}"
140140
unsigned.subtype "CUInt#{width}"
141141
}
142+
unsigned.subtype "CShape"
142143

143144
# Assign individual bits to type leaves and union bit patterns to nodes with subtypes
144145
num_bits = 0

zjit/src/hir_type/hir_type.inc.rs

Lines changed: 37 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/hir_type/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ fn write_spec(f: &mut std::fmt::Formatter, printer: &TypePrinter) -> std::fmt::R
9292
Specialization::Int(val) if ty.is_subtype(types::CInt8) => write!(f, "[{}]", (val & u8::MAX as u64) as i8),
9393
Specialization::Int(val) if ty.is_subtype(types::CInt16) => write!(f, "[{}]", (val & u16::MAX as u64) as i16),
9494
Specialization::Int(val) if ty.is_subtype(types::CInt32) => write!(f, "[{}]", (val & u32::MAX as u64) as i32),
95+
Specialization::Int(val) if ty.is_subtype(types::CShape) =>
96+
write!(f, "[{:p}]", printer.ptr_map.map_shape(crate::cruby::ShapeId((val & u32::MAX as u64) as u32))),
9597
Specialization::Int(val) if ty.is_subtype(types::CInt64) => write!(f, "[{}]", val as i64),
9698
Specialization::Int(val) if ty.is_subtype(types::CUInt8) => write!(f, "[{}]", val & u8::MAX as u64),
9799
Specialization::Int(val) if ty.is_subtype(types::CUInt16) => write!(f, "[{}]", val & u16::MAX as u64),
@@ -258,6 +260,7 @@ impl Type {
258260
Const::CUInt8(v) => Self::from_cint(types::CUInt8, v as i64),
259261
Const::CUInt16(v) => Self::from_cint(types::CUInt16, v as i64),
260262
Const::CUInt32(v) => Self::from_cint(types::CUInt32, v as i64),
263+
Const::CShape(v) => Self::from_cint(types::CShape, v.0 as i64),
261264
Const::CUInt64(v) => Self::from_cint(types::CUInt64, v as i64),
262265
Const::CPtr(v) => Self::from_cptr(v),
263266
Const::CDouble(v) => Self::from_double(v),
@@ -526,6 +529,10 @@ impl Type {
526529
if self.is_subtype(types::CUInt8) || self.is_subtype(types::CInt8) { return 1; }
527530
if self.is_subtype(types::CUInt16) || self.is_subtype(types::CInt16) { return 2; }
528531
if self.is_subtype(types::CUInt32) || self.is_subtype(types::CInt32) { return 4; }
532+
if self.is_subtype(types::CShape) {
533+
use crate::cruby::{SHAPE_ID_NUM_BITS, BITS_PER_BYTE};
534+
return (SHAPE_ID_NUM_BITS as usize / BITS_PER_BYTE).try_into().unwrap();
535+
}
529536
// CUInt64, CInt64, CPtr, CNull, CDouble, or anything else defaults to 8 bytes
530537
crate::cruby::SIZEOF_VALUE as u8
531538
}

0 commit comments

Comments
 (0)