mirror of
https://github.com/zama-ai/concrete.git
synced 2026-02-10 20:55:02 -05:00
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
112 lines
3.4 KiB
C++
112 lines
3.4 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 "mlir/Dialect/Arith/IR/Arith.h"
|
|
#include "mlir/Dialect/Func/IR/FuncOps.h"
|
|
#include "mlir/Transforms/DialectConversion.h"
|
|
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
|
|
|
|
#include "concretelang/Dialect/FHE/IR/FHEOps.h"
|
|
#include "concretelang/Dialect/FHE/Transforms/Max/Max.h"
|
|
|
|
namespace arith = mlir::arith;
|
|
namespace func = mlir::func;
|
|
|
|
namespace FHE = mlir::concretelang::FHE;
|
|
|
|
/// This rewrite pattern transforms all instances
|
|
/// of `FHE.max_eint` to `max(x - y, 0) + y`.
|
|
struct MaxEintPattern : public mlir::OpRewritePattern<FHE::MaxEintOp> {
|
|
MaxEintPattern(mlir::MLIRContext *context)
|
|
: mlir::OpRewritePattern<FHE::MaxEintOp>(context) {}
|
|
|
|
mlir::LogicalResult
|
|
matchAndRewrite(FHE::MaxEintOp maxEintOp,
|
|
mlir::PatternRewriter &rewriter) const override {
|
|
|
|
const mlir::Location loc = maxEintOp->getLoc();
|
|
|
|
const FHE::FheIntegerInterface outputTy =
|
|
maxEintOp->getResult(0).getType().cast<FHE::FheIntegerInterface>();
|
|
const int64_t outputBitWidth = outputTy.getWidth();
|
|
|
|
mlir::Value x = maxEintOp.getX();
|
|
mlir::Value y = maxEintOp.getY();
|
|
|
|
const auto xTy = x.getType().cast<FHE::FheIntegerInterface>();
|
|
const auto yTy = y.getType().cast<FHE::FheIntegerInterface>();
|
|
|
|
const auto signedTy = FHE::EncryptedSignedIntegerType::get(
|
|
this->getContext(), outputBitWidth);
|
|
|
|
if (xTy.isUnsigned()) {
|
|
x = rewriter.create<FHE::ToSignedOp>(loc, signedTy, x).getResult();
|
|
}
|
|
if (yTy.isUnsigned()) {
|
|
y = rewriter.create<FHE::ToSignedOp>(loc, signedTy, y).getResult();
|
|
}
|
|
|
|
const mlir::Value sub =
|
|
rewriter.create<FHE::SubEintOp>(loc, x, y).getResult();
|
|
|
|
const int64_t lutSize = 1 << outputBitWidth;
|
|
|
|
auto lutValues = std::vector<int64_t>();
|
|
for (int64_t i = 0; i < lutSize / 2; i++) {
|
|
lutValues.push_back(i);
|
|
}
|
|
for (int64_t i = 0; i < lutSize / 2; i++) {
|
|
lutValues.push_back(0);
|
|
}
|
|
|
|
const mlir::Attribute lutAttr = rewriter.getI64TensorAttr(lutValues);
|
|
const mlir::Value lut =
|
|
rewriter.create<arith::ConstantOp>(loc, lutAttr).getResult();
|
|
|
|
const mlir::Value max =
|
|
rewriter.create<FHE::ApplyLookupTableEintOp>(loc, outputTy, sub, lut)
|
|
.getResult();
|
|
|
|
const mlir::Value add =
|
|
rewriter.create<FHE::AddEintOp>(loc, max, maxEintOp.getY()).getResult();
|
|
|
|
rewriter.replaceOp(maxEintOp, {add});
|
|
return mlir::success();
|
|
};
|
|
};
|
|
|
|
namespace {
|
|
|
|
struct FHEMaxTransform : public FHEMaxTransformBase<FHEMaxTransform> {
|
|
void runOnOperation() final;
|
|
};
|
|
|
|
void FHEMaxTransform::runOnOperation() {
|
|
auto target = mlir::ConversionTarget(this->getContext());
|
|
target.addLegalDialect<arith::ArithDialect>();
|
|
target.addLegalDialect<FHE::FHEDialect>();
|
|
target.addIllegalOp<FHE::MaxEintOp>();
|
|
|
|
auto patterns = mlir::RewritePatternSet(&this->getContext());
|
|
patterns.insert<MaxEintPattern>(&this->getContext());
|
|
|
|
mlir::Operation *op = this->getOperation();
|
|
if (mlir::applyPatternsAndFoldGreedily(op, std::move(patterns)).failed()) {
|
|
this->signalPassFailure();
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
namespace mlir {
|
|
namespace concretelang {
|
|
|
|
std::unique_ptr<mlir::OperationPass<>> createFHEMaxTransformPass() {
|
|
return std::make_unique<FHEMaxTransform>();
|
|
}
|
|
|
|
} // namespace concretelang
|
|
} // namespace mlir
|