-
Notifications
You must be signed in to change notification settings - Fork 110
BE-197: Implement MIR inline pass for HashQL #8236
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
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
5312e64
feat: expose scc membership
indietyp f4c4e83
feat: cost configuration
indietyp b7e6599
feat: first inliner (WIP)
indietyp 95683ae
feat: choose call sites to inline
indietyp 24c16db
chore: renames
indietyp 193ec8c
feat: inlining
indietyp 675041d
feat: make members 2x smaller
indietyp f660778
feat: inline add all blocks
indietyp 4dcb73b
feat: inliner WIP
indietyp 413c868
feat: inline extract out data
indietyp 5a81ada
feat: create inliner
indietyp aadd5dc
chore: fix lints
indietyp 56a8087
chore: fix lints
indietyp e58d380
chore: experiment
indietyp fd73251
feat: checkpointing API
indietyp c56d301
feat: account for aggressive inlining in normal phased
indietyp 9ee6526
feat: finish (untested) inline
indietyp d1db3d3
chore: docs
indietyp a94d676
chore: cleanup
indietyp e2c4cc3
chore: checkpoint
indietyp 6e6d749
chore: checkpoint
indietyp cbf8fd5
feat: diagnostic
indietyp 9a043bc
chore: Default trait impl for config
indietyp 5f6acee
chore: checkpoint
indietyp c1b6880
feat: double inlining
indietyp 3021642
fix: merge conflicts
indietyp 1f60499
fix: imports
indietyp 6994e8e
chore: checkpoint
indietyp 65653f7
chore: tests
indietyp 0d62325
feat: tests
indietyp e5eb8a9
fix: inline config params
indietyp ffbd594
feat: docs
indietyp 10fc039
feat: more test coverage
indietyp 494e60c
chore: correctly gate the debug_assert via cfg and not conditional
indietyp 9d19db1
feat: rework tests to be less... large
indietyp 9b56319
feat: verify that heap is indeed max
indietyp 7e32c71
feat: use `GlobalTransformPass` for Inline
indietyp 14a0b0a
chore: checkpoint
indietyp 5b42b5f
feat: use heap for changed state tracking
indietyp ae3798d
chore: fix comment
indietyp 731bcea
fix: import
indietyp 4eea690
chore: remove dead reference
indietyp c1ba012
fix: appease the AI overlords
indietyp da59fc7
feat: inline cost esimation
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
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
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
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
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
180 changes: 180 additions & 0 deletions
180
libs/@local/hashql/compiletest/src/suite/mir_pass_transform_inline.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,180 @@ | ||
| use std::io::Write as _; | ||
|
|
||
| use hashql_ast::node::expr::Expr; | ||
| use hashql_core::{ | ||
| heap::{Heap, ResetAllocator as _, Scratch}, | ||
| r#type::environment::Environment, | ||
| }; | ||
| use hashql_diagnostics::DiagnosticIssues; | ||
| use hashql_mir::{ | ||
| body::Body, | ||
| context::MirContext, | ||
| def::{DefId, DefIdVec}, | ||
| intern::Interner, | ||
| pass::{ | ||
| Changed, GlobalTransformPass as _, GlobalTransformState, | ||
| transform::{Inline, InlineConfig}, | ||
| }, | ||
| }; | ||
|
|
||
| use super::{ | ||
| RunContext, Suite, SuiteDiagnostic, | ||
| common::process_issues, | ||
| mir_pass_transform_pre_inlining::{ | ||
| MirRenderer, RenderContext, Stage, mir_pass_transform_pre_inlining, | ||
| }, | ||
| }; | ||
| use crate::suite::{ | ||
| mir_pass_transform_pre_inlining::{D2Renderer, TextRenderer}, | ||
| mir_reify::{d2_output_enabled, mir_spawn_d2}, | ||
| }; | ||
|
|
||
| pub(crate) fn mir_pass_transform_inline<'heap>( | ||
| heap: &'heap Heap, | ||
| expr: Expr<'heap>, | ||
| config: InlineConfig, | ||
| 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, mut scratch) = mir_pass_transform_pre_inlining( | ||
| heap, | ||
| expr, | ||
| interner, | ||
| &mut render, | ||
| environment, | ||
| diagnostics, | ||
| )?; | ||
|
|
||
| let mut context = MirContext { | ||
| heap, | ||
| env: environment, | ||
| interner, | ||
| diagnostics: DiagnosticIssues::new(), | ||
| }; | ||
|
|
||
| let mut pass = Inline::new_in(config, &mut scratch); | ||
| let _: Changed = pass.run( | ||
| &mut context, | ||
| &mut GlobalTransformState::new_in(&bodies, heap), | ||
| &mut bodies, | ||
| ); | ||
| scratch.reset(); | ||
|
|
||
| process_issues(diagnostics, context.diagnostics)?; | ||
|
|
||
| render.render( | ||
| &mut RenderContext { | ||
| heap, | ||
| env: environment, | ||
| stage: Stage { | ||
| id: "inline", | ||
| title: "Inlined MIR", | ||
| }, | ||
| root, | ||
| }, | ||
| &bodies, | ||
| ); | ||
|
|
||
| Ok((root, bodies, scratch)) | ||
| } | ||
|
|
||
| pub(crate) struct MirPassTransformInline; | ||
|
|
||
| impl Suite for MirPassTransformInline { | ||
| fn priority(&self) -> usize { | ||
| 1 | ||
| } | ||
|
|
||
| fn name(&self) -> &'static str { | ||
| "mir/pass/transform/inline" | ||
| } | ||
|
|
||
| fn description(&self) -> &'static str { | ||
| "Inlining in the MIR" | ||
| } | ||
|
|
||
| fn secondary_file_extensions(&self) -> &[&str] { | ||
| &["svg"] | ||
| } | ||
|
|
||
| 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 config = InlineConfig::default(); | ||
|
|
||
| #[expect(clippy::cast_sign_loss, clippy::cast_possible_truncation)] | ||
| if let Some(aggressive_inline_cutoff) = suite_directives | ||
| .get("aggressive-inline-cutoff") | ||
| .and_then(toml::Value::as_integer) | ||
| { | ||
| config.aggressive_inline_cutoff = aggressive_inline_cutoff as usize; | ||
| } | ||
|
|
||
| #[expect(clippy::cast_possible_truncation)] | ||
| if let Some(rvalue_input_cost) = suite_directives | ||
| .get("rvalue-input-cost") | ||
| .and_then(toml::Value::as_float) | ||
| { | ||
| config.cost.rvalue_input = rvalue_input_cost as f32; | ||
| } | ||
|
|
||
| #[expect(clippy::cast_possible_truncation)] | ||
| if let Some(max_cost) = suite_directives | ||
| .get("max-cost") | ||
| .and_then(toml::Value::as_float) | ||
| { | ||
| config.heuristics.max = max_cost as f32; | ||
| } | ||
|
|
||
| let skip_output = suite_directives | ||
| .get("skip-output") | ||
| .and_then(toml::Value::as_bool) | ||
| .unwrap_or(false); | ||
|
|
||
| let mut buffer = Vec::new(); | ||
| let mut d2 = d2_output_enabled(self, suite_directives, reports).then(mir_spawn_d2); | ||
|
|
||
| mir_pass_transform_inline( | ||
| heap, | ||
| expr, | ||
| config, | ||
| &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); | ||
| } | ||
|
|
||
| if skip_output { | ||
| return Ok("[output intentionally skipped]".to_owned()); | ||
| } | ||
|
|
||
| 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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we just generally run Miri on all tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can give it a try next time, I didnt because of the time it takes to run, but let me check if that is still true