Skip to content

feat(ir): add opt-in HIR optimization passes - #364

Merged
mostafa merged 2 commits into
mainfrom
feat/ir-optimize-passes
Jul 20, 2026
Merged

feat(ir): add opt-in HIR optimization passes#364
mostafa merged 2 commits into
mainfrom
feat/ir-optimize-passes

Conversation

@mostafa

@mostafa mostafa commented Jul 20, 2026

Copy link
Copy Markdown
Member

Adds an optimize module to rsigma-ir with total, semantics-preserving passes on IrRule. The passes are opt-in for offline tooling (pack building, analysis) and are not run by the default eval or convert paths, so compiled-matcher behavior and byte-identical backend output stay unchanged.

Passes

  • flatten_condition normalizes a condition tree: merges nested same-kind boolean groups (a AND (b AND c) -> a AND b AND c), collapses Not(Not(x)) to x, unwraps single-child And/Or, and drops idempotent duplicate siblings (a AND a -> a).
  • eliminate_dead_detections removes detections that no condition can reference, honoring them/glob selector patterns (them skips _-prefixed names) and recursing into Conditional bodies.
  • common_subexpressions is a non-mutating analysis that reports detection items appearing more than once, the candidates a consumer could evaluate once and share.
  • optimize_rule applies the two structural passes in order.

Contract and testing

Each pass preserves the match decision and the set of matched selections and fields. Reported order and multiplicity are deliberately not part of the contract: dropping a duplicate reference reports a selection once instead of twice, and pruning a dead detection can reorder a them/glob selector's reported names (that order follows HashMap iteration and is not a stable eval contract).

A differential test in rsigma-eval lowers each rule, compiles the original and optimized HIR through compile_to_compiled, and asserts both agree on that normalized projection for every event across redundant/duplicate conditions, dead detections, them and glob/count selectors, and keyword rules.

Builds on #360, #362, #363.

xref #346

mostafa added 2 commits July 20, 2026 23:45
Add an optimize module with total, semantics-preserving passes on IrRule:

- flatten_condition: merge nested same-kind boolean groups, collapse
  Not(Not(x)), unwrap single-child And/Or, and drop idempotent duplicate
  siblings.
- eliminate_dead_detections: remove detections no condition can
  reference, honoring them/glob selector patterns and recursing into
  Conditional bodies.
- common_subexpressions: a non-mutating report of detection items that
  occur more than once.
- optimize_rule: run the structural passes in order.

The passes are not run by the default eval or convert paths, so
compiled-matcher behavior and byte-identical backend output are
unchanged. A differential test compiles the original and optimized HIR
through compile_to_compiled and asserts both agree on the match decision
and the set of matched selections and fields for every event.
@mostafa
mostafa merged commit 7dfa05d into main Jul 20, 2026
16 checks passed
@mostafa
mostafa deleted the feat/ir-optimize-passes branch July 20, 2026 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant