Skip to content

Commit ec456fc

Browse files
Receive "only print when ir has been modified" as a CLI parameter only_when_modified
Signed-off-by: Tomas Fabrizio Orsi <[email protected]>
1 parent 9dc31b9 commit ec456fc

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

hir/src/pass.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Print {
2727
filter: OpFilter,
2828
pass_filter: PassFilter,
2929
target: Option<compact_str::CompactString>,
30+
only_when_modified: bool,
3031
}
3132

3233
/// Filter for the different passes.
@@ -61,6 +62,7 @@ impl Print {
6162
filter: OpFilter::All,
6263
pass_filter: PassFilter::All,
6364
target: None,
65+
only_when_modified: false,
6466
}
6567
}
6668

@@ -72,22 +74,31 @@ impl Print {
7274
filter: OpFilter::Type { dialect, op },
7375
pass_filter: PassFilter::All,
7476
target: None,
77+
only_when_modified: false,
7578
}
7679
}
7780

81+
// pub fn from_config(config: IRPrintingConfig) {
82+
// if config.print
83+
// }
7884
/// Adds a PassFilter to Print. IR will only be printed before and after those passes are
7985
/// executed.
8086
pub fn with_pass_filter(mut self, passes: Vec<String>) -> Self {
8187
self.pass_filter = PassFilter::Certain(passes);
8288
self
8389
}
8490

91+
pub fn with_only_print_when_modified(&mut self) {
92+
self.only_when_modified = true;
93+
}
94+
8595
/// Create a printer that only prints `Symbol` operations containing `name`
8696
pub fn symbol_matching(name: &'static str) -> Self {
8797
Self {
8898
filter: OpFilter::Symbol(Some(name)),
8999
pass_filter: PassFilter::All,
90100
target: None,
101+
only_when_modified: false,
91102
}
92103
}
93104

@@ -105,7 +116,7 @@ impl Print {
105116
match self.filter {
106117
OpFilter::All => {
107118
let target = self.target.as_deref().unwrap_or("printer");
108-
log::trace!(target: target, "{op}");
119+
log::error!(target: target, "{op}");
109120
}
110121
OpFilter::Type {
111122
dialect,
@@ -114,21 +125,21 @@ impl Print {
114125
let name = op.name();
115126
if name.dialect() == dialect && name.name() == op_name {
116127
let target = self.target.as_deref().unwrap_or("printer");
117-
log::trace!(target: target, "{op}");
128+
log::error!(target: target, "{op}");
118129
}
119130
}
120131
OpFilter::Symbol(None) => {
121132
if let Some(sym) = op.as_symbol() {
122133
let name = sym.name().as_str();
123134
let target = self.target.as_deref().unwrap_or(name);
124-
log::trace!(target: target, "{}", sym.as_symbol_operation());
135+
log::error!(target: target, "{}", sym.as_symbol_operation());
125136
}
126137
}
127138
OpFilter::Symbol(Some(filter)) => {
128139
if let Some(sym) = op.as_symbol().filter(|sym| sym.name().as_str().contains(filter))
129140
{
130141
let target = self.target.as_deref().unwrap_or(filter);
131-
log::trace!(target: target, "{}", sym.as_symbol_operation());
142+
log::error!(target: target, "{}", sym.as_symbol_operation());
132143
}
133144
}
134145
}
@@ -165,14 +176,35 @@ impl Pass for Print {
165176
}
166177

167178
impl PassInstrumentation for Print {
168-
fn run_after_pass(&mut self, pass: &dyn OperationPass, op: &OperationRef, _changed: bool) {
169-
if self.should_print(pass) {
179+
fn run_after_pass(&mut self, pass: &dyn OperationPass, op: &OperationRef, changed: bool) {
180+
std::println!(
181+
"
182+
------------------------------------DESPUES:\
183+
{}-------------------------------------------------------
184+
",
185+
pass.name()
186+
);
187+
std::println!("RESULTADO DE CHANGED: {}", changed);
188+
#[allow(clippy::needless_bool)]
189+
// Always print, unless "only_when_modified" has been set and there have not been changes.
190+
let print_when_changed = if self.only_when_modified && !changed {
191+
false
192+
} else {
193+
true
194+
};
195+
if self.should_print(pass) && print_when_changed {
170196
let op = op.borrow();
171197
self.print_ir(op);
172198
}
173199
}
174200

175201
fn run_before_pass(&mut self, pass: &dyn OperationPass, op: &OperationRef) {
202+
std::println!(
203+
"
204+
------------------------------------ANTES:{}-------------------------------------------------------
205+
",
206+
pass.name()
207+
);
176208
if self.should_print(pass) {
177209
let op = op.borrow();
178210
self.print_ir(op);

hir/src/pass/manager.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ pub struct IRPrintingConfig {
4444
// NOTE: Taken from the Options struct
4545
print_ir_after_all: bool,
4646
print_ir_after_pass: Vec<String>,
47+
print_ir_after_modified: bool,
4748
flags: OpPrintingFlags,
4849
}
4950

5051
impl From<&Options> for IRPrintingConfig {
5152
fn from(options: &Options) -> Self {
5253
let mut irprintconfig = IRPrintingConfig::default();
54+
5355
irprintconfig.print_ir_after_all = options.print_ir_after_all;
5456
irprintconfig.print_ir_after_pass = options.print_ir_after_pass.clone();
57+
irprintconfig.print_ir_after_modified = options.print_ir_after_modified;
5558

5659
irprintconfig
5760
}
@@ -199,7 +202,12 @@ impl PassManager {
199202
None
200203
};
201204

202-
if let Some(print) = print {
205+
//TODO: Refactor this
206+
if let Some(mut print) = print {
207+
if config.print_ir_after_modified {
208+
std::dbg!("ONLY WHEN MODIFIED");
209+
print.with_only_print_when_modified();
210+
}
203211
let print = Box::new(print);
204212
self.add_instrumentation(print);
205213
}

midenc-compile/src/compiler.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ pub struct UnstableOptions {
339339
)
340340
)]
341341
pub print_ir_after_pass: Vec<String>,
342+
/// Only print the IR if the pass modified the IR structure.
343+
#[cfg_attr(
344+
feature = "std",
345+
arg(long, default_value_t = false, help_heading = "Passes")
346+
)]
347+
pub print_ir_after_modified: bool,
342348
}
343349

344350
impl CodegenOptions {
@@ -505,6 +511,7 @@ impl Compiler {
505511
options.print_cfg_after_pass = unstable.print_cfg_after_pass;
506512
options.print_ir_after_all = unstable.print_ir_after_all;
507513
options.print_ir_after_pass = unstable.print_ir_after_pass;
514+
options.print_ir_after_modified = unstable.print_ir_after_modified;
508515

509516
// Establish --target-dir
510517
let target_dir = if self.target_dir.is_absolute() {

midenc-session/src/options/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub struct Options {
5252
pub print_ir_after_all: bool,
5353
/// Print IR to stdout each time the named passes are applied
5454
pub print_ir_after_pass: Vec<String>,
55+
/// Only print the IR if the pass modified the IR structure.
56+
pub print_ir_after_modified: bool,
5557
/// Save intermediate artifacts in memory during compilation
5658
pub save_temps: bool,
5759
/// We store any leftover argument matches in the session options for use
@@ -126,6 +128,7 @@ impl Options {
126128
print_cfg_after_pass: vec![],
127129
print_ir_after_all: false,
128130
print_ir_after_pass: vec![],
131+
print_ir_after_modified: false,
129132
flags: CompileFlags::default(),
130133
}
131134
}

0 commit comments

Comments
 (0)