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 return constFoldUnaryOpConditional<FloatAttr>( 96 operands, [](const APFloat &a) -> Optional<APFloat> { 97 if (a.isNegative()) 98 return {}; 99 100 if (a.getSizeInBits(a.getSemantics()) == 64) 101 return APFloat(log2(a.convertToDouble())); 102 103 if (a.getSizeInBits(a.getSemantics()) == 32) 104 return APFloat(log2f(a.convertToFloat())); 105 106 return {}; 107 }); 108 } 109 110 //===----------------------------------------------------------------------===// 111 // Log10Op folder 112 //===----------------------------------------------------------------------===// 113 114 OpFoldResult math::Log10Op::fold(ArrayRef<Attribute> operands) { 115 return constFoldUnaryOpConditional<FloatAttr>( 116 operands, [](const APFloat &a) -> Optional<APFloat> { 117 if (a.isNegative()) 118 return {}; 119 120 switch (a.getSizeInBits(a.getSemantics())) { 121 case 64: 122 return APFloat(log10(a.convertToDouble())); 123 case 32: 124 return APFloat(log10f(a.convertToFloat())); 125 default: 126 return {}; 127 } 128 }); 129 } 130 131 //===----------------------------------------------------------------------===// 132 // Log1pOp folder 133 //===----------------------------------------------------------------------===// 134 135 OpFoldResult math::Log1pOp::fold(ArrayRef<Attribute> operands) { 136 return constFoldUnaryOpConditional<FloatAttr>( 137 operands, [](const APFloat &a) -> Optional<APFloat> { 138 switch (a.getSizeInBits(a.getSemantics())) { 139 case 64: 140 if ((a + APFloat(1.0)).isNegative()) 141 return {}; 142 return APFloat(log1p(a.convertToDouble())); 143 case 32: 144 if ((a + APFloat(1.0f)).isNegative()) 145 return {}; 146 return APFloat(log1pf(a.convertToFloat())); 147 default: 148 return {}; 149 } 150 }); 151 } 152 153 //===----------------------------------------------------------------------===// 154 // PowFOp folder 155 //===----------------------------------------------------------------------===// 156 157 OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) { 158 return constFoldBinaryOpConditional<FloatAttr>( 159 operands, [](const APFloat &a, const APFloat &b) -> Optional<APFloat> { 160 if (a.getSizeInBits(a.getSemantics()) == 64 && 161 b.getSizeInBits(b.getSemantics()) == 64) 162 return APFloat(pow(a.convertToDouble(), b.convertToDouble())); 163 164 if (a.getSizeInBits(a.getSemantics()) == 32 && 165 b.getSizeInBits(b.getSemantics()) == 32) 166 return APFloat(powf(a.convertToFloat(), b.convertToFloat())); 167 168 return {}; 169 }); 170 } 171 172 //===----------------------------------------------------------------------===// 173 // SqrtOp folder 174 //===----------------------------------------------------------------------===// 175 176 OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) { 177 return constFoldUnaryOpConditional<FloatAttr>( 178 operands, [](const APFloat &a) -> Optional<APFloat> { 179 if (a.isNegative()) 180 return {}; 181 182 switch (a.getSizeInBits(a.getSemantics())) { 183 case 64: 184 return APFloat(sqrt(a.convertToDouble())); 185 case 32: 186 return APFloat(sqrtf(a.convertToFloat())); 187 default: 188 return {}; 189 } 190 }); 191 } 192 193 /// Materialize an integer or floating point constant. 194 Operation *math::MathDialect::materializeConstant(OpBuilder &builder, 195 Attribute value, Type type, 196 Location loc) { 197 return builder.create<arith::ConstantOp>(loc, value, type); 198 } 199