Skip to content

Commit 4837727

Browse files
authored
Merge pull request #494 from lambdaclass/print-ir-passmanager
2 parents 553a2b4 + 71cad3a commit 4837727

File tree

14 files changed

+357
-95
lines changed

14 files changed

+357
-95
lines changed

dialects/hir/src/transforms/spill.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use alloc::rc::Rc;
33
use midenc_hir::{
44
adt::SmallDenseMap,
55
dialects::builtin::{Function, FunctionRef, LocalVariable},
6-
pass::{Pass, PassExecutionState},
6+
pass::{Pass, PassExecutionState, PostPassStatus},
77
BlockRef, BuilderExt, EntityMut, Op, OpBuilder, OperationName, OperationRef, Report, Rewriter,
88
SourceSpan, Spanned, Symbol, ValueRef,
99
};
@@ -38,6 +38,7 @@ impl Pass for TransformSpills {
3838
if function.is_declaration() {
3939
log::debug!(target: "insert-spills", "function has no body, no spills needed!");
4040
state.preserved_analyses_mut().preserve_all();
41+
state.set_post_pass_status(PostPassStatus::Unchanged);
4142
return Ok(());
4243
}
4344
let mut analysis =
@@ -46,6 +47,7 @@ impl Pass for TransformSpills {
4647
if !analysis.has_spills() {
4748
log::debug!(target: "insert-spills", "no spills needed!");
4849
state.preserved_analyses_mut().preserve_all();
50+
state.set_post_pass_status(PostPassStatus::Unchanged);
4951
return Ok(());
5052
}
5153

@@ -62,7 +64,17 @@ impl Pass for TransformSpills {
6264

6365
let op = function.as_operation_ref();
6466
drop(function);
65-
transforms::transform_spills(op, analysis, &mut interface, state.analysis_manager().clone())
67+
68+
let transform_result = transforms::transform_spills(
69+
op,
70+
analysis,
71+
&mut interface,
72+
state.analysis_manager().clone(),
73+
)?;
74+
75+
state.set_post_pass_status(transform_result);
76+
77+
Ok(())
6678
}
6779
}
6880

dialects/scf/src/transforms/cfg_to_scf.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use midenc_hir::{
77
diagnostics::Severity,
88
dialects::builtin,
99
dominance::DominanceInfo,
10-
pass::{Pass, PassExecutionState},
10+
pass::{Pass, PassExecutionState, PostPassStatus},
1111
Builder, EntityMut, Forward, Op, Operation, OperationName, OperationRef, RawWalk, Report,
1212
SmallVec, Spanned, Type, ValueRange, ValueRef, WalkResult,
1313
};
@@ -130,6 +130,7 @@ impl Pass for LiftControlFlowToSCF {
130130
});
131131

132132
if result.was_interrupted() {
133+
state.set_post_pass_status(PostPassStatus::Unchanged);
133134
return result.into_result();
134135
}
135136

@@ -141,6 +142,8 @@ impl Pass for LiftControlFlowToSCF {
141142
state.preserved_analyses_mut().preserve_all();
142143
}
143144

145+
state.set_post_pass_status(changed.into());
146+
144147
Ok(())
145148
}
146149
}

hir-transform/src/canonicalization.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::{boxed::Box, format, rc::Rc};
22

