Compose a pass pipeline and write passes
A Pipeline is an ordered sequence of MLIR passes that a compiled callable runs on each captured module before Backend lowering. Use this procedure to capture a function, compose native and Python passes, inspect their effect, and compile the function with the composed pipeline. Compile pipeline defines the stages that surround the caller pipeline.
Prerequisites
Build FHElium with its compiler extension, fhelium._mlir, as described in Installation. Have an Eager function over Ciphertext arguments, an Engine, and representative inputs.
1. Capture a call
from fhelium import compile as fc
captured = fc.capture(rotated_square, engine, (x,))
print(captured.to_text())
print(captured.materials.keys())2
3
4
5
fc.capture(function, engine, arguments) calls the function once and returns a CapturedFunction that holds the MLIR module, its context, and the material bindings. captured.function is the @entry function, captured.preparation the preparation function after outlining, and captured.callees the private bootstrap callees. Capture records the state Eager assigns, so the printed types show the depth, scale, domain, residue representation, and component count of every value.
2. Compose native passes
A string entry is a textual pass-pipeline element anchored at builtin.module. Function passes are nested under func.func(...):
pipeline = fc.Pipeline((*fc.default_pipeline().passes, "func.func(canonicalize)"))
pipeline.run(captured)2
fc.default_pipeline() assigns unassigned CKKS depths and scales, folds identities, places relinearization, rescaling and alignment for fhelium_logical arithmetic, lowers that arithmetic to CKKS operations, and eliminates common subexpressions; default_pipeline(entry_depth=d) assigns depth d to arguments without one, and default_pipeline(placement="all") also lets placement and rotation factoring rewrite written CKKS relinearizations, rescales and rotations. pipeline.numerics() pairs each entry with whether it can change ciphertext residues (None for Python passes and for passes it does not list). Upstream MLIR passes registered in the compiler, such as canonicalize and cse, are available by name. Pipeline.run rewrites the module in place and returns the CapturedFunction. With verify=True, the default, the module is verified after every pass; run(captured, timing=True) reports pass timing. fc.compile outlines the preparation function after the caller's pipeline, as it lowers and fuses after it, so a pipeline does not include fhelium-outline-preparation; the pass is available by that name for inspecting a captured module outside fc.compile.
3. Write a Python pass
A callable entry run(op, pass_) is a Python pass on the module. It rewrites operations through the MLIR Python bindings in fhelium._mlir:
from fhelium._mlir import ir
from fhelium._mlir.dialects import fhelium_ckks as ck
def double_self_additions(module, pass_):
def visit(op):
if op.name == "fhelium_ckks.add" and op.operands[0] == op.operands[1]:
with ir.InsertionPoint(op), op.location:
doubled = ck.MultiplyIntegerScalarOp(op.operands[0], scalar=2)
op.results[0].replace_all_uses_with(doubled.result)
return ir.WalkResult.ADVANCE
module.walk(visit)
pipeline = fc.Pipeline((double_self_additions, "func.func(canonicalize)", *fc.default_pipeline().passes))2
3
4
5
6
7
8
9
10
11
12
13
14
Operation builders take operands and attributes; native type inference computes the result type, so a rewrite does not compute value state itself. A pass that cannot apply calls pass_.signal_pass_failure(). A pass must not keep operation handles after it returns, because later native passes may erase those operations. A rewrite that leaves an operation unused relies on canonicalize to erase it.
4. Compile with the pipeline
compiled = fc.compile(rotated_square, engine=engine, pipeline=pipeline)
result = compiled(x)
(specialization,) = compiled.specializations
print(specialization.timings)2
3
4
The pipeline runs on every captured specialization, followed by preparation outlining, the lowering stage, and linking. pipeline= also accepts a function from the CallSignature to a Pipeline, which selects a pipeline per signature.
5. Replace the lowering stage
lowering= replaces the stage that runs on the entry and its callees after preparation outlining. fc.default_lowering(signature) returns the default for a CallSignature or for the call arguments:
| Pass | Options |
|---|---|
func.func(fhelium-hoist-rotations{...}) | residue-bytes, the residue size of the Ciphertext arguments; memory-limit, 90% of the total memory of that device, set when every Ciphertext argument is on one CUDA device |
func.func(fhelium-lower-ckks-to-rns{key-switches=true}) | key-switches=false keeps key switches as whole operations |
func.func(fhelium-fuse-rns) | Included when a registered fusion implementation covers the device type of the Ciphertext arguments; the built-in Triton implementation covers CUDA |
A function from the signature derives the stage from the default. The following lowering omits fusion, so each RNS and NTT operation links to its own Backend implementation:
def unfused(signature):
passes = fc.default_lowering(signature).passes
return fc.Pipeline(tuple(entry for entry in passes if "fhelium-fuse-rns" not in entry))
compiled = fc.compile(rotated_square, engine=engine, lowering=unfused)2
3
4
5
Filtering out fhelium-hoist-rotations in the same way keeps each rotation separate, and replacing the lowering entry with "func.func(fhelium-lower-ckks-to-rns{key-switches=false})" keeps key switches whole. Textual or Python passes inserted between the lowering and fusion entries run on the RNS/NTT function. With the persistent cache, lowering=None is keyed as the default stage, and a custom stage by its textual entries and the source identities of its Python passes; a Python pass whose source the scan does not identify makes the call ineligible for the disk cache, as it does in pipeline.
Verify the outcome
Compare the operation counts of captured.function before and after the pipeline, and compare the compiled result with Eager execution of the source and with a clear-message oracle. A rewrite that preserves the arithmetic of each replaced operation gives equal residues to Eager. Example 13 composes native passes, and example 14 defines a Python rewrite pass.