|
| 1 | +#include "lib/Analysis/CKKSRangeAnalysis/CKKSRangeAnalysis.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cassert> |
| 5 | +#include <functional> |
| 6 | +#include <optional> |
| 7 | + |
| 8 | +#include "lib/Analysis/SecretnessAnalysis/SecretnessAnalysis.h" |
| 9 | +#include "lib/Analysis/Utils.h" |
| 10 | +#include "lib/Dialect/Mgmt/IR/MgmtOps.h" |
| 11 | +#include "llvm/include/llvm/ADT/TypeSwitch.h" // from @llvm-project |
| 12 | +#include "llvm/include/llvm/Support/Debug.h" // from @llvm-project |
| 13 | +#include "mlir/include/mlir/Analysis/DataFlowFramework.h" // from @llvm-project |
| 14 | +#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project |
| 15 | +#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project |
| 16 | +#include "mlir/include/mlir/IR/Operation.h" // from @llvm-project |
| 17 | +#include "mlir/include/mlir/IR/TypeUtilities.h" // from @llvm-project |
| 18 | +#include "mlir/include/mlir/IR/Value.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 | +#define DEBUG_TYPE "CKKSRangeAnalysis" |
| 23 | + |
| 24 | +namespace mlir { |
| 25 | +namespace heir { |
| 26 | + |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | +// CKKSRangeAnalysis (Forward) |
| 29 | +//===----------------------------------------------------------------------===// |
| 30 | + |
| 31 | +LogicalResult CKKSRangeAnalysis::visitOperation( |
| 32 | + Operation *op, ArrayRef<const CKKSRangeLattice *> operands, |
| 33 | + ArrayRef<CKKSRangeLattice *> results) { |
| 34 | + auto propagate = [&](Value value, const CKKSRangeState &state) { |
| 35 | + auto *lattice = getLatticeElement(value); |
| 36 | + LLVM_DEBUG(llvm::dbgs() << "Propagate CKKSRangeState to " << value << ": " |
| 37 | + << state << "\n"); |
| 38 | + ChangeResult changed = lattice->join(state); |
| 39 | + propagateIfChanged(lattice, changed); |
| 40 | + }; |
| 41 | + |
| 42 | + llvm::TypeSwitch<Operation &>(*op) |
| 43 | + .Case<arith::AddFOp, arith::AddIOp>([&](auto &op) { |
| 44 | + // condition on result secretness |
| 45 | + SmallVector<OpResult> secretResults; |
| 46 | + getSecretResults(op, secretResults); |
| 47 | + if (secretResults.empty()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + auto rangeResult = Log2Arithmetic::of(0); |
| 52 | + |
| 53 | + for (auto &operand : op->getOpOperands()) { |
| 54 | + auto &rangeState = getLatticeElement(operand.get())->getValue(); |
| 55 | + if (!rangeState.isInitialized()) { |
| 56 | + return; |
| 57 | + } |
| 58 | + rangeResult = rangeResult + rangeState.getCKKSRange(); |
| 59 | + } |
| 60 | + |
| 61 | + for (auto result : secretResults) { |
| 62 | + propagate(result, CKKSRangeState(rangeResult)); |
| 63 | + } |
| 64 | + }) |
| 65 | + .Case<arith::MulFOp, arith::MulIOp>([&](auto &op) { |
| 66 | + // condition on result secretness |
| 67 | + SmallVector<OpResult> secretResults; |
| 68 | + getSecretResults(op, secretResults); |
| 69 | + if (secretResults.empty()) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + auto rangeResult = Log2Arithmetic::of(1); |
| 74 | + |
| 75 | + for (auto &operand : op->getOpOperands()) { |
| 76 | + auto &rangeState = getLatticeElement(operand.get())->getValue(); |
| 77 | + if (!rangeState.isInitialized()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + rangeResult = rangeResult * rangeState.getCKKSRange(); |
| 81 | + } |
| 82 | + |
| 83 | + for (auto result : secretResults) { |
| 84 | + propagate(result, CKKSRangeState(rangeResult)); |
| 85 | + } |
| 86 | + }) |
| 87 | + .Case<arith::ConstantOp>([&](auto &op) { |
| 88 | + // For constant, the range is [constant] |
| 89 | + std::optional<Log2Arithmetic> range = std::nullopt; |
| 90 | + TypedAttr constAttr = op.getValue(); |
| 91 | + llvm::TypeSwitch<Attribute>(constAttr) |
| 92 | + .Case<FloatAttr>([&](FloatAttr value) { |
| 93 | + range = Log2Arithmetic::of(std::fabs(value.getValueAsDouble())); |
| 94 | + }) |
| 95 | + .template Case<IntegerAttr>([&](IntegerAttr value) { |
| 96 | + range = |
| 97 | + Log2Arithmetic::of(std::abs(value.getValue().getSExtValue())); |
| 98 | + }) |
| 99 | + .template Case<DenseElementsAttr>([&](DenseElementsAttr denseAttr) { |
| 100 | + auto elementType = getElementTypeOrSelf(constAttr.getType()); |
| 101 | + if (mlir::isa<FloatType>(elementType)) { |
| 102 | + std::optional<APFloat> maxValue; |
| 103 | + for (APFloat value : denseAttr.template getValues<APFloat>()) { |
| 104 | + value.clearSign(); |
| 105 | + if (!maxValue.has_value() || |
| 106 | + maxValue->compare(value) == APFloat::cmpLessThan) { |
| 107 | + maxValue = value; |
| 108 | + } |
| 109 | + } |
| 110 | + if (maxValue.has_value()) { |
| 111 | + range = |
| 112 | + Log2Arithmetic::of(maxValue.value().convertToDouble()); |
| 113 | + } |
| 114 | + } else if (mlir::isa<IntegerType>(elementType)) { |
| 115 | + std::optional<APInt> maxValue; |
| 116 | + for (APInt value : mlir::cast<DenseElementsAttr>(constAttr) |
| 117 | + .template getValues<APInt>()) { |
| 118 | + value.clearSignBit(); |
| 119 | + if (!maxValue.has_value() || maxValue->ule(value)) { |
| 120 | + maxValue = value; |
| 121 | + } |
| 122 | + } |
| 123 | + range = Log2Arithmetic::of(maxValue.value().getSExtValue()); |
| 124 | + } |
| 125 | + }); |
| 126 | + // We can encounter DenseResourceElementsAttr, we do not know its range |
| 127 | + if (!range.has_value()) { |
| 128 | + return; |
| 129 | + } |
| 130 | + for (auto result : op->getResults()) { |
| 131 | + propagate(result, CKKSRangeState(range.value())); |
| 132 | + } |
| 133 | + }) |
| 134 | + .Case<mgmt::InitOp>([&](auto &op) { |
| 135 | + auto inputState = getLatticeElement(op->getOperand(0))->getValue(); |
| 136 | + if (!inputState.isInitialized()) { |
| 137 | + return; |
| 138 | + } |
| 139 | + // For InitOp, the range is the same as the input range |
| 140 | + propagate(op->getResult(0), inputState); |
| 141 | + }) |
| 142 | + .Case<tensor::InsertOp>([&](tensor::InsertOp op) { |
| 143 | + auto scalarState = getLatticeElement(op.getScalar())->getValue(); |
| 144 | + auto destState = getLatticeElement(op.getDest())->getValue(); |
| 145 | + if (!scalarState.isInitialized() || !destState.isInitialized()) { |
| 146 | + return; |
| 147 | + } |
| 148 | + auto resultState = |
| 149 | + CKKSRangeState::join(scalarState, destState); // Join the ranges |
| 150 | + propagate(op.getResult(), resultState); |
| 151 | + }) |
| 152 | + // Rotation does not change the CKKS range |
| 153 | + .Default([&](auto &op) { |
| 154 | + // condition on result secretness |
| 155 | + SmallVector<OpResult> secretResults; |
| 156 | + getSecretResults(&op, secretResults); |
| 157 | + if (secretResults.empty()) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + SmallVector<OpOperand *> secretOperands; |
| 162 | + getSecretOperands(&op, secretOperands); |
| 163 | + if (secretOperands.empty()) { |
| 164 | + return; |
| 165 | + } |
| 166 | + |
| 167 | + // short-circuit to get range |
| 168 | + CKKSRangeState rangeState; |
| 169 | + for (auto *operand : secretOperands) { |
| 170 | + auto &operandRangeState = |
| 171 | + getLatticeElement(operand->get())->getValue(); |
| 172 | + if (operandRangeState.isInitialized()) { |
| 173 | + rangeState = operandRangeState; |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + for (auto result : secretResults) { |
| 179 | + propagate(result, rangeState); |
| 180 | + } |
| 181 | + }); |
| 182 | + return success(); |
| 183 | +} |
| 184 | + |
| 185 | +void CKKSRangeAnalysis::visitExternalCall( |
| 186 | + CallOpInterface call, ArrayRef<const CKKSRangeLattice *> argumentLattices, |
| 187 | + ArrayRef<CKKSRangeLattice *> resultLattices) { |
| 188 | + auto callback = std::bind(&CKKSRangeAnalysis::propagateIfChangedWrapper, this, |
| 189 | + std::placeholders::_1, std::placeholders::_2); |
| 190 | + ::mlir::heir::visitExternalCall<CKKSRangeState, CKKSRangeLattice>( |
| 191 | + call, argumentLattices, resultLattices, callback); |
| 192 | +} |
| 193 | + |
| 194 | +//===----------------------------------------------------------------------===// |
| 195 | +// Utils |
| 196 | +//===----------------------------------------------------------------------===// |
| 197 | + |
| 198 | +std::optional<CKKSRangeState::CKKSRangeType> getCKKSRange( |
| 199 | + Value value, DataFlowSolver *solver) { |
| 200 | + auto *lattice = solver->lookupState<CKKSRangeLattice>(value); |
| 201 | + if (!lattice) { |
| 202 | + return std::nullopt; |
| 203 | + } |
| 204 | + if (!lattice->getValue().isInitialized()) { |
| 205 | + return std::nullopt; |
| 206 | + } |
| 207 | + return lattice->getValue().getCKKSRange(); |
| 208 | +} |
| 209 | + |
| 210 | +} // namespace heir |
| 211 | +} // namespace mlir |
0 commit comments