Skip to content

Commit 8a23cc7

Browse files
authored
Cranelift: Make ir::{Constant,Immediate} considered entities (#11207)
* Cranelift: Make `ir::{Constant,Immediate}` considered entities They reference data in out-of-line pools rather than storing their data inline in the instruction, and when an instruction containing them is moved from one `ir::Function` to another, they need their indices updated accordingly. Therefore, they really are entities rather than immediates. This recategorization means that they will now be properly mapped in `ir::InstructionData::map` calls. * fix tests
1 parent 7d72a9a commit 8a23cc7

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

cranelift/codegen/meta/src/shared/entities.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ pub(crate) struct EntityRefs {
4848

4949
/// A variable-sized list of value operands. Use for Block and function call arguments.
5050
pub(crate) varargs: OperandKind,
51+
52+
/// A constant stored in the constant pool.
53+
///
54+
/// This operand is used to pass constants to instructions like `vconst`
55+
/// while storing the actual bytes in the constant pool.
56+
pub(crate) pool_constant: OperandKind,
57+
58+
/// An unsigned 128-bit immediate integer operand, stored out-of-line in the
59+
/// `DataFlowGraph::immediates` pool.
60+
///
61+
/// This operand is used to pass entire 128-bit vectors as immediates to instructions like
62+
/// `shuffle` and `mask`.
63+
pub(crate) uimm128: OperandKind,
5164
}
5265

5366
impl EntityRefs {
@@ -101,6 +114,18 @@ impl EntityRefs {
101114
returned from an instruction.
102115
"#,
103116
),
117+
118+
pool_constant: new(
119+
"constant_handle",
120+
"ir::Constant",
121+
"A constant stored in the constant pool.",
122+
),
123+
124+
uimm128: new(
125+
"imm",
126+
"ir::Immediate",
127+
"A 128-bit immediate unsigned integer.",
128+
),
104129
}
105130
}
106131
}

