forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.rs
262 lines (226 loc) · 9.5 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use std::path::PathBuf;
use sway_ir::{
create_arg_demotion_pass, create_const_combine_pass, create_const_demotion_pass,
create_dce_pass, create_dom_fronts_pass, create_dominators_pass, create_escaped_symbols_pass,
create_mem2reg_pass, create_memcpyopt_pass, create_misc_demotion_pass, create_postorder_pass,
create_ret_demotion_pass, create_simplify_cfg_pass, optimize as opt, Context, PassGroup,
PassManager,
};
use sway_types::SourceEngine;
// -------------------------------------------------------------------------------------------------
// Utility for finding test files and running FileCheck. See actual pass invocations below.
fn run_tests<F: Fn(&str, &mut Context) -> bool>(sub_dir: &str, opt_fn: F) {
let source_engine = SourceEngine::default();
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let dir: PathBuf = format!("{manifest_dir}/tests/{sub_dir}").into();
for entry in std::fs::read_dir(dir).unwrap() {
let path = entry.unwrap().path();
let input_bytes = std::fs::read(&path).unwrap();
let input = String::from_utf8_lossy(&input_bytes);
let mut ir = sway_ir::parser::parse(&input, &source_engine).unwrap_or_else(|parse_err| {
println!("{}: {parse_err}", path.display());
panic!()
});
let first_line = input.split('\n').next().unwrap();
// The tests should return true, indicating they made modifications.
assert!(
opt_fn(first_line, &mut ir),
"Pass returned false (no changes made to {}).",
path.display()
);
let ir = ir.verify().unwrap_or_else(|err| {
println!("{err}");
panic!();
});
let output = sway_ir::printer::to_string(&ir);
let chkr = filecheck::CheckerBuilder::new()
.text(&input)
.unwrap()
.finish();
if chkr.is_empty() {
println!("{output}");
panic!("No filecheck directives found in test: {}", path.display());
}
match chkr.explain(&output, filecheck::NO_VARIABLES) {
Ok((success, report)) if !success => {
println!("--- FILECHECK FAILED FOR {}", path.display());
println!("{report}");
panic!()
}
Err(e) => {
panic!("filecheck directive error while checking: {e}");
}
_ => (),
}
}
}
// -------------------------------------------------------------------------------------------------
#[test]
fn inline() {
run_tests("inline", |first_line, ir: &mut Context| {
let mut words = first_line.split(' ').collect::<Vec<_>>();
let params = if words.is_empty() || words.remove(0) != "//" {
Vec::new()
} else {
words
};
let funcs = ir
.module_iter()
.flat_map(|module| module.function_iter(ir))
.collect::<Vec<_>>();
if params.iter().any(|&p| p == "all") {
// Just inline everything, replacing all CALL instructions.
funcs.into_iter().fold(false, |acc, func| {
opt::inline_all_function_calls(ir, &func).unwrap() || acc
})
} else {
// Get the parameters from the first line. See the inline/README.md for details. If
// there aren't any found then there won't be any constraints and it'll be the
// equivalent of asking to inline everything.
let (max_blocks, max_instrs, max_stack) =
params
.windows(2)
.fold(
(None, None, None),
|acc @ (b, i, s), param_and_arg| match param_and_arg[0] {
"blocks" => (param_and_arg[1].parse().ok(), i, s),
"instrs" => (b, param_and_arg[1].parse().ok(), s),
"stack" => (b, i, param_and_arg[1].parse().ok()),
_ => acc,
},
);
funcs.into_iter().fold(false, |acc, func| {
opt::inline_some_function_calls(
ir,
&func,
opt::is_small_fn(max_blocks, max_instrs, max_stack),
)
.unwrap()
|| acc
})
}
})
}
// -------------------------------------------------------------------------------------------------
// Clippy suggests using the map iterator below directly instead of collecting from it first, but
// if we try that then we have borrowing issues with `ir` which is used within the closure.
#[allow(clippy::needless_collect)]
#[test]
fn constants() {
run_tests("constants", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_const_combine_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn simplify_cfg() {
run_tests("simplify_cfg", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_simplify_cfg_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn dce() {
run_tests("dce", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
pass_mgr.register(create_escaped_symbols_pass());
let pass = pass_mgr.register(create_dce_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn mem2reg() {
run_tests("mem2reg", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
pass_mgr.register(create_postorder_pass());
pass_mgr.register(create_dominators_pass());
pass_mgr.register(create_dom_fronts_pass());
let pass = pass_mgr.register(create_mem2reg_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn demote_arg() {
run_tests("demote_arg", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_arg_demotion_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn demote_const() {
run_tests("demote_const", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_const_demotion_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn demote_ret() {
run_tests("demote_ret", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_ret_demotion_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn demote_misc() {
run_tests("demote_misc", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
let pass = pass_mgr.register(create_misc_demotion_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[allow(clippy::needless_collect)]
#[test]
fn memcpyopt() {
run_tests("memcpyopt", |_first_line, ir: &mut Context| {
let mut pass_mgr = PassManager::default();
let mut pass_group = PassGroup::default();
pass_mgr.register(create_escaped_symbols_pass());
let pass = pass_mgr.register(create_memcpyopt_pass());
pass_group.append_pass(pass);
pass_mgr.run(ir, &pass_group).unwrap()
})
}
// -------------------------------------------------------------------------------------------------
#[test]
fn serialize() {
// This isn't running a pass, it's just confirming that the IR can be loaded and printed, and
// FileCheck can just confirm certain instructions came out OK.
run_tests("serialize", |_, _: &mut Context| true)
}
// -------------------------------------------------------------------------------------------------