Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 12 additions & 37 deletions libs/@local/hashql/compiletest/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ use hashql_mir::{
body::Body,
context::MirContext,
def::{DefId, DefIdSlice, DefIdVec},
pass::{
Changed, GlobalAnalysisPass as _, GlobalTransformPass as _, GlobalTransformState,
analysis::SizeEstimationAnalysis,
execution::{ExecutionAnalysis, ExecutionAnalysisResidual},
transform::{Inline, InlineConfig, PostInline, PreInline},
},
pass::{self, LowerConfig, execution::ExecutionAnalysisResidual},
reify::ReifyContext,
};
use hashql_syntax_jexpr::span::Span;
Expand Down Expand Up @@ -189,9 +184,11 @@ impl<'heap> Pipeline<'heap> {
bodies: &mut bodies,
mir: &mut mir_context,
hir: &hir_context,
scratch: &self.scratch,
};

let entry = tri!(hashql_mir::reify::from_hir(node, &mut reify_context));
self.scratch.reset();

// drain the context, because we're going to re-create it
self.diagnostics.extend(
Expand All @@ -218,24 +215,14 @@ impl<'heap> Pipeline<'heap> {
bodies: &mut DefIdSlice<Body<'heap>>,
) -> Result<(), BoxedDiagnostic<'static, SpanId>> {
let mut context = MirContext::new(&self.env, interner);
let mut state = GlobalTransformState::new_in(&*bodies, self.heap);

self.scratch.reset();

let mut pass = PreInline::new_in(&mut self.scratch);
let _: Changed = pass.run(&mut context, &mut state, bodies);
self.scratch.reset();

let mut pass = Inline::new_in(InlineConfig::default(), &mut self.scratch);
let _: Changed = pass.run(&mut context, &mut state, bodies);
self.scratch.reset();

let mut pass = PostInline::new_in(&mut self.scratch);
let _: Changed = pass.run(&mut context, &mut state, bodies);
self.scratch.reset();

let status = context.diagnostics.generalize().boxed().into_status(());
process_status(&mut self.diagnostics, status)?;
let result = pass::lower(
&mut context,
&mut self.scratch,
bodies,
&LowerConfig::default(),
);
process_status(&mut self.diagnostics, result)?;

Ok(())
}
Expand All @@ -262,20 +249,8 @@ impl<'heap> Pipeline<'heap> {
> {
let mut context = MirContext::new(&self.env, interner);

let mut pass = SizeEstimationAnalysis::new_in(&self.scratch);
pass.run(&mut context, bodies);
let footprints = pass.finish();
self.scratch.reset();

let pass = ExecutionAnalysis {
footprints: &footprints,
scratch: &mut self.scratch,
};
let analysis = pass.run_all_in(&mut context, bodies, self.heap);
self.scratch.reset();

let status = context.diagnostics.generalize().boxed().into_status(());
process_status(&mut self.diagnostics, status)?;
let status = pass::place(&mut context, &mut self.scratch, bodies);
let analysis = process_status(&mut self.diagnostics, status)?;

Ok(analysis)
}
Expand Down
2 changes: 1 addition & 1 deletion libs/@local/hashql/compiletest/src/suite/eval_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl Suite for EvalPostgres {
&interner,
&bodies,
&analysis,
context.heap,
heap,
&mut scratch,
);
scratch.reset();
Expand Down
66 changes: 66 additions & 0 deletions libs/@local/hashql/compiletest/src/suite/mir_interpret.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use hashql_core::r#type::environment::Environment;
use hashql_diagnostics::Diagnostic;
use hashql_mir::{
intern::Interner,
interpret::{CallStack, Inputs, Runtime, RuntimeConfig},
};

use super::{
RunContext, Suite, SuiteDiagnostic,
mir_pass_transform_post_inline::mir_pass_transform_post_inline,
mir_pass_transform_pre_inline::TextRenderer,
};

pub(crate) struct MirInterpret;

impl Suite for MirInterpret {
fn name(&self) -> &'static str {
"mir/interpret"
}

fn description(&self) -> &'static str {
"Run the interpreter on the MIR"
}

fn secondary_file_extensions(&self) -> &[&str] {
&["mir"]
}

