Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding cggi-tfhe-rs-bool pass + Add tests + And_packed #486

Merged
merged 1 commit into from Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Expand Up @@ -15,4 +15,9 @@ hugo_stats.json
venv

# for rust codegen tests
Cargo.lock
**/Cargo.lock
tests/**/**/target/
tests/tfhe_rust_bool/end_to_end_fpga/

# vscode
.vscode/**
37 changes: 37 additions & 0 deletions include/Conversion/CGGIToTfheRustBool/BUILD
@@ -0,0 +1,37 @@
# CGGIToTfheRustBool tablegen and headers.

load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library")

package(
default_applicable_licenses = ["@heir//:license"],
default_visibility = ["//visibility:public"],
)

exports_files(
[
"CGGIToTfheRustBool.h",
],
)

gentbl_cc_library(
name = "pass_inc_gen",
tbl_outs = [
(
[
"-gen-pass-decls",
"-name=CGGIToTfheRustBool",
],
"CGGIToTfheRustBool.h.inc",
),
(
["-gen-pass-doc"],
"CGGIToTfheRustBool.md",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "CGGIToTfheRustBool.td",
deps = [
"@llvm-project//mlir:OpBaseTdFiles",
"@llvm-project//mlir:PassBaseTdFiles",
],
)
16 changes: 16 additions & 0 deletions include/Conversion/CGGIToTfheRustBool/CGGIToTfheRustBool.h
@@ -0,0 +1,16 @@
#ifndef INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_H_
#define INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_H_

#include "mlir/include/mlir/Pass/Pass.h" // from @llvm-project

namespace mlir::heir {

#define GEN_PASS_DECL
#include "include/Conversion/CGGIToTfheRustBool/CGGIToTfheRustBool.h.inc"

#define GEN_PASS_REGISTRATION
#include "include/Conversion/CGGIToTfheRustBool/CGGIToTfheRustBool.h.inc"

} // namespace mlir::heir

#endif // INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_H_
16 changes: 16 additions & 0 deletions include/Conversion/CGGIToTfheRustBool/CGGIToTfheRustBool.td
@@ -0,0 +1,16 @@
#ifndef INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_TD_
#define INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_TD_

include "mlir/Pass/PassBase.td"

def CGGIToTfheRustBool : Pass<"cggi-to-tfhe-rust-bool"> {
let summary = "Lower `cggi` to `tfhe_rust_bool` dialect.";
let dependentDialects = [
"mlir::arith::ArithDialect",
"mlir::heir::cggi::CGGIDialect",
"mlir::heir::lwe::LWEDialect",
"mlir::heir::tfhe_rust_bool::TfheRustBoolDialect",
];
}

#endif // INCLUDE_CONVERSION_CGGITOTFHERUSTBOOL_CGGITOTFHERUSTBOOL_TD_
14 changes: 14 additions & 0 deletions include/Dialect/TfheRustBool/IR/TfheRustBoolOps.td
Expand Up @@ -8,6 +8,7 @@ include "mlir/IR/BuiltinAttributes.td"
include "mlir/IR/CommonTypeConstraints.td"
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/IR/BuiltinTypes.td"


class TfheRustBool_Op<string mnemonic, list<Trait> traits = []> :
Expand Down Expand Up @@ -43,6 +44,19 @@ def XorOp : TfheRustBool_BinaryGateOp<"xor"> { let summary = "Logical XOR of two
def XnorOp : TfheRustBool_BinaryGateOp<"xnor"> { let summary = "Logical XNOR of two TFHE-rs Bool ciphertexts."; }


def AndPackedOp : TfheRustBool_Op<"and_packed", [
j2kun marked this conversation as resolved.
Show resolved Hide resolved
Pure,
AllTypesMatch<["lhs", "rhs", "output"]>
]> {
let arguments = (ins
TfheRustBool_ServerKey:$serverKey,
TensorOf<[TfheRustBool_Encrypted]>:$lhs,
TensorOf<[TfheRustBool_Encrypted]>:$rhs
);
let results = (outs TensorOf<[TfheRustBool_Encrypted]>:$output);
}


def NotOp : TfheRustBool_Op<"not", [
Pure,
AllTypesMatch<["input", "output"]>
Expand Down
2 changes: 2 additions & 0 deletions include/Target/TfheRustBool/TfheRustBoolEmitter.h
Expand Up @@ -59,6 +59,8 @@ class TfheRustBoolEmitter {
LogicalResult printOperation(XorOp op);
LogicalResult printOperation(XnorOp op);

LogicalResult printOperation(AndPackedOp op);

// Helpers for above
LogicalResult printSksMethod(::mlir::Value result, ::mlir::Value sks,
::mlir::ValueRange nonSksOperands,
Expand Down
6 changes: 3 additions & 3 deletions lib/Conversion/CGGIToTfheRust/CGGIToTfheRust.cpp
Expand Up @@ -84,9 +84,9 @@ int widthFromEncodingAttr(Attribute encoding) {
});
}

class PassTypeConverter : public TypeConverter {
class CGGIToTfheRustTypeConverter : public TypeConverter {
public:
PassTypeConverter(MLIRContext *ctx) {
CGGIToTfheRustTypeConverter(MLIRContext *ctx) {
addConversion([](Type type) { return type; });
addConversion([ctx](lwe::LWECiphertextType type) -> Type {
int width = widthFromEncodingAttr(type.getEncoding());
Expand Down Expand Up @@ -402,7 +402,7 @@ class CGGIToTfheRust : public impl::CGGIToTfheRustBase<CGGIToTfheRust> {
MLIRContext *context = &getContext();
auto *op = getOperation();

PassTypeConverter typeConverter(context);
CGGIToTfheRustTypeConverter typeConverter(context);
RewritePatternSet patterns(context);
ConversionTarget target(*context);
addStructuralConversionPatterns(typeConverter, patterns, target);
Expand Down
28 changes: 28 additions & 0 deletions lib/Conversion/CGGIToTfheRustBool/BUILD
@@ -0,0 +1,28 @@
package(
default_applicable_licenses = ["@heir//:license"],
default_visibility = ["//visibility:public"],
)

cc_library(
name = "CGGIToTfheRustBool",
srcs = ["CGGIToTfheRustBool.cpp"],
hdrs = [
"@heir//include/Conversion/CGGIToTfheRustBool:CGGIToTfheRustBool.h",
],
deps = [
"@heir//include/Conversion/CGGIToTfheRustBool:pass_inc_gen",
"@heir//lib/Conversion:Utils",
"@heir//lib/Dialect/CGGI/IR:Dialect",
"@heir//lib/Dialect/LWE/IR:Dialect",
"@heir//lib/Dialect/TfheRustBool/IR:Dialect",
"@llvm-project//llvm:Support",
"@llvm-project//mlir:ArithDialect",
"@llvm-project//mlir:FuncDialect",
"@llvm-project//mlir:IR",
"@llvm-project//mlir:MemRefDialect",
"@llvm-project//mlir:Pass",
"@llvm-project//mlir:Support",
"@llvm-project//mlir:TensorDialect",
"@llvm-project//mlir:Transforms",
],
)