forked from google/heir
/
CGGIToTfheRust.cpp
456 lines (398 loc) · 17.8 KB
/
CGGIToTfheRust.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include "include/Conversion/CGGIToTfheRust/CGGIToTfheRust.h"
#include <utility>
#include "include/Dialect/CGGI/IR/CGGIDialect.h"
#include "include/Dialect/CGGI/IR/CGGIOps.h"
#include "include/Dialect/LWE/IR/LWEAttributes.h"
#include "include/Dialect/LWE/IR/LWEDialect.h"
#include "include/Dialect/LWE/IR/LWEOps.h"
#include "include/Dialect/LWE/IR/LWETypes.h"
#include "include/Dialect/TfheRust/IR/TfheRustDialect.h"
#include "include/Dialect/TfheRust/IR/TfheRustOps.h"
#include "include/Dialect/TfheRust/IR/TfheRustTypes.h"
#include "lib/Conversion/Utils.h"
#include "llvm/include/llvm/ADT/SmallVector.h" // from @llvm-project
#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project
#include "llvm/include/llvm/Support/Casting.h" // from @llvm-project
#include "llvm/include/llvm/Support/ErrorHandling.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Func/IR/FuncOps.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/MemRef/IR/MemRef.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project
#include "mlir/include/mlir/IR/BuiltinTypes.h" // from @llvm-project
#include "mlir/include/mlir/IR/ImplicitLocOpBuilder.h" // from @llvm-project
#include "mlir/include/mlir/IR/PatternMatch.h" // from @llvm-project
#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project
#include "mlir/include/mlir/Support/LogicalResult.h" // from @llvm-project
#include "mlir/include/mlir/Transforms/DialectConversion.h" // from @llvm-project
namespace mlir::heir {
#define GEN_PASS_DEF_CGGITOTFHERUST
#include "include/Conversion/CGGIToTfheRust/CGGIToTfheRust.h.inc"
constexpr int kBinaryGateLutWidth = 4;
constexpr int kAndLut = 8;
constexpr int kOrLut = 14;
constexpr int kXorLut = 6;
Type encrytpedUIntTypeFromWidth(MLIRContext *ctx, int width) {
// Only supporting unsigned types because the LWE dialect does not have a
// notion of signedness.
switch (width) {
case 2:
return tfhe_rust::EncryptedUInt2Type::get(ctx);
case 3:
return tfhe_rust::EncryptedUInt3Type::get(ctx);
case 4:
return tfhe_rust::EncryptedUInt4Type::get(ctx);
case 8:
return tfhe_rust::EncryptedUInt8Type::get(ctx);
case 10:
return tfhe_rust::EncryptedUInt10Type::get(ctx);
case 12:
return tfhe_rust::EncryptedUInt12Type::get(ctx);
case 14:
return tfhe_rust::EncryptedUInt14Type::get(ctx);
case 16:
return tfhe_rust::EncryptedUInt16Type::get(ctx);
case 32:
return tfhe_rust::EncryptedUInt32Type::get(ctx);
case 64:
return tfhe_rust::EncryptedUInt64Type::get(ctx);
case 128:
return tfhe_rust::EncryptedUInt128Type::get(ctx);
case 256:
return tfhe_rust::EncryptedUInt256Type::get(ctx);
default:
llvm_unreachable("Unsupported bitwidth");
}
}
// Seems like this would be better as a method on the
// LWE_EncodingAttrWithScalingFactor class, but I still have the problem of the
// type returned by getEncoding being a vanilla Attribute. Probably we need a
// common interface for LWE_EncodingAttrWithScalingFactor, and cast to that?
int widthFromEncodingAttr(Attribute encoding) {
return llvm::TypeSwitch<Attribute, int>(encoding)
.Case<lwe::BitFieldEncodingAttr, lwe::UnspecifiedBitFieldEncodingAttr>(
[](auto attr) -> int { return attr.getCleartextBitwidth(); })
.Default([](Attribute attr) -> int {
llvm_unreachable("Unsupported encoding attribute");
return 0;
});
}
class CGGIToTfheRustTypeConverter : public TypeConverter {
public:
CGGIToTfheRustTypeConverter(MLIRContext *ctx) {
addConversion([](Type type) { return type; });
addConversion([ctx](lwe::LWECiphertextType type) -> Type {
int width = widthFromEncodingAttr(type.getEncoding());
return encrytpedUIntTypeFromWidth(ctx, width);
});
addConversion([this](ShapedType type) -> Type {
return type.cloneWith(type.getShape(),
this->convertType(type.getElementType()));
});
}
};
/// Returns true if the func's body contains any CGGI ops.
bool containsCGGIOps(func::FuncOp func) {
auto walkResult = func.walk([&](Operation *op) {
if (llvm::isa<cggi::CGGIDialect>(op->getDialect()))
return WalkResult::interrupt();
return WalkResult::advance();
});
return walkResult.wasInterrupted();
}
/// Returns the Value corresponding to a server key in the FuncOp containing
/// this op.
FailureOr<Value> getContextualServerKey(Operation *op) {
Value serverKey = op->getParentOfType<func::FuncOp>()
.getBody()
.getBlocks()
.front()
.getArguments()
.front();
if (!serverKey.getType().isa<tfhe_rust::ServerKeyType>()) {
return op->emitOpError()
<< "Found CGGI op in a function without a server "
"key argument. Did the AddServerKeyArg pattern fail to run?";
}
return serverKey;
}
template <class Op>
struct GenericOpPattern : public OpConversionPattern<Op> {
using OpConversionPattern<Op>::OpConversionPattern;
LogicalResult matchAndRewrite(
Op op, typename Op::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
SmallVector<Type> retTypes;
if (failed(this->getTypeConverter()->convertTypes(op->getResultTypes(),
retTypes)))
return failure();
rewriter.replaceOpWithNewOp<Op>(op, retTypes, adaptor.getOperands(),
op->getAttrs());
return success();
}
};
/// Convert a func by adding a server key argument. Converted ops in other
/// patterns need a server key SSA value available, so this pattern needs a
/// higher benefit.
struct AddServerKeyArg : public OpConversionPattern<func::FuncOp> {
AddServerKeyArg(mlir::MLIRContext *context)
: OpConversionPattern<func::FuncOp>(context, /* benefit= */ 2) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
func::FuncOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!containsCGGIOps(op)) {
return failure();
}
auto serverKeyType = tfhe_rust::ServerKeyType::get(getContext());
FunctionType originalType = op.getFunctionType();
llvm::SmallVector<Type, 4> newTypes;
newTypes.reserve(originalType.getNumInputs() + 1);
newTypes.push_back(serverKeyType);
for (auto t : originalType.getInputs()) {
newTypes.push_back(t);
}
auto newFuncType =
FunctionType::get(getContext(), newTypes, originalType.getResults());
rewriter.modifyOpInPlace(op, [&] {
op.setType(newFuncType);
// In addition to updating the type signature, we need to update the
// entry block's arguments to match the type signature
Block &block = op.getBody().getBlocks().front();
block.insertArgument(&block.getArguments().front(), serverKeyType,
op.getLoc());
});
return success();
}
};
/// Convert a Lut3Op to:
/// - generate_lookup_table
/// - scalar_left_shift
/// - add_op
/// - apply_lookup_table
///
/// Note the generated lookup tables are not uniqued across applications of this
/// pattern, so a separate step is required at the end to collect all the
/// identical lookup tables, and this can be done with a --cse pass.
struct ConvertLut3Op : public OpConversionPattern<cggi::Lut3Op> {
ConvertLut3Op(mlir::MLIRContext *context)
: OpConversionPattern<cggi::Lut3Op>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::Lut3Op op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
ImplicitLocOpBuilder b(op.getLoc(), rewriter);
FailureOr<Value> result = getContextualServerKey(op.getOperation());
if (failed(result)) return result;
Value serverKey = result.value();
// A followup -cse pass should combine repeated LUT generation ops.
auto lut = b.create<tfhe_rust::GenerateLookupTableOp>(
serverKey, adaptor.getLookupTable());
// Construct input = c << 2 + b << 1 + a
auto shiftedC = b.create<tfhe_rust::ScalarLeftShiftOp>(
serverKey, adaptor.getC(),
b.create<arith::ConstantOp>(b.getI8Type(), b.getI8IntegerAttr(2))
.getResult());
auto shiftedB = b.create<tfhe_rust::ScalarLeftShiftOp>(
serverKey, adaptor.getB(),
b.create<arith::ConstantOp>(b.getI8Type(), b.getI8IntegerAttr(1))
.getResult());
auto summedBC = b.create<tfhe_rust::AddOp>(serverKey, shiftedC, shiftedB);
auto summedABC =
b.create<tfhe_rust::AddOp>(serverKey, summedBC, adaptor.getA());
rewriter.replaceOp(
op, b.create<tfhe_rust::ApplyLookupTableOp>(serverKey, summedABC, lut));
return success();
}
};
struct ConvertLut2Op : public OpConversionPattern<cggi::Lut2Op> {
ConvertLut2Op(mlir::MLIRContext *context)
: OpConversionPattern<cggi::Lut2Op>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::Lut2Op op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
ImplicitLocOpBuilder b(op.getLoc(), rewriter);
FailureOr<Value> result = getContextualServerKey(op.getOperation());
if (failed(result)) return result;
Value serverKey = result.value();
// A followup -cse pass should combine repeated LUT generation ops.
auto lut = b.create<tfhe_rust::GenerateLookupTableOp>(
serverKey, adaptor.getLookupTable());
// Construct input = b << 1 + a
auto shiftedB = b.create<tfhe_rust::ScalarLeftShiftOp>(
serverKey, adaptor.getB(),
b.create<arith::ConstantOp>(b.getI8Type(), b.getI8IntegerAttr(1))
.getResult());
auto summedBA =
b.create<tfhe_rust::AddOp>(serverKey, shiftedB, adaptor.getA());
rewriter.replaceOp(
op, b.create<tfhe_rust::ApplyLookupTableOp>(serverKey, summedBA, lut));
return success();
}
};
LogicalResult replaceBinaryGate(Operation *op, Value lhs, Value rhs,
ConversionPatternRewriter &rewriter, int lut) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
FailureOr<Value> result = getContextualServerKey(op);
if (failed(result)) return result;
Value serverKey = result.value();
// A followup -cse pass should combine repeated LUT generation ops.
auto lookupTable = b.getIntegerAttr(
b.getIntegerType(kBinaryGateLutWidth, /*isSigned=*/false), lut);
auto lutOp =
b.create<tfhe_rust::GenerateLookupTableOp>(serverKey, lookupTable);
// Construct input = rhs << 1 + lhs
auto shiftedRhs = b.create<tfhe_rust::ScalarLeftShiftOp>(
serverKey, rhs,
b.create<arith::ConstantOp>(b.getI8Type(), b.getI8IntegerAttr(1))
.getResult());
auto input = b.create<tfhe_rust::AddOp>(serverKey, shiftedRhs, lhs);
rewriter.replaceOp(
op, b.create<tfhe_rust::ApplyLookupTableOp>(serverKey, input, lutOp));
return success();
}
struct ConvertAndOp : public OpConversionPattern<cggi::AndOp> {
ConvertAndOp(mlir::MLIRContext *context)
: OpConversionPattern<cggi::AndOp>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::AndOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
return replaceBinaryGate(op.getOperation(), adaptor.getLhs(),
adaptor.getRhs(), rewriter, kAndLut);
}
};
struct ConvertOrOp : public OpConversionPattern<cggi::OrOp> {
ConvertOrOp(mlir::MLIRContext *context)
: OpConversionPattern<cggi::OrOp>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::OrOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
return replaceBinaryGate(op.getOperation(), adaptor.getLhs(),
adaptor.getRhs(), rewriter, kOrLut);
}
};
struct ConvertXorOp : public OpConversionPattern<cggi::XorOp> {
ConvertXorOp(mlir::MLIRContext *context)
: OpConversionPattern<cggi::XorOp>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::XorOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
return replaceBinaryGate(op.getOperation(), adaptor.getLhs(),
adaptor.getRhs(), rewriter, kXorLut);
}
};
struct ConvertNotOp : public OpConversionPattern<cggi::NotOp> {
ConvertNotOp(mlir::MLIRContext *context)
: OpConversionPattern<cggi::NotOp>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
cggi::NotOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
FailureOr<Value> result = getContextualServerKey(op);
if (failed(result)) return result;
Value serverKey = result.value();
auto width = widthFromEncodingAttr(op.getInput().getType().getEncoding());
auto cleartextType = b.getIntegerType(width);
auto outputType = encrytpedUIntTypeFromWidth(getContext(), width);
// not(x) == trivial_encryption(1) - x
auto createTrivialOp = rewriter.create<tfhe_rust::CreateTrivialOp>(
op.getLoc(), outputType, serverKey,
b.create<arith::ConstantOp>(cleartextType,
b.getIntegerAttr(cleartextType, 1))
.getResult());
rewriter.replaceOp(op, b.create<tfhe_rust::SubOp>(
serverKey, createTrivialOp, adaptor.getInput()));
return success();
}
};
struct ConvertTrivialEncryptOp
: public OpConversionPattern<lwe::TrivialEncryptOp> {
ConvertTrivialEncryptOp(mlir::MLIRContext *context)
: OpConversionPattern<lwe::TrivialEncryptOp>(context, /*benefit=*/2) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
lwe::TrivialEncryptOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
FailureOr<Value> result = getContextualServerKey(op.getOperation());
if (failed(result)) return result;
Value serverKey = result.value();
lwe::EncodeOp encodeOp = op.getInput().getDefiningOp<lwe::EncodeOp>();
if (!encodeOp) {
return op.emitError() << "Expected input to TrivialEncrypt to be the "
"result of an EncodeOp, but it was "
<< op.getInput().getDefiningOp()->getName();
}
auto outputType = encrytpedUIntTypeFromWidth(
getContext(), widthFromEncodingAttr(encodeOp.getEncoding()));
auto createTrivialOp = rewriter.create<tfhe_rust::CreateTrivialOp>(
op.getLoc(), outputType, serverKey, encodeOp.getPlaintext());
rewriter.replaceOp(op, createTrivialOp);
return success();
}
};
struct ConvertEncodeOp : public OpConversionPattern<lwe::EncodeOp> {
ConvertEncodeOp(mlir::MLIRContext *context)
: OpConversionPattern<lwe::EncodeOp>(context) {}
using OpConversionPattern::OpConversionPattern;
LogicalResult matchAndRewrite(
lwe::EncodeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.eraseOp(op);
return success();
}
};
class CGGIToTfheRust : public impl::CGGIToTfheRustBase<CGGIToTfheRust> {
void runOnOperation() override {
MLIRContext *context = &getContext();
auto *op = getOperation();
CGGIToTfheRustTypeConverter typeConverter(context);
RewritePatternSet patterns(context);
ConversionTarget target(*context);
addStructuralConversionPatterns(typeConverter, patterns, target);
target.addLegalDialect<tfhe_rust::TfheRustDialect>();
target.addIllegalDialect<cggi::CGGIDialect>();
target.addIllegalDialect<lwe::LWEDialect>();
// FuncOp is marked legal by the default structural conversion patterns
// helper, just based on type conversion. We need more, but because the
// addDynamicallyLegalOp is a set-based method, we can add this after
// calling addStructuralConversionPatterns and it will overwrite the
// legality condition set in that function.
target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
bool hasServerKeyArg = op.getFunctionType().getNumInputs() > 0 &&
op.getFunctionType()
.getInputs()
.begin()
->isa<tfhe_rust::ServerKeyType>();
return typeConverter.isSignatureLegal(op.getFunctionType()) &&
typeConverter.isLegal(&op.getBody()) &&
(!containsCGGIOps(op) || hasServerKeyArg);
});
target.addDynamicallyLegalOp<memref::AllocOp, memref::DeallocOp,
memref::StoreOp, memref::LoadOp,
memref::SubViewOp, memref::CopyOp,
tensor::FromElementsOp, tensor::ExtractOp>(
[&](Operation *op) {
return typeConverter.isLegal(op->getOperandTypes()) &&
typeConverter.isLegal(op->getResultTypes());
});
// FIXME: still need to update callers to insert the new server key arg, if
// needed and possible.
patterns.add<
AddServerKeyArg, ConvertAndOp, ConvertEncodeOp, ConvertLut2Op,
ConvertLut3Op, ConvertNotOp, ConvertOrOp, ConvertTrivialEncryptOp,
ConvertXorOp, GenericOpPattern<memref::AllocOp>,
GenericOpPattern<memref::DeallocOp>, GenericOpPattern<memref::StoreOp>,
GenericOpPattern<memref::LoadOp>, GenericOpPattern<memref::SubViewOp>,
GenericOpPattern<memref::CopyOp>,
GenericOpPattern<tensor::FromElementsOp>,
GenericOpPattern<tensor::ExtractOp>>(typeConverter, context);
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
return signalPassFailure();
}
}
};
} // namespace mlir::heir