Skip to content
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

fix bug in fndedup for fuelvm instructions #6045

Merged
merged 5 commits into from
May 22, 2024
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
49 changes: 28 additions & 21 deletions sway-ir/src/optimize/fn_dedup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ fn hash_fn(
hasher: &mut FxHasher,
ignore_metadata: bool,
) {
match &context.values.get(v.0).unwrap().value {
let val = &context.values.get(v.0).unwrap().value;
std::mem::discriminant(val).hash(hasher);
match val {
crate::ValueDatum::Argument(_) | crate::ValueDatum::Instruction(_) => {
get_localised_id(v, localised_value_id).hash(hasher)
}
Expand Down Expand Up @@ -227,27 +229,32 @@ fn hash_fn(
crate::InstOp::ContractCall { name, .. } => {
name.hash(state);
}
crate::InstOp::FuelVm(fuel_vm_inst) => match fuel_vm_inst {
crate::FuelVmInstruction::Gtf { tx_field_id, .. } => tx_field_id.hash(state),
crate::FuelVmInstruction::Log { log_ty, .. } => log_ty.hash(state),
crate::FuelVmInstruction::ReadRegister(reg) => reg.hash(state),
crate::FuelVmInstruction::Revert(_)
| crate::FuelVmInstruction::JmpMem
| crate::FuelVmInstruction::Smo { .. }
| crate::FuelVmInstruction::StateClear { .. }
| crate::FuelVmInstruction::StateLoadQuadWord { .. }
| crate::FuelVmInstruction::StateLoadWord(_)
| crate::FuelVmInstruction::StateStoreQuadWord { .. }
| crate::FuelVmInstruction::StateStoreWord { .. } => (),
crate::FuelVmInstruction::WideUnaryOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideBinaryOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideModularOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideCmpOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::Retd { ptr, len } => {
ptr.hash(state);
len.hash(state);
crate::InstOp::FuelVm(fuel_vm_inst) => {
std::mem::discriminant(fuel_vm_inst).hash(state);
match fuel_vm_inst {
crate::FuelVmInstruction::Gtf { tx_field_id, .. } => {
tx_field_id.hash(state)
}
crate::FuelVmInstruction::Log { log_ty, .. } => log_ty.hash(state),
crate::FuelVmInstruction::ReadRegister(reg) => reg.hash(state),
crate::FuelVmInstruction::Revert(_)
| crate::FuelVmInstruction::JmpMem
| crate::FuelVmInstruction::Smo { .. }
| crate::FuelVmInstruction::StateClear { .. }
| crate::FuelVmInstruction::StateLoadQuadWord { .. }
| crate::FuelVmInstruction::StateLoadWord(_)
| crate::FuelVmInstruction::StateStoreQuadWord { .. }
| crate::FuelVmInstruction::StateStoreWord { .. } => (),
crate::FuelVmInstruction::WideUnaryOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideBinaryOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideModularOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::WideCmpOp { op, .. } => op.hash(state),
crate::FuelVmInstruction::Retd { ptr, len } => {
ptr.hash(state);
len.hash(state);
}
}
},
}
crate::InstOp::GetLocal(local) => function
.lookup_local_name(context, local)
.unwrap()
Expand Down
59 changes: 59 additions & 0 deletions sway-ir/tests/fn_dedup/release/fuelvm-ops.ir
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
script {

// check: fn main
entry fn main() -> u64 {
entry():
v0 = call foo()
v1 = call bar()
v3 = call foo_dup()
v2 = add v0, v1
v3 = add v2, v3
ret u64 v2
}

// check: fn foo
fn foo() -> u64 {
entry():
v0 = const u64 0
v1 = int_to_ptr v0 to ptr b256

v2 = const u64 0
v3 = int_to_ptr v0 to ptr b256

v4 = const u64 1

v5 = state_load_quad_word v1, key v3, v4
ret u64 v4
}

// not: fn foo_dup
fn foo_dup() -> u64 {
entry():
v0 = const u64 0
v1 = int_to_ptr v0 to ptr b256

v2 = const u64 0
v3 = int_to_ptr v0 to ptr b256

v4 = const u64 1

v5 = state_load_quad_word v1, key v3, v4
ret u64 v4
}

// check: fn bar
fn bar() -> u64 {
entry():
v0 = const u64 0
v1 = int_to_ptr v0 to ptr b256

v2 = const u64 0
v3 = int_to_ptr v0 to ptr b256

v4 = const u64 1

v5 = state_store_quad_word v1, key v3, v4
ret u64 v4

}
}
Loading