Implement initial emission of constants
This approach suffers from memory-size bloat during compile time due to the desire to de-duplicate the constants emitted and reduce runtime memory-size. As a first step, though, this provides an end-to-end mechanism for constants to be emitted in the MachBuffer islands.
This commit is contained in:
@@ -142,8 +142,9 @@
|
||||
|
||||
use crate::binemit::{Addend, CodeOffset, CodeSink, Reloc, StackMap};
|
||||
use crate::ir::{ExternalName, Opcode, SourceLoc, TrapCode};
|
||||
use crate::machinst::{BlockIndex, MachInstLabelUse, VCodeInst};
|
||||
use crate::machinst::{BlockIndex, MachInstLabelUse, VCodeConstant, VCodeConstants, VCodeInst};
|
||||
use crate::timing;
|
||||
use cranelift_entity::{entity_impl, SecondaryMap};
|
||||
|
||||
use log::trace;
|
||||
use smallvec::SmallVec;
|
||||
@@ -218,6 +219,8 @@ pub struct MachBuffer<I: VCodeInst> {
|
||||
/// when the offset has grown past this (`labels_at_tail_off`) point.
|
||||
/// Always <= `cur_offset()`.
|
||||
labels_at_tail_off: CodeOffset,
|
||||
/// Map used constants to their [MachLabel].
|
||||
constant_labels: SecondaryMap<VCodeConstant, MachLabel>,
|
||||
}
|
||||
|
||||
/// A `MachBuffer` once emission is completed: holds generated code and records,
|
||||
@@ -248,6 +251,7 @@ static UNKNOWN_LABEL: MachLabel = MachLabel(0xffff_ffff);
|
||||
/// appropriately when the label's location is eventually known.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct MachLabel(u32);
|
||||
entity_impl!(MachLabel);
|
||||
|
||||
impl MachLabel {
|
||||
/// Get a label for a block. (The first N MachLabels are always reseved for
|
||||
@@ -267,6 +271,12 @@ impl MachLabel {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for MachLabel {
|
||||
fn default() -> Self {
|
||||
UNKNOWN_LABEL
|
||||
}
|
||||
}
|
||||
|
||||
/// A stack map extent, when creating a stack map.
|
||||
pub enum StackMapExtent {
|
||||
/// The stack map starts at this instruction, and ends after the number of upcoming bytes
|
||||
@@ -299,6 +309,7 @@ impl<I: VCodeInst> MachBuffer<I> {
|
||||
latest_branches: SmallVec::new(),
|
||||
labels_at_tail: SmallVec::new(),
|
||||
labels_at_tail_off: 0,
|
||||
constant_labels: SecondaryMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -468,6 +479,24 @@ impl<I: VCodeInst> MachBuffer<I> {
|
||||
// Post-invariant: as for `get_label()`.
|
||||
}
|
||||
|
||||
/// Reserve the next N MachLabels for constants.
|
||||
pub fn reserve_labels_for_constants(&mut self, constants: &VCodeConstants) {
|
||||
trace!(
|
||||
"MachBuffer: next {} labels are for constants",
|
||||
constants.len()
|
||||
);
|
||||
for c in constants.keys() {
|
||||
self.constant_labels[c] = self.get_label();
|
||||
}
|
||||
|
||||
// Post-invariant: as for `get_label()`.
|
||||
}
|
||||
|
||||
/// Retrieve the reserved label for a constant.
|
||||
pub fn get_label_for_constant(&self, constant: VCodeConstant) -> MachLabel {
|
||||
self.constant_labels[constant]
|
||||
}
|
||||
|
||||
/// Bind a label to the current offset. A label can only be bound once.
|
||||
pub fn bind_label(&mut self, label: MachLabel) {
|
||||
trace!(
|
||||
@@ -998,7 +1027,13 @@ impl<I: VCodeInst> MachBuffer<I> {
|
||||
data: &[u8],
|
||||
max_distance: CodeOffset,
|
||||
) {
|
||||
let deadline = self.cur_offset() + max_distance;
|
||||
trace!(
|
||||
"defer_constant: eventually emit {} bytes aligned to {} at label {:?}",
|
||||
data.len(),
|
||||
align,
|
||||
label
|
||||
);
|
||||
let deadline = self.cur_offset().saturating_add(max_distance);
|
||||
self.island_worst_case_size += data.len() as CodeOffset;
|
||||
self.island_worst_case_size &= !(I::LabelUse::ALIGN - 1);
|
||||
self.pending_constants.push(MachLabelConstant {
|
||||
@@ -1136,14 +1171,6 @@ impl<I: VCodeInst> MachBuffer<I> {
|
||||
pub fn finish(mut self) -> MachBufferFinalized {
|
||||
let _tt = timing::vcode_emit_finish();
|
||||
|
||||
// Ensure that all labels are defined. This is a full (release-mode)
|
||||
// assert because we must avoid looping indefinitely below; an
|
||||
// unresolved label will prevent the fixup_records vec from emptying.
|
||||
assert!(self
|
||||
.label_offsets
|
||||
.iter()
|
||||
.all(|&off| off != UNKNOWN_LABEL_OFFSET));
|
||||
|
||||
while !self.pending_constants.is_empty() || !self.fixup_records.is_empty() {
|
||||
// `emit_island()` will emit any pending veneers and constants, and
|
||||
// as a side-effect, will also take care of any fixups with resolved
|
||||
@@ -1151,6 +1178,11 @@ impl<I: VCodeInst> MachBuffer<I> {
|
||||
self.emit_island();
|
||||
}
|
||||
|
||||
// Ensure that all labels have been fixed up after the last island is emitted. This is a
|
||||
// full (release-mode) assert because an unresolved label means the emitted code is
|
||||
// incorrect.
|
||||
assert!(self.fixup_records.is_empty());
|
||||
|
||||
MachBufferFinalized {
|
||||
data: self.data,
|
||||
relocs: self.relocs,
|
||||
|
||||
@@ -13,7 +13,7 @@ use crate::ir::{
|
||||
};
|
||||
use crate::machinst::{
|
||||
ABICallee, BlockIndex, BlockLoweringOrder, LoweredBlock, MachLabel, VCode, VCodeBuilder,
|
||||
VCodeInst,
|
||||
VCodeConstant, VCodeConstantData, VCodeConstants, VCodeInst,
|
||||
};
|
||||
use crate::CodegenResult;
|
||||
|
||||
@@ -162,6 +162,8 @@ pub trait LowerCtx {
|
||||
fn is_reg_needed(&self, ir_inst: Inst, reg: Reg) -> bool;
|
||||
/// Retrieve constant data given a handle.
|
||||
fn get_constant_data(&self, constant_handle: Constant) -> &ConstantData;
|
||||
/// Indicate that a constant should be emitted.
|
||||
fn use_constant(&mut self, constant: VCodeConstantData) -> VCodeConstant;
|
||||
/// Retrieve the value immediate from an instruction. This will perform necessary lookups on the
|
||||
/// `DataFlowGraph` to retrieve even large immediates.
|
||||
fn get_immediate(&self, ir_inst: Inst) -> Option<DataValue>;
|
||||
@@ -318,7 +320,8 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
|
||||
emit_info: I::Info,
|
||||
block_order: BlockLoweringOrder,
|
||||
) -> CodegenResult<Lower<'func, I>> {
|
||||
let mut vcode = VCodeBuilder::new(abi, emit_info, block_order);
|
||||
let constants = VCodeConstants::with_capacity(f.dfg.constants.len());
|
||||
let mut vcode = VCodeBuilder::new(abi, emit_info, block_order, constants);
|
||||
|
||||
let mut next_vreg: u32 = 0;
|
||||
|
||||
@@ -1010,6 +1013,10 @@ impl<'func, I: VCodeInst> LowerCtx for Lower<'func, I> {
|
||||
self.f.dfg.constants.get(constant_handle)
|
||||
}
|
||||
|
||||
fn use_constant(&mut self, constant: VCodeConstantData) -> VCodeConstant {
|
||||
self.vcode.constants().insert(constant)
|
||||
}
|
||||
|
||||
fn get_immediate(&self, ir_inst: Inst) -> Option<DataValue> {
|
||||
let inst_data = self.data(ir_inst);
|
||||
match inst_data {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
//! See the main module comment in `mod.rs` for more details on the VCode-based
|
||||
//! backend pipeline.
|
||||
|
||||
use crate::ir::{self, types, SourceLoc};
|
||||
use crate::ir::{self, types, Constant, ConstantData, SourceLoc};
|
||||
use crate::machinst::*;
|
||||
use crate::settings;
|
||||
use crate::timing;
|
||||
@@ -31,7 +31,9 @@ use regalloc::{
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::{borrow::Cow, vec::Vec};
|
||||
use cranelift_entity::{entity_impl, Keys, PrimaryMap};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::iter;
|
||||
use std::string::String;
|
||||
@@ -110,6 +112,9 @@ pub struct VCode<I: VCodeInst> {
|
||||
|
||||
/// Instruction end offsets
|
||||
insts_layout: RefCell<(Vec<u32>, u32)>,
|
||||
|
||||
/// Constants.
|
||||
constants: VCodeConstants,
|
||||
}
|
||||
|
||||
/// A builder for a VCode function body. This builder is designed for the
|
||||
@@ -149,9 +154,10 @@ impl<I: VCodeInst> VCodeBuilder<I> {
|
||||
abi: Box<dyn ABICallee<I = I>>,
|
||||
emit_info: I::Info,
|
||||
block_order: BlockLoweringOrder,
|
||||
constants: VCodeConstants,
|
||||
) -> VCodeBuilder<I> {
|
||||
let reftype_class = I::ref_type_regclass(abi.flags());
|
||||
let vcode = VCode::new(abi, emit_info, block_order);
|
||||
let vcode = VCode::new(abi, emit_info, block_order, constants);
|
||||
let stack_map_info = StackmapRequestInfo {
|
||||
reftype_class,
|
||||
reftyped_vregs: vec![],
|
||||
@@ -255,6 +261,11 @@ impl<I: VCodeInst> VCodeBuilder<I> {
|
||||
self.cur_srcloc = srcloc;
|
||||
}
|
||||
|
||||
/// Access the constants.
|
||||
pub fn constants(&mut self) -> &mut VCodeConstants {
|
||||
&mut self.vcode.constants
|
||||
}
|
||||
|
||||
/// Build the final VCode, returning the vcode itself as well as auxiliary
|
||||
/// information, such as the stack map request information.
|
||||
pub fn build(self) -> (VCode<I>, StackmapRequestInfo) {
|
||||
@@ -284,6 +295,7 @@ impl<I: VCodeInst> VCode<I> {
|
||||
abi: Box<dyn ABICallee<I = I>>,
|
||||
emit_info: I::Info,
|
||||
block_order: BlockLoweringOrder,
|
||||
constants: VCodeConstants,
|
||||
) -> VCode<I> {
|
||||
VCode {
|
||||
liveins: abi.liveins(),
|
||||
@@ -303,6 +315,7 @@ impl<I: VCodeInst> VCode<I> {
|
||||
safepoint_slots: vec![],
|
||||
prologue_epilogue_ranges: None,
|
||||
insts_layout: RefCell::new((vec![], 0)),
|
||||
constants,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -466,7 +479,10 @@ impl<I: VCodeInst> VCode<I> {
|
||||
let mut buffer = MachBuffer::new();
|
||||
let mut state = I::State::new(&*self.abi);
|
||||
|
||||
buffer.reserve_labels_for_blocks(self.num_blocks() as BlockIndex); // first N MachLabels are simply block indices.
|
||||
// The first M MachLabels are reserved for block indices, the next N MachLabels for
|
||||
// constants.
|
||||
buffer.reserve_labels_for_blocks(self.num_blocks() as BlockIndex);
|
||||
buffer.reserve_labels_for_constants(&self.constants);
|
||||
|
||||
let mut insts_layout = vec![0; self.insts.len()];
|
||||
|
||||
@@ -530,6 +546,12 @@ impl<I: VCodeInst> VCode<I> {
|
||||
}
|
||||
}
|
||||
|
||||
// Emit the constants used by the function.
|
||||
for (constant, data) in self.constants.iter() {
|
||||
let label = buffer.get_label_for_constant(constant);
|
||||
buffer.defer_constant(label, data.alignment(), data.as_slice(), u32::max_value());
|
||||
}
|
||||
|
||||
*self.insts_layout.borrow_mut() = (insts_layout, buffer.cur_offset());
|
||||
|
||||
buffer
|
||||
@@ -735,3 +757,141 @@ impl<I: VCodeInst> PrettyPrint for VCode<I> {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// This structure tracks the large constants used in VCode that will be emitted separately by the
|
||||
/// [MachBuffer].
|
||||
///
|
||||
/// First, during the lowering phase, constants are inserted using
|
||||
/// [VCodeConstants.insert]; an intermediate handle, [VCodeConstant], tracks what constants are
|
||||
/// used in this phase. Some deduplication is performed, when possible, as constant
|
||||
/// values are inserted.
|
||||
///
|
||||
/// Secondly, during the emission phase, the [MachBuffer] assigns [MachLabel]s for each of the
|
||||
/// constants so that instructions can refer to the value's memory location. The [MachBuffer]
|
||||
/// then writes the constant values to the buffer.
|
||||
#[derive(Default)]
|
||||
pub struct VCodeConstants {
|
||||
constants: PrimaryMap<VCodeConstant, VCodeConstantData>,
|
||||
pool_uses: HashMap<Constant, VCodeConstant>,
|
||||
well_known_uses: HashMap<*const [u8], VCodeConstant>,
|
||||
}
|
||||
impl VCodeConstants {
|
||||
/// Initialize the structure with the expected number of constants.
|
||||
pub fn with_capacity(expected_num_constants: usize) -> Self {
|
||||
Self {
|
||||
constants: PrimaryMap::with_capacity(expected_num_constants),
|
||||
pool_uses: HashMap::with_capacity(expected_num_constants),
|
||||
well_known_uses: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a constant; using this method indicates that a constant value will be used and thus
|
||||
/// will be emitted to the `MachBuffer`. The current implementation can deduplicate constants
|
||||
/// that are [VCodeConstantData::Pool] or [VCodeConstantData::WellKnown] but not
|
||||
/// [VCodeConstantData::Generated].
|
||||
pub fn insert(&mut self, data: VCodeConstantData) -> VCodeConstant {
|
||||
match data {
|
||||
VCodeConstantData::Generated(_) => self.constants.push(data),
|
||||
VCodeConstantData::Pool(constant, _) => match self.pool_uses.get(&constant) {
|
||||
None => {
|
||||
let vcode_constant = self.constants.push(data);
|
||||
self.pool_uses.insert(constant, vcode_constant);
|
||||
vcode_constant
|
||||
}
|
||||
Some(&vcode_constant) => vcode_constant,
|
||||
},
|
||||
VCodeConstantData::WellKnown(data_ref) => {
|
||||
match self.well_known_uses.get(&(data_ref as *const [u8])) {
|
||||
None => {
|
||||
let vcode_constant = self.constants.push(data);
|
||||
self.well_known_uses
|
||||
.insert(data_ref as *const [u8], vcode_constant);
|
||||
vcode_constant
|
||||
}
|
||||
Some(&vcode_constant) => vcode_constant,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Retrieve a byte slice for the given [VCodeConstant], if available.
|
||||
pub fn get(&self, constant: VCodeConstant) -> Option<&[u8]> {
|
||||
self.constants.get(constant).map(|d| d.as_slice())
|
||||
}
|
||||
|
||||
/// Return the number of constants inserted.
|
||||
pub fn len(&self) -> usize {
|
||||
self.constants.len()
|
||||
}
|
||||
|
||||
/// Iterate over the [VCodeConstant] keys inserted in this structure.
|
||||
pub fn keys(&self) -> Keys<VCodeConstant> {
|
||||
self.constants.keys()
|
||||
}
|
||||
|
||||
/// Iterate over the [VCodeConstant] keys and the data (as a byte slice) inserted in this
|
||||
/// structure.
|
||||
pub fn iter(&self) -> impl Iterator<Item = (VCodeConstant, &VCodeConstantData)> {
|
||||
self.constants.iter()
|
||||
}
|
||||
}
|
||||
|
||||
/// A use of a constant by one or more VCode instructions; see [VCodeConstants].
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct VCodeConstant(u32);
|
||||
entity_impl!(VCodeConstant);
|
||||
|
||||
/// Identify the different types of constant that can be inserted into [VCodeConstants]. Tracking
|
||||
/// these separately instead of as raw byte buffers allows us to avoid some duplication.
|
||||
pub enum VCodeConstantData {
|
||||
/// A constant already present in the Cranelift IR
|
||||
/// [ConstantPool](crate::ir::constant::ConstantPool).
|
||||
Pool(Constant, ConstantData),
|
||||
/// A reference to a well-known constant value that is statically encoded within the compiler.
|
||||
WellKnown(&'static [u8]),
|
||||
/// A constant value generated during lowering; the value may depend on the instruction context
|
||||
/// which makes it difficult to de-duplicate--if possible, use other variants.
|
||||
Generated(ConstantData),
|
||||
}
|
||||
impl VCodeConstantData {
|
||||
/// Retrieve the constant data as a byte slice.
|
||||
pub fn as_slice(&self) -> &[u8] {
|
||||
match self {
|
||||
VCodeConstantData::Pool(_, d) | VCodeConstantData::Generated(d) => d.as_slice(),
|
||||
VCodeConstantData::WellKnown(d) => d,
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the alignment of the constant data.
|
||||
pub fn alignment(&self) -> u32 {
|
||||
if self.as_slice().len() <= 8 {
|
||||
8
|
||||
} else {
|
||||
16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use std::mem::{size_of, size_of_val};
|
||||
|
||||
#[test]
|
||||
fn size_of_constant_structs() {
|
||||
assert_eq!(size_of::<Constant>(), 4);
|
||||
assert_eq!(size_of::<VCodeConstant>(), 4);
|
||||
assert_eq!(size_of::<ConstantData>(), 24);
|
||||
assert_eq!(size_of::<VCodeConstantData>(), 32);
|
||||
assert_eq!(
|
||||
size_of::<PrimaryMap<VCodeConstant, VCodeConstantData>>(),
|
||||
24
|
||||
);
|
||||
assert_eq!(size_of::<HashMap<Constant, VCodeConstant>>(), 48);
|
||||
assert_eq!(size_of::<HashMap<*const [u8], VCodeConstant>>(), 48);
|
||||
assert_eq!(size_of::<VCodeConstants>(), 120);
|
||||
assert_eq!(size_of_val(&VCodeConstants::with_capacity(0)), 120);
|
||||
// TODO This structure could use some significant memory-size optimization. The use of
|
||||
// HashMap to deduplicate both pool and well-known constants is clearly an issue.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user