Skip to content

BE-615: HashQL: Introduce the concept of synthetic closures in the MIR#8894

Open
indietyp wants to merge 8 commits into
bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-modulefrom
bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure
Open

BE-615: HashQL: Introduce the concept of synthetic closures in the MIR#8894
indietyp wants to merge 8 commits into
bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-modulefrom
bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure

Conversation

@indietyp

Copy link
Copy Markdown
Member

🌟 What is the purpose of this PR?

Introduces Source::Synthetic for compiler-generated wrapper bodies and extends administrative reduction to handle single-operation function bodies (TrivialClosure).

This is groundwork for making intrinsics usable in value position (e.g. passing + as an argument to a higher-order function). The Synthetic source variant will identify wrapper bodies that bridge non-first-class operations into callable closures, and TrivialClosure ensures they are always inlined away.

🔍 What does this change?

  • Adds Source::Synthetic(Symbol) to the MIR Source enum for compiler-synthesized operation wrappers
  • Adds ReductionKind::TrivialClosure to administrative reduction, recognizing single-basic-block bodies where a trivial prelude leads to a single non-call operation (Binary, Unary, Aggregate, Input, Load) whose result is returned
  • Synthetic bodies get InlineDirective::Always (same as Ctor)
  • All downstream Source matches updated: execution pipeline, statement placement, eval context, pretty printer
  • Adds [synthetic sym::path] source to the body! test macro

The TrivialClosure classification is intentionally broader than just synthetic bodies. Any function matching the shape is reducible, which unlocks optimization cascades: single-operation closures get inlined, exposing constant-foldable expressions, enabling branch elimination downstream.

Pre-Merge Checklist 🚀

🚢 Has this modified a publishable library?

This PR:

  • does not modify any publishable blocks or libraries, or modifications do not need publishing

📜 Does this require a change to the docs?

The changes in this PR:

  • are internal and do not require a docs change

🕸️ Does this require a change to the Turbo Graph?

The changes in this PR:

  • do not affect the execution graph

🐾 Next steps

  • Implement qualified_path in reification to generate synthetic wrapper bodies for intrinsics in value position
  • Intrinsic classification (first-classable vs syntactic forms)
  • Call handler optimization to recognize administrative thunk-force patterns and skip throwaway thunk body generation

🛡 What tests cover this?

  • Classification unit tests: classify_trivial_closure_binary, classify_trivial_closure_unary, classify_non_reducible_non_trivial_prelude
  • Inlining snapshot tests: inline_trivial_closure_binary, inline_trivial_closure_transitive
  • Existing compiletest suite (e.g. nested-branch-elimination now demonstrates the full optimization cascade)

❓ How to test this?

cargo test --package hashql-mir --lib -- administrative_reduction::tests

📹 Demo

The nested-branch-elimination compiletest shows the cascade effect: closures containing single comparisons (> 100, < 0) are now inlined by AR, exposing constant-foldable comparisons (50 > 100, 50 < 0), which enables the entire branch structure to collapse to return "in range".

Copilot AI review requested due to automatic review settings June 22, 2026 13:47
@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
hash Error Error Jun 30, 2026 12:49pm
petrinaut Ready Ready Preview Jun 30, 2026 12:49pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
hashdotdesign-tokens Ignored Ignored Preview Jun 30, 2026 12:49pm

@vercel vercel Bot temporarily deployed to Preview – petrinaut June 22, 2026 13:47 Inactive
@cursor

cursor Bot commented Jun 22, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches MIR body classification, inlining, administrative reduction, Postgres SQL generation, and entity property typing—compiler pipeline and query output can shift without obvious runtime API changes.

Overview
Adds Source::Synthetic(Symbol) for compiler-generated operation wrappers (always inlined like ctors), plus ReductionKind::TrivialClosure so administrative reduction inlines single-block bodies that end in one primitive op (Binary, Unary, Aggregate, Input, Load)—not only forwarding thunks/calls.

MIR / passes: Source::Intrinsic now carries an Intrinsic { id, optimize } (IntrinsicId::EntityPropertyAccess) instead of a DefId; built-in DefId list/dict op constants are removed. Execution placement, eval live-out, and pretty-printing treat Synthetic like other non–graph-read bodies. The body! macro gains [synthetic sym::path] and updated intrinsic stubs.

