Skip to main content

Module analysis

Module analysis 

Source
Expand description

Static analysis passes over compiled Program IR.

Forward-flow analyses that produce abstract domains for each opcode position. Callers (compiler, planner, caller code) can use these to:

  • emit specialised opcodes where a value’s type / nullness / cardinality is statically known,
  • reject ill-typed expressions at compile time,
  • enable further peephole passes that require type awareness.

The analyses run on the flat Arc<[Opcode]> IR and are intentionally conservative — when uncertain, they return the Unknown top element of the lattice.

Structs§

AbstractVal

Enums§

Cardinality
Monotonicity
Tracks ordering properties of array-like values through the pipeline.
Nullness
VType

Functions§

collect_accessed_fields
Collect all field names directly accessed from a program. Used by projection-pushdown analysis: if an object is later accessed only via these fields, other fields can be trimmed early.
count_ident_uses
Count LoadIdent(name) references across an entire program (including sub-programs).
dedup_subprograms
Walk program and replace every Arc<Program> inside opcodes with a canonical Arc keyed by program_signature. Structurally-identical sub-programs end up pointing at the same allocation, reducing memory and enabling downstream caches to hit on the same key.
escapes_doc
Simple escape check: does the program’s final value contain references to the input document (i.e. survive returning)? If not, the compiler may emit value-copying ops rather than Arc-sharing to free the original doc.
expr_is_pure
True if the expression is pure — no side-effecting global calls or unknown methods. Enables dropping unused let initialisers safely.
expr_uses_ident
True if any sub-expression references name as a bare identifier. Walks the AST without compiling. Shadowing by inner let / lambda / comprehension binders is respected: inner bindings with the same name hide the outer one.
find_common_subexprs
Find sub-programs (via Arc<Program> pointers inside opcodes) that appear multiple times across the program tree. Returns a map of signature → count for analysis / potential reuse.
fold_kind_check
When a KindCheck is applied to a value with a statically-known type, the check can be constant-folded to true/false at compile time.
infer_monotonicity
Walk program and determine monotonicity of the final result.
infer_result_type
Walk opcodes of program forward, simulating a stack of AbstractVals. Returns the top-of-stack abstract value at program end (i.e. the result type).
infer_result_type_with_env
Same as infer_result_type but exposes the bindings environment after the program finishes — useful for debugging the interprocedural flow.
method_result_type
Static result-type mapping for builtin methods (conservative).
opcode_cost
Rough, ordinal cost estimate per opcode. Not a wall-clock number — only useful to compare relative cost (e.g. for AndOp operand reordering).
program_cost
Total cost of a program (sum of per-op costs).
program_signature
Hash a program’s opcode sequence into a stable identifier. Two programs with the same opcodes hash to the same value — enables CSE across let initialisers or parallel -> binds.
selectivity_score
Rough selectivity estimate for AST predicates. Lower score → more selective (filters out more rows). Used to reorder and operands so cheaper / more-selective predicate runs first (short-circuit friendly).