|
19 | 19 | #include "mlir/Support/LogicalResult.h" |
20 | 20 | #include <compiler/utils/CompilerUtils.h> |
21 | 21 |
|
| 22 | +mlir::LogicalResult mlir::daphne::AllAggSumOp::canonicalize(mlir::daphne::AllAggSumOp op, |
| 23 | + mlir::PatternRewriter &rewriter) { |
| 24 | + mlir::Value input = op.getOperand(); |
| 25 | + mlir::Location location = op.getLoc(); |
| 26 | + mlir::Type result_type = op.getResult().getType(); |
| 27 | + auto unknownType = mlir::daphne::UnknownType::get(rewriter.getContext()); |
| 28 | + |
| 29 | + // Rule 1: sumAll(ewAdd(X, Y)) to ewAdd(sumAll(X), sumAll(Y)) |
| 30 | + if (auto addOp = input.getDefiningOp<mlir::daphne::EwAddOp>()) { |
| 31 | + // Checking the inputs are matrices |
| 32 | + if (!addOp.getLhs().getType().isa<mlir::daphne::MatrixType>() || |
| 33 | + !addOp.getRhs().getType().isa<mlir::daphne::MatrixType>()) { |
| 34 | + return mlir::failure(); |
| 35 | + } |
| 36 | + |
| 37 | + // Individual sums |
| 38 | + mlir::Value lSum = rewriter.create<mlir::daphne::AllAggSumOp>(location, unknownType, addOp.getLhs()); |
| 39 | + mlir::Value rSum = rewriter.create<mlir::daphne::AllAggSumOp>(location, unknownType, addOp.getRhs()); |
| 40 | + mlir::Value scalar_add = rewriter.create<mlir::daphne::EwAddOp>(location, result_type, lSum, rSum); |
| 41 | + |
| 42 | + rewriter.replaceOp(op, scalar_add); |
| 43 | + return mlir::success(); |
| 44 | + } // Rule 2: sumAll(transpose(X)) to sumAll(X) |
| 45 | + else if (auto transOp = input.getDefiningOp<mlir::daphne::TransposeOp>()) { |
| 46 | + mlir::Value input_tr = transOp.getArg(); |
| 47 | + |
| 48 | + // Inputs should be matrices |
| 49 | + if (!input_tr.getType().isa<mlir::daphne::MatrixType>()) { |
| 50 | + return mlir::failure(); |
| 51 | + } |
| 52 | + |
| 53 | + mlir::Value simplf_sumOftranspose = rewriter.create<mlir::daphne::AllAggSumOp>(location, result_type, input_tr); |
| 54 | + rewriter.replaceOp(op, simplf_sumOftranspose); |
| 55 | + return mlir::success(); |
| 56 | + } // Rule 3: sum(lambda * X) -> lambda * sum(X) |
| 57 | + else if (auto lambdaMul = input.getDefiningOp<mlir::daphne::EwMulOp>()) { |
| 58 | + mlir::Value left_o = lambdaMul.getLhs(); |
| 59 | + mlir::Value right_o = lambdaMul.getRhs(); |
| 60 | + |
| 61 | + mlir::Value scalarOperand; |
| 62 | + mlir::Value matrixOperand; |
| 63 | + |
| 64 | + bool lhsIsSca = CompilerUtils::hasScaType(left_o); |
| 65 | + bool rhsIsSca = CompilerUtils::hasScaType(right_o); |
| 66 | + |
| 67 | + // Use .getType() only for matrix detection |
| 68 | + bool lhsIsMatrix = left_o.getType().isa<mlir::daphne::MatrixType>(); |
| 69 | + bool rhsIsMatrix = right_o.getType().isa<mlir::daphne::MatrixType>(); |
| 70 | + |
| 71 | + if (lhsIsSca && rhsIsMatrix) { |
| 72 | + scalarOperand = left_o; |
| 73 | + matrixOperand = right_o; |
| 74 | + } else if (rhsIsSca && lhsIsMatrix) { |
| 75 | + scalarOperand = right_o; |
| 76 | + matrixOperand = left_o; |
| 77 | + } else { |
| 78 | + return mlir::failure(); // Unsupported combination |
| 79 | + } |
| 80 | + |
| 81 | + mlir::Value innerSum = rewriter.create<mlir::daphne::AllAggSumOp>(location, unknownType, matrixOperand); |
| 82 | + mlir::Value newMul = rewriter.create<mlir::daphne::EwMulOp>(location, result_type, scalarOperand, innerSum); |
| 83 | + rewriter.replaceOp(op, newMul); |
| 84 | + |
| 85 | + } // Rule 4: trace(X @ Y) = sum(diagVector(X @ Y)) -> sum(X * transpose(Y)) |
| 86 | + else if (auto diagVec = input.getDefiningOp<mlir::daphne::DiagVectorOp>()) { |
| 87 | + mlir::Value input_dV = diagVec.getOperand(); // This should be a matrix (result of MatMul) |
| 88 | + if (auto matMul = input_dV.getDefiningOp<mlir::daphne::MatMulOp>()) { |
| 89 | + mlir::Value lhs = matMul.getLhs(); |
| 90 | + mlir::Value rhs = matMul.getRhs(); |
| 91 | + |
| 92 | + if (!lhs.getType().isa<mlir::daphne::MatrixType>() || !rhs.getType().isa<mlir::daphne::MatrixType>()) { |
| 93 | + return mlir::failure(); |
| 94 | + } |
| 95 | + |
| 96 | + mlir::Value t_rhs = rewriter.create<mlir::daphne::TransposeOp>(location, unknownType, rhs); |
| 97 | + mlir::Value ewMul_m = rewriter.create<mlir::daphne::EwMulOp>(location, unknownType, lhs, t_rhs); |
| 98 | + mlir::Value simplifiedSum = rewriter.create<mlir::daphne::AllAggSumOp>(location, result_type, ewMul_m); |
| 99 | + |
| 100 | + rewriter.replaceOp(op, simplifiedSum); |
| 101 | + return mlir::success(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return mlir::failure(); |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | +* @brief Canonicalizes: |
| 110 | +1)(X%*%Y)[7,3] → X[7,]%*%Y[,3] |
| 111 | +
|
| 112 | +*/ |
| 113 | +mlir::LogicalResult mlir::daphne::SliceColOp::canonicalize(mlir::daphne::SliceColOp op, |
| 114 | + mlir::PatternRewriter &rewriter) { |
| 115 | + mlir::Value input = op.getOperand(0); |
| 116 | + mlir::Location location = op.getLoc(); |
| 117 | + mlir::Type result_type = op.getResult().getType(); |
| 118 | + auto unknownType = mlir::daphne::UnknownType::get(rewriter.getContext()); |
| 119 | + |
| 120 | + auto sliceRowOp = input.getDefiningOp<mlir::daphne::SliceRowOp>(); |
| 121 | + if (!sliceRowOp) { |
| 122 | + return mlir::failure(); |
| 123 | + } |
| 124 | + |
| 125 | + auto matMulOp = sliceRowOp.getOperand(0).getDefiningOp<mlir::daphne::MatMulOp>(); |
| 126 | + if (!matMulOp) { |
| 127 | + return mlir::failure(); |
| 128 | + } |
| 129 | + |
| 130 | + // matrices |
| 131 | + mlir::Value X = matMulOp.getLhs(); |
| 132 | + mlir::Value Y = matMulOp.getRhs(); |
| 133 | + |
| 134 | + // lower-upper bounds for rows |
| 135 | + mlir::Value row_l = sliceRowOp.getOperand(1); |
| 136 | + mlir::Value row_u = sliceRowOp.getOperand(2); |
| 137 | + |
| 138 | + // lower-upper bounds for columns |
| 139 | + mlir::Value col_l = op.getOperand(1); |
| 140 | + mlir::Value col_u = op.getOperand(2); |
| 141 | + |
| 142 | + // to check if a matrix is transposed |
| 143 | + mlir::Value t_X = matMulOp.getOperand(2); |
| 144 | + mlir::Value t_Y = matMulOp.getOperand(3); |
| 145 | + |
| 146 | + bool isTransposedX = CompilerUtils::isConstant<bool>(t_X).second; |
| 147 | + bool isTransposedY = CompilerUtils::isConstant<bool>(t_Y).second; |
| 148 | + |
| 149 | + mlir::Value row; |
| 150 | + mlir::Value col; |
| 151 | + |
| 152 | + if (isTransposedX && isTransposedY) { |
| 153 | + row = rewriter.create<mlir::daphne::SliceColOp>(location, unknownType, X, row_l, row_u); |
| 154 | + col = rewriter.create<mlir::daphne::SliceRowOp>(location, unknownType, Y, col_l, col_u); |
| 155 | + } else if (!isTransposedX && isTransposedY) { |
| 156 | + row = rewriter.create<mlir::daphne::SliceRowOp>(location, unknownType, X, row_l, row_u); |
| 157 | + col = rewriter.create<mlir::daphne::SliceRowOp>(location, unknownType, Y, col_l, col_u); |
| 158 | + } else if ((isTransposedX && !isTransposedY)) { |
| 159 | + row = rewriter.create<mlir::daphne::SliceColOp>(location, unknownType, X, row_l, row_u); |
| 160 | + col = rewriter.create<mlir::daphne::SliceColOp>(location, unknownType, Y, col_l, col_u); |
| 161 | + } else if (!isTransposedX && !isTransposedY) { |
| 162 | + row = rewriter.create<mlir::daphne::SliceRowOp>(location, unknownType, X, row_l, row_u); |
| 163 | + col = rewriter.create<mlir::daphne::SliceColOp>(location, unknownType, Y, col_l, col_u); |
| 164 | + } else { |
| 165 | + return mlir::failure(); |
| 166 | + } |
| 167 | + |
| 168 | + auto newMatMul = rewriter.create<mlir::daphne::MatMulOp>(location, result_type, row, col, t_X, t_Y); |
| 169 | + rewriter.replaceOp(op, newMatMul.getResult()); |
| 170 | + return mlir::success(); |
| 171 | +} |
| 172 | + |
| 173 | +/** @brief Canonicalizes: |
| 174 | +1)X[a:b, c:d] = Y -> X=Y if dims(X) = dims(Y) |
| 175 | +//only for matrices with matching element types |
| 176 | +*/ |
| 177 | +mlir::LogicalResult mlir::daphne::InsertRowOp::canonicalize(mlir::daphne::InsertRowOp op, |
| 178 | + mlir::PatternRewriter &rewriter) { |
| 179 | + mlir::Location location = op.getLoc(); |
| 180 | + mlir::Type result_type = op.getResult().getType(); |
| 181 | + |
| 182 | + auto insertCol = op.getIns().getDefiningOp<mlir::daphne::InsertColOp>(); |
| 183 | + if (!insertCol) { |
| 184 | + return mlir::failure(); |
| 185 | + } |
| 186 | + |
| 187 | + auto sliceRow = insertCol.getArg().getDefiningOp<mlir::daphne::SliceRowOp>(); |
| 188 | + if (!sliceRow) { |
| 189 | + return mlir::failure(); |
| 190 | + } |
| 191 | + |
| 192 | + mlir::Value sliceInput = sliceRow.getSource(); // X |
| 193 | + mlir::Value insertColInput = insertCol.getIns(); // Y |
| 194 | + if (!sliceInput.getType().isa<mlir::daphne::MatrixType>() || |
| 195 | + !insertColInput.getType().isa<mlir::daphne::MatrixType>()) { |
| 196 | + return mlir::failure(); |
| 197 | + } |
| 198 | + |
| 199 | + auto sliceType = sliceInput.getType().dyn_cast<mlir::daphne::MatrixType>(); |
| 200 | + auto insertColInputType = insertColInput.getType().dyn_cast<mlir::daphne::MatrixType>(); |
| 201 | + auto opResultType = op.getResult().getType().dyn_cast<mlir::daphne::MatrixType>(); |
| 202 | + |
| 203 | + if (!sliceType || !insertColInputType || !opResultType) { |
| 204 | + return mlir::failure(); |
| 205 | + } |
| 206 | + |
| 207 | + if (sliceType.getElementType() != insertColInputType.getElementType()) { |
| 208 | + return mlir::failure(); |
| 209 | + } |
| 210 | + |
| 211 | + int64_t numRows_X = sliceType.getNumRows(); |
| 212 | + int64_t numCols_X = sliceType.getNumCols(); |
| 213 | + int64_t numRows_Y = insertColInputType.getNumRows(); |
| 214 | + int64_t numCols_Y = insertColInputType.getNumCols(); |
| 215 | + |
| 216 | + if (numRows_X == -1 || numCols_X == -1 || numRows_Y == -1 || numCols_Y == -1) { |
| 217 | + return mlir::failure(); |
| 218 | + } |
| 219 | + |
| 220 | + if (numRows_X != numRows_Y || numCols_X != numCols_Y) { |
| 221 | + return mlir::failure(); |
| 222 | + } |
| 223 | + |
| 224 | + auto renamed = rewriter.create<mlir::daphne::RenameOp>(location, result_type, insertColInput); |
| 225 | + rewriter.replaceOp(op, renamed.getResult()); |
| 226 | + return mlir::success(); |
| 227 | +} |
| 228 | + |
22 | 229 | mlir::LogicalResult mlir::daphne::VectorizedPipelineOp::canonicalize(mlir::daphne::VectorizedPipelineOp op, |
23 | 230 | mlir::PatternRewriter &rewriter) { |
24 | 231 | // // Find duplicate inputs |
|
0 commit comments