-
Notifications
You must be signed in to change notification settings - Fork 25
/
BGVTraits.h
53 lines (45 loc) · 1.67 KB
/
BGVTraits.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
#ifndef HEIR_INCLUDE_DIALECT_BGV_IR_BGVTRAITS_H_
#define HEIR_INCLUDE_DIALECT_BGV_IR_BGVTRAITS_H_
#include "include/Dialect/LWE/IR/LWETypes.h"
#include "include/Dialect/Polynomial/IR/PolynomialAttributes.h"
#include "mlir/include/mlir/IR/OpDefinition.h" // from @llvm-project
#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project
namespace mlir::heir::bgv {
// TODO(#212): Move to LWE dialect/namespace
// Trait that ensures that all operands and results ciphertext have the same set
// of rings.
template <typename ConcreteType>
class SameOperandsAndResultRings
: public OpTrait::TraitBase<ConcreteType, SameOperandsAndResultRings> {
public:
static LogicalResult verifyTrait(Operation *op) {
polynomial::RingAttr rings = nullptr;
for (auto rTy : op->getResultTypes()) {
auto ct = dyn_cast<lwe::RLWECiphertextType>(rTy);
if (!ct) continue;
if (rings == nullptr) {
rings = ct.getRlweParams().getRing();
continue;
}
if (rings != ct.getRlweParams().getRing()) {
return op->emitOpError()
<< "requires all operands and results to have the same rings";
}
}
for (auto oTy : op->getOperandTypes()) {
auto ct = dyn_cast<lwe::RLWECiphertextType>(oTy);
if (!ct) continue; // Check only ciphertexts
if (rings == nullptr) {
rings = ct.getRlweParams().getRing();
continue;
}
if (rings != ct.getRlweParams().getRing()) {
return op->emitOpError()
<< "requires all operands and results to have the same rings";
}
}
return success();
}
};
} // namespace mlir::heir::bgv
#endif // HEIR_INCLUDE_DIALECT_BGV_IR_BGVTRAITS_H_