33
use midenc_hir::{
4-
pass::{OperationPass, Pass, PassExecutionState},
4+
pass::{OperationPass, Pass, PassExecutionState, PostPassStatus},
55
patterns::{self, FrozenRewritePatternSet, GreedyRewriteConfig, RewritePatternSet},
66
Context, EntityMut, Operation, OperationName, Report, Spanned,
77
};
@@ -96,6 +96,7 @@ impl Pass for Canonicalizer {
9696
) -> Result<(), Report> {
9797
let Some(rewrites) = self.rewrites.as_ref() else {
9898
log::debug!("skipping canonicalization as there are no rewrite patterns to apply");
99+
state.set_post_pass_status(PostPassStatus::Unchanged);
99100
return Ok(());
100101
};
101102
let op = {
@@ -129,15 +130,21 @@ impl Pass for Canonicalizer {
129130
}
130131

131132
let op = op.borrow();
132-
match converged {
133+
let changed = match converged {
133134
Ok(changed) => {
134-
log::debug!("canonicalization converged for '{}', changed={changed}", op.name())
135+
log::debug!("canonicalization converged for '{}', changed={changed}", op.name());
136+
changed
135137
}
136-
Err(changed) => log::warn!(
137-
"canonicalization failed to converge for '{}', changed={changed}",
138-
op.name()
139-
),
140-
}
138+
Err(changed) => {
139+
log::warn!(
140+
"canonicalization failed to converge for '{}', changed={changed}",
141+
op.name()
142+
);
143+
changed
144+
}
145+
};
146+
let ir_changed = changed.into();
147+
state.set_post_pass_status(ir_changed);
141148

142149
Ok(())
143150
}

hir-transform/src/sccp.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl SparseConditionalConstantPropagation {
5656
fn rewrite(
5757
&mut self,
5858
op: &mut Operation,
59-
_state: &mut PassExecutionState,
59+
state: &mut PassExecutionState,
6060
solver: &DataFlowSolver,
6161
) -> Result<(), Report> {
6262
let mut worklist = SmallVec::<[BlockRef; 8]>::default();
@@ -76,6 +76,7 @@ impl SparseConditionalConstantPropagation {
7676

7777
add_to_worklist(op.regions(), &mut worklist);
7878

79+
let mut replaced_any = false;
7980
while let Some(mut block) = worklist.pop() {
8081
let mut block = block.borrow_mut();
8182
let body = block.body_mut();
@@ -91,8 +92,10 @@ impl SparseConditionalConstantPropagation {
9192
let mut replaced_all = num_results != 0;
9293
for index in 0..num_results {
9394
let result = { op.borrow().get_result(index).borrow().as_value_ref() };
94-
replaced_all &=
95-
replace_with_constant(solver, &mut builder, &mut folder, result);
95+
let replaced = replace_with_constant(solver, &mut builder, &mut folder, result);
96+
97+
replaced_any |= replaced;
98+
replaced_all &= replaced;
9699
}
97100

98101
// If all of the results of the operation were replaced, try to erase the operation
@@ -112,7 +115,7 @@ impl SparseConditionalConstantPropagation {
112115
builder.set_insertion_point_to_start(block.as_block_ref());
113116

114117
for arg in block.arguments() {
115-
replace_with_constant(
118+
replaced_any |= replace_with_constant(
116119
solver,
117120
&mut builder,
118121
&mut folder,
@@ -121,6 +124,8 @@ impl SparseConditionalConstantPropagation {
121124
}
122125
}
123126

127+
state.set_post_pass_status(replaced_any.into());
128+
124129
Ok(())
125130
}
126131
}

hir-transform/src/sink.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use midenc_hir::{
44
adt::SmallDenseMap,
55
dominance::DominanceInfo,
66
matchers::{self, Matcher},
7-
pass::{Pass, PassExecutionState},
7+
pass::{Pass, PassExecutionState, PostPassStatus},
88
traits::{ConstantLike, Terminator},
99
Backward, Builder, EntityMut, Forward, FxHashSet, OpBuilder, Operation, OperationName,
1010
OperationRef, ProgramPoint, RawWalk, Region, RegionBranchOpInterface,
@@ -105,6 +105,7 @@ impl Pass for ControlFlowSink {
105105

106106
let dominfo = state.analysis_manager().get_analysis::<DominanceInfo>()?;
107107

108+
let mut sunk = PostPassStatus::Unchanged;
108109
operation.raw_prewalk_all::<Forward, _>(|op: OperationRef| {
109110
let regions_to_sink = {
110111
let op = op.borrow();
@@ -118,7 +119,7 @@ impl Pass for ControlFlowSink {
118119
};
119120

120121
// Sink side-effect free operations.
121-
control_flow_sink(
122+
sunk = control_flow_sink(
122123
&regions_to_sink,
123124
&dominfo,
124125
|op: &Operation, _region: &Region| op.is_memory_effect_free(),
@@ -132,6 +133,8 @@ impl Pass for ControlFlowSink {
132133
);
133134
});
134135

136+
state.set_post_pass_status(sunk);
137+
135138
Ok(())
136139
}
137140
}
@@ -171,7 +174,7 @@ impl Pass for SinkOperandDefs {
171174
fn run_on_operation(
172175
&mut self,
173176
op: EntityMut<'_, Self::Target>,
174-
_state: &mut PassExecutionState,
177+
state: &mut PassExecutionState,
175178
) -> Result<(), Report> {
176179
let operation = op.as_operation_ref();
177180
drop(op);
@@ -184,6 +187,7 @@ impl Pass for SinkOperandDefs {
184187
// then process the worklist, moving everything into position.
185188
let mut worklist = alloc::collections::VecDeque::default();
186189

190+
let mut changed = PostPassStatus::Unchanged;
187191
// Visit ops in "true" post-order (i.e. block bodies are visited bottom-up).
188192
operation.raw_postwalk_all::<Backward, _>(|operation: OperationRef| {
189193
// Determine if any of this operation's operands represent one of the following:
@@ -308,6 +312,7 @@ impl Pass for SinkOperandDefs {
308312
log::trace!(target: "sink-operand-defs", " rewriting operand {operand_value} as {replacement}");
309313
operand.borrow_mut().set(replacement);
310314

315+
changed = PostPassStatus::Changed;
311316
// If no other uses of this value remain, then remove the original
312317
// operation, as it is now dead.
313318
if !operand_value.borrow().is_used() {
@@ -354,6 +359,7 @@ impl Pass for SinkOperandDefs {
354359
log::trace!(target: "sink-operand-defs", " rewriting operand {operand_value} as {replacement}");
355360
sink_state.replacements.insert(operand_value, replacement);
356361
operand.borrow_mut().set(replacement);
362+
changed = PostPassStatus::Changed;
357363
} else {
358364
log::trace!(target: "sink-operand-defs", " defining op is a constant with no other uses, moving into place");
359365
// The original op can be moved
@@ -397,6 +403,7 @@ impl Pass for SinkOperandDefs {
397403
}
398404
}
399405

406+
state.set_post_pass_status(changed);
400407
Ok(())
401408
}
402409
}
@@ -548,12 +555,14 @@ pub fn control_flow_sink<P, F>(
548555
dominfo: &DominanceInfo,
549556
should_move_into_region: P,
550557
move_into_region: F,
551-
) where
558+
) -> PostPassStatus
559+
where
552560
P: Fn(&Operation, &Region) -> bool,
553561
F: Fn(OperationRef, RegionRef),
554562
{
555563
let sinker = Sinker::new(dominfo, should_move_into_region, move_into_region);
556-
sinker.sink_regions(regions);
564+
let sunk_regions = sinker.sink_regions(regions);
565+
(sunk_regions > 0).into()
557566
}
558567

559568
/// Populates `regions` with regions of the provided region branch op that are executed at most once

hir-transform/src/spill.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use midenc_hir::{
44
adt::{SmallDenseMap, SmallSet},
55
cfg::Graph,
66
dominance::{DomTreeNode, DominanceFrontier, DominanceInfo},
7-
pass::AnalysisManager,
7+
pass::{AnalysisManager, PostPassStatus},
88
traits::SingleRegion,
99
BlockRef, Builder, Context, FxHashMap, OpBuilder, OpOperand, Operation, OperationRef,
1010
ProgramPoint, Region, RegionBranchOpInterface, RegionBranchPoint, RegionRef, Report, Rewriter,
@@ -113,7 +113,7 @@ pub fn transform_spills(
113113
analysis: &mut SpillAnalysis,
114114
interface: &mut dyn TransformSpillsInterface,
115115
analysis_manager: AnalysisManager,
116-
) -> Result<(), Report> {
116+
) -> Result<PostPassStatus, Report> {
117117
assert!(
118118
op.borrow().implements::<dyn SingleRegion>(),
119119
"the spills transformation is not supported when the root op is multi-region"
@@ -252,7 +252,7 @@ pub fn transform_spills(
252252
)?;
253253
}
254254

255-
Ok(())
255+
Ok(PostPassStatus::Changed)
256256
}
257257

258258
fn rewrite_single_block_spills(

hir/src/ir/print.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
AttributeValue, EntityWithId, SuccessorOperands, Value,
1010
};
1111

12+
#[derive(Debug)]
1213
pub struct OpPrintingFlags {
1314
pub print_entry_block_headers: bool,
1415
pub print_intrinsic_attributes: bool,

0 commit comments

Comments
 (0)