-
Notifications
You must be signed in to change notification settings - Fork 110
BE-266: HashQL: Implement PreInlining pass for MIR optimization #8233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
indietyp
merged 7 commits into
main
from
bm/be-266-hashql-implement-pre-inlining-fix-point-loop
Jan 17, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1e8f62b
feat: pre inlining step
indietyp 5affd6a
chore: docs
indietyp 12dc951
feat: tests
indietyp 63b5059
feat: short circuit in more places
indietyp 844fb31
chore: reset alloc before pass
indietyp b74f30d
feat: have pipeline use pre-inlining
indietyp 3e0b23b
fix: format
indietyp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
276 changes: 276 additions & 0 deletions
276
libs/@local/hashql/compiletest/src/suite/mir_pass_transform_pre_inlining.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,276 @@ | ||
| use std::io::{self, Write as _}; | ||
|
|
||
| use hashql_ast::node::expr::Expr; | ||
| use hashql_core::{ | ||
| heap::{Heap, Scratch}, | ||
| r#type::environment::Environment, | ||
| }; | ||
| use hashql_diagnostics::DiagnosticIssues; | ||
| use hashql_mir::{ | ||
| body::Body, | ||
| context::MirContext, | ||
| def::{DefId, DefIdSlice, DefIdVec}, | ||
| intern::Interner, | ||
| pass::{Changed, GlobalTransformPass as _, GlobalTransformState, transform::PreInlining}, | ||
| }; | ||
|
|
||
| use super::{RunContext, Suite, SuiteDiagnostic, common::process_issues, mir_reify::mir_reify}; | ||
| use crate::suite::{ | ||
| common::Header, | ||
| mir_reify::{d2_output_enabled, mir_format_d2, mir_format_text, mir_spawn_d2}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Copy, Clone, PartialEq, Eq)] | ||
| pub(crate) struct Stage { | ||
| id: &'static str, | ||
| title: &'static str, | ||
| } | ||
|
|
||
| pub(crate) struct RenderContext<'env, 'heap> { | ||
| pub heap: &'heap Heap, | ||
| pub env: &'env Environment<'heap>, | ||
| pub stage: Stage, | ||
| pub root: DefId, | ||
| } | ||
|
|
||
| pub(crate) trait MirRenderer { | ||
| fn render<'heap>( | ||
| &mut self, | ||
| context: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ); | ||
| } | ||
|
|
||
| impl<R> MirRenderer for &mut R | ||
| where | ||
| R: MirRenderer, | ||
| { | ||
| fn render<'heap>( | ||
| &mut self, | ||
| context: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ) { | ||
| R::render(self, context, bodies); | ||
| } | ||
| } | ||
|
|
||
| impl<R> MirRenderer for Option<R> | ||
| where | ||
| R: MirRenderer, | ||
| { | ||
| fn render<'heap>( | ||
| &mut self, | ||
| context: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ) { | ||
| if let Some(renderer) = self.as_mut() { | ||
| renderer.render(context, bodies); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct TextRenderer<W> { | ||
| inner: W, | ||
| index: usize, | ||
| } | ||
|
|
||
| impl<W> TextRenderer<W> { | ||
| pub(crate) const fn new(inner: W) -> Self { | ||
| Self { inner, index: 0 } | ||
| } | ||
| } | ||
|
|
||
| impl<W> MirRenderer for TextRenderer<W> | ||
| where | ||
| W: io::Write, | ||
| { | ||
| fn render<'heap>( | ||
| &mut self, | ||
| RenderContext { | ||
| heap, | ||
| env, | ||
| stage: Stage { | ||
| id: _, | ||
| title: header, | ||
| }, | ||
| root, | ||
| }: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ) { | ||
| if self.index > 0 { | ||
| write!(self.inner, "\n\n").expect("should be able to write to buffer"); | ||
| } | ||
|
|
||
| writeln!(self.inner, "{}\n", Header::new(header)) | ||
| .expect("should be able to write to buffer"); | ||
| mir_format_text(heap, env, &mut self.inner, *root, bodies); | ||
| self.index += 1; | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct D2Renderer<W> { | ||
| inner: W, | ||
| } | ||
|
|
||
| impl<W> D2Renderer<W> { | ||
| pub(crate) const fn new(inner: W) -> Self { | ||
| Self { inner } | ||
| } | ||
| } | ||
|
|
||
| impl<W> MirRenderer for D2Renderer<W> | ||
| where | ||
| W: io::Write, | ||
| { | ||
| fn render<'heap>( | ||
| &mut self, | ||
| RenderContext { | ||
| heap, | ||
| env, | ||
| stage: Stage { id, title: header }, | ||
| root, | ||
| }: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ) { | ||
| writeln!(self.inner, "{id}: '{header}' {{").expect("should be able to write to buffer"); | ||
| mir_format_d2(heap, env, &mut self.inner, *root, bodies); | ||
| writeln!(self.inner, "}}").expect("should be able to write to buffer"); | ||
| } | ||
| } | ||
|
|
||
| impl<A, B> MirRenderer for (A, B) | ||
| where | ||
| A: MirRenderer, | ||
| B: MirRenderer, | ||
| { | ||
| #[expect(clippy::min_ident_chars)] | ||
| fn render<'heap>( | ||
| &mut self, | ||
| context: &mut RenderContext<'_, 'heap>, | ||
| bodies: &DefIdSlice<Body<'heap>>, | ||
| ) { | ||
| let (a, b) = self; | ||
|
|
||
| a.render(context, bodies); | ||
| b.render(context, bodies); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn mir_pass_transform_pre_inlining<'heap>( | ||
| heap: &'heap Heap, | ||
| expr: Expr<'heap>, | ||
| interner: &Interner<'heap>, | ||
| mut render: impl MirRenderer, | ||
| environment: &mut Environment<'heap>, | ||
| diagnostics: &mut Vec<SuiteDiagnostic>, | ||
| ) -> Result<(DefId, DefIdVec<Body<'heap>>, Scratch), SuiteDiagnostic> { | ||
| let (root, mut bodies) = mir_reify(heap, expr, interner, environment, diagnostics)?; | ||
|
|
||
| render.render( | ||
| &mut RenderContext { | ||
| heap, | ||
| env: environment, | ||
| stage: Stage { | ||
| id: "initial", | ||
| title: "Initial MIR", | ||
| }, | ||
| root, | ||
| }, | ||
| &bodies, | ||
| ); | ||
|
|
||
| let mut context = MirContext { | ||
| heap, | ||
| env: environment, | ||
| interner, | ||
| diagnostics: DiagnosticIssues::new(), | ||
| }; | ||
| let mut scratch = Scratch::new(); | ||
|
|
||
| let mut pass = PreInlining::new_in(&mut scratch); | ||
| let _: Changed = pass.run( | ||
| &mut context, | ||
| &mut GlobalTransformState::new_in(&bodies, heap), | ||
| &mut bodies, | ||
| ); | ||
|
|
||
| process_issues(diagnostics, context.diagnostics)?; | ||
|
|
||
| render.render( | ||
| &mut RenderContext { | ||
| heap, | ||
| env: environment, | ||
| stage: Stage { | ||
| id: "pre-inlining", | ||
| title: "Pre-inlining MIR", | ||
| }, | ||
| root, | ||
| }, | ||
| &bodies, | ||
| ); | ||
|
|
||
| Ok((root, bodies, scratch)) | ||
| } | ||
|
|
||
| pub(crate) struct MirPassTransformPreInlining; | ||
|
|
||
| impl Suite for MirPassTransformPreInlining { | ||
| fn priority(&self) -> usize { | ||
| 1 | ||
| } | ||
|
|
||
| fn secondary_file_extensions(&self) -> &[&str] { | ||
| &["svg"] | ||
| } | ||
|
|
||
| fn name(&self) -> &'static str { | ||
| "mir/pass/transform/pre-inlining" | ||
| } | ||
|
|
||
| fn description(&self) -> &'static str { | ||
| "Pre-inlining transformations in the MIR" | ||
| } | ||
|
|
||
| fn run<'heap>( | ||
| &self, | ||
| RunContext { | ||
| heap, | ||
| diagnostics, | ||
| suite_directives, | ||
| reports, | ||
| secondary_outputs, | ||
| .. | ||
| }: RunContext<'_, 'heap>, | ||
| expr: Expr<'heap>, | ||
| ) -> Result<String, SuiteDiagnostic> { | ||
| let mut environment = Environment::new(heap); | ||
| let interner = Interner::new(heap); | ||
|
|
||
| let mut buffer = Vec::new(); | ||
| let mut d2 = d2_output_enabled(self, suite_directives, reports).then(mir_spawn_d2); | ||
|
|
||
| mir_pass_transform_pre_inlining( | ||
| heap, | ||
| expr, | ||
| &interner, | ||
| ( | ||
| TextRenderer::new(&mut buffer), | ||
| d2.as_mut().map(|(writer, _)| D2Renderer::new(writer)), | ||
| ), | ||
| &mut environment, | ||
| diagnostics, | ||
| )?; | ||
|
|
||
| if let Some((mut writer, handle)) = d2 { | ||
| writer.flush().expect("should be able to write to buffer"); | ||
| drop(writer); | ||
|
|
||
| let diagram = handle.join().expect("should be able to join handle"); | ||
| let diagram = String::from_utf8_lossy_owned(diagram); | ||
|
|
||
| secondary_outputs.insert("svg", diagram); | ||
| } | ||
|
|
||
| Ok(String::from_utf8_lossy_owned(buffer)) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.