Skip to content

Commit bb26381

Browse files
committed
Introduce log2_min_function_alignment flag
This is required for cg_clif to implement -Zmin-function-alignment.
1 parent a26e78f commit bb26381

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ pub(crate) fn define() -> SettingGroup {
398398
0,
399399
);
400400

401+
settings.add_num(
402+
"log2_min_function_alignment",
403+
"The log2 of the minimum alignment of functions",
404+
"The bigger of this value and the default alignment will be used as actual alignment.",
405+
0,
406+
);
407+
401408
// When adding new settings please check if they can also be added
402409
// in cranelift/fuzzgen/src/lib.rs for fuzzing.
403410
settings.build()

cranelift/codegen/src/machinst/buffer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ enum ForceVeneers {
239239
pub struct MachBuffer<I: VCodeInst> {
240240
/// The buffer contents, as raw bytes.
241241
data: SmallVec<[u8; 1024]>,
242+
/// The required alignment of this buffer.
243+
min_alignment: u32,
242244
/// Any relocations referring to this code. Note that only *external*
243245
/// relocations are tracked here; references to labels within the buffer are
244246
/// resolved before emission.
@@ -433,6 +435,7 @@ impl<I: VCodeInst> MachBuffer<I> {
433435
pub fn new() -> MachBuffer<I> {
434436
MachBuffer {
435437
data: SmallVec::new(),
438+
min_alignment: I::function_alignment().minimum,
436439
relocs: SmallVec::new(),
437440
traps: SmallVec::new(),
438441
call_sites: SmallVec::new(),
@@ -607,7 +610,7 @@ impl<I: VCodeInst> MachBuffer<I> {
607610
/// at the ISA's minimum function alignment and can be increased due to
608611
/// constant requirements.
609612
fn finish_constants(&mut self, constants: &VCodeConstants) -> u32 {
610-
let mut alignment = I::function_alignment().minimum;
613+
let mut alignment = self.min_alignment;
611614
for (constant, offset) in mem::take(&mut self.used_constants) {
612615
let constant = constants.get(constant);
613616
let data = constant.as_slice();
@@ -1665,6 +1668,15 @@ impl<I: VCodeInst> MachBuffer<I> {
16651668
stack_map.finalize(emit_state.frame_layout().sp_to_sized_stack_slots());
16661669
self.user_stack_maps.push((return_addr, span, stack_map));
16671670
}
1671+
1672+
/// Increase the alignment of the buffer to the given alignment if bigger
1673+
/// than the current alignment.
1674+
pub fn set_log2_min_function_alignment(&mut self, align_to: u8) {
1675+
self.min_alignment = self.min_alignment.max(
1676+
1u32.checked_shl(u32::from(align_to))
1677+
.expect("log2_min_function_alignment too large"),
1678+
);
1679+
}
16681680
}
16691681

16701682
impl<I: VCodeInst> Extend<u8> for MachBuffer<I> {

cranelift/codegen/src/machinst/lower.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
382382
block_order,
383383
constants,
384384
VCodeBuildDirection::Backward,
385+
flags.log2_min_function_alignment(),
385386
);
386387

387388
// We usually need two VRegs per instruction result, plus extras for

cranelift/codegen/src/machinst/vcode.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ pub struct VCode<I: VCodeInst> {
195195

196196
/// Facts on VRegs, for proof-carrying code verification.
197197
facts: Vec<Option<Fact>>,
198+
199+
log2_min_function_alignment: u8,
198200
}
199201

200202
/// The result of `VCode::emit`. Contains all information computed
@@ -281,8 +283,16 @@ impl<I: VCodeInst> VCodeBuilder<I> {
281283
block_order: BlockLoweringOrder,
282284
constants: VCodeConstants,
283285
direction: VCodeBuildDirection,
286+
log2_min_function_alignment: u8,
284287
) -> Self {
285-
let vcode = VCode::new(sigs, abi, emit_info, block_order, constants);
288+
let vcode = VCode::new(
289+
sigs,
290+
abi,
291+
emit_info,
292+
block_order,
293+
constants,
294+
log2_min_function_alignment,
295+
);
286296

287297
VCodeBuilder {
288298
vcode,
@@ -602,6 +612,7 @@ impl<I: VCodeInst> VCode<I> {
602612
emit_info: I::Info,
603613
block_order: BlockLoweringOrder,
604614
constants: VCodeConstants,
615+
log2_min_function_alignment: u8,
605616
) -> Self {
606617
let n_blocks = block_order.lowered_order().len();
607618
VCode {
@@ -630,6 +641,7 @@ impl<I: VCodeInst> VCode<I> {
630641
constants,
631642
debug_value_labels: vec![],
632643
facts: vec![],
644+
log2_min_function_alignment,
633645
}
634646
}
635647

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

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

719732
// The first M MachLabels are reserved for block indices.

crates/wasmtime/src/engine.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ impl Engine {
392392
| "regalloc_algorithm"
393393
| "is_pic"
394394
| "bb_padding_log2_minus_one"
395+
| "log2_min_function_alignment"
395396
| "machine_code_cfg_info"
396397
| "tls_model" // wasmtime doesn't use tls right now
397398
| "stack_switch_model" // wasmtime doesn't use stack switching right now

0 commit comments

Comments
 (0)