Commit Graph

57 Commits

Author SHA1 Message Date
Quentin Bourgerie
b598f0ffca refactor(compiler): Move the conversion from scf to std on the pipeline intead of embedded in a pass 2021-11-12 16:50:29 +01:00
youben11
99cce18c6a feat: get RT lib path from py and use as sharedlib
Try to find the runtime library automatically (should only work on
proper installation of the package), and fail silently by not passing
any RT lib. The RT lib can also be specified manually. The RT lib will
be used as a shared library by the JIT compiler.
2021-11-12 15:06:49 +01:00
Andi Drebes
3118983287 enhance(compiler): Add function JITLambda::Arguments::getResultWidth
Add a new method `JITLambda::Arguments::getResultWidth` returning the
width of a scalar result or the element type of a tensor result at a
given position.
2021-11-12 12:00:39 +01:00
Andi Drebes
e4cd340e36 enhance(compiler): JIT: Support result tensors with arbitrary element types
Currently, `JITLambda::Arguments` assumes result tensors are always
composed of `uint64_t` elements. This change adds support for
arbitrary scalar element types.
2021-11-12 12:00:39 +01:00
youben11
56e261d140 fix(compiler): pass dimensions at TensorLambdaArg creation 2021-11-08 11:55:02 +01:00
Andi Drebes
a670ee3f85 enhance(compiler): Use const pointers in JITLambda::Arguments::setArg
All results in code compiled by zamacompiler are passed as return
values, which means that all tensors passed as function arguments are
constant inputs that are never written.

This patch changes the arguments used as data pointers for input
tensors in `JITLambda::Arguments::setArg()` from `void*` to `const
void*` to emphasize their use as inputs and to allow for constant
arrays to be passed as function inputs.
2021-11-04 19:07:54 +01:00
Andi Drebes
1ad3d57f66 feat(compiler): Add method JITLambda::Argument::getResultType(size_t)
Add a method `JITLambda::Argument::getResultType(size_t pos)` that
returns the type of the result with the index `pos` of a
`JITLambda::Argument`.
2021-11-03 18:32:06 +01:00
Andi Drebes
5999d402ec enhance(compiler): CompilerEngine: Forward parse errors instead of printing them
By default, `mlir::SourceMgrDiagnosticVerifierHandler` used by
`CompilerEngine::compile()` prints parse errors to
`llvm::errs()`. This makes it impossible for a caller of
`CompilerEngine::compile()` to process parse errors or to suppress the
emission of error messages to the standard error stream altogether.

This change captures parse errors in a string via a string-backed
output stream and forwards the error message in the `llvm::Error`
instance of the return value.
2021-11-03 12:05:12 +01:00
Quentin Bourgerie
be92b4580d Merge branch 'master' into hlfhelinalg-binary-op-lowering 2021-10-29 13:47:35 +02:00
Quentin Bourgerie
85d102c9b2 refactor(compiler): Simplify the compiler flow and re enable --passes compiler option
No more need to compute the fhe context at high level
2021-10-29 13:44:34 +02:00
Quentin Bourgerie
41cba63113 refactor(compiler): Move the keyset generation from CompilerEngine to JitCompilerEngine 2021-10-29 13:44:34 +02:00
Andi Drebes
1187cfbd62 refactor(compiler): Refactor CompilerEngine and related classes
This commit contains several incremental improvements towards a clear
interface for lambdas:

  - Unification of static and JIT compilation by using the static
    compilation path of `CompilerEngine` within a new subclass
    `JitCompilerEngine`.

  - Clear ownership for compilation artefacts through
    `CompilationContext`, making it impossible to destroy objects used
    directly or indirectly before destruction of their users.

  - Clear interface for lambdas generated by the compiler through
    `JitCompilerEngine::Lambda` with a templated call operator,
    encapsulating otherwise manual orchestration of `CompilerEngine`,
    `JITLambda`, and `CompilerEngine::Argument`.

  - Improved error handling through `llvm::Expected<T>` and proper
    error checking following the conventions for `llvm::Expected<T>`
    and `llvm::Error`.

Co-authored-by: youben11 <ayoub.benaissa@zama.ai>
2021-10-29 13:44:34 +02:00
Andi Drebes
e76aee7e10 feat(compiler): Add class StreamStringError with a stream interface for llvm::Error
Composing error messages for `llvm::Error` is either done by using
`llvm::createStringError()` with an appropriate format string and
arguments or by writing to a `std::string`-backed
`llvm::raw_string_ostream` and passing the result to
`llvm::make_error<llvm::StringError>()` verbatim.

The new class `StreamStringError` encapsulates the latter solution
into a class with an appropriate stream operator and implicit cast
operators to `llvm::Error` and `llvm::Expected`.

Example usage:

   llvm::Error foo(int i, size_t s, ...) {
      ...
      if(...) {
        return StreamStringError()
               << "Some error message with an integer: "
               << i << " and a size_t: " << s;
      }
      ...
   }
