/
TfheRustEmitter.h
100 lines (84 loc) · 4.32 KB
/
TfheRustEmitter.h
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
#ifndef INCLUDE_TARGET_TFHERUST_TFHERUSTEMITTER_H_
#define INCLUDE_TARGET_TFHERUST_TFHERUSTEMITTER_H_
#include <string>
#include <string_view>
#include "include/Analysis/SelectVariableNames/SelectVariableNames.h"
#include "include/Dialect/TfheRust/IR/TfheRustDialect.h"
#include "include/Dialect/TfheRust/IR/TfheRustOps.h"
#include "llvm/include/llvm/Support/raw_ostream.h" // from @llvm-project
#include "mlir/include/mlir/Dialect/Affine/IR/AffineOps.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/BuiltinOps.h" // from @llvm-project
#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project
#include "mlir/include/mlir/IR/Types.h" // from @llvm-project
#include "mlir/include/mlir/IR/Value.h" // from @llvm-project
#include "mlir/include/mlir/IR/ValueRange.h" // from @llvm-project
#include "mlir/include/mlir/Support/IndentedOstream.h" // from @llvm-project
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project
#include "mlir/include/mlir/Support/LogicalResult.h" // from @llvm-project
namespace mlir {
namespace heir {
namespace tfhe_rust {
void registerToTfheRustTranslation();
/// Translates the given operation to TfheRust.
::mlir::LogicalResult translateToTfheRust(::mlir::Operation *op,
llvm::raw_ostream &os);
class TfheRustEmitter {
public:
TfheRustEmitter(raw_ostream &os, SelectVariableNames *variableNames);
LogicalResult translate(::mlir::Operation &operation);
private:
/// Output stream to emit to.
raw_indented_ostream os;
/// Pre-populated analysis selecting unique variable names for all the SSA
/// values.
SelectVariableNames *variableNames;
// Server key arg to create default values when initializing arrays
std::string serverKeyArg_;
// Functions for printing individual ops
LogicalResult printOperation(::mlir::ModuleOp op);
LogicalResult printOperation(::mlir::arith::ConstantOp op);
LogicalResult printOperation(::mlir::arith::IndexCastOp op);
LogicalResult printOperation(::mlir::arith::ShLIOp op);
LogicalResult printOperation(::mlir::arith::AndIOp op);
LogicalResult printOperation(::mlir::arith::ShRSIOp op);
LogicalResult printOperation(::mlir::arith::TruncIOp op);
LogicalResult printOperation(::mlir::func::FuncOp op);
LogicalResult printOperation(::mlir::func::ReturnOp op);
LogicalResult printOperation(AddOp op);
LogicalResult printOperation(BitAndOp op);
LogicalResult printOperation(CreateTrivialOp op);
LogicalResult printOperation(affine::AffineForOp op);
LogicalResult printOperation(tensor::ExtractOp op);
LogicalResult printOperation(tensor::FromElementsOp op);
LogicalResult printOperation(memref::AllocOp op);
LogicalResult printOperation(memref::GetGlobalOp op);
LogicalResult printOperation(memref::LoadOp op);
LogicalResult printOperation(memref::StoreOp op);
LogicalResult printOperation(ApplyLookupTableOp op);
LogicalResult printOperation(GenerateLookupTableOp op);
LogicalResult printOperation(ScalarLeftShiftOp op);
// Helpers for above
LogicalResult printSksMethod(::mlir::Value result, ::mlir::Value sks,
::mlir::ValueRange nonSksOperands,
std::string_view op,
SmallVector<std::string> operandTypes = {});
LogicalResult printBinaryOp(::mlir::Value result, ::mlir::Value lhs,
::mlir::Value rhs, std::string_view op);
void printStoreOp(memref::StoreOp op, std::string valueToStore);
void printLoadOp(memref::LoadOp op);
std::string operationType(Operation *op);
// Emit a TfheRust type
LogicalResult emitType(Type type);
FailureOr<std::string> convertType(Type type);
// Emit a default value for the given type
FailureOr<std::string> defaultValue(Type type);
void emitAssignPrefix(Value result, bool mut = false, std::string type = "");
};
} // namespace tfhe_rust
} // namespace heir
} // namespace mlir
#endif // INCLUDE_TARGET_TFHERUST_TFHERUSTEMITTER_H_