Skip to content

Refactor jump label realization #7088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 11 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: 4 additions & 3 deletions sway-core/src/asm_generation/fuel/abstract_instruction_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::{
allocated_abstract_instruction_set::AllocatedAbstractInstructionSet, register_allocator,
},
asm_lang::{
allocated_ops::AllocatedOp, Op, OrganizationalOp, RealizedOp, VirtualOp, VirtualRegister,
allocated_ops::AllocatedOp, JumpType, Op, OrganizationalOp, RealizedOp, VirtualOp,
VirtualRegister,
},
};

Expand Down Expand Up @@ -41,9 +42,9 @@ impl AbstractInstructionSet {
.enumerate()
.filter_map(|(idx, ops)| match (&ops[0].opcode, &ops[1].opcode) {
(
Either::Right(OrganizationalOp::Jump(dst_label)),
Either::Right(OrganizationalOp::Jump { to, type_, .. }),
Either::Right(OrganizationalOp::Label(label)),
) if dst_label == label => Some(idx),
) if to == label && !matches!(type_, JumpType::Call) => Some(idx),
_otherwise => None,
})
.collect();
Expand Down
650 changes: 332 additions & 318 deletions sway-core/src/asm_generation/fuel/allocated_abstract_instruction_set.rs

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions sway-core/src/asm_generation/fuel/fuel_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
FinalizedAsm, ProgramKind,
},
asm_lang::{
virtual_register::*, Label, Op, VirtualImmediate06, VirtualImmediate12, VirtualImmediate18,
VirtualOp, WideCmp, WideOperations,
virtual_register::*, JumpType, Label, Op, VirtualImmediate06, VirtualImmediate12,
VirtualImmediate18, VirtualOp, WideCmp, WideOperations,
},
decl_engine::DeclRefFunction,
metadata::MetadataManager,
Expand Down Expand Up @@ -174,7 +174,11 @@ impl AsmBuilder for FuelAsmBuilder<'_, '_> {

// call decode
self.before_entries.push(Op {
opcode: Either::Right(crate::asm_lang::ControlFlowOp::Call(*decode_fn_label)),
opcode: Either::Right(crate::asm_lang::ControlFlowOp::Jump {
to: *decode_fn_label,
type_: JumpType::Call,
force_far: false,
}),
comment: format!("decode configurable {}", name),
owning_span: None,
});
Expand Down
8 changes: 6 additions & 2 deletions sway-core/src/asm_generation/fuel/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
asm_lang::{
virtual_register::{self, *},
Op, OrganizationalOp, VirtualImmediate12, VirtualImmediate18, VirtualImmediate24,
JumpType, Op, OrganizationalOp, VirtualImmediate12, VirtualImmediate18, VirtualImmediate24,
VirtualOp,
},
decl_engine::DeclRef,
Expand Down Expand Up @@ -191,7 +191,11 @@ impl FuelAsmBuilder<'_, '_> {
// Jump to function and insert return label.
let (fn_label, _) = self.func_to_labels(function);
self.cur_bytecode.push(Op {
opcode: Either::Right(OrganizationalOp::Call(fn_label)),
opcode: Either::Right(OrganizationalOp::Jump {
to: fn_label,
type_: JumpType::Call,
force_far: false,
}),
comment: format!("[call]: call {}", function.get_name(self.context)),
owning_span: None,
});
Expand Down
22 changes: 11 additions & 11 deletions sway-core/src/asm_generation/fuel/optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hash::{FxHashMap, FxHashSet};

use crate::{
asm_generation::fuel::compiler_constants,
asm_lang::{ControlFlowOp, Label, VirtualImmediate12, VirtualOp, VirtualRegister},
asm_lang::{ControlFlowOp, JumpType, Label, VirtualImmediate12, VirtualOp, VirtualRegister},
};

use super::{
Expand Down Expand Up @@ -273,16 +273,16 @@ impl AbstractInstructionSet {
let mut op_def = op.def_registers();
op_def.append(&mut op.def_const_registers());

if let Either::Right(ControlFlowOp::Jump(_) | ControlFlowOp::JumpIfNotZero(..)) =
op.opcode
{
// Block boundary. Start afresh.
cur_live.clone_from(liveness.get(ix).expect("Incorrect liveness info"));
// Add use(op) to cur_live.
for u in op_use {
cur_live.insert(u.clone());
if let Either::Right(ControlFlowOp::Jump { type_, .. }) = &op.opcode {
if !matches!(type_, JumpType::Call) {
// Block boundary. Start afresh.
cur_live.clone_from(liveness.get(ix).expect("Incorrect liveness info"));
// Add use(op) to cur_live.
for u in op_use {
cur_live.insert(u.clone());
}
continue;
}
continue;
}

let dead = op_def.iter().all(|def| !cur_live.contains(def))
Expand Down Expand Up @@ -344,7 +344,7 @@ impl AbstractInstructionSet {
reachables[op_idx] = true;
let op = &ops[op_idx];
for s in &op.successors(op_idx, ops, &label_to_index) {
if !reachables[*s] {
if !reachables[*s] && !worklist.contains(s) {
worklist.push(*s);
}
}
Expand Down
26 changes: 21 additions & 5 deletions sway-core/src/asm_generation/fuel/programs/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
},
asm_lang::{
allocated_ops::{AllocatedOpcode, AllocatedRegister},
AllocatedAbstractOp, ConstantRegister, ControlFlowOp, Label, VirtualImmediate12,
AllocatedAbstractOp, ConstantRegister, ControlFlowOp, JumpType, Label, VirtualImmediate12,
VirtualImmediate18, VirtualImmediate24,
},
decl_engine::DeclRefFunction,
Expand Down Expand Up @@ -199,7 +199,11 @@ impl AbstractProgram {
},
// word 1.5
AllocatedAbstractOp {
opcode: Either::Right(ControlFlowOp::Jump(label)),
opcode: Either::Right(ControlFlowOp::Jump {
to: label,
type_: JumpType::Unconditional,
force_far: false,
}),
comment: String::new(),
owning_span: None,
},
Expand Down Expand Up @@ -249,7 +253,11 @@ impl AbstractProgram {
fn append_jump_to_entry(&mut self, asm: &mut AllocatedAbstractInstructionSet) {
let entry = self.entries.iter().find(|x| x.name == "__entry").unwrap();
asm.ops.push(AllocatedAbstractOp {
opcode: Either::Right(ControlFlowOp::Jump(entry.label)),
opcode: Either::Right(ControlFlowOp::Jump {
to: entry.label,
type_: JumpType::Unconditional,
force_far: false,
}),
comment: "jump to ABI function selector".into(),
owning_span: None,
});
Expand Down Expand Up @@ -332,15 +340,23 @@ impl AbstractProgram {
// Jump to the function label if the selector was equal.
asm.ops.push(AllocatedAbstractOp {
// If the comparison result is _not_ equal to 0, then it was indeed equal.
opcode: Either::Right(ControlFlowOp::JumpIfNotZero(CMP_RESULT_REG, entry.label)),
opcode: Either::Right(ControlFlowOp::Jump {
to: entry.label,
type_: JumpType::NotZero(CMP_RESULT_REG),
force_far: false,
}),
comment: "[function selection]: jump to selected contract function".into(),
owning_span: None,
});
}

if let Some(fallback_fn) = fallback_fn {
asm.ops.push(AllocatedAbstractOp {
opcode: Either::Right(ControlFlowOp::Call(fallback_fn)),
opcode: Either::Right(ControlFlowOp::Jump {
to: fallback_fn,
type_: JumpType::Call,
force_far: false,
}),
comment: "[function selection]: call contract fallback function".into(),
owning_span: None,
});
Expand Down
Loading