Files
concrete/compilers/concrete-compiler/compiler/lib/Support/CompilationFeedback.cpp
Andi Drebes c8c969773e Rebase onto llvm-project 465ee9bfb26d with local changes
This commit rebases the compiler onto commit 465ee9bfb26d from
llvm-project with locally maintained patches on top, i.e.:

  * 5d8669d669ee: Fix the element alignment (size) for memrefCopy
  * 4239163ea337: fix: Do not fold the memref.subview if the offset are
                  != 0 and strides != 1
  * 72c5decfcc21: remove github stuff from llvm
  * 8d0ce8f9eca1: Support arbitrary element types in named operations
                  via attributes
  * 94f64805c38c: Copy attributes of scf.for on bufferization and make
                  it an allocation hoisting barrier

Main upstream changes from llvm-project that required modification of
concretecompiler:

  * Switch to C++17
  * Various changes in the interfaces for linalg named operations
  * Transition from `llvm::Optional` to `std::optional`
  * Use of enums instead of string values for iterator types in linalg
  * Changed default naming convention of getter methods in
    ODS-generated operation classes from `some_value()` to
    `getSomeValue()`
  * Renaming of Arithmetic dialect to Arith
  * Refactoring of side effect interfaces (i.e., renaming from
    `NoSideEffect` to `Pure`)
  * Re-design of the data flow analysis framework
  * Refactoring of build targets for Python bindings
  * Refactoring of array attributes with integer values
  * Renaming of `linalg.init_tensor` to `tensor.empty`
  * Emission of `linalg.map` operations in bufferization of the Tensor
    dialect requiring another linalg conversion pass and registration
    of the bufferization op interfaces for linalg operations
  * Refactoring of the one-shot bufferizer
  * Necessity to run the expand-strided-metadata, affine-to-std and
    finalize-memref-to-llvm passes before converson to the LLVM
    dialect
  * Renaming of `BlockAndValueMapping` to `IRMapping`
  * Changes in the build function of `LLVM::CallOp`
  * Refactoring of the construction of `llvm::ArrayRef` and
    `llvm::MutableArrayRef` (direct invocation of constructor instead
    of builder functions for some cases)
  * New naming conventions for generated SSA values requiring rewrite
    of some check tests
  * Refactoring of `mlir::LLVM::lookupOrCreateMallocFn()`
  * Interface changes in generated type parsers
  * New dependencies for to mlir_float16_utils and
    MLIRSparseTensorRuntime for the runtime
  * Overhaul of MLIR-c deleting `mlir-c/Registration.h`
  * Deletion of library MLIRLinalgToSPIRV
  * Deletion of library MLIRLinalgAnalysis
  * Deletion of library MLIRMemRefUtils
  * Deletion of library MLIRQuantTransforms
  * Deletion of library MLIRVectorToROCDL
2023-03-09 17:47:16 +01:00

114 lines
4.1 KiB
C++

// Part of the Concrete Compiler Project, under the BSD3 License with Zama
// Exceptions. See
// https://github.com/zama-ai/concrete-compiler-internal/blob/main/LICENSE.txt
// for license information.
#include <cassert>
#include <fstream>
#include "boost/outcome.h"
#include "llvm/Support/JSON.h"
#include "concretelang/Support/CompilationFeedback.h"
namespace mlir {
namespace concretelang {
void CompilationFeedback::fillFromClientParameters(
::concretelang::clientlib::ClientParameters params) {
// Compute the size of secret keys
totalSecretKeysSize = 0;
for (auto sk : params.secretKeys) {
totalSecretKeysSize += sk.byteSize();
}
// Compute the boostrap keys size
totalBootstrapKeysSize = 0;
for (auto bskParam : params.bootstrapKeys) {
assert(bskParam.inputSecretKeyID < params.secretKeys.size());
auto inputKey = params.secretKeys[bskParam.inputSecretKeyID];
assert(bskParam.outputSecretKeyID < params.secretKeys.size());
auto outputKey = params.secretKeys[bskParam.outputSecretKeyID];
totalBootstrapKeysSize +=
bskParam.byteSize(inputKey.lweSize(), outputKey.lweSize());
}
// Compute the keyswitch keys size
totalKeyswitchKeysSize = 0;
for (auto kskParam : params.keyswitchKeys) {
assert(kskParam.inputSecretKeyID < params.secretKeys.size());
auto inputKey = params.secretKeys[kskParam.inputSecretKeyID];
assert(kskParam.outputSecretKeyID < params.secretKeys.size());
auto outputKey = params.secretKeys[kskParam.outputSecretKeyID];
totalKeyswitchKeysSize +=
kskParam.byteSize(inputKey.lweSize(), outputKey.lweSize());
}
// Compute the size of inputs
totalInputsSize = 0;
for (auto gate : params.inputs) {
totalInputsSize += gate.byteSize(params.secretKeys);
}
// Compute the size of outputs
totalOutputsSize = 0;
for (auto gate : params.outputs) {
totalOutputsSize += gate.byteSize(params.secretKeys);
}
// Extract CRT decomposition
crtDecompositionsOfOutputs = {};
for (auto gate : params.outputs) {
std::vector<int64_t> decomposition;
if (gate.encryption.has_value()) {
decomposition = gate.encryption->encoding.crt;
}
crtDecompositionsOfOutputs.push_back(decomposition);
}
}
outcome::checked<CompilationFeedback, StringError>
CompilationFeedback::load(std::string jsonPath) {
std::ifstream file(jsonPath);
std::string content((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>()));
if (file.fail()) {
return StringError("Cannot read file: ") << jsonPath;
}
auto expectedCompFeedback = llvm::json::parse<CompilationFeedback>(content);
if (auto err = expectedCompFeedback.takeError()) {
return StringError("Cannot open client parameters: ")
<< llvm::toString(std::move(err)) << "\n"
<< content << "\n";
}
return expectedCompFeedback.get();
}
llvm::json::Value toJSON(const mlir::concretelang::CompilationFeedback &v) {
llvm::json::Object object{
{"complexity", v.complexity},
{"pError", v.pError},
{"globalPError", v.globalPError},
{"totalSecretKeysSize", v.totalSecretKeysSize},
{"totalBootstrapKeysSize", v.totalBootstrapKeysSize},
{"totalKeyswitchKeysSize", v.totalKeyswitchKeysSize},
{"totalInputsSize", v.totalInputsSize},
{"totalOutputsSize", v.totalOutputsSize},
{"crtDecompositionsOfOutputs", v.crtDecompositionsOfOutputs},
};
return object;
}
bool fromJSON(const llvm::json::Value j,
mlir::concretelang::CompilationFeedback &v, llvm::json::Path p) {
llvm::json::ObjectMapper O(j, p);
return O && O.map("complexity", v.complexity) && O.map("pError", v.pError) &&
O.map("globalPError", v.globalPError) &&
O.map("totalSecretKeysSize", v.totalSecretKeysSize) &&
O.map("totalBootstrapKeysSize", v.totalBootstrapKeysSize) &&
O.map("totalKeyswitchKeysSize", v.totalKeyswitchKeysSize) &&
O.map("totalInputsSize", v.totalInputsSize) &&
O.map("totalOutputsSize", v.totalOutputsSize) &&
O.map("crtDecompositionsOfOutputs", v.crtDecompositionsOfOutputs);
}
} // namespace concretelang
} // namespace mlir