2021-10-29 13:44:34 +02:00
Andi Drebes
b12be45143 feat(compiler): Add method getResultVectorSize to JITLambda::Argument
Add method `JITLambda::Argument::getResultVectorSize` that returns the
number of elements of the result if the result is a vector.
2021-10-29 13:44:34 +02:00
Quentin Bourgerie
ba54560680 feat(compiler): Lower HLFHELinalg binary operators 2021-10-29 12:17:38 +02:00
Quentin Bourgerie
0d4e10169b feat(compiler): Introduce the HLFHELinalg dialect and a first operator HLFHELinalg.add_eint_int 2021-10-29 12:16:41 +02:00
youben11
ab7a208112 fix: store OpPassManager& before call to addPass
weird bug with c++ 6.3 on the manylinux_2_24 image (Debian9) generating
erroneous asm instructions for call to nest on PassManager
2021-10-26 16:31:04 +02:00
youben11
7b29600721 refactor: don't use designated initializers 2021-10-26 16:31:04 +02:00
Quentin Bourgerie
247cc489c5 refactor(compiler): Refactor JITLambda::Argument::setArg 2021-10-21 14:40:07 +02:00
Quentin Bourgerie
3b2f21a715 enhance(compiler): Handle multi-dimensional tensor #167 2021-10-15 16:44:19 +02:00
youben11
01757fa6d5 fix: forward errors instead of creating new ones
LLVM errors should be handled/consumed. Creating a new one and leaving
the previous one alive will crash the compiler. Whenever we don't want a
crash (e.g. logging the error is enough), but still wanna continue the
execution, we can just consume it.
2021-10-12 11:50:15 +01:00
youben11
3406b322d5 fix: prefix builtin ops w/ builtin. 2021-10-07 14:38:50 +01:00
youben11
7a48900fae chore: build zamacompiler as an LLVM external proj 2021-10-07 14:38:50 +01:00
Andi Drebes
2acfa63eb7 feat(compiler): Determine FHE circuit constraints instead of using default values
This replaces the default FHE circuit constrains (maximum encrypted
integer width of 7 bits and a Minimal Arithmetic Noise Padding of 10
with the results of the `MaxMANP` pass, which determines these values
automatically from the input program.

Since the maximum encrypted integer width and the maximum value for
the Minimal Arithmetic Noise Padding can only be derived from HLFHE
operations, the circuit constraints are determined automatically by
`zamacompiler` only if the option `--entry-dialect=hlfhe` was
specified.

For lower-level dialects, `zamacompiler` has been provided with the
options `--assume-max-eint-precision=...` and `--assume-max-manp=...`
that allow a user to specify the values for the maximum required
precision and maximum values for the Minimal Arithmetic Noise Padding.
2021-09-28 11:35:58 +02:00
Andi Drebes
2ed1720234 bugfix(compiler): Do not apply offset of -1 to 2-norm in getV0Parameter
A value of 1 is perfectly valid for the 2-norm of the constant vector
of a dot operation. Hence, the log2 of that value of 0 is also
perfectly valid. However, the offset of -1 in `getV0Parameter` applied
to that value yields an offset into a static lookup table of -1, which
triggers a segmentation fault.

This patch removes the offset of -1 for the log2 value of the 2-norm
in `getV0Parameter`.
2021-09-28 11:35:58 +02:00
Andi Drebes
54661528a8 feat(compiler): Add action dump-hlfhe-manp
The new option --acion=dump-hlfhe-manp invokes the Minimal Arithmetic
Noise Padding Analysis pass based on the squared 2-norm metric from
`lib/Dialect/HLFHE/Analysis/MANP.cpp` and dumps the module afterwards
with an extra attribute `MANP` for each HLFHE operation.
2021-09-28 11:35:58 +02:00
Andi Drebes
30374ebb2c refactor(compiler): Introduce compilation pipeline with multiple entries / exits
This refactoring commit restructures the compilation pipeline of
`zamacompiler`, such that it is possible to enter and exit the
pipeline at different points, effectively defining the level of
abstraction at the input and the required level of abstraction for the
output.

The entry point is specified using the `--entry-dialect`
argument. Valid choices are:

  `--entry-dialect=hlfhe`:   Source contains HLFHE operations
  `--entry-dialect=midlfhe`: Source contains MidLFHE operations
  `--entry-dialect=lowlfhe`: Source contains LowLFHE operations
  `--entry-dialect=std`:     Source does not contain any FHE Operations
  `--entry-dialect=llvm`:    Source is in LLVM dialect

The exit point is defined by an action, specified using --action.

  `--action=roundtrip`:
     Parse the source file to in-memory representation and immediately
     dump as text without any processing

  `--action=dump-midlfhe`:
     Lower source to MidLFHE and dump result as text

  `--action=dump-lowlfhe`:
     Lower source to LowLFHE and dump result as text

  `--action=dump-std`:
     Lower source to only standard MLIR dialects (i.e., all FHE
     operations have already been lowered)

  `--action=dump-llvm-dialect`:
     Lower source to MLIR's LLVM dialect (i.e., the LLVM dialect, not
     LLVM IR)

  `--action=dump-llvm-ir`:
     Lower source to plain LLVM IR (i.e., not the LLVM dialect, but
     actual LLVM IR)

  `--action=dump-optimized-llvm-ir`:
     Lower source to plain LLVM IR (i.e., not the LLVM dialect, but
     actual LLVM IR), pass the result through the LLVM optimizer and
     print the result.

  `--action=dump-jit-invoke`:
     Execute the full lowering pipeline to optimized LLVM IR, JIT
     compile the result, invoke the function specified in
     `--jit-funcname` with the parameters from `--jit-args` and print
     the functions return value.
