-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
bugpoint.rs
1115 lines (952 loc) · 38.1 KB
/
bugpoint.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
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
//! CLI tool to reduce Cranelift IR files crashing during compilation.
use crate::utils::read_to_string;
use anyhow::{Context as _, Result};
use clap::Parser;
use cranelift::prelude::Value;
use cranelift_codegen::cursor::{Cursor, FuncCursor};
use cranelift_codegen::flowgraph::ControlFlowGraph;
use cranelift_codegen::ir::types::{F32, F64, I128, I64};
use cranelift_codegen::ir::{
self, Block, FuncRef, Function, GlobalValueData, Inst, InstBuilder, InstructionData,
StackSlots, TrapCode,
};
use cranelift_codegen::isa::TargetIsa;
use cranelift_codegen::Context;
use cranelift_entity::PrimaryMap;
use cranelift_reader::{parse_sets_and_triple, parse_test, ParseOptions};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use std::collections::HashMap;
use std::path::PathBuf;
/// Reduce size of clif file causing panic during compilation.
#[derive(Parser)]
pub struct Options {
/// Specify an input file to be used. Use '-' for stdin.
file: PathBuf,
/// Configure Cranelift settings
#[arg(long = "set")]
settings: Vec<String>,
/// Specify the target architecture.
target: String,
/// Be more verbose
#[arg(short, long)]
verbose: bool,
}
pub fn run(options: &Options) -> Result<()> {
let parsed = parse_sets_and_triple(&options.settings, &options.target)?;
let fisa = parsed.as_fisa();
let buffer = read_to_string(&options.file)?;
let test_file = parse_test(&buffer, ParseOptions::default())
.with_context(|| format!("failed to parse {}", options.file.display()))?;
// If we have an isa from the command-line, use that. Otherwise if the
// file contains a unique isa, use that.
let isa = if let Some(isa) = fisa.isa {
isa
} else if let Some(isa) = test_file.isa_spec.unique_isa() {
isa
} else {
anyhow::bail!("compilation requires a target isa");
};
std::env::set_var("RUST_BACKTRACE", "0"); // Disable backtraces to reduce verbosity
for (func, _) in test_file.functions {
let (orig_block_count, orig_inst_count) = (block_count(&func), inst_count(&func));
match reduce(isa, func, options.verbose) {
Ok((func, crash_msg)) => {
println!("Crash message: {}", crash_msg);
println!("\n{}", func);
println!(
"{} blocks {} insts -> {} blocks {} insts",
orig_block_count,
orig_inst_count,
block_count(&func),
inst_count(&func)
);
}
Err(err) => println!("Warning: {}", err),
}
}
Ok(())
}
enum ProgressStatus {
/// The mutation raised or reduced the amount of instructions or blocks.
ExpandedOrShrinked,
/// The mutation only changed an instruction. Performing another round of mutations may only
/// reduce the test case if another mutation shrank the test case.
Changed,
/// No need to re-test if the program crashes, because the mutation had no effect, but we want
/// to keep on iterating.
Skip,
}
trait Mutator {
fn name(&self) -> &'static str;
fn mutation_count(&self, func: &Function) -> usize;
fn mutate(&mut self, func: Function) -> Option<(Function, String, ProgressStatus)>;
/// Gets called when the returned mutated function kept on causing the crash. This can be used
/// to update position of the next item to look at. Does nothing by default.
fn did_crash(&mut self) {}
}
/// Try to remove instructions.
struct RemoveInst {
block: Block,
inst: Inst,
}
impl RemoveInst {
fn new(func: &Function) -> Self {
let first_block = func.layout.entry_block().unwrap();
let first_inst = func.layout.first_inst(first_block).unwrap();
Self {
block: first_block,
inst: first_inst,
}
}
}
impl Mutator for RemoveInst {
fn name(&self) -> &'static str {
"remove inst"
}
fn mutation_count(&self, func: &Function) -> usize {
inst_count(func)
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(|(prev_block, prev_inst)| {
func.layout.remove_inst(prev_inst);
let msg = if func.layout.block_insts(prev_block).next().is_none() {
// Make sure empty blocks are removed, as `next_inst_ret_prev` depends on non empty blocks
func.layout.remove_block(prev_block);
format!("Remove inst {} and empty block {}", prev_inst, prev_block)
} else {
format!("Remove inst {}", prev_inst)
};
(func, msg, ProgressStatus::ExpandedOrShrinked)
})
}
}
/// Try to replace instructions with `iconst` or `fconst`.
struct ReplaceInstWithConst {
block: Block,
inst: Inst,
}
impl ReplaceInstWithConst {
fn new(func: &Function) -> Self {
let first_block = func.layout.entry_block().unwrap();
let first_inst = func.layout.first_inst(first_block).unwrap();
Self {
block: first_block,
inst: first_inst,
}
}
}
impl Mutator for ReplaceInstWithConst {
fn name(&self) -> &'static str {
"replace inst with const"
}
fn mutation_count(&self, func: &Function) -> usize {
inst_count(func)
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(
|(_prev_block, prev_inst)| {
let num_results = func.dfg.inst_results(prev_inst).len();
let opcode = func.dfg.insts[prev_inst].opcode();
if num_results == 0
|| opcode == ir::Opcode::Iconst
|| opcode == ir::Opcode::F32const
|| opcode == ir::Opcode::F64const
{
return (func, format!(""), ProgressStatus::Skip);
}
// We replace a i128 const with a uextend+iconst, so we need to match that here
// to avoid processing those multiple times
if opcode == ir::Opcode::Uextend {
let ret_ty = func.dfg.value_type(func.dfg.first_result(prev_inst));
let is_uextend_i128 = ret_ty == I128;
let arg = func.dfg.inst_args(prev_inst)[0];
let arg_def = func.dfg.value_def(arg);
let arg_is_iconst = arg_def
.inst()
.map(|inst| func.dfg.insts[inst].opcode() == ir::Opcode::Iconst)
.unwrap_or(false);
if is_uextend_i128 && arg_is_iconst {
return (func, format!(""), ProgressStatus::Skip);
}
}
// At least 2 results. Replace each instruction with as many const instructions as
// there are results.
let mut pos = FuncCursor::new(&mut func).at_inst(prev_inst);
// Copy result SSA names into our own vector; otherwise we couldn't mutably borrow pos
// in the loop below.
let results = pos.func.dfg.inst_results(prev_inst).to_vec();
// Detach results from the previous instruction, since we're going to reuse them.
pos.func.dfg.clear_results(prev_inst);
let mut inst_names = Vec::new();
for r in &results {
let new_inst_name = replace_with_const(&mut pos, *r);
inst_names.push(new_inst_name);
}
// Remove the instruction.
assert_eq!(pos.remove_inst(), prev_inst);
let progress = if results.len() == 1 {
ProgressStatus::Changed
} else {
ProgressStatus::ExpandedOrShrinked
};
(
func,
format!("Replace inst {} with {}", prev_inst, inst_names.join(" / ")),
progress,
)
},
)
}
}
/// Try to replace instructions with `trap`.
struct ReplaceInstWithTrap {
block: Block,
inst: Inst,
}
impl ReplaceInstWithTrap {
fn new(func: &Function) -> Self {
let first_block = func.layout.entry_block().unwrap();
let first_inst = func.layout.first_inst(first_block).unwrap();
Self {
block: first_block,
inst: first_inst,
}
}
}
impl Mutator for ReplaceInstWithTrap {
fn name(&self) -> &'static str {
"replace inst with trap"
}
fn mutation_count(&self, func: &Function) -> usize {
inst_count(func)
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(
|(_prev_block, prev_inst)| {
let status = if func.dfg.insts[prev_inst].opcode() == ir::Opcode::Trap {
ProgressStatus::Skip
} else {
func.dfg.replace(prev_inst).trap(TrapCode::User(0));
ProgressStatus::Changed
};
(
func,
format!("Replace inst {} with trap", prev_inst),
status,
)
},
)
}
}
/// Try to move instructions to entry block.
struct MoveInstToEntryBlock {
block: Block,
inst: Inst,
}
impl MoveInstToEntryBlock {
fn new(func: &Function) -> Self {
let first_block = func.layout.entry_block().unwrap();
let first_inst = func.layout.first_inst(first_block).unwrap();
Self {
block: first_block,
inst: first_inst,
}
}
}
impl Mutator for MoveInstToEntryBlock {
fn name(&self) -> &'static str {
"move inst to entry block"
}
fn mutation_count(&self, func: &Function) -> usize {
inst_count(func)
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(|(prev_block, prev_inst)| {
// Don't move instructions that are already in entry block
// and instructions that end blocks.
let first_block = func.layout.entry_block().unwrap();
if first_block == prev_block || self.block != prev_block {
return (
func,
format!("did nothing for {}", prev_inst),
ProgressStatus::Skip,
);
}
let last_inst_of_first_block = func.layout.last_inst(first_block).unwrap();
func.layout.remove_inst(prev_inst);
func.layout.insert_inst(prev_inst, last_inst_of_first_block);
(
func,
format!("Move inst {} to entry block", prev_inst),
ProgressStatus::ExpandedOrShrinked,
)
})
}
}
/// Try to remove a block.
struct RemoveBlock {
block: Block,
}
impl RemoveBlock {
fn new(func: &Function) -> Self {
Self {
block: func.layout.entry_block().unwrap(),
}
}
}
impl Mutator for RemoveBlock {
fn name(&self) -> &'static str {
"remove block"
}
fn mutation_count(&self, func: &Function) -> usize {
block_count(func)
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
func.layout.next_block(self.block).map(|next_block| {
self.block = next_block;
while let Some(inst) = func.layout.last_inst(self.block) {
func.layout.remove_inst(inst);
}
func.layout.remove_block(self.block);
(
func,
format!("Remove block {}", next_block),
ProgressStatus::ExpandedOrShrinked,
)
})
}
}
/// Try to replace the block params with constants.
struct ReplaceBlockParamWithConst {
block: Block,
params_remaining: usize,
}
impl ReplaceBlockParamWithConst {
fn new(func: &Function) -> Self {
let first_block = func.layout.entry_block().unwrap();
Self {
block: first_block,
params_remaining: func.dfg.num_block_params(first_block),
}
}
}
impl Mutator for ReplaceBlockParamWithConst {
fn name(&self) -> &'static str {
"replace block parameter with const"
}
fn mutation_count(&self, func: &Function) -> usize {
func.layout
.blocks()
.map(|block| func.dfg.num_block_params(block))
.sum()
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
while self.params_remaining == 0 {
self.block = func.layout.next_block(self.block)?;
self.params_remaining = func.dfg.num_block_params(self.block);
}
self.params_remaining -= 1;
let param_index = self.params_remaining;
let param = func.dfg.block_params(self.block)[param_index];
func.dfg.remove_block_param(param);
let first_inst = func.layout.first_inst(self.block).unwrap();
let mut pos = FuncCursor::new(&mut func).at_inst(first_inst);
let new_inst_name = replace_with_const(&mut pos, param);
let mut cfg = ControlFlowGraph::new();
cfg.compute(&func);
// Remove parameters in branching instructions that point to this block
for pred in cfg.pred_iter(self.block) {
let dfg = &mut func.dfg;
for branch in dfg.insts[pred.inst].branch_destination_mut(&mut dfg.jump_tables) {
if branch.block(&dfg.value_lists) == self.block {
branch.remove(param_index, &mut dfg.value_lists);
}
}
}
if Some(self.block) == func.layout.entry_block() {
// Entry block params must match function params
func.signature.params.remove(param_index);
}
Some((
func,
format!(
"Replaced param {} of {} by {}",
param, self.block, new_inst_name
),
ProgressStatus::ExpandedOrShrinked,
))
}
}
/// Try to remove unused entities.
struct RemoveUnusedEntities {
kind: u32,
}
impl RemoveUnusedEntities {
fn new() -> Self {
Self { kind: 0 }
}
}
impl Mutator for RemoveUnusedEntities {
fn name(&self) -> &'static str {
"remove unused entities"
}
fn mutation_count(&self, _func: &Function) -> usize {
4
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
let name = match self.kind {
0 => {
let mut ext_func_usage_map = HashMap::new();
for block in func.layout.blocks() {
for inst in func.layout.block_insts(block) {
match func.dfg.insts[inst] {
// Add new cases when there are new instruction formats taking a `FuncRef`.
InstructionData::Call { func_ref, .. }
| InstructionData::FuncAddr { func_ref, .. } => {
ext_func_usage_map
.entry(func_ref)
.or_insert_with(Vec::new)
.push(inst);
}
_ => {}
}
}
}
let mut ext_funcs = PrimaryMap::new();
for (func_ref, ext_func_data) in func.dfg.ext_funcs.clone().into_iter() {
if let Some(func_ref_usage) = ext_func_usage_map.get(&func_ref) {
let new_func_ref = ext_funcs.push(ext_func_data.clone());
for &inst in func_ref_usage {
match func.dfg.insts[inst] {
// Keep in sync with the above match.
InstructionData::Call {
ref mut func_ref, ..
}
| InstructionData::FuncAddr {
ref mut func_ref, ..
} => {
*func_ref = new_func_ref;
}
_ => unreachable!(),
}
}
}
}
func.dfg.ext_funcs = ext_funcs;
"Remove unused ext funcs"
}
1 => {
#[derive(Copy, Clone)]
enum SigRefUser {
Instruction(Inst),
ExtFunc(FuncRef),
}
let mut signatures_usage_map = HashMap::new();
for block in func.layout.blocks() {
for inst in func.layout.block_insts(block) {
// Add new cases when there are new instruction formats taking a `SigRef`.
if let InstructionData::CallIndirect { sig_ref, .. } = func.dfg.insts[inst]
{
signatures_usage_map
.entry(sig_ref)
.or_insert_with(Vec::new)
.push(SigRefUser::Instruction(inst));
}
}
}
for (func_ref, ext_func_data) in func.dfg.ext_funcs.iter() {
signatures_usage_map
.entry(ext_func_data.signature)
.or_insert_with(Vec::new)
.push(SigRefUser::ExtFunc(func_ref));
}
let mut signatures = PrimaryMap::new();
for (sig_ref, sig_data) in func.dfg.signatures.clone().into_iter() {
if let Some(sig_ref_usage) = signatures_usage_map.get(&sig_ref) {
let new_sig_ref = signatures.push(sig_data.clone());
for &sig_ref_user in sig_ref_usage {
match sig_ref_user {
SigRefUser::Instruction(inst) => match func.dfg.insts[inst] {
// Keep in sync with the above match.
InstructionData::CallIndirect {
ref mut sig_ref, ..
} => {
*sig_ref = new_sig_ref;
}
_ => unreachable!(),
},
SigRefUser::ExtFunc(func_ref) => {
func.dfg.ext_funcs[func_ref].signature = new_sig_ref;
}
}
}
}
}
func.dfg.signatures = signatures;
"Remove unused signatures"
}
2 => {
let mut stack_slot_usage_map = HashMap::new();
for block in func.layout.blocks() {
for inst in func.layout.block_insts(block) {
match func.dfg.insts[inst] {
// Add new cases when there are new instruction formats taking a `StackSlot`.
InstructionData::StackLoad { stack_slot, .. }
| InstructionData::StackStore { stack_slot, .. } => {
stack_slot_usage_map
.entry(stack_slot)
.or_insert_with(Vec::new)
.push(inst);
}
_ => {}
}
}
}
let mut stack_slots = StackSlots::new();
for (stack_slot, stack_slot_data) in func.sized_stack_slots.clone().iter() {
if let Some(stack_slot_usage) = stack_slot_usage_map.get(&stack_slot) {
let new_stack_slot = stack_slots.push(stack_slot_data.clone());
for &inst in stack_slot_usage {
match &mut func.dfg.insts[inst] {
// Keep in sync with the above match.
InstructionData::StackLoad { stack_slot, .. }
| InstructionData::StackStore { stack_slot, .. } => {
*stack_slot = new_stack_slot;
}
_ => unreachable!(),
}
}
}
}
func.sized_stack_slots = stack_slots;
"Remove unused stack slots"
}
3 => {
let mut global_value_usage_map = HashMap::new();
for block in func.layout.blocks() {
for inst in func.layout.block_insts(block) {
// Add new cases when there are new instruction formats taking a `GlobalValue`.
if let InstructionData::UnaryGlobalValue { global_value, .. } =
func.dfg.insts[inst]
{
global_value_usage_map
.entry(global_value)
.or_insert_with(Vec::new)
.push(inst);
}
}
}
for (_global_value, global_value_data) in func.global_values.iter() {
match *global_value_data {
GlobalValueData::VMContext | GlobalValueData::Symbol { .. } => {}
// These can create cyclic references, which cause complications. Just skip
// the global value removal for now.
// FIXME Handle them in a better way.
GlobalValueData::Load { .. }
| GlobalValueData::IAddImm { .. }
| GlobalValueData::DynScaleTargetConst { .. } => return None,
}
}
let mut global_values = PrimaryMap::new();
for (global_value, global_value_data) in func.global_values.clone().into_iter() {
if let Some(global_value_usage) = global_value_usage_map.get(&global_value) {
let new_global_value = global_values.push(global_value_data.clone());
for &inst in global_value_usage {
match &mut func.dfg.insts[inst] {
// Keep in sync with the above match.
InstructionData::UnaryGlobalValue { global_value, .. } => {
*global_value = new_global_value;
}
_ => unreachable!(),
}
}
}
}
func.global_values = global_values;
"Remove unused global values"
}
_ => return None,
};
self.kind += 1;
Some((func, name.to_owned(), ProgressStatus::Changed))
}
}
struct MergeBlocks {
block: Block,
prev_block: Option<Block>,
}
impl MergeBlocks {
fn new(func: &Function) -> Self {
Self {
block: func.layout.entry_block().unwrap(),
prev_block: None,
}
}
}
impl Mutator for MergeBlocks {
fn name(&self) -> &'static str {
"merge blocks"
}
fn mutation_count(&self, func: &Function) -> usize {
// N blocks may result in at most N-1 merges.
block_count(func) - 1
}
fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
let block = match func.layout.next_block(self.block) {
Some(block) => block,
None => return None,
};
self.block = block;
let mut cfg = ControlFlowGraph::new();
cfg.compute(&func);
if cfg.pred_iter(block).count() != 1 {
return Some((
func,
format!("did nothing for {}", block),
ProgressStatus::Skip,
));
}
let pred = cfg.pred_iter(block).next().unwrap();
// If the branch instruction that lead us to this block wasn't an unconditional jump, then
// we have a conditional jump sequence that we should not break.
let branch_dests = func.dfg.insts[pred.inst].branch_destination(&func.dfg.jump_tables);
if branch_dests.len() != 1 {
return Some((
func,
format!("did nothing for {}", block),
ProgressStatus::Skip,
));
}
let branch_args = branch_dests[0].args_slice(&func.dfg.value_lists).to_vec();
// TODO: should we free the entity list associated with the block params?
let block_params = func
.dfg
.detach_block_params(block)
.as_slice(&func.dfg.value_lists)
.to_vec();
assert_eq!(block_params.len(), branch_args.len());
// If there were any block parameters in block, then the last instruction in pred will
// fill these parameters. Make the block params aliases of the terminator arguments.
for (block_param, arg) in block_params.into_iter().zip(branch_args) {
if block_param != arg {
func.dfg.change_to_alias(block_param, arg);
}
}
// Remove the terminator branch to the current block.
func.layout.remove_inst(pred.inst);
// Move all the instructions to the predecessor.
while let Some(inst) = func.layout.first_inst(block) {
func.layout.remove_inst(inst);
func.layout.append_inst(inst, pred.block);
}
// Remove the predecessor block.
func.layout.remove_block(block);
// Record the previous block: if we caused a crash (as signaled by a call to did_crash), then
// we'll start back to this block.
self.prev_block = Some(pred.block);
Some((
func,
format!("merged {} and {}", pred.block, block),
ProgressStatus::ExpandedOrShrinked,
))
}
fn did_crash(&mut self) {
self.block = self.prev_block.unwrap();
}
}
fn replace_with_const(pos: &mut FuncCursor, param: Value) -> &'static str {
let ty = pos.func.dfg.value_type(param);
if ty == F32 {
pos.ins().with_result(param).f32const(0.0);
"f32const"
} else if ty == F64 {
pos.ins().with_result(param).f64const(0.0);
"f64const"
} else if ty.is_ref() {
pos.ins().with_result(param).null(ty);
"null"
} else if ty.is_vector() {
let zero_data = vec![0; ty.bytes() as usize].into();
let zero_handle = pos.func.dfg.constants.insert(zero_data);
pos.ins().with_result(param).vconst(ty, zero_handle);
"vconst"
} else if ty == I128 {
let res = pos.ins().iconst(I64, 0);
pos.ins().with_result(param).uextend(I128, res);
"iconst+uextend"
} else {
// Default to an integer type and possibly create verifier error
pos.ins().with_result(param).iconst(ty, 0);
"iconst"
}
}
fn next_inst_ret_prev(
func: &Function,
block: &mut Block,
inst: &mut Inst,
) -> Option<(Block, Inst)> {
let prev = (*block, *inst);
if let Some(next_inst) = func.layout.next_inst(*inst) {
*inst = next_inst;
return Some(prev);
}
if let Some(next_block) = func.layout.next_block(*block) {
*block = next_block;
*inst = func.layout.first_inst(*block).expect("no inst");
return Some(prev);
}
None
}
fn block_count(func: &Function) -> usize {
func.layout.blocks().count()
}
fn inst_count(func: &Function) -> usize {
func.layout
.blocks()
.map(|block| func.layout.block_insts(block).count())
.sum()
}
fn resolve_aliases(func: &mut Function) {
for block in func.stencil.layout.blocks() {
for inst in func.stencil.layout.block_insts(block) {
func.stencil.dfg.resolve_aliases_in_arguments(inst);
}
}
}
/// Resolve aliases only if function still crashes after this.
fn try_resolve_aliases(context: &mut CrashCheckContext, func: &mut Function) {
let mut func_with_resolved_aliases = func.clone();
resolve_aliases(&mut func_with_resolved_aliases);
if let CheckResult::Crash(_) = context.check_for_crash(&func_with_resolved_aliases) {
*func = func_with_resolved_aliases;
}
}
fn reduce(isa: &dyn TargetIsa, mut func: Function, verbose: bool) -> Result<(Function, String)> {
let mut context = CrashCheckContext::new(isa);
if let CheckResult::Succeed = context.check_for_crash(&func) {
anyhow::bail!("Given function compiled successfully or gave a verifier error.");
}
try_resolve_aliases(&mut context, &mut func);
let progress_bar = ProgressBar::with_draw_target(0, ProgressDrawTarget::stdout());
progress_bar.set_style(
ProgressStyle::default_bar().template("{bar:60} {prefix:40} {pos:>4}/{len:>4} {msg}"),
);
for pass_idx in 0..100 {
let mut should_keep_reducing = false;
let mut phase = 0;
loop {
let mut mutator: Box<dyn Mutator> = match phase {
0 => Box::new(RemoveInst::new(&func)),
1 => Box::new(ReplaceInstWithConst::new(&func)),
2 => Box::new(ReplaceInstWithTrap::new(&func)),
3 => Box::new(MoveInstToEntryBlock::new(&func)),
4 => Box::new(RemoveBlock::new(&func)),
5 => Box::new(ReplaceBlockParamWithConst::new(&func)),
6 => Box::new(RemoveUnusedEntities::new()),
7 => Box::new(MergeBlocks::new(&func)),
_ => break,
};
progress_bar.set_prefix(&format!("pass {} phase {}", pass_idx, mutator.name()));
progress_bar.set_length(mutator.mutation_count(&func) as u64);
// Reset progress bar.
progress_bar.set_position(0);
progress_bar.set_draw_delta(0);
for _ in 0..10000 {
progress_bar.inc(1);
let (mutated_func, msg, mutation_kind) = match mutator.mutate(func.clone()) {
Some(res) => res,
None => {
break;
}
};
if let ProgressStatus::Skip = mutation_kind {
// The mutator didn't change anything, but we want to try more mutator
// iterations.
continue;
}
progress_bar.set_message(&msg);
match context.check_for_crash(&mutated_func) {
CheckResult::Succeed => {
// Mutating didn't hit the problem anymore, discard changes.
continue;
}
CheckResult::Crash(_) => {
// Panic remained while mutating, make changes definitive.
func = mutated_func;
// Notify the mutator that the mutation was successful.
mutator.did_crash();
let verb = match mutation_kind {
ProgressStatus::ExpandedOrShrinked => {
should_keep_reducing = true;
"shrink"
}
ProgressStatus::Changed => "changed",
ProgressStatus::Skip => unreachable!(),
};
if verbose {
progress_bar.println(format!("{}: {}", msg, verb));
}
}
}
}
phase += 1;
}
progress_bar.println(format!(
"After pass {}, remaining insts/blocks: {}/{} ({})",
pass_idx,
inst_count(&func),
block_count(&func),
if should_keep_reducing {
"will keep reducing"
} else {
"stop reducing"
}
));
if !should_keep_reducing {
// No new shrinking opportunities have been found this pass. This means none will ever
// be found. Skip the rest of the passes over the function.
break;
}
}
try_resolve_aliases(&mut context, &mut func);
progress_bar.finish();
let crash_msg = match context.check_for_crash(&func) {
CheckResult::Succeed => unreachable!("Used to crash, but doesn't anymore???"),
CheckResult::Crash(crash_msg) => crash_msg,
};
Ok((func, crash_msg))
}
struct CrashCheckContext<'a> {
/// Cached `Context`, to prevent repeated allocation.
context: Context,
/// Cached code memory, to prevent repeated allocation.
code_memory: Vec<u8>,
/// The target isa to compile for.
isa: &'a dyn TargetIsa,
}
fn get_panic_string(panic: Box<dyn std::any::Any>) -> String {
let panic = match panic.downcast::<&'static str>() {
Ok(panic_msg) => {
return panic_msg.to_string();
}
Err(panic) => panic,
};
match panic.downcast::<String>() {
Ok(panic_msg) => *panic_msg,
Err(_) => "Box<Any>".to_string(),
}
}
enum CheckResult {
/// The function compiled fine, or the verifier noticed an error.
Succeed,
/// The compilation of the function panicked.
Crash(String),
}
impl<'a> CrashCheckContext<'a> {
fn new(isa: &'a dyn TargetIsa) -> Self {
CrashCheckContext {
context: Context::new(),
code_memory: Vec::new(),
isa,
}
}
#[cfg_attr(test, allow(unreachable_code))]
fn check_for_crash(&mut self, func: &Function) -> CheckResult {