-
Notifications
You must be signed in to change notification settings - Fork 6
/
ir_construct.rs
480 lines (424 loc) · 15.2 KB
/
ir_construct.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use common::{ConstantOp, ReturnOp};
use expect_test::{expect, Expect};
use pliron::derive::def_op;
use pliron::{
basic_block::BasicBlock,
builtin::{
op_interfaces::OneResultInterface,
types::{IntegerType, Signedness},
},
common_traits::Verify,
context::Context,
debug_info::set_operation_result_name,
graph::walkers::{
self,
interruptible::{self, walk_advance, walk_break},
IRNode, WALKCONFIG_POSTORDER_FORWARD, WALKCONFIG_POSTORDER_REVERSE,
WALKCONFIG_PREORDER_FORWARD,
},
impl_canonical_syntax, impl_verify_succ,
irfmt::parsers::spaced,
location,
op::Op,
operation::Operation,
parsable::{self, state_stream_from_iterator, Parsable},
printable::Printable,
result::Result,
};
use crate::common::{const_ret_in_mod, setup_context_dialects};
use combine::parser::Parser;
mod common;
// Test erasing the entire top module.
#[test]
fn construct_and_erase() -> Result<()> {
let ctx = &mut setup_context_dialects();
let module_op = const_ret_in_mod(ctx)?.0.get_operation();
Operation::erase(module_op, ctx);
assert!(ctx.operations.is_empty() && ctx.basic_blocks.is_empty() && ctx.regions.is_empty());
Ok(())
}
// Ensure that erasing an op with uses panics.
#[test]
#[should_panic(expected = "Operation with use(s) being erased")]
fn removed_used_op() {
let ctx = &mut setup_context_dialects();
// const_ret_in_mod builds a module with a function.
let (_, _, const_op, _) = const_ret_in_mod(ctx).unwrap();
// const_op is used in the return. Erasing it must panic.
Operation::erase(const_op.get_operation(), ctx);
}
// Testing replacing all uses of c0 with c1.
#[test]
fn replace_c0_with_c1() -> Result<()> {
let ctx = &mut setup_context_dialects();
// const_ret_in_mod builds a module with a function.
let (module_op, _, const_op, _) = const_ret_in_mod(ctx).unwrap();
let const1_op = ConstantOp::new(ctx, 1);
const1_op
.get_operation()
.insert_after(ctx, const_op.get_operation());
set_operation_result_name(ctx, const1_op.get_operation(), 0, "c1".try_into().unwrap());
let const0_val = const_op.get_result(ctx);
const0_val.replace_some_uses_with(ctx, |_, _| true, &const1_op.get_result(ctx));
Operation::erase(const_op.get_operation(), ctx);
module_op.get_operation().verify(ctx)?;
Ok(())
}
// Replace ret_op's first operand (which is c0) with c1.
// Erase c0. Verify.
#[test]
fn replace_c0_with_c1_operand() -> Result<()> {
let ctx = &mut setup_context_dialects();
// const_ret_in_mod builds a module with a function.
let (module_op, _, const_op, ret_op) = const_ret_in_mod(ctx).unwrap();
let const1_op = ConstantOp::new(ctx, 1);
const1_op
.get_operation()
.insert_after(ctx, const_op.get_operation());
set_operation_result_name(ctx, const1_op.get_operation(), 0, "c1".try_into().unwrap());
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
c1_op_5v1_res0 = test.constant builtin.integer <0x1: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
}"#]]
.assert_eq(&printed);
Operation::replace_operand(ret_op.get_operation(), ctx, 0, const1_op.get_result(ctx));
Operation::erase(const_op.get_operation(), ctx);
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c1_op_5v1_res0 = test.constant builtin.integer <0x1: builtin.int<si64>>;
test.return c1_op_5v1_res0
}
}"#]]
.assert_eq(&printed);
module_op.get_operation().verify(ctx)?;
Ok(())
}
#[def_op("test.dual_def")]
struct DualDefOp {}
impl_verify_succ!(DualDefOp);
impl_canonical_syntax!(DualDefOp);
/// If an Op has multiple results, or a block multiple args,
/// replacing all uses of one with the other should work.
/// (since our RefCell is at the Op or block level, we shouldn't
/// end up with a multiple borrow panic).
#[test]
fn test_replace_within_same_def_site() {
let ctx = &mut setup_context_dialects();
DualDefOp::register(ctx, DualDefOp::parser_fn);
let u64_ty = IntegerType::get(ctx, 64, Signedness::Signed).into();
let dual_def_op = Operation::new(
ctx,
DualDefOp::get_opid_static(),
vec![u64_ty, u64_ty],
vec![],
vec![],
0,
);
let (res1, res2) = (
dual_def_op.deref(ctx).get_result(0),
dual_def_op.deref(ctx).get_result(1),
);
let (module_op, func_op, const_op, ret_op) = const_ret_in_mod(ctx).unwrap();
dual_def_op.insert_before(ctx, ret_op.get_operation());
const_op
.get_result(ctx)
.replace_some_uses_with(ctx, |_, _| true, &res1);
res1.replace_some_uses_with(ctx, |_, _| true, &res2);
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_4v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
op_1v1_res0, op_1v1_res1 = test.dual_def () [] []: <() -> (builtin.int<si64>, builtin.int<si64>)>;
test.return op_1v1_res1
}
}"#]]
.assert_eq(&printed);
let dual_arg_block = BasicBlock::new(ctx, None, vec![u64_ty, u64_ty]);
let (arg1, arg2) = (
dual_arg_block.deref(ctx).get_argument(0),
dual_arg_block.deref(ctx).get_argument(1),
);
dual_arg_block.insert_after(ctx, func_op.get_entry_block(ctx));
let ret_op = ReturnOp::new(ctx, arg1);
ret_op.get_operation().insert_at_back(dual_arg_block, ctx);
arg1.replace_some_uses_with(ctx, |_, _| true, &arg2);
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_4v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
op_1v1_res0, op_1v1_res1 = test.dual_def () [] []: <() -> (builtin.int<si64>, builtin.int<si64>)>;
test.return op_1v1_res1
^block_3v1(block_3v1_arg0:builtin.int<si64>,block_3v1_arg1:builtin.int<si64>):
test.return block_3v1_arg1
}
}"#]]
.assert_eq(&printed);
}
#[test]
/// A test to just print a constructed IR to stdout.
fn print_simple() -> Result<()> {
let ctx = &mut setup_context_dialects();
let module_op = const_ret_in_mod(ctx)?.0.get_operation();
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
}"#]]
.assert_eq(&printed);
println!("{}", printed);
Ok(())
}
#[test]
fn parse_simple() -> Result<()> {
let input = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.int <si64>)> {
^entry_block_1_0():
c0_op_2_0_res0 = test.constant builtin.integer <0x0: builtin.int <si64>>;
test.return c0_op_2_0_res0
^exit(a : builtin.int <si32>):
}
}"#;
let ctx = &mut setup_context_dialects();
let op = {
let state_stream = state_stream_from_iterator(
input.chars(),
parsable::State::new(ctx, location::Source::InMemory),
);
spaced(Operation::parser(())).parse(state_stream).unwrap().0
};
println!("{}", op.disp(ctx));
Ok(())
}
fn expect_parse_error(input: &str, expected_err: Expect) {
let ctx = &mut setup_context_dialects();
let state_stream = state_stream_from_iterator(
input.chars(),
parsable::State::new(ctx, location::Source::InMemory),
);
let actual_err = spaced(Operation::parser(()))
.parse(state_stream)
.err()
.unwrap();
expected_err.assert_eq(&actual_err.to_string());
}
#[test]
fn parse_err_multiple_def() {
let input_multiple_ssa_defs = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.int <si64>)> {
^entry_block_1_0():
c0_op_2_0_res0 = test.constant builtin.integer <0x0: builtin.int <si64>>;
c0_op_2_0_res0 = test.constant builtin.integer <0x0: builtin.int <si64>>;
test.return c0_op_2_0_res0
^exit():
}
}"#;
let expected_err = expect![[r#"
Parse error at line: 7, column: 17
Identifier c0_op_2_0_res0 defined more than once in the scope
"#]];
expect_parse_error(input_multiple_ssa_defs, expected_err);
let input_multiple_label_defs = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.int <si64>)> {
^entry_block_1_0():
c0_op_2_0_res0 = test.constant builtin.integer <0x0: builtin.int <si64>>;
test.return c0_op_2_0_res0
^entry_block_1_0():
}
}"#;
let expected_err = expect![[r#"
Parse error at line: 8, column: 13
Identifier entry_block_1_0 defined more than once in the scope
"#]];
expect_parse_error(input_multiple_label_defs, expected_err);
}
#[test]
fn parse_err_unresolved_def() {
let input_multiple_defs = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.int <si64>)> {
^entry_block_1_0():
test.return c0_op_2_0_res0
}
}"#;
let expected_err = expect![[r#"
Parse error at line: 4, column: 78
Identifier c0_op_2_0_res0 was not resolved to any definition in the scope
"#]];
expect_parse_error(input_multiple_defs, expected_err);
}
#[test]
fn parse_err_block_label_colon() {
let input_label_colon_missing = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.int <si64>)> {
^entry_block_1_0():
c0_op_2_0_res0 = test.constant builtin.integer <0x0: builtin.int <si64>>;
test.return c0_op_2_0_res0
^exit()
}
}"#;
let expected_err = expect![[r#"
Parse error at line: 9, column: 13
Unexpected `}`
Expected whitespace or `:`
"#]];
expect_parse_error(input_label_colon_missing, expected_err);
}
#[test]
fn parse_err_block_args() {
let input_label_colon_missing = r#"
builtin.module @bar {
^block_0_0(a : builtin.int <si32>, b):
}"#;
let expected_err = expect![[r#"
Parse error at line: 3, column: 45
Unexpected `)`
Expected whitespaces or `:`
"#]];
expect_parse_error(input_label_colon_missing, expected_err);
}
#[test]
fn test_preorder_forward_walk() {
let ctx = &mut setup_context_dialects();
let module_op = const_ret_in_mod(ctx).unwrap().0.get_operation();
let mut state = Vec::new();
walkers::walk_op(
ctx,
&mut state,
&WALKCONFIG_PREORDER_FORWARD,
module_op,
|_ctx, state, node| {
if let IRNode::Operation(op) = node {
state.push(op);
}
},
);
let ops = state.into_iter().fold("".to_string(), |accum, op| {
accum + &op.disp(ctx).to_string() + "\n"
});
expect![[r#"
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
}
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>
test.return c0_op_3v1_res0
"#]]
.assert_eq(&ops);
}
#[test]
fn test_postorder_forward_walk() {
let ctx = &mut setup_context_dialects();
let module_op = const_ret_in_mod(ctx).unwrap().0.get_operation();
let mut state = Vec::new();
walkers::walk_op(
ctx,
&mut state,
&WALKCONFIG_POSTORDER_FORWARD,
module_op,
|_ctx, state, node| {
if let IRNode::Operation(op) = node {
state.push(op);
}
},
);
let ops = state.into_iter().fold("".to_string(), |accum, op| {
accum + &op.disp(ctx).to_string() + "\n"
});
expect![[r#"
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>
test.return c0_op_3v1_res0
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
builtin.module @bar {
^block_1v1():
builtin.func @foo: builtin.function<() -> (builtin.int<si64>)> {
^entry_block_2v1():
c0_op_3v1_res0 = test.constant builtin.integer <0x0: builtin.int<si64>>;
test.return c0_op_3v1_res0
}
}
"#]]
.assert_eq(&ops);
}
#[test]
fn test_walker_find_op() {
let ctx = &mut setup_context_dialects();
let (module_op, _, const_op, _) = const_ret_in_mod(ctx).unwrap();
let const1_op = ConstantOp::new(ctx, 1);
const1_op
.get_operation()
.insert_after(ctx, const_op.get_operation());
set_operation_result_name(ctx, const1_op.get_operation(), 0, "c1".try_into().unwrap());
// A function to breaks the walk when a [ConstantOp] is found.
fn finder(
ctx: &mut Context,
_: &mut (),
node: IRNode,
) -> interruptible::WalkResult<ConstantOp> {
if let IRNode::Operation(op) = node {
if let Some(const_op) = Operation::get_op(op, ctx).downcast_ref::<ConstantOp>() {
return walk_break(*const_op);
}
}
walk_advance()
}
let res1 = walkers::interruptible::walk_op(
ctx,
&mut (),
&WALKCONFIG_PREORDER_FORWARD,
module_op.get_operation(),
finder,
);
assert!(matches!(res1, interruptible::WalkResult::Break(c) if c == const_op));
let res2 = walkers::interruptible::walk_op(
ctx,
&mut (),
&WALKCONFIG_POSTORDER_REVERSE,
module_op.get_operation(),
finder,
);
assert!(matches!(res2, interruptible::WalkResult::Break(c) if c == const1_op));
}