Skip to content

Commit

Permalink
Switch the IR unit tests to use FileCheck. (FuelLabs#1419)
Browse files Browse the repository at this point in the history
This is both simpler and a more expressive way to test IR passes.  It
uses special embedded directives to confirm that an IR transformation
has been applied correctly.
  • Loading branch information
otrho authored Apr 29, 2022
1 parent 765df4d commit 77f6146
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 362 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions sway-ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ description = "Sway intermediate representation."
name = "opt"

[dependencies]
filecheck = "0.5"
generational-arena = "0.2"
peg = "0.7"
pest = { version = "3.0.4", package = "fuel-pest" }
prettydiff = "0.5"
sway-types = { version = "0.11.0", path = "../sway-types" }
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
// regex: VAR=v\d+

script {
// check: fn main
fn main() -> u64 {
local ptr u64 a
local ptr b256 arg_for_get_b256
local mut ptr { u64, b256 } args_struct_for_get_s
local ptr b256 b
local ptr { u64, b256 } s

// * The struct that we build for the args to a contract call should be const, except for the
// bitcast.

entry:
v0 = const u64 1111
// check: $(bc=$VAR) = bitcast $VAR to u64
v1 = bitcast v0 to u64
v2 = const { b256, u64, u64 } { b256 undef, u64 undef, u64 undef }
v3 = const b256 0x0c1c50c2bf5ba4bb351b4249a2f5e7d86556fcb4a6ae90465ff6c86126eeb3c0
// not: insert_value $VAR, { b256, u64, u64 }, $VAR, 0
v4 = insert_value v2, { b256, u64, u64 }, v3, 0
v5 = const u64 2559618804
// not: insert_value $VAR, { b256, u64, u64 }, $VAR, 1
v6 = insert_value v4, { b256, u64, u64 }, v5, 1
//check: insert_value $VAR, { b256, u64, u64 }, $bc, 2
v7 = insert_value v6, { b256, u64, u64 }, v1, 2
v8 = const u64 0
v9 = const b256 0x0000000000000000000000000000000000000000000000000000000000000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ script {
v6 = const b256 0x0102030405060708010203040506070801020304050607080102030405060708
v7 = insert_value v5, { b256, { bool, u64 } }, v6, 0
v8 = insert_value v7, { b256, { bool, u64 } }, v4, 1

// * There should be no insert_values remaining, they're all const.
// not: insert_value

v9 = get_ptr ptr { b256, { bool, u64 } } record, ptr { b256, { bool, u64 } }, 0

// * Instead just a populated const struct.
// check: const { b256, { bool, u64 } } { b256 0x0102030405060708010203040506070801020304050607080102030405060708, { bool, u64 } { bool true, u64 76 } }

store v8, ptr v9
v10 = get_ptr ptr { b256, { bool, u64 } } record, ptr { b256, { bool, u64 } }, 0
v11 = extract_value v10, { b256, { bool, u64 } }, 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// x
// }

// regex: VAR=v\d+
// regex: ID=[[:alpha:]0-9_]+

script {
fn a(b: bool) -> u64 {
local ptr u64 x
Expand All @@ -41,15 +44,30 @@ script {
ret u64 v5
}

// 'b' is passed to a() and used for the CBR, so check below that the value we pass to a() is
// inlined and used for CBR and the two blocks in a() too, and then followed by what's after the
// call.

// check: fn main
fn main() -> u64 {
local mut ptr u64 x

entry:
v0 = get_ptr mut ptr u64 x, ptr u64, 0
v1 = const u64 0
store v1, ptr v0

// check: $(arg=$VAR) = const bool true
v2 = const bool true

// not: call
v3 = call a(v2)

// check: cbr $arg, $(t=$ID), $(f=$ID)
// check: $t:
// check: $f:

// check: get_ptr mut ptr u64 x, ptr u64, 0
v4 = get_ptr mut ptr u64 x, ptr u64, 0
store v3, ptr v4
v5 = get_ptr mut ptr u64 x, ptr u64, 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
// not(true) || not(false)
// }

// regex: ID=[[:alpha:]0-9_]+
// regex: LABEL=[[:alpha:]0-9_]+:
// regex: VAR=v\d+

script {
fn not(v: bool) -> bool {
entry:
Expand All @@ -31,16 +35,45 @@ script {
ret bool v2
}

// check: fn main
fn main() -> bool {
entry:
v0 = const bool true
// not: call
v1 = call not(v0)

// * Confirm both the blocks from not() are here, they branch to the same block and that block has a
// phi from each of them.
// check: cbr
// check: $(bl0_lab=$LABEL)
// check: const bool false
// check: br $(bl0_to=$ID)
// check: $(bl1_lab=$LABEL)
// check: const bool true
// check: br $bl0_to
// check: $bl0_to:
// check: phi($bl0_lab $VAR, $bl1_lab $VAR)

// * Match this cbr to not be confused with the inlined cbr below. A more unique marker in not()
// would be better...
// check: cbr
cbr v1, block1, block0

block0:
v2 = phi(entry: v1)
v3 = const bool false
// not: call
v4 = call not(v3)

// * Check same again for this inlined instance. The consts are re-used so can't check them.
// check: cbr
// check: $(bl2_lab=$LABEL)
// check: br $(bl2_to=$ID)
// check: $(bl3_lab=$LABEL)
// check: br $bl2_to
// check: $bl2_to:
// check: phi($bl2_lab $VAR, $bl3_lab $VAR)

br block1

block1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,33 @@
// a(22)
// }

// regex: VAR=v\d+
// regex: LABEL=[[:alpha:]0-9]+:

script {
fn a(b: u64) -> u64 {
entry:
ret u64 b
}


// check: fn main
fn main() -> u64 {
entry:

// check: $(arg0=$VAR) = const u64 11
v0 = const u64 11
// not: call
v1 = call a(v0)
// check: phi($LABEL $arg0)

// check: $(arg1=$VAR) = const u64 22
v2 = const u64 22
// not: call
v3 = call a(v2)
// check: phi($LABEL $arg1)

ret u64 v3
}
}

49 changes: 0 additions & 49 deletions sway-ir/tests/ir_to_ir/constants_contract_calls.out_ir

This file was deleted.

14 changes: 0 additions & 14 deletions sway-ir/tests/ir_to_ir/constants_insert_value.out_ir

This file was deleted.

33 changes: 0 additions & 33 deletions sway-ir/tests/ir_to_ir/constants_storage_load.out_ir

This file was deleted.

35 changes: 0 additions & 35 deletions sway-ir/tests/ir_to_ir/constants_storage_store.in_ir

This file was deleted.

Loading

0 comments on commit 77f6146

Please sign in to comment.