Skip to main content

clif_util/
bugpoint.rs

1//! CLI tool to reduce Cranelift IR files crashing during compilation.
2
3use crate::utils::read_to_string;
4use anyhow::{Context as _, Result};
5use clap::Parser;
6use cranelift::prelude::Value;
7use cranelift_codegen::Context;
8use cranelift_codegen::cursor::{Cursor, FuncCursor};
9use cranelift_codegen::flowgraph::ControlFlowGraph;
10use cranelift_codegen::ir::types::{F32, F64, I64, I128};
11use cranelift_codegen::ir::{
12    self, Block, FuncRef, Function, GlobalValueData, Inst, InstBuilder, InstructionData,
13    StackSlots, TrapCode,
14};
15use cranelift_codegen::isa::TargetIsa;
16use cranelift_entity::PrimaryMap;
17use cranelift_reader::{ParseOptions, parse_sets_and_triple, parse_test};
18use std::collections::HashMap;
19use std::path::PathBuf;
20
21/// Reduce size of clif file causing panic during compilation.
22#[derive(Parser)]
23pub struct Options {
24    /// Specify an input file to be used. Use '-' for stdin.
25    file: PathBuf,
26
27    /// Configure Cranelift settings
28    #[arg(long = "set")]
29    settings: Vec<String>,
30
31    /// Specify the target architecture.
32    target: String,
33
34    /// Be more verbose
35    #[arg(short, long)]
36    verbose: bool,
37}
38
39pub fn run(options: &Options) -> Result<()> {
40    let parsed = parse_sets_and_triple(&options.settings, &options.target)?;
41    let fisa = parsed.as_fisa();
42
43    let buffer = read_to_string(&options.file)?;
44    let test_file = parse_test(&buffer, ParseOptions::default())
45        .with_context(|| format!("failed to parse {}", options.file.display()))?;
46
47    // If we have an isa from the command-line, use that. Otherwise if the
48    // file contains a unique isa, use that.
49    let isa = if let Some(isa) = fisa.isa {
50        isa
51    } else if let Some(isa) = test_file.isa_spec.unique_isa() {
52        isa
53    } else {
54        anyhow::bail!("compilation requires a target isa");
55    };
56
57    unsafe {
58        std::env::set_var("RUST_BACKTRACE", "0"); // Disable backtraces to reduce verbosity
59    }
60
61    for (func, _) in test_file.functions {
62        let (orig_block_count, orig_inst_count) = (block_count(&func), inst_count(&func));
63
64        match reduce(isa, func, options.verbose) {
65            Ok((func, crash_msg)) => {
66                println!("Crash message: {crash_msg}");
67                println!("\n{func}");
68                println!(
69                    "{} blocks {} insts -> {} blocks {} insts",
70                    orig_block_count,
71                    orig_inst_count,
72                    block_count(&func),
73                    inst_count(&func)
74                );
75            }
76            Err(err) => println!("Warning: {err}"),
77        }
78    }
79
80    Ok(())
81}
82
83enum ProgressStatus {
84    /// The mutation raised or reduced the amount of instructions or blocks.
85    ExpandedOrShrinked,
86
87    /// The mutation only changed an instruction. Performing another round of mutations may only
88    /// reduce the test case if another mutation shrank the test case.
89    Changed,
90
91    /// No need to re-test if the program crashes, because the mutation had no effect, but we want
92    /// to keep on iterating.
93    Skip,
94}
95
96trait Mutator {
97    fn name(&self) -> &'static str;
98    fn mutate(&mut self, func: Function) -> Option<(Function, String, ProgressStatus)>;
99
100    /// Gets called when the returned mutated function kept on causing the crash. This can be used
101    /// to update position of the next item to look at. Does nothing by default.
102    fn did_crash(&mut self) {}
103}
104
105/// Try to remove instructions.
106struct RemoveInst {
107    block: Block,
108    inst: Inst,
109}
110
111impl RemoveInst {
112    fn new(func: &Function) -> Self {
113        let first_block = func.layout.entry_block().unwrap();
114        let first_inst = func.layout.first_inst(first_block).unwrap();
115        Self {
116            block: first_block,
117            inst: first_inst,
118        }
119    }
120}
121
122impl Mutator for RemoveInst {
123    fn name(&self) -> &'static str {
124        "remove inst"
125    }
126
127    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
128        next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(|(prev_block, prev_inst)| {
129            func.layout.remove_inst(prev_inst);
130            let msg = if func.layout.block_insts(prev_block).next().is_none() {
131                // Make sure empty blocks are removed, as `next_inst_ret_prev` depends on non empty blocks
132                func.layout.remove_block(prev_block);
133                format!("Remove inst {prev_inst} and empty block {prev_block}")
134            } else {
135                format!("Remove inst {prev_inst}")
136            };
137            (func, msg, ProgressStatus::ExpandedOrShrinked)
138        })
139    }
140}
141
142/// Try to replace instructions with `iconst` or `fconst`.
143struct ReplaceInstWithConst {
144    block: Block,
145    inst: Inst,
146}
147
148impl ReplaceInstWithConst {
149    fn new(func: &Function) -> Self {
150        let first_block = func.layout.entry_block().unwrap();
151        let first_inst = func.layout.first_inst(first_block).unwrap();
152        Self {
153            block: first_block,
154            inst: first_inst,
155        }
156    }
157}
158
159impl Mutator for ReplaceInstWithConst {
160    fn name(&self) -> &'static str {
161        "replace inst with const"
162    }
163
164    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
165        next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(
166            |(_prev_block, prev_inst)| {
167                let num_results = func.dfg.inst_results(prev_inst).len();
168
169                let opcode = func.dfg.insts[prev_inst].opcode();
170                if num_results == 0
171                    || opcode == ir::Opcode::Iconst
172                    || opcode == ir::Opcode::F32const
173                    || opcode == ir::Opcode::F64const
174                {
175                    return (func, format!(""), ProgressStatus::Skip);
176                }
177
178                // We replace a i128 const with a uextend+iconst, so we need to match that here
179                // to avoid processing those multiple times
180                if opcode == ir::Opcode::Uextend {
181                    let ret_ty = func.dfg.value_type(func.dfg.first_result(prev_inst));
182                    let is_uextend_i128 = ret_ty == I128;
183
184                    let arg = func.dfg.inst_args(prev_inst)[0];
185                    let arg_def = func.dfg.value_def(arg);
186                    let arg_is_iconst = arg_def
187                        .inst()
188                        .map(|inst| func.dfg.insts[inst].opcode() == ir::Opcode::Iconst)
189                        .unwrap_or(false);
190
191                    if is_uextend_i128 && arg_is_iconst {
192                        return (func, format!(""), ProgressStatus::Skip);
193                    }
194                }
195
196                // At least 2 results. Replace each instruction with as many const instructions as
197                // there are results.
198                let mut pos = FuncCursor::new(&mut func).at_inst(prev_inst);
199
200                // Copy result SSA names into our own vector; otherwise we couldn't mutably borrow pos
201                // in the loop below.
202                let results = pos.func.dfg.inst_results(prev_inst).to_vec();
203
204                // Detach results from the previous instruction, since we're going to reuse them.
205                pos.func.dfg.clear_results(prev_inst);
206
207                let mut inst_names = Vec::new();
208                for r in &results {
209                    let new_inst_name = replace_with_const(&mut pos, *r);
210                    inst_names.push(new_inst_name);
211                }
212
213                // Remove the instruction.
214                assert_eq!(pos.remove_inst(), prev_inst);
215
216                let progress = if results.len() == 1 {
217                    ProgressStatus::Changed
218                } else {
219                    ProgressStatus::ExpandedOrShrinked
220                };
221
222                (
223                    func,
224                    format!("Replace inst {} with {}", prev_inst, inst_names.join(" / ")),
225                    progress,
226                )
227            },
228        )
229    }
230}
231
232/// Try to replace instructions with `trap`.
233struct ReplaceInstWithTrap {
234    block: Block,
235    inst: Inst,
236}
237
238impl ReplaceInstWithTrap {
239    fn new(func: &Function) -> Self {
240        let first_block = func.layout.entry_block().unwrap();
241        let first_inst = func.layout.first_inst(first_block).unwrap();
242        Self {
243            block: first_block,
244            inst: first_inst,
245        }
246    }
247}
248
249impl Mutator for ReplaceInstWithTrap {
250    fn name(&self) -> &'static str {
251        "replace inst with trap"
252    }
253
254    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
255        next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(
256            |(_prev_block, prev_inst)| {
257                let status = if func.dfg.insts[prev_inst].opcode() == ir::Opcode::Trap {
258                    ProgressStatus::Skip
259                } else {
260                    func.replace(prev_inst).trap(TrapCode::unwrap_user(1));
261                    ProgressStatus::Changed
262                };
263                (func, format!("Replace inst {prev_inst} with trap"), status)
264            },
265        )
266    }
267}
268
269/// Try to move instructions to entry block.
270struct MoveInstToEntryBlock {
271    block: Block,
272    inst: Inst,
273}
274
275impl MoveInstToEntryBlock {
276    fn new(func: &Function) -> Self {
277        let first_block = func.layout.entry_block().unwrap();
278        let first_inst = func.layout.first_inst(first_block).unwrap();
279        Self {
280            block: first_block,
281            inst: first_inst,
282        }
283    }
284}
285
286impl Mutator for MoveInstToEntryBlock {
287    fn name(&self) -> &'static str {
288        "move inst to entry block"
289    }
290
291    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
292        next_inst_ret_prev(&func, &mut self.block, &mut self.inst).map(|(prev_block, prev_inst)| {
293            // Don't move instructions that are already in entry block
294            // and instructions that end blocks.
295            let first_block = func.layout.entry_block().unwrap();
296            if first_block == prev_block || self.block != prev_block {
297                return (
298                    func,
299                    format!("did nothing for {prev_inst}"),
300                    ProgressStatus::Skip,
301                );
302            }
303
304            let last_inst_of_first_block = func.layout.last_inst(first_block).unwrap();
305            func.layout.remove_inst(prev_inst);
306            func.layout.insert_inst(prev_inst, last_inst_of_first_block);
307
308            (
309                func,
310                format!("Move inst {prev_inst} to entry block"),
311                ProgressStatus::ExpandedOrShrinked,
312            )
313        })
314    }
315}
316
317/// Try to remove a block.
318struct RemoveBlock {
319    block: Block,
320}
321
322impl RemoveBlock {
323    fn new(func: &Function) -> Self {
324        Self {
325            block: func.layout.entry_block().unwrap(),
326        }
327    }
328}
329
330impl Mutator for RemoveBlock {
331    fn name(&self) -> &'static str {
332        "remove block"
333    }
334
335    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
336        func.layout.next_block(self.block).map(|next_block| {
337            self.block = next_block;
338            while let Some(inst) = func.layout.last_inst(self.block) {
339                func.layout.remove_inst(inst);
340            }
341            func.layout.remove_block(self.block);
342            (
343                func,
344                format!("Remove block {next_block}"),
345                ProgressStatus::ExpandedOrShrinked,
346            )
347        })
348    }
349}
350
351/// Try to replace the block params with constants.
352struct ReplaceBlockParamWithConst {
353    block: Block,
354    params_remaining: usize,
355}
356
357impl ReplaceBlockParamWithConst {
358    fn new(func: &Function) -> Self {
359        let first_block = func.layout.entry_block().unwrap();
360        Self {
361            block: first_block,
362            params_remaining: func.dfg.num_block_params(first_block),
363        }
364    }
365}
366
367impl Mutator for ReplaceBlockParamWithConst {
368    fn name(&self) -> &'static str {
369        "replace block parameter with const"
370    }
371
372    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
373        while self.params_remaining == 0 {
374            self.block = func.layout.next_block(self.block)?;
375            self.params_remaining = func.dfg.num_block_params(self.block);
376        }
377
378        self.params_remaining -= 1;
379        let param_index = self.params_remaining;
380
381        let param = func.dfg.block_params(self.block)[param_index];
382        func.dfg.remove_block_param(param);
383
384        let first_inst = func.layout.first_inst(self.block).unwrap();
385        let mut pos = FuncCursor::new(&mut func).at_inst(first_inst);
386        let new_inst_name = replace_with_const(&mut pos, param);
387
388        let mut cfg = ControlFlowGraph::new();
389        cfg.compute(&func);
390
391        // Remove parameters in branching instructions that point to this block
392        for pred in cfg.pred_iter(self.block) {
393            let dfg = &mut func.dfg;
394            for branch in dfg.insts[pred.inst]
395                .branch_destination_mut(&mut dfg.jump_tables, &mut dfg.exception_tables)
396            {
397                if branch.block(&dfg.value_lists) == self.block {
398                    branch.remove(param_index, &mut dfg.value_lists);
399                }
400            }
401        }
402
403        if Some(self.block) == func.layout.entry_block() {
404            // Entry block params must match function params
405            func.signature.params.remove(param_index);
406        }
407
408        Some((
409            func,
410            format!(
411                "Replaced param {} of {} by {}",
412                param, self.block, new_inst_name
413            ),
414            ProgressStatus::ExpandedOrShrinked,
415        ))
416    }
417}
418
419/// Try to remove unused entities.
420struct RemoveUnusedEntities {
421    kind: u32,
422}
423
424impl RemoveUnusedEntities {
425    fn new() -> Self {
426        Self { kind: 0 }
427    }
428}
429
430impl Mutator for RemoveUnusedEntities {
431    fn name(&self) -> &'static str {
432        "remove unused entities"
433    }
434
435    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
436        let name = match self.kind {
437            0 => {
438                let mut ext_func_usage_map = HashMap::new();
439                for block in func.layout.blocks() {
440                    for inst in func.layout.block_insts(block) {
441                        match func.dfg.insts[inst] {
442                            // Add new cases when there are new instruction formats taking a `FuncRef`.
443                            InstructionData::Call { func_ref, .. }
444                            | InstructionData::FuncAddr { func_ref, .. } => {
445                                ext_func_usage_map
446                                    .entry(func_ref)
447                                    .or_insert_with(Vec::new)
448                                    .push(inst);
449                            }
450                            _ => {}
451                        }
452                    }
453                }
454
455                let mut ext_funcs = PrimaryMap::new();
456
457                for (func_ref, ext_func_data) in func.dfg.ext_funcs.clone().into_iter() {
458                    if let Some(func_ref_usage) = ext_func_usage_map.get(&func_ref) {
459                        let new_func_ref = ext_funcs.push(ext_func_data.clone());
460                        for &inst in func_ref_usage {
461                            match func.dfg.insts[inst] {
462                                // Keep in sync with the above match.
463                                InstructionData::Call {
464                                    ref mut func_ref, ..
465                                }
466                                | InstructionData::FuncAddr {
467                                    ref mut func_ref, ..
468                                } => {
469                                    *func_ref = new_func_ref;
470                                }
471                                _ => unreachable!(),
472                            }
473                        }
474                    }
475                }
476
477                func.dfg.ext_funcs = ext_funcs;
478
479                "Remove unused ext funcs"
480            }
481            1 => {
482                #[derive(Copy, Clone)]
483                enum SigRefUser {
484                    Instruction(Inst),
485                    ExtFunc(FuncRef),
486                }
487
488                let mut signatures_usage_map = HashMap::new();
489                for block in func.layout.blocks() {
490                    for inst in func.layout.block_insts(block) {
491                        // Add new cases when there are new instruction formats taking a `SigRef`.
492                        if let InstructionData::CallIndirect { sig_ref, .. } = func.dfg.insts[inst]
493                        {
494                            signatures_usage_map
495                                .entry(sig_ref)
496                                .or_insert_with(Vec::new)
497                                .push(SigRefUser::Instruction(inst));
498                        }
499                    }
500                }
501                for (func_ref, ext_func_data) in func.dfg.ext_funcs.iter() {
502                    signatures_usage_map
503                        .entry(ext_func_data.signature)
504                        .or_insert_with(Vec::new)
505                        .push(SigRefUser::ExtFunc(func_ref));
506                }
507
508                let mut signatures = PrimaryMap::new();
509
510                for (sig_ref, sig_data) in func.dfg.signatures.clone().into_iter() {
511                    if let Some(sig_ref_usage) = signatures_usage_map.get(&sig_ref) {
512                        let new_sig_ref = signatures.push(sig_data.clone());
513                        for &sig_ref_user in sig_ref_usage {
514                            match sig_ref_user {
515                                SigRefUser::Instruction(inst) => match func.dfg.insts[inst] {
516                                    // Keep in sync with the above match.
517                                    InstructionData::CallIndirect {
518                                        ref mut sig_ref, ..
519                                    } => {
520                                        *sig_ref = new_sig_ref;
521                                    }
522                                    _ => unreachable!(),
523                                },
524                                SigRefUser::ExtFunc(func_ref) => {
525                                    func.dfg.ext_funcs[func_ref].signature = new_sig_ref;
526                                }
527                            }
528                        }
529                    }
530                }
531
532                func.dfg.signatures = signatures;
533
534                "Remove unused signatures"
535            }
536            2 => {
537                let mut stack_slot_usage_map = HashMap::new();
538                for block in func.layout.blocks() {
539                    for inst in func.layout.block_insts(block) {
540                        match func.dfg.insts[inst] {
541                            // Add new cases when there are new instruction formats taking a `StackSlot`.
542                            InstructionData::StackAddr { stack_slot, .. } => {
543                                stack_slot_usage_map
544                                    .entry(stack_slot)
545                                    .or_insert_with(Vec::new)
546                                    .push(inst);
547                            }
548
549                            _ => {}
550                        }
551                    }
552                }
553
554                let mut stack_slots = StackSlots::new();
555
556                for (stack_slot, stack_slot_data) in func.sized_stack_slots.clone().iter() {
557                    if let Some(stack_slot_usage) = stack_slot_usage_map.get(&stack_slot) {
558                        let new_stack_slot = stack_slots.push(stack_slot_data.clone());
559                        for &inst in stack_slot_usage {
560                            match &mut func.dfg.insts[inst] {
561                                // Keep in sync with the above match.
562                                InstructionData::StackAddr { stack_slot, .. } => {
563                                    *stack_slot = new_stack_slot;
564                                }
565                                _ => unreachable!(),
566                            }
567                        }
568                    }
569                }
570
571                func.sized_stack_slots = stack_slots;
572
573                "Remove unused stack slots"
574            }
575            3 => {
576                let mut global_value_usage_map = HashMap::new();
577                for block in func.layout.blocks() {
578                    for inst in func.layout.block_insts(block) {
579                        // Add new cases when there are new instruction formats taking a `GlobalValue`.
580                        if let InstructionData::UnaryGlobalValue { global_value, .. } =
581                            func.dfg.insts[inst]
582                        {
583                            global_value_usage_map
584                                .entry(global_value)
585                                .or_insert_with(Vec::new)
586                                .push(inst);
587                        }
588                    }
589                }
590
591                for (_global_value, global_value_data) in func.global_values.iter() {
592                    match *global_value_data {
593                        GlobalValueData::VMContext | GlobalValueData::Symbol { .. } => {}
594                        // These can create cyclic references, which cause complications. Just skip
595                        // the global value removal for now.
596                        // FIXME Handle them in a better way.
597                        GlobalValueData::Load { .. }
598                        | GlobalValueData::IAddImm { .. }
599                        | GlobalValueData::DynScaleTargetConst { .. } => return None,
600                    }
601                }
602
603                let mut global_values = PrimaryMap::new();
604
605                for (global_value, global_value_data) in func.global_values.clone().into_iter() {
606                    if let Some(global_value_usage) = global_value_usage_map.get(&global_value) {
607                        let new_global_value = global_values.push(global_value_data.clone());
608                        for &inst in global_value_usage {
609                            match &mut func.dfg.insts[inst] {
610                                // Keep in sync with the above match.
611                                InstructionData::UnaryGlobalValue { global_value, .. } => {
612                                    *global_value = new_global_value;
613                                }
614                                _ => unreachable!(),
615                            }
616                        }
617                    }
618                }
619
620                func.global_values = global_values;
621
622                "Remove unused global values"
623            }
624            _ => return None,
625        };
626        self.kind += 1;
627        Some((func, name.to_owned(), ProgressStatus::Changed))
628    }
629}
630
631struct MergeBlocks {
632    block: Block,
633    prev_block: Option<Block>,
634}
635
636impl MergeBlocks {
637    fn new(func: &Function) -> Self {
638        Self {
639            block: func.layout.entry_block().unwrap(),
640            prev_block: None,
641        }
642    }
643}
644
645impl Mutator for MergeBlocks {
646    fn name(&self) -> &'static str {
647        "merge blocks"
648    }
649
650    fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> {
651        let block = match func.layout.next_block(self.block) {
652            Some(block) => block,
653            None => return None,
654        };
655
656        self.block = block;
657
658        let mut cfg = ControlFlowGraph::new();
659        cfg.compute(&func);
660
661        if cfg.pred_iter(block).count() != 1 {
662            return Some((
663                func,
664                format!("did nothing for {block}"),
665                ProgressStatus::Skip,
666            ));
667        }
668
669        let pred = cfg.pred_iter(block).next().unwrap();
670
671        // If the branch instruction that lead us to this block wasn't an unconditional jump, then
672        // we have a conditional jump sequence that we should not break.
673        let branch_dests = func.dfg.insts[pred.inst]
674            .branch_destination(&func.dfg.jump_tables, &func.dfg.exception_tables);
675        if branch_dests.len() != 1 {
676            return Some((
677                func,
678                format!("did nothing for {block}"),
679                ProgressStatus::Skip,
680            ));
681        }
682
683        let branch_args = branch_dests[0]
684            .args(&func.dfg.value_lists)
685            .collect::<Vec<_>>();
686
687        // TODO: should we free the entity list associated with the block params?
688        let block_params = func
689            .dfg
690            .detach_block_params(block)
691            .as_slice(&func.dfg.value_lists)
692            .to_vec();
693
694        assert_eq!(block_params.len(), branch_args.len());
695
696        // If there were any block parameters in block, then the last instruction in pred will
697        // fill these parameters. Make the block params aliases of the terminator arguments.
698        for (block_param, arg) in block_params.into_iter().zip(branch_args) {
699            if let Some(arg) = arg.as_value() {
700                if block_param != arg {
701                    func.dfg.change_to_alias(block_param, arg);
702                }
703            }
704        }
705
706        // Remove the terminator branch to the current block.
707        func.layout.remove_inst(pred.inst);
708
709        // Move all the instructions to the predecessor.
710        while let Some(inst) = func.layout.first_inst(block) {
711            func.layout.remove_inst(inst);
712            func.layout.append_inst(inst, pred.block);
713        }
714
715        // Remove the predecessor block.
716        func.layout.remove_block(block);
717
718        // Record the previous block: if we caused a crash (as signaled by a call to did_crash), then
719        // we'll start back to this block.
720        self.prev_block = Some(pred.block);
721
722        Some((
723            func,
724            format!("merged {} and {}", pred.block, block),
725            ProgressStatus::ExpandedOrShrinked,
726        ))
727    }
728
729    fn did_crash(&mut self) {
730        self.block = self.prev_block.unwrap();
731    }
732}
733
734fn replace_with_const(pos: &mut FuncCursor, param: Value) -> &'static str {
735    let ty = pos.func.dfg.value_type(param);
736    if ty == F32 {
737        pos.ins().with_result(param).f32const(0.0);
738        "f32const"
739    } else if ty == F64 {
740        pos.ins().with_result(param).f64const(0.0);
741        "f64const"
742    } else if ty.is_vector() {
743        let zero_data = vec![0; ty.bytes() as usize].into();
744        let zero_handle = pos.func.dfg.constants.insert(zero_data);
745        pos.ins().with_result(param).vconst(ty, zero_handle);
746        "vconst"
747    } else if ty == I128 {
748        let res = pos.ins().iconst(I64, 0);
749        pos.ins().with_result(param).uextend(I128, res);
750        "iconst+uextend"
751    } else {
752        // Default to an integer type and possibly create verifier error
753        pos.ins().with_result(param).iconst(ty, 0);
754        "iconst"
755    }
756}
757
758fn next_inst_ret_prev(
759    func: &Function,
760    block: &mut Block,
761    inst: &mut Inst,
762) -> Option<(Block, Inst)> {
763    let prev = (*block, *inst);
764    if let Some(next_inst) = func.layout.next_inst(*inst) {
765        *inst = next_inst;
766        return Some(prev);
767    }
768    if let Some(next_block) = func.layout.next_block(*block) {
769        *block = next_block;
770        *inst = func.layout.first_inst(*block).expect("no inst");
771        return Some(prev);
772    }
773    None
774}
775
776fn block_count(func: &Function) -> usize {
777    func.layout.blocks().count()
778}
779
780fn inst_count(func: &Function) -> usize {
781    func.layout
782        .blocks()
783        .map(|block| func.layout.block_insts(block).count())
784        .sum()
785}
786
787/// Resolve aliases only if function still crashes after this.
788fn try_resolve_aliases(context: &mut CrashCheckContext, func: &mut Function) {
789    let mut func_with_resolved_aliases = func.clone();
790    func_with_resolved_aliases.dfg.resolve_all_aliases();
791    if let CheckResult::Crash(_) = context.check_for_crash(&func_with_resolved_aliases) {
792        *func = func_with_resolved_aliases;
793    }
794}
795
796/// Remove sourcelocs if the function still crashes after they are removed, to make the reduced clif IR easier to read.
797fn try_remove_srclocs(context: &mut CrashCheckContext, func: &mut Function) {
798    let mut func_with_removed_sourcelocs = func.clone();
799    func_with_removed_sourcelocs.srclocs.clear();
800    if let CheckResult::Crash(_) = context.check_for_crash(&func_with_removed_sourcelocs) {
801        *func = func_with_removed_sourcelocs;
802    }
803}
804
805fn reduce(isa: &dyn TargetIsa, mut func: Function, verbose: bool) -> Result<(Function, String)> {
806    let mut context = CrashCheckContext::new(isa);
807
808    if let CheckResult::Succeed = context.check_for_crash(&func) {
809        anyhow::bail!("Given function compiled successfully or gave a verifier error.");
810    }
811
812    try_resolve_aliases(&mut context, &mut func);
813    try_remove_srclocs(&mut context, &mut func);
814
815    for pass_idx in 0..100 {
816        let mut should_keep_reducing = false;
817        let mut phase = 0;
818
819        loop {
820            let mut mutator: Box<dyn Mutator> = match phase {
821                0 => Box::new(RemoveInst::new(&func)),
822                1 => Box::new(ReplaceInstWithConst::new(&func)),
823                2 => Box::new(ReplaceInstWithTrap::new(&func)),
824                3 => Box::new(MoveInstToEntryBlock::new(&func)),
825                4 => Box::new(RemoveBlock::new(&func)),
826                5 => Box::new(ReplaceBlockParamWithConst::new(&func)),
827                6 => Box::new(RemoveUnusedEntities::new()),
828                7 => Box::new(MergeBlocks::new(&func)),
829                _ => break,
830            };
831
832            println!("pass {} phase {}", pass_idx, mutator.name());
833
834            for _ in 0..10000 {
835                let (mutated_func, msg, mutation_kind) = match mutator.mutate(func.clone()) {
836                    Some(res) => res,
837                    None => {
838                        break;
839                    }
840                };
841
842                if let ProgressStatus::Skip = mutation_kind {
843                    // The mutator didn't change anything, but we want to try more mutator
844                    // iterations.
845                    continue;
846                }
847
848                match context.check_for_crash(&mutated_func) {
849                    CheckResult::Succeed => {
850                        // Mutating didn't hit the problem anymore, discard changes.
851                        continue;
852                    }
853                    CheckResult::Crash(_) => {
854                        // Panic remained while mutating, make changes definitive.
855                        func = mutated_func;
856
857                        // Notify the mutator that the mutation was successful.
858                        mutator.did_crash();
859
860                        let verb = match mutation_kind {
861                            ProgressStatus::ExpandedOrShrinked => {
862                                should_keep_reducing = true;
863                                "shrink"
864                            }
865                            ProgressStatus::Changed => "changed",
866                            ProgressStatus::Skip => unreachable!(),
867                        };
868                        if verbose {
869                            println!("{msg}: {verb}");
870                        }
871                    }
872                }
873            }
874
875            phase += 1;
876        }
877
878        println!(
879            "After pass {}, remaining insts/blocks: {}/{} ({})",
880            pass_idx,
881            inst_count(&func),
882            block_count(&func),
883            if should_keep_reducing {
884                "will keep reducing"
885            } else {
886                "stop reducing"
887            }
888        );
889
890        if !should_keep_reducing {
891            // No new shrinking opportunities have been found this pass. This means none will ever
892            // be found. Skip the rest of the passes over the function.
893            break;
894        }
895    }
896
897    try_resolve_aliases(&mut context, &mut func);
898
899    let crash_msg = match context.check_for_crash(&func) {
900        CheckResult::Succeed => unreachable!("Used to crash, but doesn't anymore???"),
901        CheckResult::Crash(crash_msg) => crash_msg,
902    };
903
904    Ok((func, crash_msg))
905}
906
907struct CrashCheckContext<'a> {
908    /// Cached `Context`, to prevent repeated allocation.
909    context: Context,
910
911    /// The target isa to compile for.
912    isa: &'a dyn TargetIsa,
913}
914
915fn get_panic_string(panic: Box<dyn std::any::Any>) -> String {
916    let panic = match panic.downcast::<&'static str>() {
917        Ok(panic_msg) => {
918            return panic_msg.to_string();
919        }
920        Err(panic) => panic,
921    };
922    match panic.downcast::<String>() {
923        Ok(panic_msg) => *panic_msg,
924        Err(_) => "Box<Any>".to_string(),
925    }
926}
927
928enum CheckResult {
929    /// The function compiled fine, or the verifier noticed an error.
930    Succeed,
931
932    /// The compilation of the function panicked.
933    Crash(String),
934}
935
936impl<'a> CrashCheckContext<'a> {
937    fn new(isa: &'a dyn TargetIsa) -> Self {
938        CrashCheckContext {
939            context: Context::new(),
940            isa,
941        }
942    }
943
944    fn check_for_crash(&mut self, func: &Function) -> CheckResult {
945        self.context.clear();
946
947        self.context.func = func.clone();
948
949        use std::io::Write;
950        std::io::stdout().flush().unwrap(); // Flush stdout to sync with panic messages on stderr
951
952        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
953            cranelift_codegen::verifier::verify_function(&func, self.isa).err()
954        })) {
955            Ok(Some(_)) => return CheckResult::Succeed,
956            Ok(None) => {}
957            // The verifier panicked. Compiling it will probably give the same panic.
958            // We treat it as succeeding to make it possible to reduce for the actual error.
959            // FIXME prevent verifier panic on removing block0.
960            Err(_) => return CheckResult::Succeed,
961        }
962
963        #[cfg(test)]
964        if true {
965            // For testing purposes we emulate a panic caused by the existence of
966            // a `call` instruction.
967            let contains_call = func.layout.blocks().any(|block| {
968                func.layout
969                    .block_insts(block)
970                    .any(|inst| match func.dfg.insts[inst] {
971                        InstructionData::Call { .. } => true,
972                        _ => false,
973                    })
974            });
975            if contains_call {
976                return CheckResult::Crash("test crash".to_string());
977            } else {
978                return CheckResult::Succeed;
979            }
980        }
981
982        let old_panic_hook = std::panic::take_hook();
983        std::panic::set_hook(Box::new(|_| {})); // silence panics
984
985        let res = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
986            let _ = self.context.compile(self.isa, &mut Default::default());
987        })) {
988            Ok(()) => CheckResult::Succeed,
989            Err(err) => CheckResult::Crash(get_panic_string(err)),
990        };
991
992        std::panic::set_hook(old_panic_hook);
993
994        res
995    }
996}
997
998#[cfg(test)]
999mod tests {
1000    use super::*;
1001
1002    fn run_test(test_str: &str, expected_str: &str) {
1003        let test_file = parse_test(test_str, ParseOptions::default()).unwrap();
1004
1005        // If we have an isa from the command-line, use that. Otherwise if the
1006        // file contains a unique isa, use that.
1007        let isa = test_file.isa_spec.unique_isa().expect("Unknown isa");
1008
1009        for (func, _) in test_file.functions {
1010            let (reduced_func, crash_msg) =
1011                reduce(isa, func, false).expect("Couldn't reduce test case");
1012            assert_eq!(crash_msg, "test crash");
1013
1014            let (func_reduced_twice, crash_msg) =
1015                reduce(isa, reduced_func.clone(), false).expect("Couldn't re-reduce test case");
1016            assert_eq!(crash_msg, "test crash");
1017
1018            assert_eq!(
1019                block_count(&func_reduced_twice),
1020                block_count(&reduced_func),
1021                "reduction wasn't maximal for blocks"
1022            );
1023            assert_eq!(
1024                inst_count(&func_reduced_twice),
1025                inst_count(&reduced_func),
1026                "reduction wasn't maximal for insts"
1027            );
1028
1029            let actual_ir = format!("{reduced_func}");
1030            let expected_ir = expected_str.replace("\r\n", "\n");
1031            assert!(
1032                expected_ir == actual_ir,
1033                "Expected:\n{expected_ir}\nGot:\n{actual_ir}",
1034            );
1035        }
1036    }
1037
1038    #[test]
1039    fn test_reduce() {
1040        const TEST: &str = include_str!("../tests/bugpoint_test.clif");
1041        const EXPECTED: &str = include_str!("../tests/bugpoint_test_expected.clif");
1042        run_test(TEST, EXPECTED);
1043    }
1044
1045    #[test]
1046    fn test_consts() {
1047        const TEST: &str = include_str!("../tests/bugpoint_consts.clif");
1048        const EXPECTED: &str = include_str!("../tests/bugpoint_consts_expected.clif");
1049        run_test(TEST, EXPECTED);
1050    }
1051}