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 // PowFOp folder 112 //===----------------------------------------------------------------------===// 113 114 OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) { 115 return constFoldBinaryOpConditional<FloatAttr>( 116 operands, [](const APFloat &a, const APFloat &b) -> Optional<APFloat> { 117 if (a.getSizeInBits(a.getSemantics()) == 64 && 118 b.getSizeInBits(b.getSemantics()) == 64) 119 return APFloat(pow(a.convertToDouble(), b.convertToDouble())); 120 121 if (a.getSizeInBits(a.getSemantics()) == 32 && 122 b.getSizeInBits(b.getSemantics()) == 32) 123 return APFloat(powf(a.convertToFloat(), b.convertToFloat())); 124 125 return {}; 126 }); 127 } 128 129 //===----------------------------------------------------------------------===// 130 // SqrtOp folder 131 //===----------------------------------------------------------------------===// 132 133 OpFoldResult math::SqrtOp::fold(ArrayRef<Attribute> operands) { 134 return constFoldUnaryOpConditional<FloatAttr>( 135 operands, [](const APFloat &a) -> Optional<APFloat> { 136 if (a.isNegative()) 137 return {}; 138 139 switch (a.getSizeInBits(a.getSemantics())) { 140 case 64: 141 return APFloat(sqrt(a.convertToDouble())); 142 case 32: 143 return APFloat(sqrtf(a.convertToFloat())); 144 default: 145 return {}; 146 } 147 }); 148 } 149 150 /// Materialize an integer or floating point constant. 151 Operation *math::MathDialect::materializeConstant(OpBuilder &builder, 152 Attribute value, Type type, 153 Location loc) { 154 return builder.create<arith::ConstantOp>(loc, value, type); 155 } 156