fn run<'heap>(
&self,
RunContext {
heap,
diagnostics,
secondary_outputs,
..
}: RunContext<'_, 'heap>,
expr: hashql_ast::node::expr::Expr<'heap>,
) -> Result<String, SuiteDiagnostic> {
let mut environment = Environment::new(heap);
let interner = Interner::new(heap);

let mut buffer = Vec::new();

let (root, bodies, _) = mir_pass_transform_post_inline(
heap,
expr,
&interner,
TextRenderer::new(&mut buffer),
&mut environment,
diagnostics,
)?;

secondary_outputs.insert("mir", String::from_utf8_lossy_owned(buffer));

let inputs = Inputs::new();
let mut runtime = Runtime::new(RuntimeConfig::default(), &bodies, &inputs);
let callstack = CallStack::new(&runtime, root, []);

let output = runtime
.run(callstack, |_| unimplemented!())
.map_err(Diagnostic::generalize)
.map_err(Diagnostic::boxed)?;

Ok(format!("{output:#?}"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ impl Suite for MirPassAnalysisDataDependency {

let mut buffer = Vec::new();

let (root, mut bodies) = mir_reify(heap, expr, &interner, &mut environment, diagnostics)?;
let (root, mut bodies, _) =
mir_reify(heap, expr, &interner, &mut environment, diagnostics)?;

writeln!(buffer, "{}\n", Header::new("MIR")).expect("should be able to write to buffer");
mir_format_text(heap, &environment, &mut buffer, root, &bodies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub(crate) fn mir_pass_transform_cfg_simplify<'heap>(
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)?;
let (root, mut bodies, mut scratch) =
mir_reify(heap, expr, interner, environment, diagnostics)?;

render(heap, environment, root, &bodies);

Expand All @@ -59,7 +60,6 @@ pub(crate) fn mir_pass_transform_cfg_simplify<'heap>(
interner,
diagnostics: DiagnosticIssues::new(),
};
let mut scratch = Scratch::new();

let mut pass = CfgSimplify::new_in(&mut scratch);
for body in bodies.as_mut_slice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub(crate) fn mir_pass_transform_pre_inline<'heap>(
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)?;
let (root, mut bodies, _) = mir_reify(heap, expr, interner, environment, diagnostics)?;

render.render(
&mut RenderContext {
Expand Down
11 changes: 7 additions & 4 deletions libs/@local/hashql/compiletest/src/suite/mir_reify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use error_stack::ReportSink;
use hashql_ast::node::expr::Expr;
use hashql_core::{
heap::Heap,
heap::{Heap, ResetAllocator as _, Scratch},
id::IdVec,
module::ModuleRegistry,
pretty::Formatter,
Expand All @@ -32,7 +32,8 @@ pub(crate) fn mir_reify<'heap>(
interner: &Interner<'heap>,
environment: &mut Environment<'heap>,
diagnostics: &mut Vec<SuiteDiagnostic>,
) -> Result<(DefId, DefIdVec<Body<'heap>>), SuiteDiagnostic> {
) -> Result<(DefId, DefIdVec<Body<'heap>>, Scratch), SuiteDiagnostic> {
let mut scratch = Scratch::new();
let registry = ModuleRegistry::new(environment);
let hir_interner = hashql_hir::intern::Interner::new(heap);
let mut hir_context = HirContext::new(&hir_interner, &registry);
Expand Down Expand Up @@ -66,11 +67,13 @@ pub(crate) fn mir_reify<'heap>(
bodies: &mut bodies,
mir: &mut mir_context,
hir: &hir_context,
scratch: &scratch,
},
),
)?;
scratch.reset();

Ok((root, bodies))
Ok((root, bodies, scratch))
Comment thread
indietyp marked this conversation as resolved.
}

pub(crate) fn mir_format_text<'heap>(
Expand Down Expand Up @@ -208,7 +211,7 @@ impl Suite for MirReifySuite {
let mut environment = Environment::new(heap);
let interner = Interner::new(heap);

let (root, bodies) = mir_reify(heap, expr, &interner, &mut environment, diagnostics)?;
let (root, bodies, _) = mir_reify(heap, expr, &interner, &mut environment, diagnostics)?;

let mut buffer = Vec::new();
mir_format_text(heap, &environment, &mut buffer, root, &bodies);
Expand Down
4 changes: 3 additions & 1 deletion libs/@local/hashql/compiletest/src/suite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod hir_lower_normalization;
mod hir_lower_specialization;
mod hir_lower_thunking;
mod hir_reify;
mod mir_interpret;
mod mir_pass_analysis_data_dependency;
mod mir_pass_transform_administrative_reduction;
mod mir_pass_transform_cfg_simplify;
Expand Down Expand Up @@ -59,7 +60,7 @@ use self::{
hir_lower_normalization::HirLowerNormalizationSuite,
hir_lower_specialization::HirLowerSpecializationSuite,
hir_lower_thunking::HirLowerThunkingSuite, hir_reify::HirReifySuite,
mir_pass_analysis_data_dependency::MirPassAnalysisDataDependency,
mir_interpret::MirInterpret, mir_pass_analysis_data_dependency::MirPassAnalysisDataDependency,
mir_pass_transform_administrative_reduction::MirPassTransformAdministrativeReduction,
mir_pass_transform_cfg_simplify::MirPassTransformCfgSimplify,
mir_pass_transform_dse::MirPassTransformDse,
Expand Down Expand Up @@ -163,6 +164,7 @@ const SUITES: &[&dyn Suite] = &[
&HirLowerTypeInferenceIntrinsicsSuite,
&HirLowerTypeInferenceSuite,
&HirReifySuite,
&MirInterpret,
&MirPassAnalysisDataDependency,
&MirPassTransformAdministrativeReduction,
&MirPassTransformCfgSimplify,
Expand Down
14 changes: 13 additions & 1 deletion libs/@local/hashql/core/src/graph/linked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use alloc::alloc::Global;
use core::{
alloc::Allocator,
fmt,
ops::{Index, IndexMut},
};

Expand Down Expand Up @@ -213,7 +214,7 @@ impl<E> Edge<E> {
/// }
/// # else { unreachable!() }
/// ```
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct LinkedGraph<N, E, A: Allocator = Global> {
/// All nodes in the graph, indexed by [`NodeId`].
nodes: IdVec<NodeId, Node<N>, A>,
Expand Down Expand Up @@ -541,6 +542,17 @@ impl<N, E> Default for LinkedGraph<N, E> {
}
}

impl<N: core::fmt::Debug, E: core::fmt::Debug, A: Allocator> core::fmt::Debug
for LinkedGraph<N, E, A>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LinkedGraph")
.field("nodes", &self.nodes)
.field("edges", &self.edges)
.finish()
}
}

impl<N, E, A: Allocator> IndexMut<NodeId> for LinkedGraph<N, E, A> {
fn index_mut(&mut self, index: NodeId) -> &mut Self::Output {
&mut self.nodes[index]
Expand Down
Loading
Loading