cranelift/codegen/meta/src/shared/formats.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ impl Formats {
5757

5858
unary_ieee64: Builder::new("UnaryIeee64").imm(&imm.ieee64).build(),
5959

60-
unary_const: Builder::new("UnaryConst").imm(&imm.pool_constant).build(),
60+
unary_const: Builder::new("UnaryConst")
61+
.imm(&entities.pool_constant)
62+
.build(),
6163

6264
unary_global_value: Builder::new("UnaryGlobalValue")
6365
.imm(&entities.global_value)
@@ -94,7 +96,7 @@ impl Formats {
9496
shuffle: Builder::new("Shuffle")
9597
.value()
9698
.value()
97-
.imm(&imm.uimm128)
99+
.imm(&entities.uimm128)
98100
.build(),
99101

100102
int_compare: Builder::new("IntCompare")

cranelift/codegen/meta/src/shared/immediates.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,6 @@ pub(crate) struct Immediates {
1414
/// counts on shift instructions.
1515
pub uimm8: OperandKind,
1616

17-
/// An unsigned 128-bit immediate integer operand.
18-
///
19-
/// This operand is used to pass entire 128-bit vectors as immediates to instructions like
20-
/// const.
21-
pub uimm128: OperandKind,
22-
23-
/// A constant stored in the constant pool.
24-
///
25-
/// This operand is used to pass constants to instructions like vconst while storing the
26-
/// actual bytes in the constant pool.
27-
pub pool_constant: OperandKind,
28-
2917
/// A 32-bit immediate signed offset.
3018
///
3119
/// This is used to represent an immediate address offset in load/store instructions.
@@ -109,16 +97,6 @@ impl Immediates {
10997
"ir::immediates::Uimm8",
11098
"An 8-bit immediate unsigned integer.",
11199
),
112-
uimm128: new_imm(
113-
"imm",
114-
"ir::Immediate",
115-
"A 128-bit immediate unsigned integer.",
116-
),
117-
pool_constant: new_imm(
118-
"constant_handle",
119-
"ir::Constant",
120-
"A constant stored in the constant pool.",
121-
),
122100
offset32: new_imm(
123101
"offset",
124102
"ir::immediates::Offset32",

cranelift/codegen/meta/src/shared/instructions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ pub(crate) fn define(
14781478
"#,
14791479
&formats.unary_const,
14801480
)
1481-
.operands_in(vec![Operand::new("N", &imm.pool_constant)])
1481+
.operands_in(vec![Operand::new("N", &entities.pool_constant)])
14821482
.operands_out(vec![
14831483
Operand::new("a", f128_).with_doc("A constant f128 scalar value"),
14841484
]),
@@ -1495,7 +1495,7 @@ pub(crate) fn define(
14951495
&formats.unary_const,
14961496
)
14971497
.operands_in(vec![
1498-
Operand::new("N", &imm.pool_constant)
1498+
Operand::new("N", &entities.pool_constant)
14991499
.with_doc("The 16 immediate bytes of a 128-bit vector"),
15001500
])
15011501
.operands_out(vec![
@@ -1530,7 +1530,7 @@ pub(crate) fn define(
15301530
.operands_in(vec![
15311531
Operand::new("a", Tx16).with_doc("A vector value"),
15321532
Operand::new("b", Tx16).with_doc("A vector value"),
1533-
Operand::new("mask", &imm.uimm128)
1533+
Operand::new("mask", &entities.uimm128)
15341534
.with_doc("The 16 immediate bytes used for selecting the elements to shuffle"),
15351535
])
15361536
.operands_out(vec![Operand::new("a", Tx16).with_doc("A vector value")]),

cranelift/codegen/src/ir/instructions.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,12 @@ pub trait InstructionMapper {
10621062
&mut self,
10631063
dynamic_stack_slot: ir::DynamicStackSlot,
10641064
) -> ir::DynamicStackSlot;
1065+
1066+
/// Map a function over a `Constant`.
1067+
fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant;
1068+
1069+
/// Map a function over an `Immediate`.
1070+
fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate;
10651071
}
10661072

10671073
impl<'a, T> InstructionMapper for &'a mut T
@@ -1110,6 +1116,14 @@ where
11101116
) -> ir::DynamicStackSlot {
11111117
(**self).map_dynamic_stack_slot(dynamic_stack_slot)
11121118
}
1119+
1120+
fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant {
1121+
(**self).map_constant(constant)
1122+
}
1123+
1124+
fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate {
1125+
(**self).map_immediate(immediate)
1126+
}
11131127
}
11141128

11151129
#[cfg(test)]
@@ -1320,6 +1334,14 @@ mod tests {
13201334
) -> ir::DynamicStackSlot {
13211335
DynamicStackSlot::from_u32(dynamic_stack_slot.as_u32() + 1)
13221336
}
1337+
1338+
fn map_constant(&mut self, constant: ir::Constant) -> ir::Constant {
1339+
ir::Constant::from_u32(constant.as_u32() + 1)
1340+
}
1341+
1342+
fn map_immediate(&mut self, immediate: ir::Immediate) -> ir::Immediate {
1343+
ir::Immediate::from_u32(immediate.as_u32() + 1)
1344+
}
13231345
}
13241346

13251347
let mut pool = ValueListPool::new();
@@ -1450,5 +1472,31 @@ mod tests {
14501472
dynamic_stack_slot: DynamicStackSlot::from_u32(1),
14511473
},
14521474
);
1475+
1476+
// Mapping `Constant`s
1477+
assert_eq!(
1478+
map(InstructionData::UnaryConst {
1479+
opcode: ir::Opcode::Vconst,
1480+
constant_handle: ir::Constant::from_u32(2)
1481+
}),
1482+
InstructionData::UnaryConst {
1483+
opcode: ir::Opcode::Vconst,
1484+
constant_handle: ir::Constant::from_u32(3)
1485+
},
1486+
);
1487+
1488+
// Mapping `Immediate`s
1489+
assert_eq!(
1490+
map(InstructionData::Shuffle {
1491+
opcode: ir::Opcode::Shuffle,
1492+
args: [Value::from_u32(0), Value::from_u32(1)],
1493+
imm: ir::Immediate::from_u32(41),
1494+
}),
1495+
InstructionData::Shuffle {
1496+
opcode: ir::Opcode::Shuffle,
1497+
args: [Value::from_u32(1), Value::from_u32(2)],
1498+
imm: ir::Immediate::from_u32(42),
1499+
},
1500+
);
14531501
}
14541502
}

0 commit comments

Comments
 (0)