2021-09-28 11:35:58 +02:00
Andi Drebes
ddebedd1d6 refactor(compiler): Make function getV0Parameter return a pointer to a constant
The function `getV0Parameter()` returns a pointer to a static array,
which is not intended to be mutable. Reflect this in the return type
of the function.
2021-09-28 11:35:58 +02:00
Andi Drebes
6a76177a47 refactor(compiler): Move JIT functionality to separate source file 2021-09-28 11:35:58 +02:00
Andi Drebes
b9e2690823 refactor(compiler): Replace LOG_{VERBOSE,ERROR} macros with C++-style streams
Replace the macros `LOG_VERBOSE` and `LOG_ERROR` with C++-style
streams retrieved through `log_verbose()` and `log_error()`. This
aligns with the `MLIR` infrastructure and avoids pollution of the
global namespace through a common header file in subsequent
refactoring commits splitting the functionality of `src/main.cpp` into
multiple files.
2021-09-28 11:35:58 +02:00
Quentin Bourgerie
b4776a619d test(compiler): Reenable 7 bits test, we should change how the test are done to take care of the probability of exactness 2021-09-27 13:37:11 +02:00
Quentin Bourgerie
0f93fa1dcc fix(compiler/parameters): new parameters for v0 (p=0.99993 for 1-6 and 0.95 for 7) 2021-09-27 13:37:11 +02:00
Quentin Bourgerie
7454c30485 fix(compiler/parameters): Update parameters 2021-09-27 13:37:11 +02:00
Quentin Bourgerie
d9be22c1b1 fix(compiler/runtime): decode with integers 2021-09-27 13:37:11 +02:00
Quentin Bourgerie
3a254bcb87 fix(compiler): Integrate the security estimator to compute variances 2021-09-20 13:46:08 +02:00
Quentin Bourgerie
8cc0af1220 fix(compiler): Add a pass to unparametrize LowLFHE ciphertext to remove unrelized_convesrion_cast for linalg bufferization 2021-09-09 20:35:28 +02:00
youben11
746d991af6 refactor: ins forward decl w generic ty @pass-init
Insert forward declarations with generic types at pass initialization.
More docs for all the pass for lowering LUT
2021-09-09 20:35:28 +02:00
youben11
3893ad39bb fix: divide and round to decode decrypted ct
Doing a right shift will ommit the bits that can be used to round to the
nearest integer
2021-09-09 20:35:28 +02:00
youben11
c6b1480cc6 feat: generate runtime context at Argument creation 2021-09-09 20:35:28 +02:00
youben11
8aee5dd5c3 chore(runtime): link concrete statically with runtime lib 2021-09-09 20:35:28 +02:00
Quentin Bourgerie
d03adbd7dc enhance(compiler): Add options for verbose passes 2021-09-08 11:44:51 +02:00
Quentin Bourgerie
de7129fe8e feat(compiler/llvm-pipeline): Bufferize linalg and convert to loops 2021-09-08 11:44:51 +02:00
Quentin Bourgerie
697d4033e1 fix(compiler): typo 2021-09-06 10:26:40 +02:00
Quentin Bourgerie
af0789f128 enhance(compiler/runtime): Add runtime tools to handle tensor inputs and outputs 2021-09-06 10:26:40 +02:00
Quentin Bourgerie
bc975d904e feat(compiler): introduce bufferization passes in lowering pipeline to llvm 2021-09-06 10:26:40 +02:00
Quentin Bourgerie
19704b4b13 fix(compiler/lowlfhe): Use the lwe ciphertext parameters to allocate instead of global fhe context 2021-08-24 16:29:04 +02:00
Quentin Bourgerie
67f0fc0f45 enhance(compiler): Introduce MidLFHE dag parametrization 2021-08-24 16:21:31 +02:00
Quentin Bourgerie
8057ee7553 refactor(compiler): Prepare the MidLFHE parameters injection 2021-08-24 16:21:31 +02:00
youben11
f948db1228 feat(python): CompilerEngine to compile and run 2021-08-18 17:38:46 +02:00
Quentin Bourgerie
8796754513 fix(compiler/lowlfhe): Fix wrong shift value for EncodeIntOp lowering
That's work before because we enfoce 6 as global constraint while our runJit tests are on 7 bits
2021-08-16 18:54:56 +02:00