Skip to content

Commit

Permalink
Traverse IR blocks using a reverse post order traverse of the CFG (Fu…
Browse files Browse the repository at this point in the history
…elLabs#3942)

Closes FuelLabs#3815 

This change updates how IR blocks are traversed when compiling them into
assembly. Specifically, a reverse post-order traversal is used.
Following this order forces all IR values to be converted to registers
before all their uses. Even though the initial IR (out of IR gen) may
have the right order of blocks, CFG-altering passes like simplify-cfg
can change that order, so a correct traversal of the final CFG is
needed.
  • Loading branch information
mohammadfawaz authored Feb 1, 2023
1 parent 0e204d5 commit fb2503b
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
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 @@ -205,8 +205,12 @@ impl<'ir> FuelAsmBuilder<'ir> {
// Compile instructions.
let mut warnings = Vec::new();
let mut errors = Vec::new();
for block in function.block_iter(self.context) {
self.insert_block_label(block);

// Traverse the IR blocks in reverse post order. This guarantees that each block is
// processed after all its CFG predecessors have been processed.
let po = sway_ir::dominator::compute_post_order(self.context, &function);
for block in po.po_to_block.iter().rev() {
self.insert_block_label(*block);
for instr_val in block.instruction_iter(self.context) {
check!(
self.compile_instruction(&instr_val, func_is_entry),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[package]]
name = 'complex_cfg'
source = 'member'
dependencies = ['std']

[[package]]
name = 'core'
source = 'path+from-root-8B200AD7FF2D8C10'

[[package]]
name = 'std'
source = 'path+from-root-8B200AD7FF2D8C10'
dependencies = ['core']
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "complex_cfg"

[dependencies]
std = { path = "../../../../../../../sway-lib-std" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
script;

use std::u128::U128;

pub enum Error {
Overflow: (),
}

fn main() {
let x = U128 {
upper: 0,
lower: 0,
};
let cond = false;
require(cond || (x < U128::from((1, 1)) || x == U128::from((1, 1))), Error::Overflow);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 0 }
validate_abi = false
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"typeArguments": null
},
"name": "C0",
"offset": 3268
"offset": 3244
},
{
"configurableType": {
Expand All @@ -16,7 +16,7 @@
"typeArguments": null
},
"name": "C1",
"offset": 3276
"offset": 3252
},
{
"configurableType": {
Expand All @@ -25,7 +25,7 @@
"typeArguments": null
},
"name": "C2",
"offset": 3292
"offset": 3268
},
{
"configurableType": {
Expand All @@ -34,7 +34,7 @@
"typeArguments": []
},
"name": "C3",
"offset": 3356
"offset": 3332
},
{
"configurableType": {
Expand All @@ -43,7 +43,7 @@
"typeArguments": []
},
"name": "C4",
"offset": 3372
"offset": 3348
},
{
"configurableType": {
Expand All @@ -52,7 +52,7 @@
"typeArguments": []
},
"name": "C5",
"offset": 3388
"offset": 3364
},
{
"configurableType": {
Expand All @@ -61,7 +61,7 @@
"typeArguments": null
},
"name": "C6",
"offset": 3436
"offset": 3412
},
{
"configurableType": {
Expand All @@ -70,7 +70,7 @@
"typeArguments": null
},
"name": "C7",
"offset": 3460
"offset": 3436
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ script;
use basic_storage_abi::{BasicStorage, Quad};

fn main() -> u64 {
let addr = abi(BasicStorage, 0x1664598d277f39504d86fd45604e20eb3547bd4cea6b22f17854fbd6f690f53e);
let addr = abi(BasicStorage, 0xd0897bfc4fd711f847986c1a22444c23da58a9458133c5e99519afab57ccd242);
let key = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
let value = 4242;

Expand Down

0 comments on commit fb2503b

Please sign in to comment.