feat(ir): add opt-in HIR optimization passes - #364
Merged
Conversation
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.
This was referenced Jul 21, 2026
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds an
optimizemodule torsigma-irwith total, semantics-preserving passes onIrRule. 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_conditionnormalizes a condition tree: merges nested same-kind boolean groups (a AND (b AND c)->a AND b AND c), collapsesNot(Not(x))tox, unwraps single-childAnd/Or, and drops idempotent duplicate siblings (a AND a->a).eliminate_dead_detectionsremoves detections that no condition can reference, honoringthem/glob selector patterns (themskips_-prefixed names) and recursing intoConditionalbodies.common_subexpressionsis a non-mutating analysis that reports detection items appearing more than once, the candidates a consumer could evaluate once and share.optimize_ruleapplies 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 followsHashMapiteration and is not a stable eval contract).A differential test in
rsigma-evallowers each rule, compiles the original and optimized HIR throughcompile_to_compiled, and asserts both agree on that normalized projection for every event across redundant/duplicate conditions, dead detections,themand glob/count selectors, and keyword rules.Builds on #360, #362, #363.
xref #346