Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cranelift/codegen/meta/src/shared/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ pub(crate) fn define() -> SettingGroup {
0,
);

settings.add_num(
"log2_min_function_alignment",
"The log2 of the minimum alignment of functions",
"The bigger of this value and the default alignment will be used as actual alignment.",
0,
);

// When adding new settings please check if they can also be added
// in cranelift/fuzzgen/src/lib.rs for fuzzing.
settings.build()
Expand Down
14 changes: 13 additions & 1 deletion cranelift/codegen/src/machinst/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ enum ForceVeneers {
pub struct MachBuffer<I: VCodeInst> {
/// The buffer contents, as raw bytes.
data: SmallVec<[u8; 1024]>,
/// The required alignment of this buffer.
min_alignment: u32,
/// Any relocations referring to this code. Note that only *external*
/// relocations are tracked here; references to labels within the buffer are
/// resolved before emission.
Expand Down Expand Up @@ -433,6 +435,7 @@ impl<I: VCodeInst> MachBuffer<I> {
pub fn new() -> MachBuffer<I> {
MachBuffer {
data: SmallVec::new(),
min_alignment: I::function_alignment().minimum,
relocs: SmallVec::new(),
traps: SmallVec::new(),
call_sites: SmallVec::new(),
Expand Down Expand Up @@ -607,7 +610,7 @@ impl<I: VCodeInst> MachBuffer<I> {
/// at the ISA's minimum function alignment and can be increased due to
/// constant requirements.
fn finish_constants(&mut self, constants: &VCodeConstants) -> u32 {
let mut alignment = I::function_alignment().minimum;
let mut alignment = self.min_alignment;
for (constant, offset) in mem::take(&mut self.used_constants) {
let constant = constants.get(constant);
let data = constant.as_slice();
Expand Down Expand Up @@ -1665,6 +1668,15 @@ impl<I: VCodeInst> MachBuffer<I> {
stack_map.finalize(emit_state.frame_layout().sp_to_sized_stack_slots());
self.user_stack_maps.push((return_addr, span, stack_map));
}

/// Increase the alignment of the buffer to the given alignment if bigger
/// than the current alignment.
pub fn set_log2_min_function_alignment(&mut self, align_to: u8) {
self.min_alignment = self.min_alignment.max(
1u32.checked_shl(u32::from(align_to))
.expect("log2_min_function_alignment too large"),
);
}
}

impl<I: VCodeInst> Extend<u8> for MachBuffer<I> {
Expand Down
1 change: 1 addition & 0 deletions cranelift/codegen/src/machinst/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
block_order,
constants,
VCodeBuildDirection::Backward,
flags.log2_min_function_alignment(),
);

// We usually need two VRegs per instruction result, plus extras for
Expand Down
15 changes: 14 additions & 1 deletion cranelift/codegen/src/machinst/vcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ pub struct VCode<I: VCodeInst> {

/// Facts on VRegs, for proof-carrying code verification.
facts: Vec<Option<Fact>>,

log2_min_function_alignment: u8,
}

/// The result of `VCode::emit`. Contains all information computed
Expand Down Expand Up @@ -281,8 +283,16 @@ impl<I: VCodeInst> VCodeBuilder<I> {
block_order: BlockLoweringOrder,
constants: VCodeConstants,
direction: VCodeBuildDirection,
log2_min_function_alignment: u8,
) -> Self {
let vcode = VCode::new(sigs, abi, emit_info, block_order, constants);
let vcode = VCode::new(
sigs,
abi,
emit_info,
block_order,
constants,
log2_min_function_alignment,
);

VCodeBuilder {
vcode,
Expand Down Expand Up @@ -602,6 +612,7 @@ impl<I: VCodeInst> VCode<I> {
emit_info: I::Info,
block_order: BlockLoweringOrder,
constants: VCodeConstants,
log2_min_function_alignment: u8,
) -> Self {
let n_blocks = block_order.lowered_order().len();
VCode {
Expand Down Expand Up @@ -630,6 +641,7 @@ impl<I: VCodeInst> VCode<I> {
constants,
debug_value_labels: vec![],
facts: vec![],
log2_min_function_alignment,
}
}

Expand Down Expand Up @@ -714,6 +726,7 @@ impl<I: VCodeInst> VCode<I> {

let _tt = timing::vcode_emit();
let mut buffer = MachBuffer::new();
buffer.set_log2_min_function_alignment(self.log2_min_function_alignment);
let mut bb_starts: Vec<Option<CodeOffset>> = vec![];

// The first M MachLabels are reserved for block indices.
Expand Down
1 change: 1 addition & 0 deletions cranelift/codegen/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ libcall_call_conv = "isa_default"
probestack_size_log2 = 12
probestack_strategy = "outline"
bb_padding_log2_minus_one = 0
log2_min_function_alignment = 0
regalloc_checker = false
regalloc_verbose_logs = false
enable_alias_analysis = true
Expand Down
8 changes: 2 additions & 6 deletions cranelift/jit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,7 @@ impl Module for JITModule {
let compiled_code = ctx.compiled_code().unwrap();

let size = compiled_code.code_info().total_size as usize;
let align = alignment
.max(self.isa.function_alignment().minimum as u64)
.max(self.isa.symbol_alignment());
let align = alignment.max(self.isa.symbol_alignment());
let ptr = self
.memory
.code
Expand Down Expand Up @@ -702,9 +700,7 @@ impl Module for JITModule {
}

let size = bytes.len();
let align = alignment
.max(self.isa.function_alignment().minimum as u64)
.max(self.isa.symbol_alignment());
let align = alignment.max(self.isa.symbol_alignment());
let ptr = self
.memory
.code
Expand Down
4 changes: 1 addition & 3 deletions cranelift/object/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,7 @@ impl ObjectModule {
}
*defined = true;

let align = alignment
.max(self.isa.function_alignment().minimum.into())
.max(self.isa.symbol_alignment());
let align = alignment.max(self.isa.symbol_alignment());
let section = if self.per_function_section {
// FIXME pass empty symbol name once add_subsection produces `.text` as section name
// instead of `.text.` when passed an empty symbol name. (object#748) Until then pass
Expand Down
1 change: 1 addition & 0 deletions crates/wasmtime/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ impl Engine {
| "regalloc_algorithm"
| "is_pic"
| "bb_padding_log2_minus_one"
| "log2_min_function_alignment"
| "machine_code_cfg_info"
| "tls_model" // wasmtime doesn't use tls right now
| "stack_switch_model" // wasmtime doesn't use stack switching right now
Expand Down