Skip to content

Commit 0682209

Browse files
authored
Deterministic printing of IR values (#7490)
Closes #4517 <!-- CURSOR_SUMMARY --> --- > [!NOTE] > IR printer now generates deterministic value IDs; tests and IR harness updated to match new naming and ensure stable round-trips. > > - **IR Printer**: > - Deterministic value naming via stable keys (`v{:?}` from `value.0.data()`); added `slotmap::Key` import. > - **Tests/Fixtures**: > - Switch FileCheck regex to `VAL=v\d+v\d+` and update expectations across IR tests/snapshots to new value IDs. > - Tweak const-folding tests to expect `const ...` lines without temporary names. > - **IR Test Harness**: > - Update default regex definitions; verify determinism by parsing/printing twice and comparing outputs. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit cfad08d. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 54e0169 commit 0682209

File tree

64 files changed

+1959
-1918
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1959
-1918
lines changed

sway-ir/src/optimize/constants.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ mod tests {
366366
use crate::{optimize::tests::*, CONST_FOLDING_NAME};
367367

368368
fn assert_operator(t: &str, opcode: &str, l: &str, r: Option<&str>, result: Option<&str>) {
369-
let expected = result.map(|result| format!("v0 = const {t} {result}"));
369+
let expected = result.map(|result| format!("const {t} {result}"));
370370
let expected = expected.as_ref().map(|x| vec![x.as_str()]);
371371
let body = format!(
372372
"
@@ -495,9 +495,9 @@ mod tests {
495495
ret u64 result6, !0
496496
}",
497497
Some([
498-
"v0 = get_local __ptr u64, LOCAL",
499-
"v1 = load v0",
500-
"ret u64 v1",
498+
"v3v1 = get_local __ptr u64, LOCAL",
499+
"v4v1 = load v3v1",
500+
"ret u64 v4v1",
501501
]),
502502
);
503503
}

sway-ir/src/printer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::collections::{BTreeMap, HashMap};
88

9+
use slotmap::Key;
910
use sway_types::SourceEngine;
1011

1112
use crate::{
@@ -1401,7 +1402,7 @@ impl Namer {
14011402

14021403
fn default_name(&mut self, value: &Value) -> String {
14031404
self.names.get(value).cloned().unwrap_or_else(|| {
1404-
let new_name = format!("v{}", self.next_value_idx);
1405+
let new_name = format!("v{:?}", value.0.data());
14051406
self.next_value_idx += 1;
14061407
self.names.insert(*value, new_name.clone());
14071408
new_name
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// regex: ID=[[:alpha:]_0-9]+
2+
13
script {
24
fn main() -> bool {
35
entry():
46
v0 = const u256 0x0000000000000000000000000000000000000000000000000000000000000000
57
v1 = const u256 0x0000000000000000000000000000000000000000000000000000000000000001
68

79
v10 = cmp eq v0 v0
8-
//check: v0 = const bool true
10+
//check: $ID = const bool true
911
ret bool v10
1012
}
1113
}

sway-ir/tests/constants/u256_ops.ir

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// regex: ID=[[:alpha:]_0-9]+
2+
13
script {
24
fn main() -> u256 {
35
entry():
@@ -22,7 +24,10 @@ script {
2224
v18 = not v17
2325
v19 = not v18
2426
v20 = xor v19, v6
25-
//check: v0 = const u256 0x0000000000000000000000000000000000000000000000000000000000000002
27+
// check: entry
28+
// nextln: $ID = const u256 0x0000000000000000000000000000000000000000000000000000000000000002
29+
// check-not: const
30+
// check: ret
2631
ret u256 v20
2732
}
2833
}

sway-ir/tests/cse/cse1.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// regex: ID=[[:alpha:]0-9]+
2-
// regex: VAR=v\d+
2+
// regex: VAR=v\d+v\d+
33

44
script {
55
fn main() -> bool {

sway-ir/tests/cse/cse2.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// regex: ID=[[:alpha:]0-9]+
2-
// regex: VAR=v\d+
2+
// regex: VAR=v\d+v\d+
33

44
script {
55
fn main() -> bool {

sway-ir/tests/cse/cse3.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// regex: ID=[[:alpha:]0-9]+
2-
// regex: VAR=v\d+
2+
// regex: VAR=v\d+v\d+
33

44
script {
55
entry fn main(a: u64, b: u64) -> () {

sway-ir/tests/dce/copy_prop_1.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ script {
1616
}
1717
}
1818

19-
// regex: VAL=v\d+
19+
// regex: VAL=v\d+v\d+
2020

2121
// not: mem_copy_val $VAL, $VAL

sway-ir/tests/dce/copy_prop_2.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ script {
1616
}
1717
}
1818

19-
// regex: VAL=v\d+
19+
// regex: VAL=v\d+v\d+
2020

2121
// not: mem_copy_val $VAL, $VAL

sway-ir/tests/dce/copy_prop_3.ir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ script {
1717
}
1818
}
1919

20-
// regex: VAL=v\d+
20+
// regex: VAL=v\d+v\d+
2121

2222
// not: mem_copy_val $VAL, $VAL

0 commit comments

Comments
 (0)