|
| 1 | +#include "lib/Dialect/LWE/Transforms/AnnotatePlaintextLevel.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstdint> |
| 5 | +#include <optional> |
| 6 | + |
| 7 | +#include "lib/Dialect/LWE/IR/LWEOps.h" |
| 8 | +#include "lib/Dialect/LWE/IR/LWETraits.h" |
| 9 | +#include "lib/Dialect/LWE/IR/LWETypes.h" |
| 10 | +#include "lib/Dialect/ModuleAttributes.h" |
| 11 | +#include "llvm/include/llvm/ADT/DenseSet.h" // from @llvm-project |
| 12 | +#include "llvm/include/llvm/ADT/STLExtras.h" // from @llvm-project |
| 13 | +#include "llvm/include/llvm/ADT/SmallVector.h" // from @llvm-project |
| 14 | +#include "mlir/include/mlir/IR/BuiltinOps.h" // from @llvm-project |
| 15 | +#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project |
| 16 | +#include "mlir/include/mlir/IR/TypeUtilities.h" // from @llvm-project |
| 17 | +#include "mlir/include/mlir/IR/Value.h" // from @llvm-project |
| 18 | +#include "mlir/include/mlir/IR/Visitors.h" // from @llvm-project |
| 19 | +#include "mlir/include/mlir/Interfaces/CallInterfaces.h" // from @llvm-project |
| 20 | +#include "mlir/include/mlir/Support/LLVM.h" // from @llvm-project |
| 21 | + |
| 22 | +namespace mlir { |
| 23 | +namespace heir { |
| 24 | +namespace lwe { |
| 25 | + |
| 26 | +#define GEN_PASS_DEF_ANNOTATEPLAINTEXTLEVEL |
| 27 | +#include "lib/Dialect/LWE/Transforms/Passes.h.inc" |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +// Return the highest level among an op's ciphertext operands and results, or |
| 32 | +// nullopt if it touches no ciphertext carrying a modulus chain. |
| 33 | +std::optional<int64_t> highestCiphertextLevel(Operation* op) { |
| 34 | + std::optional<int64_t> result; |
| 35 | + auto join = [&](Value value) { |
| 36 | + auto ctType = |
| 37 | + dyn_cast<LWECiphertextType>(getElementTypeOrSelf(value.getType())); |
| 38 | + if (!ctType) return; |
| 39 | + std::optional<int64_t> level = getLevel(ctType); |
| 40 | + if (!level) return; |
| 41 | + result = result ? std::max(*result, *level) : *level; |
| 42 | + }; |
| 43 | + for (Value operand : op->getOperands()) join(operand); |
| 44 | + for (Value opResult : op->getResults()) join(opResult); |
| 45 | + return result; |
| 46 | +} |
| 47 | + |
| 48 | +// Return true if an op's results account for every place a value it consumes |
| 49 | +// can end up, so the walk can follow them. Ops with regions route operands into |
| 50 | +// block arguments (`scf.for`'s iter_args) and calls route them into a callee, |
| 51 | +// neither of which the walk models; a terminator has no results to follow. |
| 52 | +bool forwardsThroughResults(Operation* op) { |
| 53 | + return op->getNumResults() > 0 && op->getNumRegions() == 0 && |
| 54 | + !isa<CallOpInterface>(op) && |
| 55 | + llvm::all_of(op->getResults(), [](Value result) { |
| 56 | + return isa<LWEPlaintextType>(getElementTypeOrSelf(result.getType())); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// Return the level to encode the plaintext at, or nullopt to leave it at the |
| 61 | +// top of the modulus chain. Plaintexts are not always consumed directly: the |
| 62 | +// encode op may feed a `tensor.from_elements` or similar first, so keep walking |
| 63 | +// forward through the ops that forward it until one that consumes it is |
| 64 | +// reached. |
| 65 | +// |
| 66 | +// The two kinds of consumer constrain the level differently: |
| 67 | +// |
| 68 | +// - A ct-pt op sets a *lower bound*. The backend combines at the lower of the |
| 69 | +// two levels, so encoding above the ciphertext costs limbs but nothing |
| 70 | +// else, while encoding below it silently truncates that ciphertext. The |
| 71 | +// highest such use is therefore the smallest level that serves them all. |
| 72 | +// |
| 73 | +// - An encryption sets an *exact* level. It has no ciphertext operand to be |
| 74 | +// clamped against: the backend builds the ciphertext at the plaintext's own |
| 75 | +// level. Both `lwe.rlwe_encrypt` and `lwe.trivial_encrypt` work |
| 76 | +// this way. |
| 77 | +// |
| 78 | +// One encoding can serve both only when the encryption's level also covers |
| 79 | +// every ct-pt use. When it does not, or when two encryptions want different |
| 80 | +// levels, no annotation is correct and the fallback takes over. |
| 81 | +std::optional<int64_t> findUseLevel(RLWEEncodeOp encodeOp) { |
| 82 | + std::optional<int64_t> combinedLevel; |
| 83 | + std::optional<int64_t> encryptedLevel; |
| 84 | + SmallVector<Value> worklist = {encodeOp.getOutput()}; |
| 85 | + DenseSet<Operation*> visited; |
| 86 | + |
| 87 | + while (!worklist.empty()) { |
| 88 | + Value value = worklist.pop_back_val(); |
| 89 | + for (Operation* user : value.getUsers()) { |
| 90 | + if (!visited.insert(user).second) continue; |
| 91 | + |
| 92 | + if (isa<RLWEEncryptOp, TrivialEncryptOp>(user)) { |
| 93 | + // A consumer whose ciphertexts carry no modulus chain constrains |
| 94 | + // nothing, and leaves the plaintext to whatever its other uses need. |
| 95 | + std::optional<int64_t> level = highestCiphertextLevel(user); |
| 96 | + if (!level) continue; |
| 97 | + if (encryptedLevel && *encryptedLevel != *level) return std::nullopt; |
| 98 | + encryptedLevel = *level; |
| 99 | + continue; |
| 100 | + } |
| 101 | + if (user->hasTrait<IsCiphertextPlaintextOp>()) { |
| 102 | + // Likewise for a ct-pt op whose ciphertexts carry no modulus chain. |
| 103 | + if (std::optional<int64_t> level = highestCiphertextLevel(user)) { |
| 104 | + combinedLevel = |
| 105 | + combinedLevel ? std::max(*combinedLevel, *level) : *level; |
| 106 | + } |
| 107 | + continue; |
| 108 | + } |
| 109 | + if (!forwardsThroughResults(user)) return std::nullopt; |
| 110 | + for (Value userResult : user->getResults()) { |
| 111 | + worklist.push_back(userResult); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (!encryptedLevel) return combinedLevel; |
| 117 | + if (combinedLevel && *combinedLevel > *encryptedLevel) return std::nullopt; |
| 118 | + return encryptedLevel; |
| 119 | +} |
| 120 | + |
| 121 | +struct AnnotatePlaintextLevel |
| 122 | + : impl::AnnotatePlaintextLevelBase<AnnotatePlaintextLevel> { |
| 123 | + using AnnotatePlaintextLevelBase::AnnotatePlaintextLevelBase; |
| 124 | + |
| 125 | + void runOnOperation() override { |
| 126 | + ModuleOp module = getOperation(); |
| 127 | + |
| 128 | + // BFV does no level management: every ciphertext sits at the bottom of the |
| 129 | + // modulus chain, while the backend's ciphertexts still span the whole chain |
| 130 | + // (the extra modulus is an artifact of the encryption technique). So the |
| 131 | + // chain's `current` is not an encoding level here, and there is nothing to |
| 132 | + // save by lowering one. |
| 133 | + if (moduleIsBFV(module)) { |
| 134 | + module->walk([&](RLWEEncodeOp encodeOp) { encodeOp.removeLevelAttr(); }); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + module->walk([&](RLWEEncodeOp encodeOp) { |
| 139 | + std::optional<int64_t> level = findUseLevel(encodeOp); |
| 140 | + // Drop a level that no longer has a use to justify it |
| 141 | + if (!level) { |
| 142 | + encodeOp.removeLevelAttr(); |
| 143 | + return; |
| 144 | + } |
| 145 | + encodeOp.setLevel(*level); |
| 146 | + }); |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace |
| 151 | + |
| 152 | +} // namespace lwe |
| 153 | +} // namespace heir |
| 154 | +} // namespace mlir |
0 commit comments