1 //===- MathOps.cpp - MLIR operations for math implementation --------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "mlir/Dialect/Arithmetic/IR/Arithmetic.h" 10 #include "mlir/Dialect/CommonFolders.h" 11 #include "mlir/Dialect/Math/IR/Math.h" 12 #include "mlir/IR/Builders.h" 13 14 using namespace mlir; 15 using namespace mlir::math; 16 17 //===----------------------------------------------------------------------===// 18 // TableGen'd op method definitions 19 //===----------------------------------------------------------------------===// 20 21 #define GET_OP_CLASSES 22 #include "mlir/Dialect/Math/IR/MathOps.cpp.inc" 23 24 //===----------------------------------------------------------------------===// 25 // AbsOp folder 26 //===----------------------------------------------------------------------===// 27 28 OpFoldResult math::AbsOp::fold(ArrayRef<Attribute> operands) { 29 return constFoldUnaryOp<FloatAttr>(operands, [](const APFloat &a) { 30 const APFloat &result(a); 31 return abs(result); 32 }); 33 } 34 35 //===----------------------------------------------------------------------===// 36 // CeilOp folder 37 //===----------------------------------------------------------------------===// 38 39 OpFoldResult math::CeilOp::fold(ArrayRef<Attribute> operands) { 40 return constFoldUnaryOp<FloatAttr>(operands, [](const APFloat &a) { 41 APFloat result(a); 42 result.roundToIntegral(llvm::RoundingMode::TowardPositive); 43 return result; 44 }); 45 } 46 47 //===----------------------------------------------------------------------===// 48 // CopySignOp folder 49 //===----------------------------------------------------------------------===// 50 51 OpFoldResult math::CopySignOp::fold(ArrayRef<Attribute> operands) { 52 return constFoldBinaryOp<FloatAttr>(operands, 53 [](const APFloat &a, const APFloat &b) { 54 APFloat result(a); 55 result.copySign(b); 56 return result; 57 }); 58 } 59 60 //===----------------------------------------------------------------------===// 61 // CountLeadingZerosOp folder 62 //===----------------------------------------------------------------------===// 63 64 OpFoldResult math::CountLeadingZerosOp::fold(ArrayRef<Attribute> operands) { 65 return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) { 66 return APInt(a.getBitWidth(), a.countLeadingZeros()); 67 }); 68 } 69 70 //===----------------------------------------------------------------------===// 71 // CountTrailingZerosOp folder 72 //===----------------------------------------------------------------------===// 73 74 OpFoldResult math::CountTrailingZerosOp::fold(ArrayRef<Attribute> operands) { 75 return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) { 76 return APInt(a.getBitWidth(), a.countTrailingZeros()); 77 }); 78 } 79 80 //===----------------------------------------------------------------------===// 81 // CtPopOp folder 82 //===----------------------------------------------------------------------===// 83 84 OpFoldResult math::CtPopOp::fold(ArrayRef<Attribute> operands) { 85 return constFoldUnaryOp<IntegerAttr>(operands, [](const APInt &a) { 86 return APInt(a.getBitWidth(), a.countPopulation()); 87 }); 88 } 89 90 //===----------------------------------------------------------------------===// 91 // Log2Op folder 92 //===----------------------------------------------------------------------===// 93 94 OpFoldResult math::Log2Op::fold(ArrayRef<Attribute> operands) { 95 auto constOperand = operands.front(); 96 if (!constOperand) 97 return {}; 98 99 auto attr = constOperand.dyn_cast<FloatAttr>(); 100 if (!attr) 101 return {}; 102 103 auto ft = getType().cast<FloatType>(); 104 105 APFloat apf = attr.getValue(); 106 107 if (apf.isNegative()) 108 return {}; 109 110 if (ft.getWidth() == 64) 111 return FloatAttr::get(getType(), log2(apf.convertToDouble())); 112 113 if (ft.getWidth() == 32) 114 return FloatAttr::get(getType(), log2f(apf.convertToFloat())); 115 116 return {}; 117 } 118 119 //===----------------------------------------------------------------------===// 120 // PowFOp folder 121 //===----------------------------------------------------------------------===// 122 123 OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) { 124 auto ft = getType().dyn_cast<FloatType>(); 125 if (!ft) 126 return {}; 127 128 APFloat vals[2]{APFloat(ft.getFloatSemantics()), 129 APFloat(ft.getFloatSemantics())}; 130 for (int i = 0; i < 2; ++i) { 131 if (!operands[i]) 132 return {}; 133 134 auto attr = operands[i].dyn_cast<FloatAttr>(); 135 if (!attr) 136 return {}; 137 138 vals[i] = attr.getValue(); 139 } 140 141 if (ft.getWidth() == 64) 142 return FloatAttr::get( 143 getType(), pow(vals[0].convertToDouble(), vals[1].convertToDouble())); 144 145 if (ft.getWidth() == 32) 146 return FloatAttr::get( 147 getType(), powf(vals[0].convertToFloat(), vals[1].convertToFloat())); 148 149 return {}; 150 } 151 152 OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) { 153 auto constOperand = operands.front(); 154 if (!constOperand) 155 return {}; 156 157 auto attr = constOperand.dyn_cast<FloatAttr>(); 158 if (!attr) 159 return {}; 160 161 auto ft = getType().cast<FloatType>(); 162 163 APFloat apf = attr.getValue(); 164 165 if (apf.isNegative()) 166 return {}; 167 168 if (ft.getWidth() == 64) 169 return FloatAttr::get(getType(), sqrt(apf.convertToDouble())); 170 171 if (ft.getWidth() == 32) 172 return FloatAttr::get(getType(), sqrtf(apf.convertToFloat())); 173 174 return {}; 175 } 176 177 /// Materialize an integer or floating point constant. 178 Operation *math::MathDialect::materializeConstant(OpBuilder &builder, 179 Attribute value, Type type, 180 Location loc) { 181 return builder.create<arith::ConstantOp>(loc, value, type); 182 } 183