1 //! CLI tool to reduce Cranelift IR files crashing during compilation. 2 3 use crate::utils::read_to_string; 4 use anyhow::{Context as _, Result}; 5 use clap::Parser; 6 use cranelift::prelude::Value; 7 use cranelift_codegen::Context; 8 use cranelift_codegen::cursor::{Cursor, FuncCursor}; 9 use cranelift_codegen::flowgraph::ControlFlowGraph; 10 use cranelift_codegen::ir::types::{F32, F64, I64, I128}; 11 use cranelift_codegen::ir::{ 12 self, Block, FuncRef, Function, GlobalValueData, Inst, InstBuilder, InstructionData, 13 StackSlots, TrapCode, 14 }; 15 use cranelift_codegen::isa::TargetIsa; 16 use cranelift_entity::PrimaryMap; 17 use cranelift_reader::{ParseOptions, parse_sets_and_triple, parse_test}; 18 use std::collections::HashMap; 19 use std::path::PathBuf; 20 21 /// Reduce size of clif file causing panic during compilation. 22 #[derive(Parser)] 23 pub 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 39 pub 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 83 enum 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 96 trait 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. 106 struct RemoveInst { 107 block: Block, 108 inst: Inst, 109 } 110 111 impl 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 122 impl 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`. 143 struct ReplaceInstWithConst { 144 block: Block, 145 inst: Inst, 146 } 147 148 impl 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 159 impl 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`. 233 struct ReplaceInstWithTrap { 234 block: Block, 235 inst: Inst, 236 } 237 238 impl 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 249 impl 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.dfg.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. 270 struct MoveInstToEntryBlock { 271 block: Block, 272 inst: Inst, 273 } 274 275 impl 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 286 impl 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. 318 struct RemoveBlock { 319 block: Block, 320 } 321 322 impl RemoveBlock { 323 fn new(func: &Function) -> Self { 324 Self { 325 block: func.layout.entry_block().unwrap(), 326 } 327 } 328 } 329 330 impl 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. 352 struct ReplaceBlockParamWithConst { 353 block: Block, 354 params_remaining: usize, 355 } 356 357 impl 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 367 impl 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. 420 struct RemoveUnusedEntities { 421 kind: u32, 422 } 423 424 impl RemoveUnusedEntities { 425 fn new() -> Self { 426 Self { kind: 0 } 427 } 428 } 429 430 impl 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::StackLoad { stack_slot, .. } 543 | InstructionData::StackStore { stack_slot, .. } => { 544 stack_slot_usage_map 545 .entry(stack_slot) 546 .or_insert_with(Vec::new) 547 .push(inst); 548 } 549 550 _ => {} 551 } 552 } 553 } 554 555 let mut stack_slots = StackSlots::new(); 556 557 for (stack_slot, stack_slot_data) in func.sized_stack_slots.clone().iter() { 558 if let Some(stack_slot_usage) = stack_slot_usage_map.get(&stack_slot) { 559 let new_stack_slot = stack_slots.push(stack_slot_data.clone()); 560 for &inst in stack_slot_usage { 561 match &mut func.dfg.insts[inst] { 562 // Keep in sync with the above match. 563 InstructionData::StackLoad { stack_slot, .. } 564 | InstructionData::StackStore { stack_slot, .. } => { 565 *stack_slot = new_stack_slot; 566 } 567 _ => unreachable!(), 568 } 569 } 570 } 571 } 572 573 func.sized_stack_slots = stack_slots; 574 575 "Remove unused stack slots" 576 } 577 3 => { 578 let mut global_value_usage_map = HashMap::new(); 579 for block in func.layout.blocks() { 580 for inst in func.layout.block_insts(block) { 581 // Add new cases when there are new instruction formats taking a `GlobalValue`. 582 if let InstructionData::UnaryGlobalValue { global_value, .. } = 583 func.dfg.insts[inst] 584 { 585 global_value_usage_map 586 .entry(global_value) 587 .or_insert_with(Vec::new) 588 .push(inst); 589 } 590 } 591 } 592 593 for (_global_value, global_value_data) in func.global_values.iter() { 594 match *global_value_data { 595 GlobalValueData::VMContext | GlobalValueData::Symbol { .. } => {} 596 // These can create cyclic references, which cause complications. Just skip 597 // the global value removal for now. 598 // FIXME Handle them in a better way. 599 GlobalValueData::Load { .. } 600 | GlobalValueData::IAddImm { .. } 601 | GlobalValueData::DynScaleTargetConst { .. } => return None, 602 } 603 } 604 605 let mut global_values = PrimaryMap::new(); 606 607 for (global_value, global_value_data) in func.global_values.clone().into_iter() { 608 if let Some(global_value_usage) = global_value_usage_map.get(&global_value) { 609 let new_global_value = global_values.push(global_value_data.clone()); 610 for &inst in global_value_usage { 611 match &mut func.dfg.insts[inst] { 612 // Keep in sync with the above match. 613 InstructionData::UnaryGlobalValue { global_value, .. } => { 614 *global_value = new_global_value; 615 } 616 _ => unreachable!(), 617 } 618 } 619 } 620 } 621 622 func.global_values = global_values; 623 624 "Remove unused global values" 625 } 626 _ => return None, 627 }; 628 self.kind += 1; 629 Some((func, name.to_owned(), ProgressStatus::Changed)) 630 } 631 } 632 633 struct MergeBlocks { 634 block: Block, 635 prev_block: Option<Block>, 636 } 637 638 impl MergeBlocks { 639 fn new(func: &Function) -> Self { 640 Self { 641 block: func.layout.entry_block().unwrap(), 642 prev_block: None, 643 } 644 } 645 } 646 647 impl Mutator for MergeBlocks { 648 fn name(&self) -> &'static str { 649 "merge blocks" 650 } 651 652 fn mutate(&mut self, mut func: Function) -> Option<(Function, String, ProgressStatus)> { 653 let block = match func.layout.next_block(self.block) { 654 Some(block) => block, 655 None => return None, 656 }; 657 658 self.block = block; 659 660 let mut cfg = ControlFlowGraph::new(); 661 cfg.compute(&func); 662 663 if cfg.pred_iter(block).count() != 1 { 664 return Some(( 665 func, 666 format!("did nothing for {block}"), 667 ProgressStatus::Skip, 668 )); 669 } 670 671 let pred = cfg.pred_iter(block).next().unwrap(); 672 673 // If the branch instruction that lead us to this block wasn't an unconditional jump, then 674 // we have a conditional jump sequence that we should not break. 675 let branch_dests = func.dfg.insts[pred.inst] 676 .branch_destination(&func.dfg.jump_tables, &func.dfg.exception_tables); 677 if branch_dests.len() != 1 { 678 return Some(( 679 func, 680 format!("did nothing for {block}"), 681 ProgressStatus::Skip, 682 )); 683 } 684 685 let branch_args = branch_dests[0] 686 .args(&func.dfg.value_lists) 687 .collect::<Vec<_>>(); 688 689 // TODO: should we free the entity list associated with the block params? 690 let block_params = func 691 .dfg 692 .detach_block_params(block) 693 .as_slice(&func.dfg.value_lists) 694 .to_vec(); 695 696 assert_eq!(block_params.len(), branch_args.len()); 697 698 // If there were any block parameters in block, then the last instruction in pred will 699 // fill these parameters. Make the block params aliases of the terminator arguments. 700 for (block_param, arg) in block_params.into_iter().zip(branch_args) { 701 if let Some(arg) = arg.as_value() { 702 if block_param != arg { 703 func.dfg.change_to_alias(block_param, arg); 704 } 705 } 706 } 707 708 // Remove the terminator branch to the current block. 709 func.layout.remove_inst(pred.inst); 710 711 // Move all the instructions to the predecessor. 712 while let Some(inst) = func.layout.first_inst(block) { 713 func.layout.remove_inst(inst); 714 func.layout.append_inst(inst, pred.block); 715 } 716 717 // Remove the predecessor block. 718 func.layout.remove_block(block); 719 720 // Record the previous block: if we caused a crash (as signaled by a call to did_crash), then 721 // we'll start back to this block. 722 self.prev_block = Some(pred.block); 723 724 Some(( 725 func, 726 format!("merged {} and {}", pred.block, block), 727 ProgressStatus::ExpandedOrShrinked, 728 )) 729 } 730 731 fn did_crash(&mut self) { 732 self.block = self.prev_block.unwrap(); 733 } 734 } 735 736 fn replace_with_const(pos: &mut FuncCursor, param: Value) -> &'static str { 737 let ty = pos.func.dfg.value_type(param); 738 if ty == F32 { 739 pos.ins().with_result(param).f32const(0.0); 740 "f32const" 741 } else if ty == F64 { 742 pos.ins().with_result(param).f64const(0.0); 743 "f64const" 744 } else if ty.is_vector() { 745 let zero_data = vec![0; ty.bytes() as usize].into(); 746 let zero_handle = pos.func.dfg.constants.insert(zero_data); 747 pos.ins().with_result(param).vconst(ty, zero_handle); 748 "vconst" 749 } else if ty == I128 { 750 let res = pos.ins().iconst(I64, 0); 751 pos.ins().with_result(param).uextend(I128, res); 752 "iconst+uextend" 753 } else { 754 // Default to an integer type and possibly create verifier error 755 pos.ins().with_result(param).iconst(ty, 0); 756 "iconst" 757 } 758 } 759 760 fn next_inst_ret_prev( 761 func: &Function, 762 block: &mut Block, 763 inst: &mut Inst, 764 ) -> Option<(Block, Inst)> { 765 let prev = (*block, *inst); 766 if let Some(next_inst) = func.layout.next_inst(*inst) { 767 *inst = next_inst; 768 return Some(prev); 769 } 770 if let Some(next_block) = func.layout.next_block(*block) { 771 *block = next_block; 772 *inst = func.layout.first_inst(*block).expect("no inst"); 773 return Some(prev); 774 } 775 None 776 } 777 778 fn block_count(func: &Function) -> usize { 779 func.layout.blocks().count() 780 } 781 782 fn inst_count(func: &Function) -> usize { 783 func.layout 784 .blocks() 785 .map(|block| func.layout.block_insts(block).count()) 786 .sum() 787 } 788 789 /// Resolve aliases only if function still crashes after this. 790 fn try_resolve_aliases(context: &mut CrashCheckContext, func: &mut Function) { 791 let mut func_with_resolved_aliases = func.clone(); 792 func_with_resolved_aliases.dfg.resolve_all_aliases(); 793 if let CheckResult::Crash(_) = context.check_for_crash(&func_with_resolved_aliases) { 794 *func = func_with_resolved_aliases; 795 } 796 } 797 798 /// Remove sourcelocs if the function still crashes after they are removed, to make the reduced clif IR easier to read. 799 fn try_remove_srclocs(context: &mut CrashCheckContext, func: &mut Function) { 800 let mut func_with_removed_sourcelocs = func.clone(); 801 func_with_removed_sourcelocs.srclocs.clear(); 802 if let CheckResult::Crash(_) = context.check_for_crash(&func_with_removed_sourcelocs) { 803 *func = func_with_removed_sourcelocs; 804 } 805 } 806 807 fn reduce(isa: &dyn TargetIsa, mut func: Function, verbose: bool) -> Result<(Function, String)> { 808 let mut context = CrashCheckContext::new(isa); 809 810 if let CheckResult::Succeed = context.check_for_crash(&func) { 811 anyhow::bail!("Given function compiled successfully or gave a verifier error."); 812 } 813 814 try_resolve_aliases(&mut context, &mut func); 815 try_remove_srclocs(&mut context, &mut func); 816 817 for pass_idx in 0..100 { 818 let mut should_keep_reducing = false; 819 let mut phase = 0; 820 821 loop { 822 let mut mutator: Box<dyn Mutator> = match phase { 823 0 => Box::new(RemoveInst::new(&func)), 824 1 => Box::new(ReplaceInstWithConst::new(&func)), 825 2 => Box::new(ReplaceInstWithTrap::new(&func)), 826 3 => Box::new(MoveInstToEntryBlock::new(&func)), 827 4 => Box::new(RemoveBlock::new(&func)), 828 5 => Box::new(ReplaceBlockParamWithConst::new(&func)), 829 6 => Box::new(RemoveUnusedEntities::new()), 830 7 => Box::new(MergeBlocks::new(&func)), 831 _ => break, 832 }; 833 834 println!("pass {} phase {}", pass_idx, mutator.name()); 835 836 for _ in 0..10000 { 837 let (mutated_func, msg, mutation_kind) = match mutator.mutate(func.clone()) { 838 Some(res) => res, 839 None => { 840 break; 841 } 842 }; 843 844 if let ProgressStatus::Skip = mutation_kind { 845 // The mutator didn't change anything, but we want to try more mutator 846 // iterations. 847 continue; 848 } 849 850 match context.check_for_crash(&mutated_func) { 851 CheckResult::Succeed => { 852 // Mutating didn't hit the problem anymore, discard changes. 853 continue; 854 } 855 CheckResult::Crash(_) => { 856 // Panic remained while mutating, make changes definitive. 857 func = mutated_func; 858 859 // Notify the mutator that the mutation was successful. 860 mutator.did_crash(); 861 862 let verb = match mutation_kind { 863 ProgressStatus::ExpandedOrShrinked => { 864 should_keep_reducing = true; 865 "shrink" 866 } 867 ProgressStatus::Changed => "changed", 868 ProgressStatus::Skip => unreachable!(), 869 }; 870 if verbose { 871 println!("{msg}: {verb}"); 872 } 873 } 874 } 875 } 876 877 phase += 1; 878 } 879 880 println!( 881 "After pass {}, remaining insts/blocks: {}/{} ({})", 882 pass_idx, 883 inst_count(&func), 884 block_count(&func), 885 if should_keep_reducing { 886 "will keep reducing" 887 } else { 888 "stop reducing" 889 } 890 ); 891 892 if !should_keep_reducing { 893 // No new shrinking opportunities have been found this pass. This means none will ever 894 // be found. Skip the rest of the passes over the function. 895 break; 896 } 897 } 898 899 try_resolve_aliases(&mut context, &mut func); 900 901 let crash_msg = match context.check_for_crash(&func) { 902 CheckResult::Succeed => unreachable!("Used to crash, but doesn't anymore???"), 903 CheckResult::Crash(crash_msg) => crash_msg, 904 }; 905 906 Ok((func, crash_msg)) 907 } 908 909 struct CrashCheckContext<'a> { 910 /// Cached `Context`, to prevent repeated allocation. 911 context: Context, 912 913 /// The target isa to compile for. 914 isa: &'a dyn TargetIsa, 915 } 916 917 fn get_panic_string(panic: Box<dyn std::any::Any>) -> String { 918 let panic = match panic.downcast::<&'static str>() { 919 Ok(panic_msg) => { 920 return panic_msg.to_string(); 921 } 922 Err(panic) => panic, 923 }; 924 match panic.downcast::<String>() { 925 Ok(panic_msg) => *panic_msg, 926 Err(_) => "Box<Any>".to_string(), 927 } 928 } 929 930 enum CheckResult { 931 /// The function compiled fine, or the verifier noticed an error. 932 Succeed, 933 934 /// The compilation of the function panicked. 935 Crash(String), 936 } 937 938 impl<'a> CrashCheckContext<'a> { 939 fn new(isa: &'a dyn TargetIsa) -> Self { 940 CrashCheckContext { 941 context: Context::new(), 942 isa, 943 } 944 } 945 946 fn check_for_crash(&mut self, func: &Function) -> CheckResult { 947 self.context.clear(); 948 949 self.context.func = func.clone(); 950 951 use std::io::Write; 952 std::io::stdout().flush().unwrap(); // Flush stdout to sync with panic messages on stderr 953 954 match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { 955 cranelift_codegen::verifier::verify_function(&func, self.isa).err() 956 })) { 957 Ok(Some(_)) => return CheckResult::Succeed, 958 Ok(None) => {} 959 // The verifier panicked. Compiling it will probably give the same panic. 960 // We treat it as succeeding to make it possible to reduce for the actual error. 961 // FIXME prevent verifier panic on removing block0. 962 Err(_) => return CheckResult::Succeed, 963 } 964 965 #[cfg(test)] 966 if true { 967 // For testing purposes we emulate a panic caused by the existence of 968 // a `call` instruction. 969 let contains_call = func.layout.blocks().any(|block| { 970 func.layout 971 .block_insts(block) 972 .any(|inst| match func.dfg.insts[inst] { 973 InstructionData::Call { .. } => true, 974 _ => false, 975 }) 976 }); 977 if contains_call { 978 return CheckResult::Crash("test crash".to_string()); 979 } else { 980 return CheckResult::Succeed; 981 } 982 } 983 984 let old_panic_hook = std::panic::take_hook(); 985 std::panic::set_hook(Box::new(|_| {})); // silence panics 986 987 let res = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { 988 let _ = self.context.compile(self.isa, &mut Default::default()); 989 })) { 990 Ok(()) => CheckResult::Succeed, 991 Err(err) => CheckResult::Crash(get_panic_string(err)), 992 }; 993 994 std::panic::set_hook(old_panic_hook); 995 996 res 997 } 998 } 999 1000 #[cfg(test)] 1001 mod tests { 1002 use super::*; 1003 1004 fn run_test(test_str: &str, expected_str: &str) { 1005 let test_file = parse_test(test_str, ParseOptions::default()).unwrap(); 1006 1007 // If we have an isa from the command-line, use that. Otherwise if the 1008 // file contains a unique isa, use that. 1009 let isa = test_file.isa_spec.unique_isa().expect("Unknown isa"); 1010 1011 for (func, _) in test_file.functions { 1012 let (reduced_func, crash_msg) = 1013 reduce(isa, func, false).expect("Couldn't reduce test case"); 1014 assert_eq!(crash_msg, "test crash"); 1015 1016 let (func_reduced_twice, crash_msg) = 1017 reduce(isa, reduced_func.clone(), false).expect("Couldn't re-reduce test case"); 1018 assert_eq!(crash_msg, "test crash"); 1019 1020 assert_eq!( 1021 block_count(&func_reduced_twice), 1022 block_count(&reduced_func), 1023 "reduction wasn't maximal for blocks" 1024 ); 1025 assert_eq!( 1026 inst_count(&func_reduced_twice), 1027 inst_count(&reduced_func), 1028 "reduction wasn't maximal for insts" 1029 ); 1030 1031 let actual_ir = format!("{reduced_func}"); 1032 let expected_ir = expected_str.replace("\r\n", "\n"); 1033 assert!( 1034 expected_ir == actual_ir, 1035 "Expected:\n{expected_ir}\nGot:\n{actual_ir}", 1036 ); 1037 } 1038 } 1039 1040 #[test] 1041 fn test_reduce() { 1042 const TEST: &str = include_str!("../tests/bugpoint_test.clif"); 1043 const EXPECTED: &str = include_str!("../tests/bugpoint_test_expected.clif"); 1044 run_test(TEST, EXPECTED); 1045 } 1046 1047 #[test] 1048 fn test_consts() { 1049 const TEST: &str = include_str!("../tests/bugpoint_consts.clif"); 1050 const EXPECTED: &str = include_str!("../tests/bugpoint_consts_expected.clif"); 1051 run_test(TEST, EXPECTED); 1052 } 1053 } 1054