Stdlib / eval collateral: core::json types are factored into a shared types module; graph::entity::property is typed to return unknown() (not Option<?>) pending pattern matching. Postgres golden SQL switches entity_editions to a lateral subquery join; many .aux.mir fixtures reorder locals to match lowering. Pool is parameterized by Allocator (new_in / with_recycler_in); reify uses allocator-backed MixedBitSetPool.

Tests: New AR snapshots for trivial-closure inlining, lower-pipeline snapshots for unreachable CFGs, and updated inline/post-inline expectations where AR + folding collapse branches.

Reviewed by Cursor Bugbot for commit f03d9a1. Bugbot is set up for automated code reviews on this repo. Configure here.

indietyp commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.92308% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.99%. Comparing base (fccf37b) to head (996e625).

Files with missing lines Patch % Lines
libs/@local/hashql/core/src/collections/pool.rs 44.44% 5 Missing ⚠️
...@local/hashql/core/src/module/std_lib/core/json.rs 93.75% 1 Missing ⚠️
Additional details and impacted files
@@                                           Coverage Diff                                            @@
##           bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module    #8894      +/-   ##
========================================================================================================
+ Coverage                                                                 74.98%   81.99%   +7.00%     
========================================================================================================
  Files                                                                       198      168      -30     
  Lines                                                                     29923    25143    -4780     
  Branches                                                                    801      717      -84     
========================================================================================================
- Hits                                                                      22439    20616    -1823     
+ Misses                                                                     7260     4320    -2940     
+ Partials                                                                    224      207      -17     
Flag Coverage Δ
rust.hash-graph-store ?
rust.hashql-core 79.28% <76.92%> (-0.01%) ⬇️
rust.hashql-syntax-jexpr 94.04% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@indietyp indietyp force-pushed the bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module branch from 00cf591 to 85a5c9a Compare June 23, 2026 09:41
@indietyp indietyp force-pushed the bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module branch from 85a5c9a to ceb05c9 Compare June 23, 2026 09:52
Copilot AI review requested due to automatic review settings June 23, 2026 09:52
@indietyp indietyp force-pushed the bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure branch from a0c8ad1 to 0ddf199 Compare June 23, 2026 09:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 51 out of 52 changed files in this pull request and generated 1 comment.

Comment thread libs/@local/hashql/mir/src/def.rs
@indietyp indietyp force-pushed the bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure branch from 0ddf199 to d051ddb Compare June 23, 2026 10:02
@indietyp indietyp force-pushed the bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module branch from ceb05c9 to df8bde2 Compare June 23, 2026 10:02

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d051ddb. Configure here.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 51 out of 52 changed files in this pull request and generated 4 comments.

Comment on lines 27 to 29
impl DefId {
/// Built-in dictionary insert operation (immutable).
///
/// This operation inserts a key-value pair into a dictionary,
/// returning a new dictionary with the added pair. The original
/// dictionary remains unchanged.
pub const DICT_INSERT: Self = Self::new(0xFFFF_FE00);
/// Built-in dictionary insert operation (mutable).
///
/// This operation inserts a key-value pair into a dictionary in-place,
/// modifying the original dictionary. Used for efficient dictionary
/// construction and updates.
pub const DICT_INSERT_MUT: Self = Self::new(0xFFFF_FE01);
/// Built-in dictionary remove operation (immutable).
///
/// This operation removes a key-value pair from a dictionary,
/// returning a new dictionary without the specified key. The
/// original dictionary remains unchanged.
pub const DICT_REMOVE: Self = Self::new(0xFFFF_FE02);
/// Built-in dictionary remove operation (mutable).
///
/// This operation removes a key-value pair from a dictionary in-place,
/// modifying the original dictionary and returning the removed value
/// if the key existed.
pub const DICT_REMOVE_MUT: Self = Self::new(0xFFFF_FE03);
/// Built-in list pop operation (immutable).
///
/// This operation removes the last element from a list, returning
/// both the element and a new list without the element. The original
/// list remains unchanged.
pub const LIST_POP: Self = Self::new(0xFFFF_FE04);
/// Built-in list pop operation (mutable).
///
/// This operation removes the last element from a list in-place,
/// returning the removed element while modifying the original list.
pub const LIST_POP_MUT: Self = Self::new(0xFFFF_FE05);
/// Built-in list push operation (immutable).
///
/// This operation appends an element to a list, returning a new list
/// without modifying the original. Used for functional-style list
/// manipulation where immutability is preferred.
pub const LIST_PUSH: Self = Self::new(0xFFFF_FE06);
/// Built-in list push operation (mutable).
///
/// This operation appends an element to a list in-place, modifying
/// the original list. Used for imperative-style list manipulation
/// where performance is critical.
pub const LIST_PUSH_MUT: Self = Self::new(0xFFFF_FE07);
pub const PLACEHOLDER: Self = Self::MAX;
}
Comment on lines +140 to +142
// In this case it may either be a forwarding closure **or** a synthetic closure, this
// depends on the final statement, in either case the last statement must be an assignment.
let StatementKind::Assign(Assign { lhs, ref rhs }) = final_stmt.kind else {
| RValue::Load(_) => Self::TrivialClosure,
};

