Skip to content

Commit b806be3

Browse files
j2kuncopybara-github
authored andcommitted
Add remaining pipeline plumbing for loop support
PiperOrigin-RevId: 878645040
1 parent f120680 commit b806be3

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

lib/Dialect/Secret/Conversions/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ cc_library(
2424
"@llvm-project//mlir:DialectUtils",
2525
"@llvm-project//mlir:FuncDialect",
2626
"@llvm-project//mlir:IR",
27+
"@llvm-project//mlir:SCFDialect",
2728
"@llvm-project//mlir:Support",
2829
"@llvm-project//mlir:TensorDialect",
2930
"@llvm-project//mlir:TransformUtils",

lib/Dialect/Secret/Conversions/Patterns.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/include/mlir/Dialect/Affine/IR/AffineOps.h" // from @llvm-project
2323
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project
2424
#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
25+
#include "mlir/include/mlir/Dialect/SCF/IR/SCF.h" // from @llvm-project
2526
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
2627
#include "mlir/include/mlir/Dialect/Utils/StaticValueUtils.h" // from @llvm-project
2728
#include "mlir/include/mlir/IR/Attributes.h" // from @llvm-project
@@ -516,15 +517,12 @@ void addSecretToSchemeDefaultConversionTargetsAndPatterns(
516517
ContextAwareTypeConverter& typeConverter) {
517518
target.addLegalDialect<lwe::LWEDialect, arith::ArithDialect,
518519
tensor::TensorDialect>();
519-
target.addLegalOp<ModuleOp>();
520-
521520
target.addIllegalDialect<secret::SecretDialect>();
522-
target.addIllegalOp<mgmt::ModReduceOp, mgmt::RelinearizeOp,
523-
secret::GenericOp>();
521+
target.addIllegalOp<mgmt::ModReduceOp, mgmt::RelinearizeOp>();
524522

525-
target.addDynamicallyLegalOp<affine::AffineForOp, affine::AffineYieldOp>(
526-
[&](Operation* op) { return typeConverter.isLegal(op); });
527-
target.addDynamicallyLegalOp<func::CallOp>(
523+
target.addDynamicallyLegalOp<affine::AffineForOp, affine::AffineYieldOp,
524+
affine::AffineIfOp, scf::ForOp, scf::IfOp,
525+
scf::YieldOp, func::CallOp>(
528526
[&](Operation* op) { return typeConverter.isLegal(op); });
529527
target.markUnknownOpDynamicallyLegal(
530528
[&](Operation* op) { return !hasSecretOperandsOrResults(op); });
@@ -538,7 +536,11 @@ void addSecretToSchemeDefaultConversionTargetsAndPatterns(
538536
SecretGenericOpConversion<tensor::EmptyOp, tensor::EmptyOp>,
539537
SecretGenericFuncCallConversion, ConvertExtractSlice,
540538
ConvertInsertSlice, ConvertAnyContextAware<affine::AffineForOp>,
539+
ConvertAnyContextAware<affine::AffineIfOp>,
541540
ConvertAnyContextAware<affine::AffineYieldOp>,
541+
ConvertAnyContextAware<scf::ForOp>,
542+
ConvertAnyContextAware<scf::IfOp>,
543+
ConvertAnyContextAware<scf::YieldOp>,
542544
ConvertAnyContextAware<tensor::ExtractOp>,
543545
ConvertAnyContextAware<tensor::InsertOp>,
544546
ConvertAnyContextAware<func::CallOp>>(typeConverter,

lib/Pipelines/ArithmeticPipelineRegistration.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lib/Dialect/LWE/Conversions/LWEToLattigo/LWEToLattigo.h"
1111
#include "lib/Dialect/LWE/Conversions/LWEToOpenfhe/LWEToOpenfhe.h"
1212
#include "lib/Dialect/LWE/Transforms/AddDebugPort.h"
13+
#include "lib/Dialect/LWE/Transforms/ImplementTrivialEncryptionAsAddition.h"
1314
#include "lib/Dialect/Lattigo/Transforms/AllocToInPlace.h"
1415
#include "lib/Dialect/Lattigo/Transforms/ConfigureCryptoContext.h"
1516
#include "lib/Dialect/Openfhe/Transforms/AllocToInPlace.h"
@@ -412,6 +413,9 @@ void mlirToRLWEPipeline(OpPassManager& pm,
412413
pm.addPass(createCanonicalizerPass());
413414
pm.addPass(createCSEPass());
414415

416+
// TODO(#2554): skip this pass if the backend supports trivial encryption
417+
pm.addPass(lwe::createImplementTrivialEncryptionAsAddition());
418+
415419
// Add a __preprocessed helper for offline pre-packing of plaintexts
416420
auto splitPreprocessingOptions = SplitPreprocessingOptions{};
417421
splitPreprocessingOptions.maxReturnValues = options.splitPreprocessing;

lib/Pipelines/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ cc_library(
108108
"@heir//lib/Dialect/LWE/Conversions/LWEToOpenfhe",
109109
"@heir//lib/Dialect/LWE/Conversions/LWEToPolynomial",
110110
"@heir//lib/Dialect/LWE/Transforms:AddDebugPort",
111+
"@heir//lib/Dialect/LWE/Transforms:ImplementTrivialEncryptionAsAddition",
111112
"@heir//lib/Dialect/Lattigo/Transforms:AllocToInPlace",
112113
"@heir//lib/Dialect/Lattigo/Transforms:ConfigureCryptoContext",
113114
"@heir//lib/Dialect/Openfhe/Transforms:AllocToInPlace",

lib/Utils/ContextAwareConversionUtils.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,15 @@ class SecretGenericOpConversion
181181
// OperandAndResultAttrInterface, and this gives special names to the
182182
// attributes, so it doesn't make sense to copy those names to the
183183
// converted op.
184-
convertArrayOfDicts(op.getAllOperandAttrsAttr(), attrsToPreserve);
184+
//
185+
// Note that these attributes are deduped, so for example, if there is a
186+
// mgmt.mgmt attribute on the operands and results of a generic, only the
187+
// result mgmt.mgmt attribute will be copied to be the mgmt.mgmt attribute
188+
// associated with the new op result. This is what we want, but if the order
189+
// of these calls were swapped, the _operand_ mgmt attrs would become
190+
// associated with the new _result_ SSA value, which is incorrect.
185191
convertArrayOfDicts(op.getAllResultAttrsAttr(), attrsToPreserve);
192+
convertArrayOfDicts(op.getAllOperandAttrsAttr(), attrsToPreserve);
186193
DenseSet<StringRef> seenNames;
187194
SmallVector<NamedAttribute> dedupedAttrsToPreserve;
188195
for (NamedAttribute preservedAttr : attrsToPreserve) {

0 commit comments

Comments
 (0)