Repository navigation
Expand file tree
/
Copy pathir_construct.rs
More file actions
1723 lines (1495 loc) · 59 KB
/
Copy pathir_construct.rs
File metadata and controls
1723 lines (1495 loc) · 59 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) The pliron contributors
use common::{ConstantOp, ReturnOp, const_ret_in_mod};
use expect_test::{Expect, expect};
use pliron::{
basic_block::{BasicBlock, BasicBlockVerifyErr},
builtin::{
attributes::StringAttr,
op_interfaces::{
IsTerminatorInterface, OneRegionInterface, OneResultInterface,
SingleBlockRegionInterface,
},
types::{IntegerType, Signedness},
},
context::{Context, Ptr},
debug_info::{erase_given_names, get_block_arg_name, get_operation_result_name},
derive::pliron_op,
dict_key,
graph::walkers::{
self, IRNode, WALKCONFIG_POSTORDER_FORWARD, WALKCONFIG_POSTORDER_REVERSE,
WALKCONFIG_PREORDER_FORWARD,
interruptible::{self, walk_advance, walk_break},
},
irfmt::parsers::spaced,
op::{Op, verify_op},
operation::{DefUseVerifyErr, Operation, verify_operation},
parsable::parse_from_str,
printable::Printable,
result::Result,
r#type::TypeHandle,
};
#[cfg(target_family = "wasm")]
use wasm_bindgen_test::*;
mod common;
// Test erasing the entire top module.
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn construct_and_erase() -> Result<()> {
let ctx = &mut Context::new();
let module_op = const_ret_in_mod(ctx)?.0.get_operation();
Operation::erase(module_op, ctx);
assert!(ctx.is_ir_empty());
Ok(())
}
// Ensure that erasing an op with uses panics.
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
#[should_panic]
fn removed_used_op() {
let ctx = &mut Context::new();
// 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]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn replace_c0_with_c1() -> Result<()> {
let ctx = &mut Context::new();
// 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());
const1_op
.get_result(ctx)
.set_name(ctx, Some("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);
verify_op(&module_op, ctx)?;
Ok(())
}
// Replace ret_op's first operand (which is c0) with c1.
// Erase c0. Verify.
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn replace_c0_with_c1_operand() -> Result<()> {
let ctx = &mut Context::new();
// 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());
const1_op
.get_result(ctx)
.set_name(ctx, Some("c1".try_into().unwrap()));
let printed = format!("{}", module_op.get_operation().disp(ctx));
expect![[r#"
builtin.module @bar
{
^block1v1():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)>
{
^entry_block2v1():
c0_v0 = test.constant builtin.integer <0: si64> !0;
c1_v1 = test.constant builtin.integer <1: si64> !1;
test.return c0_v0
}
}
outlined_attributes:
!0 = [builtin_debug_info = builtin.debug_info [c0]]
!1 = [builtin_debug_info = builtin.debug_info [c1]]
"#]]
.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.get_operation().disp(ctx));
expect![[r#"
builtin.module @bar
{
^block1v1():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)>
{
^entry_block2v1():
c1_v1 = test.constant builtin.integer <1: si64> !0;
test.return c1_v1
}
}
outlined_attributes:
!0 = [builtin_debug_info = builtin.debug_info [c1]]
"#]]
.assert_eq(&printed);
verify_op(&module_op, ctx)?;
Ok(())
}
#[pliron_op(name = "test.dual_def", format, verifier = "succ")]
struct 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]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_replace_within_same_def_site() {
let ctx = &mut Context::new();
let u64_ty = IntegerType::get(ctx, 64, Signedness::Signed).into();
let dual_def_op = Operation::new(
ctx,
DualDefOp::get_concrete_op_info(),
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.get_operation().disp(ctx));
expect![[r#"
builtin.module @bar
{
^block1v1():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)>
{
^entry_block2v1():
c0_v2 = test.constant builtin.integer <0: si64> !0;
v0, v1 = test.dual_def () [] []: <() -> (builtin.integer si64, builtin.integer si64)>;
test.return v1
}
}
outlined_attributes:
!0 = [builtin_debug_info = builtin.debug_info [c0]]
"#]]
.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.get_operation().disp(ctx));
expect![[r#"
builtin.module @bar
{
^block1v1():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)>
{
^entry_block2v1():
c0_v2 = test.constant builtin.integer <0: si64> !0;
v0, v1 = test.dual_def () [] []: <() -> (builtin.integer si64, builtin.integer si64)>;
test.return v1
^block3v1(v3: builtin.integer si64, v4: builtin.integer si64):
test.return v4
}
}
outlined_attributes:
!0 = [builtin_debug_info = builtin.debug_info [c0]]
"#]]
.assert_eq(&printed);
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_operand_push_pop_insert_remove() -> Result<()> {
let ctx = &mut Context::new();
let (module_op, _func_op, const0_op, ret_op) = const_ret_in_mod(ctx)?;
let ret_ptr = ret_op.get_operation();
let const1_op = ConstantOp::new(ctx, 1);
const1_op
.get_operation()
.insert_before(ctx, ret_op.get_operation());
let const2_op = ConstantOp::new(ctx, 2);
const2_op
.get_operation()
.insert_before(ctx, ret_op.get_operation());
let c0 = const0_op.get_result(ctx);
let c1 = const1_op.get_result(ctx);
let c2 = const2_op.get_result(ctx);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 1);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c0);
let pushed_idx = Operation::push_operand(ret_ptr, ctx, c1);
assert_eq!(pushed_idx, 1);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 2);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c0);
assert_eq!(ret_ptr.deref(ctx).get_operand(1), c1);
for r#use in c1.uses(ctx) {
assert!(r#use.get_def(ctx) == c1);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 1);
}
for r#use in c0.uses(ctx) {
assert!(r#use.get_def(ctx) == c0);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
verify_op(&module_op, ctx)?;
let popped = Operation::pop_operand(ret_ptr, ctx);
assert_eq!(popped, c1);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 1);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c0);
assert!(c1.uses(ctx).is_empty()); // c1 should have no uses now.
for r#use in c0.uses(ctx) {
assert!(r#use.get_def(ctx) == c0);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
verify_op(&module_op, ctx)?;
Operation::insert_operand(ret_ptr, ctx, 0, c1);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 2);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c1);
assert_eq!(ret_ptr.deref(ctx).get_operand(1), c0);
for r#use in c1.uses(ctx) {
assert!(r#use.get_def(ctx) == c1);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
for r#use in c0.uses(ctx) {
assert!(r#use.get_def(ctx) == c0);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 1);
}
verify_op(&module_op, ctx)?;
Operation::insert_operand(ret_ptr, ctx, 2, c2);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 3);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c1);
assert_eq!(ret_ptr.deref(ctx).get_operand(1), c0);
assert_eq!(ret_ptr.deref(ctx).get_operand(2), c2);
for r#use in c1.uses(ctx) {
assert!(r#use.get_def(ctx) == c1);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
for r#use in c0.uses(ctx) {
assert!(r#use.get_def(ctx) == c0);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 1);
}
for r#use in c2.uses(ctx) {
assert!(r#use.get_def(ctx) == c2);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 2);
}
verify_op(&module_op, ctx)?;
let removed_mid = Operation::remove_operand(ret_ptr, ctx, 1);
assert_eq!(removed_mid, c0);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 2);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c1);
assert_eq!(ret_ptr.deref(ctx).get_operand(1), c2);
for r#use in c1.uses(ctx) {
assert!(r#use.get_def(ctx) == c1);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
for r#use in c2.uses(ctx) {
assert!(r#use.get_def(ctx) == c2);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 1);
}
verify_op(&module_op, ctx)?;
let removed_front = Operation::remove_operand(ret_ptr, ctx, 0);
assert_eq!(removed_front, c1);
assert_eq!(ret_ptr.deref(ctx).get_num_operands(), 1);
assert_eq!(ret_ptr.deref(ctx).get_operand(0), c2);
for r#use in c2.uses(ctx) {
assert!(r#use.get_def(ctx) == c2);
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 0);
}
verify_op(&module_op, ctx)?;
// Add c2 as an operand again,
Operation::insert_operand(ret_ptr, ctx, 0, c2);
assert!(c2.uses(ctx).len() == 2); // c2 should now have two uses.
for r#use in c2.uses(ctx) {
assert!(r#use.get_def(ctx) == c2);
// c2 should now have two uses.
assert!(
r#use.user_op() == ret_ptr
&& (r#use.find_index(ctx) == 0 || r#use.find_index(ctx) == 1)
);
}
// Add c0 now
Operation::insert_operand(ret_ptr, ctx, 1, c0);
assert!(c0.uses(ctx).len() == 1); // c0 should now have 1 use.
assert!(c2.uses(ctx).len() == 2); // c2 should still have two uses.
for r#use in c0.uses(ctx) {
assert!(r#use.get_def(ctx) == c0);
// c0 should now have one use.
assert!(r#use.user_op() == ret_ptr && r#use.find_index(ctx) == 1);
}
for r#use in c2.uses(ctx) {
assert!(r#use.get_def(ctx) == c2);
// c2 should still have two uses.
assert!(
r#use.user_op() == ret_ptr
&& (r#use.find_index(ctx) == 0 || r#use.find_index(ctx) == 2)
);
}
Ok(())
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_result_push_pop_insert_remove() -> Result<()> {
let ctx = &mut Context::new();
let i64_ty: TypeHandle = IntegerType::get(ctx, 64, Signedness::Signed).into();
let i32_ty: TypeHandle = IntegerType::get(ctx, 32, Signedness::Signed).into();
// Create a DualDefOp with two i64 results.
let op = Operation::new(
ctx,
DualDefOp::get_concrete_op_info(),
vec![i64_ty, i64_ty],
vec![],
vec![],
0,
);
let r0 = op.deref(ctx).get_result(0);
let r1 = op.deref(ctx).get_result(1);
r0.set_name(ctx, Some("r0".try_into().unwrap()));
r1.set_name(ctx, Some("r1".try_into().unwrap()));
assert_eq!(op.deref(ctx).get_num_results(), 2);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
assert_eq!(op.deref(ctx).get_type(0), i64_ty);
assert_eq!(op.deref(ctx).get_type(1), i64_ty);
assert_eq!(
get_operation_result_name(ctx, op, 0),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r1".try_into().unwrap())
);
// Push a new i32 result at the end.
let pushed_idx = Operation::push_result(op, ctx, i32_ty);
assert_eq!(pushed_idx, 2);
assert_eq!(op.deref(ctx).get_num_results(), 3);
// r0 and r1 still have the same indices.
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
let r2 = op.deref(ctx).get_result(2);
assert_eq!(r2.find_index(ctx), 2);
assert_eq!(op.deref(ctx).get_type(2), i32_ty);
assert_eq!(get_operation_result_name(ctx, op, 2), None);
// Pop the last result (r2 has no uses).
Operation::pop_result(op, ctx);
assert_eq!(op.deref(ctx).get_num_results(), 2);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
assert_eq!(
get_operation_result_name(ctx, op, 0),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r1".try_into().unwrap())
);
// Insert an i32 result at index 0, shifting r0 -> 1 and r1 -> 2.
Operation::insert_result(op, ctx, 0, i32_ty);
assert_eq!(op.deref(ctx).get_num_results(), 3);
assert_eq!(op.deref(ctx).get_type(0), i32_ty);
assert_eq!(r0.find_index(ctx), 1);
assert_eq!(r1.find_index(ctx), 2);
assert_eq!(get_operation_result_name(ctx, op, 0), None);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 2),
Some("r1".try_into().unwrap())
);
// Remove index 0 (the freshly inserted i32, which has no uses).
Operation::remove_result(op, ctx, 0);
assert_eq!(op.deref(ctx).get_num_results(), 2);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
assert_eq!(
get_operation_result_name(ctx, op, 0),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r1".try_into().unwrap())
);
// Insert an i32 result at index 1 (between r0 and r1), shifting r1 -> 2.
Operation::insert_result(op, ctx, 1, i32_ty);
assert_eq!(op.deref(ctx).get_num_results(), 3);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(op.deref(ctx).get_type(1), i32_ty);
assert_eq!(r1.find_index(ctx), 2);
// Remove index 1 (no uses), shifting r1 back to 1.
Operation::remove_result(op, ctx, 1);
assert_eq!(op.deref(ctx).get_num_results(), 2);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
// Insert at the end (index 2).
Operation::insert_result(op, ctx, 2, i32_ty);
assert_eq!(op.deref(ctx).get_num_results(), 3);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
assert_eq!(op.deref(ctx).get_type(2), i32_ty);
assert_eq!(
get_operation_result_name(ctx, op, 0),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r1".try_into().unwrap())
);
assert_eq!(get_operation_result_name(ctx, op, 2), None);
// Remove from the end.
Operation::remove_result(op, ctx, 2);
assert_eq!(op.deref(ctx).get_num_results(), 2);
assert_eq!(r0.find_index(ctx), 0);
assert_eq!(r1.find_index(ctx), 1);
assert_eq!(
get_operation_result_name(ctx, op, 0),
Some("r0".try_into().unwrap())
);
assert_eq!(
get_operation_result_name(ctx, op, 1),
Some("r1".try_into().unwrap())
);
Ok(())
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_block_arg_push_pop_insert_remove() -> Result<()> {
let ctx = &mut Context::new();
let i64_ty: TypeHandle = IntegerType::get(ctx, 64, Signedness::Signed).into();
let i32_ty: TypeHandle = IntegerType::get(ctx, 32, Signedness::Signed).into();
// Create a block with two i64 arguments.
let block = BasicBlock::new(ctx, None, vec![i64_ty, i64_ty]);
let a0 = block.deref(ctx).get_argument(0);
let a1 = block.deref(ctx).get_argument(1);
a0.set_name(ctx, Some("a0".try_into().unwrap()));
a1.set_name(ctx, Some("a1".try_into().unwrap()));
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
assert_eq!(
get_block_arg_name(ctx, block, 0),
Some("a0".try_into().unwrap())
);
assert_eq!(
get_block_arg_name(ctx, block, 1),
Some("a1".try_into().unwrap())
);
// Push a new i32 argument at the end.
let pushed_idx = BasicBlock::push_argument(block, ctx, i32_ty);
assert_eq!(pushed_idx, 2);
assert_eq!(block.deref(ctx).get_num_arguments(), 3);
// a0 and a1 retain their indices.
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
let a2 = block.deref(ctx).get_argument(2);
assert_eq!(a2.find_index(ctx), 2);
assert_eq!(get_block_arg_name(ctx, block, 2), None);
// Pop the last argument (a2 has no uses).
BasicBlock::pop_argument(block, ctx);
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
// Insert an i32 argument at index 0, shifting a0 -> 1 and a1 -> 2.
BasicBlock::insert_argument(block, ctx, 0, i32_ty);
assert_eq!(block.deref(ctx).get_num_arguments(), 3);
assert_eq!(a0.find_index(ctx), 1);
assert_eq!(a1.find_index(ctx), 2);
assert_eq!(get_block_arg_name(ctx, block, 0), None);
assert_eq!(
get_block_arg_name(ctx, block, 1),
Some("a0".try_into().unwrap())
);
assert_eq!(
get_block_arg_name(ctx, block, 2),
Some("a1".try_into().unwrap())
);
// Remove index 0 (no uses), restoring a0 -> 0 and a1 -> 1.
BasicBlock::remove_argument(block, ctx, 0);
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
assert_eq!(
get_block_arg_name(ctx, block, 0),
Some("a0".try_into().unwrap())
);
assert_eq!(
get_block_arg_name(ctx, block, 1),
Some("a1".try_into().unwrap())
);
// Insert an i32 argument at index 1 (between a0 and a1), shifting a1 -> 2.
BasicBlock::insert_argument(block, ctx, 1, i32_ty);
assert_eq!(block.deref(ctx).get_num_arguments(), 3);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 2);
// Remove index 1 (no uses), shifting a1 back to 1.
BasicBlock::remove_argument(block, ctx, 1);
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
// Insert at the end (index 2).
BasicBlock::insert_argument(block, ctx, 2, i32_ty);
assert_eq!(block.deref(ctx).get_num_arguments(), 3);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
assert_eq!(
get_block_arg_name(ctx, block, 0),
Some("a0".try_into().unwrap())
);
assert_eq!(
get_block_arg_name(ctx, block, 1),
Some("a1".try_into().unwrap())
);
assert_eq!(get_block_arg_name(ctx, block, 2), None);
// Remove from the end.
BasicBlock::remove_argument(block, ctx, 2);
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(a0.find_index(ctx), 0);
assert_eq!(a1.find_index(ctx), 1);
assert_eq!(
get_block_arg_name(ctx, block, 0),
Some("a0".try_into().unwrap())
);
assert_eq!(
get_block_arg_name(ctx, block, 1),
Some("a1".try_into().unwrap())
);
Ok(())
}
/// Tests that `Value` objects held as operands correctly track their
/// index within the defining operation's result list when results are
/// inserted, removed, pushed, or popped ahead of / behind them.
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_result_index_tracking_with_uses() -> Result<()> {
let ctx = &mut Context::new();
let i64_ty: TypeHandle = IntegerType::get(ctx, 64, Signedness::Signed).into();
let i32_ty: TypeHandle = IntegerType::get(ctx, 32, Signedness::Signed).into();
// Create a DualDefOp with two i64 results: r0 at index 0, r1 at index 1.
let dual_op = Operation::new(
ctx,
DualDefOp::get_concrete_op_info(),
vec![i64_ty, i64_ty],
vec![],
vec![],
0,
);
let r0 = dual_op.deref(ctx).get_result(0);
let r1 = dual_op.deref(ctx).get_result(1);
// Build a user op that holds r0 as its first operand, r1 as its second.
let user_op = ReturnOp::new(ctx, r0).get_operation();
Operation::push_operand(user_op, ctx, r1);
// Sanity: operands are r0 and r1; their result indices are 0 and 1.
assert_eq!(user_op.deref(ctx).get_operand(0), r0);
assert_eq!(user_op.deref(ctx).get_operand(1), r1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Insert a new i32 result at index 0 of dual_op, shifting r0 -> 1 and r1 -> 2.
Operation::insert_result(dual_op, ctx, 0, i32_ty);
assert_eq!(dual_op.deref(ctx).get_num_results(), 3);
assert_eq!(user_op.deref(ctx).get_operand(0), r0);
assert_eq!(user_op.deref(ctx).get_operand(1), r1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 1);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 2);
// Remove the new result at index 0, restoring r0 -> 0 and r1 -> 1.
Operation::remove_result(dual_op, ctx, 0);
assert_eq!(dual_op.deref(ctx).get_num_results(), 2);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Insert a new result between r0 and r1 (at index 1), shifting r1 -> 2.
Operation::insert_result(dual_op, ctx, 1, i32_ty);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 2);
// Remove at index 1, restoring r1 -> 1.
Operation::remove_result(dual_op, ctx, 1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Push a result at the end (after r1); r0 and r1 indices are unchanged.
let pushed_idx = Operation::push_result(dual_op, ctx, i32_ty);
assert_eq!(pushed_idx, 2);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Pop the trailing result; indices still unchanged.
Operation::pop_result(dual_op, ctx);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
Ok(())
}
/// Tests that `Value` objects held as operands correctly track their
/// index within the defining block's argument list when arguments are
/// inserted, removed, pushed, or popped ahead of / behind them.
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_block_arg_index_tracking_with_uses() -> Result<()> {
let ctx = &mut Context::new();
let i64_ty: TypeHandle = IntegerType::get(ctx, 64, Signedness::Signed).into();
let i32_ty: TypeHandle = IntegerType::get(ctx, 32, Signedness::Signed).into();
// Create a block with two i64 arguments: a0 at index 0, a1 at index 1.
let block = BasicBlock::new(ctx, None, vec![i64_ty, i64_ty]);
let a0 = block.deref(ctx).get_argument(0);
let a1 = block.deref(ctx).get_argument(1);
// Build a user op that holds a0 as its first operand, a1 as its second.
let user_op = ReturnOp::new(ctx, a0).get_operation();
Operation::push_operand(user_op, ctx, a1);
// Sanity: operands are a0 and a1; their argument indices are 0 and 1.
assert_eq!(user_op.deref(ctx).get_operand(0), a0);
assert_eq!(user_op.deref(ctx).get_operand(1), a1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Insert a new i32 argument at index 0, shifting a0 -> 1 and a1 -> 2.
BasicBlock::insert_argument(block, ctx, 0, i32_ty);
assert_eq!(block.deref(ctx).get_num_arguments(), 3);
assert_eq!(user_op.deref(ctx).get_operand(0), a0);
assert_eq!(user_op.deref(ctx).get_operand(1), a1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 1);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 2);
// Remove the new argument at index 0, restoring a0 -> 0 and a1 -> 1.
BasicBlock::remove_argument(block, ctx, 0);
assert_eq!(block.deref(ctx).get_num_arguments(), 2);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Insert a new argument between a0 and a1 (at index 1), shifting a1 -> 2.
BasicBlock::insert_argument(block, ctx, 1, i32_ty);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 2);
// Remove at index 1, restoring a1 -> 1.
BasicBlock::remove_argument(block, ctx, 1);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Push an argument at the end (after a1); a0 and a1 indices are unchanged.
let pushed_idx = BasicBlock::push_argument(block, ctx, i32_ty);
assert_eq!(pushed_idx, 2);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
// Pop the trailing argument; indices still unchanged.
BasicBlock::pop_argument(block, ctx);
assert_eq!(user_op.deref(ctx).get_operand(0).find_index(ctx), 0);
assert_eq!(user_op.deref(ctx).get_operand(1).find_index(ctx), 1);
Ok(())
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn test_successor_push_pop_insert_remove() -> Result<()> {
let ctx = &mut Context::new();
let (module_op, func_op, const_op, ret_op) = const_ret_in_mod(ctx)?;
let entry_block = func_op.get_entry_block(ctx);
let common_pred = BasicBlock::new(ctx, None, vec![]);
common_pred.insert_after(ctx, entry_block);
let succ0 = BasicBlock::new(ctx, None, vec![]);
succ0.insert_after(ctx, common_pred);
let succ1 = BasicBlock::new(ctx, None, vec![]);
succ1.insert_after(ctx, succ0);
let succ2 = BasicBlock::new(ctx, None, vec![]);
succ2.insert_after(ctx, succ1);
let succ0_ret = ReturnOp::new(ctx, const_op.get_result(ctx));
succ0_ret.get_operation().insert_at_back(succ0, ctx);
let succ1_ret = ReturnOp::new(ctx, const_op.get_result(ctx));
succ1_ret.get_operation().insert_at_back(succ1, ctx);
let succ2_ret = ReturnOp::new(ctx, const_op.get_result(ctx));
succ2_ret.get_operation().insert_at_back(succ2, ctx);
let branch_like = Operation::new(
ctx,
BranchOp::get_concrete_op_info(),
vec![],
vec![],
vec![succ0, succ1, succ2],
0,
);
branch_like.insert_before(ctx, ret_op.get_operation());
Operation::erase(ret_op.get_operation(), ctx);
let branch_like = Operation::new(
ctx,
BranchOp::get_concrete_op_info(),
vec![],
vec![],
vec![succ0],
0,
);
branch_like.insert_at_back(common_pred, ctx);
fn assert_block_pred_uses(ctx: &Context, block: Ptr<BasicBlock>) {
for block_use in block.uses(ctx) {
assert!(block_use.get_def(ctx) == block);
}
}
// We now have where entry_block branches to common_pred, succ0, succ1, and succ2.
// So all blocks are reachable, and hence pass the verifier dominance checks.
verify_op(&module_op, ctx)?;
assert_eq!(branch_like.deref(ctx).get_num_successors(), 1);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ0);
assert_block_pred_uses(ctx, succ0);
assert_eq!(succ0.num_preds(ctx), 2);
assert_eq!(succ1.num_preds(ctx), 1);
assert_eq!(succ2.num_preds(ctx), 1);
let pushed_idx = Operation::push_successor(branch_like, ctx, succ1);
assert_eq!(pushed_idx, 1);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 2);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ0);
assert_block_pred_uses(ctx, succ0);
assert_eq!(branch_like.deref(ctx).get_successor(1), succ1);
assert_block_pred_uses(ctx, succ1);
assert_eq!(succ0.num_preds(ctx), 2);
assert_eq!(succ1.num_preds(ctx), 2);
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&common_pred) && succ0_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&common_pred) && succ1_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
let popped = Operation::pop_successor(branch_like, ctx);
assert_eq!(popped, succ1);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 1);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ0);
assert_block_pred_uses(ctx, succ0);
assert_eq!(succ0.num_preds(ctx), 2);
assert_eq!(succ1.num_preds(ctx), 1);
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&common_pred) && succ0_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
Operation::insert_successor(branch_like, ctx, 0, succ1);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 2);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ1);
assert_block_pred_uses(ctx, succ1);
assert_eq!(branch_like.deref(ctx).get_successor(1), succ0);
assert_block_pred_uses(ctx, succ0);
assert_eq!(succ0.num_preds(ctx), 2);
assert_eq!(succ1.num_preds(ctx), 2);
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&common_pred) && succ0_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&common_pred) && succ1_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
Operation::insert_successor(branch_like, ctx, 2, succ2);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 3);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ1);
assert_block_pred_uses(ctx, succ1);
assert_eq!(branch_like.deref(ctx).get_successor(1), succ0);
assert_block_pred_uses(ctx, succ0);
assert_eq!(branch_like.deref(ctx).get_successor(2), succ2);
assert_block_pred_uses(ctx, succ2);
assert_eq!(succ0.num_preds(ctx), 2);
assert_eq!(succ1.num_preds(ctx), 2);
assert_eq!(succ2.num_preds(ctx), 2);
let succ2_preds = succ2.preds(ctx);
assert!(succ2_preds.contains(&common_pred) && succ2_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&common_pred) && succ1_preds.contains(&entry_block));
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&common_pred) && succ0_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
let removed_mid = Operation::remove_successor(branch_like, ctx, 1);
assert_eq!(removed_mid, succ0);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 2);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ1);
assert_block_pred_uses(ctx, succ1);
assert_eq!(branch_like.deref(ctx).get_successor(1), succ2);
assert_block_pred_uses(ctx, succ2);
assert_eq!(succ0.num_preds(ctx), 1);
assert_eq!(succ1.num_preds(ctx), 2);
assert_eq!(succ2.num_preds(ctx), 2);
let succ2_preds = succ2.preds(ctx);
assert!(succ2_preds.contains(&common_pred) && succ2_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&common_pred) && succ1_preds.contains(&entry_block));
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
let removed_front = Operation::remove_successor(branch_like, ctx, 0);
assert_eq!(removed_front, succ1);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 1);
assert_eq!(branch_like.deref(ctx).get_successor(0), succ2);
assert_block_pred_uses(ctx, succ2);
assert_eq!(succ0.num_preds(ctx), 1);
assert_eq!(succ1.num_preds(ctx), 1);
assert_eq!(succ2.num_preds(ctx), 2);
let succ2_preds = succ2.preds(ctx);
assert!(succ2_preds.contains(&common_pred) && succ2_preds.contains(&entry_block));
let succ1_preds = succ1.preds(ctx);
assert!(succ1_preds.contains(&entry_block));
let succ0_preds = succ0.preds(ctx);
assert!(succ0_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
// Add another branch to succ2 so that it has 3 preds, with common_pred being occurring twice.
Operation::insert_successor(branch_like, ctx, 0, succ2);
assert_eq!(branch_like.deref(ctx).get_num_successors(), 2);
assert_eq!(succ2.num_preds(ctx), 3);
let succ2_preds = succ2.preds(ctx);
assert!(succ2_preds.contains(&common_pred) && succ2_preds.contains(&entry_block));
verify_op(&module_op, ctx)?;
Ok(())
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
/// A test to just print a constructed IR to stdout.
fn print_simple() -> Result<()> {
let ctx = &mut Context::new();
let module_op = const_ret_in_mod(ctx)?.0.get_operation();
let printed = format!("{}", module_op.disp(ctx));
expect![[r#"
builtin.module @bar
{
^block1v1():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)>
{
^entry_block2v1():
c0_v0 = test.constant builtin.integer <0: si64> !0;
test.return c0_v0
}
}
outlined_attributes:
!0 = [builtin_debug_info = builtin.debug_info [c0]]
"#]]
.assert_eq(&printed);
println!("{printed}");
Ok(())
}
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn parse_simple() -> Result<()> {
let input = r#"
builtin.module @bar {
^block_0_0():
builtin.func @foo: builtin.function <() -> (builtin.integer si64)> {
^entry_block_1_0():
c0_op_2_0_res0 = test.constant builtin.integer <0: si64>;
test.return c0_op_2_0_res0
^exit(a : builtin.integer si32):
}
}"#;
let ctx = &mut Context::new();
let op = parse_from_str(spaced(Operation::top_level_parser()), ctx, input).unwrap();
println!("{}", op.disp(ctx));
Ok(())
}
dict_key!(ATTR_KEY_TEST_ON_FUNC_VALUE, "test_on_func_value");
dict_key!(ATTR_KEY_BLOCK_TEST, "block_test_attr");
#[test]
#[cfg_attr(target_family = "wasm", wasm_bindgen_test)]
fn parse_function_with_attrs() -> Result<()> {
let ctx = &mut Context::new();
let (module_op, _, _const_op, ret_op) = const_ret_in_mod(ctx).unwrap();
let func_op = ret_op
.get_operation()