// Terminator must return the call's result.
Comment on lines 384 to +386
(@source intrinsic) => {
$crate::body::Source::Intrinsic($crate::def::DefId::PLACEHOLDER)
$crate::body::Source::Intrinsic($crate::intrinsic::Intrinsic { id: $crate::intrinsic::IntrinsicId::EntityPropertyAccess, optimize: true })
};
@indietyp indietyp force-pushed the bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module branch from fccf37b to b14202a Compare June 30, 2026 12:44
@indietyp indietyp force-pushed the bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure branch from 996e625 to 0a0050d Compare June 30, 2026 12:44
Copilot AI review requested due to automatic review settings June 30, 2026 12:45
@indietyp indietyp force-pushed the bm/be-615-hashql-introduce-synthetic-closures-and-trivial-closure branch from 0a0050d to f03d9a1 Compare June 30, 2026 12:45
@indietyp indietyp force-pushed the bm/be-607-hashql-remove-smallvec-from-the-standardlibrary-for-module branch from b14202a to 6696c2c Compare June 30, 2026 12:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 51 out of 52 changed files in this pull request and generated 1 comment.

Comment on lines 27 to 29
impl DefId {
/// Built-in dictionary insert operation (immutable).
///
/// This operation inserts a key-value pair into a dictionary,
/// returning a new dictionary with the added pair. The original
/// dictionary remains unchanged.
pub const DICT_INSERT: Self = Self::new(0xFFFF_FE00);
/// Built-in dictionary insert operation (mutable).
///
/// This operation inserts a key-value pair into a dictionary in-place,
/// modifying the original dictionary. Used for efficient dictionary
/// construction and updates.
pub const DICT_INSERT_MUT: Self = Self::new(0xFFFF_FE01);
/// Built-in dictionary remove operation (immutable).
///
/// This operation removes a key-value pair from a dictionary,
/// returning a new dictionary without the specified key. The
/// original dictionary remains unchanged.
pub const DICT_REMOVE: Self = Self::new(0xFFFF_FE02);
/// Built-in dictionary remove operation (mutable).
///
/// This operation removes a key-value pair from a dictionary in-place,
/// modifying the original dictionary and returning the removed value
/// if the key existed.
pub const DICT_REMOVE_MUT: Self = Self::new(0xFFFF_FE03);
/// Built-in list pop operation (immutable).
///
/// This operation removes the last element from a list, returning
/// both the element and a new list without the element. The original
/// list remains unchanged.
pub const LIST_POP: Self = Self::new(0xFFFF_FE04);
/// Built-in list pop operation (mutable).
///
/// This operation removes the last element from a list in-place,
/// returning the removed element while modifying the original list.
pub const LIST_POP_MUT: Self = Self::new(0xFFFF_FE05);
/// Built-in list push operation (immutable).
///
/// This operation appends an element to a list, returning a new list
/// without modifying the original. Used for functional-style list
/// manipulation where immutability is preferred.
pub const LIST_PUSH: Self = Self::new(0xFFFF_FE06);
/// Built-in list push operation (mutable).
///
/// This operation appends an element to a list in-place, modifying
/// the original list. Used for imperative-style list manipulation
/// where performance is critical.
pub const LIST_PUSH_MUT: Self = Self::new(0xFFFF_FE07);
pub const PLACEHOLDER: Self = Self::MAX;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/libs Relates to first-party libraries/crates/packages (area) area/tests New or updated tests type/eng > backend Owned by the @backend team

Development

Successfully merging this pull request may close these issues.

2 participants