Add config variables for printing IR during mono stages

This commit is contained in:
ayazhafiz 2022-01-11 18:40:32 -05:00
parent e655ab7d3b
commit 8d372edda1
3 changed files with 57 additions and 17 deletions

View file

@ -164,3 +164,24 @@ The compiler is invoked from the CLI via `build_file` in cli/src/build.rs
| Code gen (unoptimized but fast, Wasm) | gen_wasm/src/lib.rs: build_module |
For a more detailed understanding of the compilation phases, see the `Phase`, `BuildTask`, and `Msg` enums in `load/src/file.rs`.
## Debugging intermediate representations
### The mono IR
If you observe a miscomplication, you may first want to check the generated mono
IR for your code - maybe there was a problem during specialization or layout
generation. One way to do this is to add a test to `test_mono/src/tests.rs`
and run the tests with `cargo test -p test_mono`; this will write the mono
IR to a file.
You may also want to set some or all of the following environment variables:
- `PRINT_IR_AFTER_SPECIALIZATION=1` prints the mono IR after function
specialization to stdout
- `PRINT_IR_AFTER_RESET_REUSE=1` prints the mono IR after insertion of
reset/reuse isntructions to stdout
- `PRINT_IR_AFTER_REFCOUNT=1` prints the mono IR after insertion of reference
counting instructions to stdout
- `PRETTY_PRINT_IR_SYMBOLS=1` instructs the pretty printer to dump all the
information it knows about the mono IR whenever it is printed

View file

@ -1647,6 +1647,23 @@ fn start_tasks<'a>(
Ok(())
}
#[cfg(debug_assertions)]
fn debug_print_ir(state: &State, flag: &str) {
if env::var(flag) != Ok("1".into()) {
return;
}
let procs_string = state
.procedures
.values()
.map(|proc| proc.to_pretty(200))
.collect::<Vec<_>>();
let result = procs_string.join("\n");
println!("{}", result);
}
fn update<'a>(
mut state: State<'a>,
msg: Msg<'a>,
@ -2075,6 +2092,9 @@ fn update<'a>(
&& state.dependencies.solved_all()
&& state.goal_phase == Phase::MakeSpecializations
{
#[cfg(debug_assertions)]
debug_print_ir(&state, "PRINT_IR_AFTER_SPECIALIZATION");
Proc::insert_reset_reuse_operations(
arena,
module_id,
@ -2083,21 +2103,14 @@ fn update<'a>(
&mut state.procedures,
);
// Uncomment to display the mono IR of the module, for debug purposes
if false {
let procs_string = state
.procedures
.values()
.map(|proc| proc.to_pretty(200))
.collect::<Vec<_>>();
let result = procs_string.join("\n");
println!("{}", result);
}
#[cfg(debug_assertions)]
debug_print_ir(&state, "PRINT_IR_AFTER_RESET_REUSE");
Proc::insert_refcount_operations(arena, &mut state.procedures);
#[cfg(debug_assertions)]
debug_print_ir(&state, "PRINT_IR_AFTER_REFCOUNT");
// This is not safe with the new non-recursive RC updates that we do for tag unions
//
// Proc::optimize_refcount_operations(

View file

@ -20,7 +20,13 @@ use roc_types::subs::{Content, FlatType, StorageSubs, Subs, Variable, VariableSu
use std::collections::HashMap;
use ven_pretty::{BoxAllocator, DocAllocator, DocBuilder};
pub const PRETTY_PRINT_IR_SYMBOLS: bool = false;
fn pretty_print_ir_symbols() -> bool {
#[cfg(debug_assertions)]
if std::env::var("PRETTY_PRINT_IR_SYMBOLS") == Ok("1".into()) {
return true;
}
return false;
}
// if your changes cause this number to go down, great!
// please change it to the lower number.
@ -268,7 +274,7 @@ impl<'a> Proc<'a> {
.iter()
.map(|(_, symbol)| symbol_to_doc(alloc, *symbol));
if PRETTY_PRINT_IR_SYMBOLS {
if pretty_print_ir_symbols() {
alloc
.text("procedure : ")
.append(symbol_to_doc(alloc, self.name))
@ -1119,7 +1125,7 @@ impl<'a> BranchInfo<'a> {
tag_id,
scrutinee,
layout: _,
} if PRETTY_PRINT_IR_SYMBOLS => alloc
} if pretty_print_ir_symbols() => alloc
.hardline()
.append(" BranchInfo: { scrutinee: ")
.append(symbol_to_doc(alloc, *scrutinee))
@ -1128,7 +1134,7 @@ impl<'a> BranchInfo<'a> {
.append("} "),
_ => {
if PRETTY_PRINT_IR_SYMBOLS {
if pretty_print_ir_symbols() {
alloc.text(" <no branch info>")
} else {
alloc.text("")
@ -1463,7 +1469,7 @@ where
{
use roc_module::ident::ModuleName;
if PRETTY_PRINT_IR_SYMBOLS {
if pretty_print_ir_symbols() {
alloc.text(format!("{:?}", symbol))
} else {
let text = format!("{}", symbol);