Inspect compiled IR
A compiled function passes through three printable forms: the captured fhelium_ckks module, the module after the caller pipeline and preparation outlining, and the lowered fhelium_rns/fhelium_ntt module that is linked to the Backend. Use this procedure to print each form, read value state from types, count operations per stage, and check the captured function against Eager. Compiler IR defines the dialects and types.
Prerequisites
Have a function over Ciphertext arguments, an Engine, and representative inputs, as in Compile a callable.
1. Print the captured module
from fhelium import compile as fc
captured = fc.capture(rotated_square, engine, (x,))
print(captured.to_text())2
3
4
The printed module defines the alias #chain for the modulus-chain attribute once and refers to it in every type. Each ciphertext type lists depth, scale, domain, residues, and components; each key material lists its role, prime_ids, and digit partition. Prime ids below the number of Q primes name Q primes, and the following ids name P primes.
2. Read value state from types
from fhelium.compile import mlir as fm
for op in captured.function.entry_block.operations:
for result in op.results:
state = fm.value_state(result.type)
if state.kind is not None:
print(op.name, state.depth, state.scale, state.polynomial_domain, state.component_count)2
3
4
5
6
7
value_state returns a ValueState with the kind, depth, actual scale, polynomial domain, residue representation, plaintext encoding, component count, modulus basis, batch shape, and compressed-plaintext layout of a type; kind is None for other types. Depth -1 and scale 0 mark state that the assignment passes have not filled.
3. Compare stages
from collections import Counter
def counts(function):
return Counter(op.operation.name for op in function.entry_block.operations)
pipeline = fc.default_pipeline()
fc.Pipeline((*pipeline.passes, "fhelium-outline-preparation")).run(captured)
print(counts(captured.function))
if captured.preparation is not None:
print(captured.preparation)
compiled = fc.compile(rotated_square, engine=engine, pipeline=pipeline, cache=False)
compiled(x)
(specialization,) = compiled.specializations
lowered = specialization.compiled.captured
print(counts(lowered.function))
print(lowered.to_text())2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fc.compile runs fhelium-outline-preparation after the caller's pipeline; appending it here shows the module that lowering receives. After outlining, ciphertext-independent operations, such as plaintext encoding from Tensors, are in captured.preparation, and @entry reads their results as prepared_<i> materials. The lowered function contains fhelium_rns.table values, fhelium_rns and fhelium_ntt operations, key switches that stayed whole, and fhelium_rns.fused regions whose implementation attribute names the generated kernel family. specialization.compiled exists for a specialization compiled in the current process; a persistent-cache hit has no captured module and records path "trace" or "source". Compile with cache=False to inspect the lowered module of every call.
Pipeline.run(captured, timing=True) prints MLIR pass timing, and specialization.timings holds the host seconds of capture, pipeline, preparation, lowering, linking, and scheduling.
4. Check the captured function against Eager
result = fm.execute(captured, engine, (x,), check_types=True)execute runs each captured operation through the Engine method it was captured from and runs the preparation function first when its results are not bound. With check_types=True, it compares the Eager state of every result with its MLIR type and raises StateMismatch at the first difference, which locates a pass that assigned a state Eager does not produce.
Verify the outcome
The captured module's argument and result types match the depth and scale of the Eager inputs and outputs, and the interpreted result decrypts to the same message as Eager execution of the source. Example 13 prints a captured module and the operation counts after the pipeline and after lowering.