fix(compiler): Do not rely on the input type on ConcreteToBConcrete conversion as it could be already rewritten by a previous pattern (close #809)

This commit is contained in:
Quentin Bourgerie
2022-12-02 18:35:46 +01:00
parent f05b1bd1ea
commit 1f45b9dfa5
2 changed files with 38 additions and 11 deletions

View File

@@ -208,12 +208,10 @@ struct LowerKeySwitch : public mlir::OpRewritePattern<
// construct attributes for in/out dimensions
mlir::concretelang::Concrete::LweCiphertextType outType = ksOp.getType();
auto outDimAttr = rewriter.getI32IntegerAttr(outType.getDimension());
auto inputType =
ksOp.ciphertext()
.getType()
.cast<mlir::concretelang::Concrete::LweCiphertextType>();
mlir::IntegerAttr inputDimAttr =
rewriter.getI32IntegerAttr(inputType.getDimension());
auto inputType = converter.convertType(ksOp.ciphertext().getType())
.cast<mlir::RankedTensorType>();
auto inputDimension = inputType.getShape().back() - 1;
mlir::IntegerAttr inputDimAttr = rewriter.getI32IntegerAttr(inputDimension);
mlir::Operation *bKeySwitchOp = rewriter.replaceOpWithNewOp<
mlir::concretelang::BConcrete::KeySwitchLweTensorOp>(
@@ -286,11 +284,11 @@ struct LowerBootstrap : public mlir::OpRewritePattern<
ConcreteToBConcreteTypeConverter converter;
mlir::concretelang::Concrete::LweCiphertextType outType = bsOp.getType();
auto inputType =
bsOp.input_ciphertext()
.getType()
.cast<mlir::concretelang::Concrete::LweCiphertextType>();
auto inputDimAttr = rewriter.getI32IntegerAttr(inputType.getDimension());
auto inputType = converter.convertType(bsOp.input_ciphertext().getType())
.cast<mlir::RankedTensorType>();
auto inputDimension = inputType.getShape().back() - 1;
mlir::IntegerAttr inputDimAttr = rewriter.getI32IntegerAttr(inputDimension);
auto outputPrecisionAttr = rewriter.getI32IntegerAttr(outType.getP());
mlir::Operation *bBootstrapOp = rewriter.replaceOpWithNewOp<
mlir::concretelang::BConcrete::BootstrapLweTensorOp>(

View File

@@ -289,3 +289,32 @@ tests:
- scalar: 63
outputs:
- scalar: 12
---
# https://github.com/zama-ai/concrete-compiler-internal/issues/809
description: bug_809
program: |
func.func @main(%arg0: !FHE.eint<6>) -> !FHE.eint<6> {
%cst = arith.constant dense<[1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1]> : tensor<64xi64>
%0 = "FHE.apply_lookup_table"(%arg0, %cst) : (!FHE.eint<6>, tensor<64xi64>) -> !FHE.eint<6>
%cst_0 = arith.constant dense<[0, 10, 20, 30, 40, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73]> : tensor<64xi64>
%1 = "FHE.apply_lookup_table"(%arg0, %cst_0) : (!FHE.eint<6>, tensor<64xi64>) -> !FHE.eint<6>
%2 = "FHE.add_eint"(%0, %1) : (!FHE.eint<6>, !FHE.eint<6>) -> !FHE.eint<6>
return %2 : !FHE.eint<6>
}
tests:
- inputs:
- scalar: 0
outputs:
- scalar: 1
- inputs:
- scalar: 5
outputs:
- scalar: 15
- inputs:
- scalar: 62
outputs:
- scalar: 72
- inputs:
- scalar: 63
outputs:
- scalar: 74