| 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/Arith/IR/Arith.h" |
| 10 | #include "mlir/Dialect/CommonFolders.h" |
| 11 | #include "mlir/Dialect/Math/IR/Math.h" |
| 12 | #include "mlir/Dialect/UB/IR/UBOps.h" |
| 13 | #include "mlir/IR/Builders.h" |
| 14 | #include <optional> |
| 15 | |
| 16 | using namespace mlir; |
| 17 | using namespace mlir::math; |
| 18 | |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | // Common helpers |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | /// Return the type of the same shape (scalar, vector or tensor) containing i1. |
| 24 | static Type getI1SameShape(Type type) { |
| 25 | auto i1Type = IntegerType::get(context: type.getContext(), width: 1); |
| 26 | if (auto shapedType = llvm::dyn_cast<ShapedType>(Val&: type)) |
| 27 | return shapedType.cloneWith(shape: std::nullopt, elementType: i1Type); |
| 28 | if (llvm::isa<UnrankedTensorType>(Val: type)) |
| 29 | return UnrankedTensorType::get(elementType: i1Type); |
| 30 | return i1Type; |
| 31 | } |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // TableGen'd op method definitions |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #define GET_OP_CLASSES |
| 38 | #include "mlir/Dialect/Math/IR/MathOps.cpp.inc" |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // AbsFOp folder |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | OpFoldResult math::AbsFOp::fold(FoldAdaptor adaptor) { |
| 45 | return constFoldUnaryOp<FloatAttr>(operands: adaptor.getOperands(), |
| 46 | calculate: [](const APFloat &a) { return abs(X: a); }); |
| 47 | } |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | // AbsIOp folder |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
| 53 | OpFoldResult math::AbsIOp::fold(FoldAdaptor adaptor) { |
| 54 | return constFoldUnaryOp<IntegerAttr>(operands: adaptor.getOperands(), |
| 55 | calculate: [](const APInt &a) { return a.abs(); }); |
| 56 | } |
| 57 | |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | // AcosOp folder |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | OpFoldResult math::AcosOp::fold(FoldAdaptor adaptor) { |
| 63 | return constFoldUnaryOpConditional<FloatAttr>( |
| 64 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 65 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 66 | case APFloat::Semantics::S_IEEEdouble: |
| 67 | return APFloat(acos(x: a.convertToDouble())); |
| 68 | case APFloat::Semantics::S_IEEEsingle: |
| 69 | return APFloat(acosf(x: a.convertToFloat())); |
| 70 | default: |
| 71 | return {}; |
| 72 | } |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | // AcoshOp folder |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | OpFoldResult math::AcoshOp::fold(FoldAdaptor adaptor) { |
| 81 | return constFoldUnaryOpConditional<FloatAttr>( |
| 82 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 83 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 84 | case APFloat::Semantics::S_IEEEdouble: |
| 85 | return APFloat(acosh(x: a.convertToDouble())); |
| 86 | case APFloat::Semantics::S_IEEEsingle: |
| 87 | return APFloat(acoshf(x: a.convertToFloat())); |
| 88 | default: |
| 89 | return {}; |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // AsinOp folder |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | OpFoldResult math::AsinOp::fold(FoldAdaptor adaptor) { |
| 99 | return constFoldUnaryOpConditional<FloatAttr>( |
| 100 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 101 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 102 | case APFloat::Semantics::S_IEEEdouble: |
| 103 | return APFloat(asin(x: a.convertToDouble())); |
| 104 | case APFloat::Semantics::S_IEEEsingle: |
| 105 | return APFloat(asinf(x: a.convertToFloat())); |
| 106 | default: |
| 107 | return {}; |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // AsinhOp folder |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | OpFoldResult math::AsinhOp::fold(FoldAdaptor adaptor) { |
| 117 | return constFoldUnaryOpConditional<FloatAttr>( |
| 118 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 119 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 120 | case APFloat::Semantics::S_IEEEdouble: |
| 121 | return APFloat(asinh(x: a.convertToDouble())); |
| 122 | case APFloat::Semantics::S_IEEEsingle: |
| 123 | return APFloat(asinhf(x: a.convertToFloat())); |
| 124 | default: |
| 125 | return {}; |
| 126 | } |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | // AtanOp folder |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
| 134 | OpFoldResult math::AtanOp::fold(FoldAdaptor adaptor) { |
| 135 | return constFoldUnaryOpConditional<FloatAttr>( |
| 136 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 137 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 138 | case APFloat::Semantics::S_IEEEdouble: |
| 139 | return APFloat(atan(x: a.convertToDouble())); |
| 140 | case APFloat::Semantics::S_IEEEsingle: |
| 141 | return APFloat(atanf(x: a.convertToFloat())); |
| 142 | default: |
| 143 | return {}; |
| 144 | } |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | // AtanhOp folder |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
| 152 | OpFoldResult math::AtanhOp::fold(FoldAdaptor adaptor) { |
| 153 | return constFoldUnaryOpConditional<FloatAttr>( |
| 154 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 155 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 156 | case APFloat::Semantics::S_IEEEdouble: |
| 157 | return APFloat(atanh(x: a.convertToDouble())); |
| 158 | case APFloat::Semantics::S_IEEEsingle: |
| 159 | return APFloat(atanhf(x: a.convertToFloat())); |
| 160 | default: |
| 161 | return {}; |
| 162 | } |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | //===----------------------------------------------------------------------===// |
| 167 | // Atan2Op folder |
| 168 | //===----------------------------------------------------------------------===// |
| 169 | |
| 170 | OpFoldResult math::Atan2Op::fold(FoldAdaptor adaptor) { |
| 171 | return constFoldBinaryOpConditional<FloatAttr>( |
| 172 | operands: adaptor.getOperands(), |
| 173 | calculate: [](const APFloat &a, const APFloat &b) -> std::optional<APFloat> { |
| 174 | if (a.isZero() && b.isZero()) |
| 175 | return llvm::APFloat::getNaN(Sem: a.getSemantics()); |
| 176 | |
| 177 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 178 | case APFloat::Semantics::S_IEEEdouble: |
| 179 | return APFloat(atan2(y: a.convertToDouble(), x: b.convertToDouble())); |
| 180 | case APFloat::Semantics::S_IEEEsingle: |
| 181 | return APFloat(atan2f(y: a.convertToFloat(), x: b.convertToFloat())); |
| 182 | default: |
| 183 | return {}; |
| 184 | } |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | //===----------------------------------------------------------------------===// |
| 189 | // CbrtOp folder |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
| 192 | OpFoldResult math::CbrtOp::fold(FoldAdaptor adaptor) { |
| 193 | return constFoldUnaryOpConditional<FloatAttr>( |
| 194 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 195 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 196 | case APFloat::Semantics::S_IEEEdouble: |
| 197 | return APFloat(cbrt(x: a.convertToDouble())); |
| 198 | case APFloat::Semantics::S_IEEEsingle: |
| 199 | return APFloat(cbrtf(x: a.convertToFloat())); |
| 200 | default: |
| 201 | return {}; |
| 202 | } |
| 203 | }); |
| 204 | } |
| 205 | |
| 206 | //===----------------------------------------------------------------------===// |
| 207 | // CeilOp folder |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
| 210 | OpFoldResult math::CeilOp::fold(FoldAdaptor adaptor) { |
| 211 | return constFoldUnaryOp<FloatAttr>( |
| 212 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) { |
| 213 | APFloat result(a); |
| 214 | result.roundToIntegral(RM: llvm::RoundingMode::TowardPositive); |
| 215 | return result; |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | // CopySignOp folder |
| 221 | //===----------------------------------------------------------------------===// |
| 222 | |
| 223 | OpFoldResult math::CopySignOp::fold(FoldAdaptor adaptor) { |
| 224 | return constFoldBinaryOp<FloatAttr>(operands: adaptor.getOperands(), |
| 225 | calculate: [](const APFloat &a, const APFloat &b) { |
| 226 | APFloat result(a); |
| 227 | result.copySign(RHS: b); |
| 228 | return result; |
| 229 | }); |
| 230 | } |
| 231 | |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | // CosOp folder |
| 234 | //===----------------------------------------------------------------------===// |
| 235 | |
| 236 | OpFoldResult math::CosOp::fold(FoldAdaptor adaptor) { |
| 237 | return constFoldUnaryOpConditional<FloatAttr>( |
| 238 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 239 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 240 | case APFloat::Semantics::S_IEEEdouble: |
| 241 | return APFloat(cos(x: a.convertToDouble())); |
| 242 | case APFloat::Semantics::S_IEEEsingle: |
| 243 | return APFloat(cosf(x: a.convertToFloat())); |
| 244 | default: |
| 245 | return {}; |
| 246 | } |
| 247 | }); |
| 248 | } |
| 249 | |
| 250 | //===----------------------------------------------------------------------===// |
| 251 | // CoshOp folder |
| 252 | //===----------------------------------------------------------------------===// |
| 253 | |
| 254 | OpFoldResult math::CoshOp::fold(FoldAdaptor adaptor) { |
| 255 | return constFoldUnaryOpConditional<FloatAttr>( |
| 256 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 257 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 258 | case APFloat::Semantics::S_IEEEdouble: |
| 259 | return APFloat(cosh(x: a.convertToDouble())); |
| 260 | case APFloat::Semantics::S_IEEEsingle: |
| 261 | return APFloat(coshf(x: a.convertToFloat())); |
| 262 | default: |
| 263 | return {}; |
| 264 | } |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // SinOp folder |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | OpFoldResult math::SinOp::fold(FoldAdaptor adaptor) { |
| 273 | return constFoldUnaryOpConditional<FloatAttr>( |
| 274 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 275 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 276 | case APFloat::Semantics::S_IEEEdouble: |
| 277 | return APFloat(sin(x: a.convertToDouble())); |
| 278 | case APFloat::Semantics::S_IEEEsingle: |
| 279 | return APFloat(sinf(x: a.convertToFloat())); |
| 280 | default: |
| 281 | return {}; |
| 282 | } |
| 283 | }); |
| 284 | } |
| 285 | |
| 286 | //===----------------------------------------------------------------------===// |
| 287 | // SinhOp folder |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
| 290 | OpFoldResult math::SinhOp::fold(FoldAdaptor adaptor) { |
| 291 | return constFoldUnaryOpConditional<FloatAttr>( |
| 292 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 293 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 294 | case APFloat::Semantics::S_IEEEdouble: |
| 295 | return APFloat(sinh(x: a.convertToDouble())); |
| 296 | case APFloat::Semantics::S_IEEEsingle: |
| 297 | return APFloat(sinhf(x: a.convertToFloat())); |
| 298 | default: |
| 299 | return {}; |
| 300 | } |
| 301 | }); |
| 302 | } |
| 303 | |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | // SinCosOp |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | std::optional<SmallVector<int64_t, 4>> |
| 309 | math::SincosOp::getIterationShapeToUnroll() { |
| 310 | if (auto vt = mlir::dyn_cast<VectorType>(Val: getOperand().getType())) |
| 311 | return llvm::to_vector<4>(Range: vt.getShape()); |
| 312 | return std::nullopt; |
| 313 | } |
| 314 | |
| 315 | LogicalResult math::SincosOp::fold(FoldAdaptor adaptor, |
| 316 | SmallVectorImpl<OpFoldResult> &result) { |
| 317 | auto foldSincos = [](const APFloat &a, double (*fnDouble)(double), |
| 318 | float (*fnFloat)(float)) -> std::optional<APFloat> { |
| 319 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 320 | case APFloat::Semantics::S_IEEEdouble: |
| 321 | return APFloat(fnDouble(a.convertToDouble())); |
| 322 | case APFloat::Semantics::S_IEEEsingle: |
| 323 | return APFloat(fnFloat(a.convertToFloat())); |
| 324 | default: |
| 325 | return {}; |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | Attribute sinRes = constFoldUnaryOpConditional<FloatAttr>( |
| 330 | operands: adaptor.getOperands(), |
| 331 | calculate: [&](const APFloat &a) { return foldSincos(a, sin, sinf); }); |
| 332 | Attribute cosRes = constFoldUnaryOpConditional<FloatAttr>( |
| 333 | operands: adaptor.getOperands(), |
| 334 | calculate: [&](const APFloat &a) { return foldSincos(a, cos, cosf); }); |
| 335 | |
| 336 | if (sinRes && cosRes) { |
| 337 | result.push_back(Elt: sinRes); |
| 338 | result.push_back(Elt: cosRes); |
| 339 | return success(); |
| 340 | } |
| 341 | return failure(); |
| 342 | } |
| 343 | |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | // CountLeadingZerosOp folder |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | |
| 348 | OpFoldResult math::CountLeadingZerosOp::fold(FoldAdaptor adaptor) { |
| 349 | return constFoldUnaryOp<IntegerAttr>( |
| 350 | operands: adaptor.getOperands(), |
| 351 | calculate: [](const APInt &a) { return APInt(a.getBitWidth(), a.countl_zero()); }); |
| 352 | } |
| 353 | |
| 354 | //===----------------------------------------------------------------------===// |
| 355 | // CountTrailingZerosOp folder |
| 356 | //===----------------------------------------------------------------------===// |
| 357 | |
| 358 | OpFoldResult math::CountTrailingZerosOp::fold(FoldAdaptor adaptor) { |
| 359 | return constFoldUnaryOp<IntegerAttr>( |
| 360 | operands: adaptor.getOperands(), |
| 361 | calculate: [](const APInt &a) { return APInt(a.getBitWidth(), a.countr_zero()); }); |
| 362 | } |
| 363 | |
| 364 | //===----------------------------------------------------------------------===// |
| 365 | // CtPopOp folder |
| 366 | //===----------------------------------------------------------------------===// |
| 367 | |
| 368 | OpFoldResult math::CtPopOp::fold(FoldAdaptor adaptor) { |
| 369 | return constFoldUnaryOp<IntegerAttr>( |
| 370 | operands: adaptor.getOperands(), |
| 371 | calculate: [](const APInt &a) { return APInt(a.getBitWidth(), a.popcount()); }); |
| 372 | } |
| 373 | |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | // ErfOp folder |
| 376 | //===----------------------------------------------------------------------===// |
| 377 | |
| 378 | OpFoldResult math::ErfOp::fold(FoldAdaptor adaptor) { |
| 379 | return constFoldUnaryOpConditional<FloatAttr>( |
| 380 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 381 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 382 | case APFloat::Semantics::S_IEEEdouble: |
| 383 | return APFloat(erf(a.convertToDouble())); |
| 384 | case APFloat::Semantics::S_IEEEsingle: |
| 385 | return APFloat(erff(a.convertToFloat())); |
| 386 | default: |
| 387 | return {}; |
| 388 | } |
| 389 | }); |
| 390 | } |
| 391 | |
| 392 | //===----------------------------------------------------------------------===// |
| 393 | // ErfcOp folder |
| 394 | //===----------------------------------------------------------------------===// |
| 395 | |
| 396 | OpFoldResult math::ErfcOp::fold(FoldAdaptor adaptor) { |
| 397 | return constFoldUnaryOpConditional<FloatAttr>( |
| 398 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 399 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 400 | case APFloat::Semantics::S_IEEEdouble: |
| 401 | return APFloat(erfc(a.convertToDouble())); |
| 402 | case APFloat::Semantics::S_IEEEsingle: |
| 403 | return APFloat(erfcf(a.convertToFloat())); |
| 404 | default: |
| 405 | return {}; |
| 406 | } |
| 407 | }); |
| 408 | } |
| 409 | |
| 410 | //===----------------------------------------------------------------------===// |
| 411 | // IPowIOp folder |
| 412 | //===----------------------------------------------------------------------===// |
| 413 | |
| 414 | OpFoldResult math::IPowIOp::fold(FoldAdaptor adaptor) { |
| 415 | return constFoldBinaryOpConditional<IntegerAttr>( |
| 416 | operands: adaptor.getOperands(), |
| 417 | calculate: [](const APInt &base, const APInt &power) -> std::optional<APInt> { |
| 418 | unsigned width = base.getBitWidth(); |
| 419 | auto zeroValue = APInt::getZero(numBits: width); |
| 420 | // i1 folding is ambiguous with signed semantics, don't fold. |
| 421 | if (width == 1) |
| 422 | return {}; |
| 423 | APInt oneValue{width, 1ULL, /*isSigned=*/true}; |
| 424 | APInt minusOneValue{width, -1ULL, /*isSigned=*/true}; |
| 425 | |
| 426 | if (power.isZero()) |
| 427 | return oneValue; |
| 428 | |
| 429 | if (power.isNegative()) { |
| 430 | // Leave 0 raised to negative power not folded. |
| 431 | if (base.isZero()) |
| 432 | return {}; |
| 433 | if (base.isOne()) |
| 434 | return oneValue; |
| 435 | // If abs(base) > 1, then the result is zero. |
| 436 | if (base.ne(RHS: minusOneValue)) |
| 437 | return zeroValue; |
| 438 | // base == -1: |
| 439 | // -1: power is odd |
| 440 | // 1: power is even |
| 441 | if (power[0] == 1) |
| 442 | return minusOneValue; |
| 443 | |
| 444 | return oneValue; |
| 445 | } |
| 446 | |
| 447 | // power is positive. |
| 448 | APInt result = oneValue; |
| 449 | APInt curBase = base; |
| 450 | APInt curPower = power; |
| 451 | while (true) { |
| 452 | if (curPower[0] == 1) |
| 453 | result *= curBase; |
| 454 | curPower.lshrInPlace(ShiftAmt: 1); |
| 455 | if (curPower.isZero()) |
| 456 | return result; |
| 457 | curBase *= curBase; |
| 458 | } |
| 459 | }); |
| 460 | } |
| 461 | |
| 462 | //===----------------------------------------------------------------------===// |
| 463 | // LogOp folder |
| 464 | //===----------------------------------------------------------------------===// |
| 465 | |
| 466 | OpFoldResult math::LogOp::fold(FoldAdaptor adaptor) { |
| 467 | return constFoldUnaryOpConditional<FloatAttr>( |
| 468 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 469 | if (a.isNegative()) |
| 470 | return {}; |
| 471 | |
| 472 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 473 | case APFloat::Semantics::S_IEEEdouble: |
| 474 | return APFloat(log(x: a.convertToDouble())); |
| 475 | case APFloat::Semantics::S_IEEEsingle: |
| 476 | return APFloat(logf(x: a.convertToFloat())); |
| 477 | default: |
| 478 | return {}; |
| 479 | } |
| 480 | }); |
| 481 | } |
| 482 | |
| 483 | //===----------------------------------------------------------------------===// |
| 484 | // Log2Op folder |
| 485 | //===----------------------------------------------------------------------===// |
| 486 | |
| 487 | OpFoldResult math::Log2Op::fold(FoldAdaptor adaptor) { |
| 488 | return constFoldUnaryOpConditional<FloatAttr>( |
| 489 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 490 | if (a.isNegative()) |
| 491 | return {}; |
| 492 | |
| 493 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 494 | case APFloat::Semantics::S_IEEEdouble: |
| 495 | return APFloat(log2(x: a.convertToDouble())); |
| 496 | case APFloat::Semantics::S_IEEEsingle: |
| 497 | return APFloat(log2f(x: a.convertToFloat())); |
| 498 | default: |
| 499 | return {}; |
| 500 | } |
| 501 | }); |
| 502 | } |
| 503 | |
| 504 | //===----------------------------------------------------------------------===// |
| 505 | // Log10Op folder |
| 506 | //===----------------------------------------------------------------------===// |
| 507 | |
| 508 | OpFoldResult math::Log10Op::fold(FoldAdaptor adaptor) { |
| 509 | return constFoldUnaryOpConditional<FloatAttr>( |
| 510 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 511 | if (a.isNegative()) |
| 512 | return {}; |
| 513 | |
| 514 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 515 | case APFloat::Semantics::S_IEEEdouble: |
| 516 | return APFloat(log10(x: a.convertToDouble())); |
| 517 | case APFloat::Semantics::S_IEEEsingle: |
| 518 | return APFloat(log10f(x: a.convertToFloat())); |
| 519 | default: |
| 520 | return {}; |
| 521 | } |
| 522 | }); |
| 523 | } |
| 524 | |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | // Log1pOp folder |
| 527 | //===----------------------------------------------------------------------===// |
| 528 | |
| 529 | OpFoldResult math::Log1pOp::fold(FoldAdaptor adaptor) { |
| 530 | return constFoldUnaryOpConditional<FloatAttr>( |
| 531 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 532 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 533 | case APFloat::Semantics::S_IEEEdouble: |
| 534 | if ((a + APFloat(1.0)).isNegative()) |
| 535 | return {}; |
| 536 | return APFloat(log1p(x: a.convertToDouble())); |
| 537 | case APFloat::Semantics::S_IEEEsingle: |
| 538 | if ((a + APFloat(1.0f)).isNegative()) |
| 539 | return {}; |
| 540 | return APFloat(log1pf(x: a.convertToFloat())); |
| 541 | default: |
| 542 | return {}; |
| 543 | } |
| 544 | }); |
| 545 | } |
| 546 | |
| 547 | //===----------------------------------------------------------------------===// |
| 548 | // PowFOp folder |
| 549 | //===----------------------------------------------------------------------===// |
| 550 | |
| 551 | OpFoldResult math::PowFOp::fold(FoldAdaptor adaptor) { |
| 552 | return constFoldBinaryOpConditional<FloatAttr>( |
| 553 | operands: adaptor.getOperands(), |
| 554 | calculate: [](const APFloat &a, const APFloat &b) -> std::optional<APFloat> { |
| 555 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 556 | case APFloat::Semantics::S_IEEEdouble: |
| 557 | return APFloat(pow(x: a.convertToDouble(), y: b.convertToDouble())); |
| 558 | case APFloat::Semantics::S_IEEEsingle: |
| 559 | return APFloat(powf(x: a.convertToFloat(), y: b.convertToFloat())); |
| 560 | default: |
| 561 | return {}; |
| 562 | } |
| 563 | }); |
| 564 | } |
| 565 | |
| 566 | //===----------------------------------------------------------------------===// |
| 567 | // RsqrtOp folder |
| 568 | //===----------------------------------------------------------------------===// |
| 569 | |
| 570 | OpFoldResult math::RsqrtOp::fold(FoldAdaptor adaptor) { |
| 571 | return constFoldUnaryOpConditional<FloatAttr>( |
| 572 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 573 | if (a.isNegative()) |
| 574 | return {}; |
| 575 | |
| 576 | APFloat one(a.getSemantics(), 1); |
| 577 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 578 | case APFloat::Semantics::S_IEEEdouble: |
| 579 | return one / APFloat(sqrt(x: a.convertToDouble())); |
| 580 | case APFloat::Semantics::S_IEEEsingle: |
| 581 | return one / APFloat(sqrtf(x: a.convertToFloat())); |
| 582 | default: |
| 583 | return {}; |
| 584 | } |
| 585 | }); |
| 586 | } |
| 587 | |
| 588 | //===----------------------------------------------------------------------===// |
| 589 | // SqrtOp folder |
| 590 | //===----------------------------------------------------------------------===// |
| 591 | |
| 592 | OpFoldResult math::SqrtOp::fold(FoldAdaptor adaptor) { |
| 593 | return constFoldUnaryOpConditional<FloatAttr>( |
| 594 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 595 | if (a.isNegative()) |
| 596 | return {}; |
| 597 | |
| 598 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 599 | case APFloat::Semantics::S_IEEEdouble: |
| 600 | return APFloat(sqrt(x: a.convertToDouble())); |
| 601 | case APFloat::Semantics::S_IEEEsingle: |
| 602 | return APFloat(sqrtf(x: a.convertToFloat())); |
| 603 | default: |
| 604 | return {}; |
| 605 | } |
| 606 | }); |
| 607 | } |
| 608 | |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | // ExpOp folder |
| 611 | //===----------------------------------------------------------------------===// |
| 612 | |
| 613 | OpFoldResult math::ExpOp::fold(FoldAdaptor adaptor) { |
| 614 | return constFoldUnaryOpConditional<FloatAttr>( |
| 615 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 616 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 617 | case APFloat::Semantics::S_IEEEdouble: |
| 618 | return APFloat(exp(x: a.convertToDouble())); |
| 619 | case APFloat::Semantics::S_IEEEsingle: |
| 620 | return APFloat(expf(x: a.convertToFloat())); |
| 621 | default: |
| 622 | return {}; |
| 623 | } |
| 624 | }); |
| 625 | } |
| 626 | |
| 627 | //===----------------------------------------------------------------------===// |
| 628 | // Exp2Op folder |
| 629 | //===----------------------------------------------------------------------===// |
| 630 | |
| 631 | OpFoldResult math::Exp2Op::fold(FoldAdaptor adaptor) { |
| 632 | return constFoldUnaryOpConditional<FloatAttr>( |
| 633 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 634 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 635 | case APFloat::Semantics::S_IEEEdouble: |
| 636 | return APFloat(exp2(x: a.convertToDouble())); |
| 637 | case APFloat::Semantics::S_IEEEsingle: |
| 638 | return APFloat(exp2f(x: a.convertToFloat())); |
| 639 | default: |
| 640 | return {}; |
| 641 | } |
| 642 | }); |
| 643 | } |
| 644 | |
| 645 | //===----------------------------------------------------------------------===// |
| 646 | // ExpM1Op folder |
| 647 | //===----------------------------------------------------------------------===// |
| 648 | |
| 649 | OpFoldResult math::ExpM1Op::fold(FoldAdaptor adaptor) { |
| 650 | return constFoldUnaryOpConditional<FloatAttr>( |
| 651 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 652 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 653 | case APFloat::Semantics::S_IEEEdouble: |
| 654 | return APFloat(expm1(x: a.convertToDouble())); |
| 655 | case APFloat::Semantics::S_IEEEsingle: |
| 656 | return APFloat(expm1f(x: a.convertToFloat())); |
| 657 | default: |
| 658 | return {}; |
| 659 | } |
| 660 | }); |
| 661 | } |
| 662 | |
| 663 | //===----------------------------------------------------------------------===// |
| 664 | // IsFiniteOp folder |
| 665 | //===----------------------------------------------------------------------===// |
| 666 | |
| 667 | OpFoldResult math::IsFiniteOp::fold(FoldAdaptor adaptor) { |
| 668 | if (auto val = dyn_cast_or_null<FloatAttr>(Val: adaptor.getOperand())) { |
| 669 | return BoolAttr::get(context: val.getContext(), value: val.getValue().isFinite()); |
| 670 | } |
| 671 | if (auto splat = dyn_cast_or_null<SplatElementsAttr>(Val: adaptor.getOperand())) { |
| 672 | return DenseElementsAttr::get( |
| 673 | type: cast<ShapedType>(Val: getType()), |
| 674 | values: APInt(1, splat.getSplatValue<APFloat>().isFinite())); |
| 675 | } |
| 676 | return {}; |
| 677 | } |
| 678 | |
| 679 | //===----------------------------------------------------------------------===// |
| 680 | // IsInfOp folder |
| 681 | //===----------------------------------------------------------------------===// |
| 682 | |
| 683 | OpFoldResult math::IsInfOp::fold(FoldAdaptor adaptor) { |
| 684 | if (auto val = dyn_cast_or_null<FloatAttr>(Val: adaptor.getOperand())) { |
| 685 | return BoolAttr::get(context: val.getContext(), value: val.getValue().isInfinity()); |
| 686 | } |
| 687 | if (auto splat = dyn_cast_or_null<SplatElementsAttr>(Val: adaptor.getOperand())) { |
| 688 | return DenseElementsAttr::get( |
| 689 | type: cast<ShapedType>(Val: getType()), |
| 690 | values: APInt(1, splat.getSplatValue<APFloat>().isInfinity())); |
| 691 | } |
| 692 | return {}; |
| 693 | } |
| 694 | |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | // IsNaNOp folder |
| 697 | //===----------------------------------------------------------------------===// |
| 698 | |
| 699 | OpFoldResult math::IsNaNOp::fold(FoldAdaptor adaptor) { |
| 700 | if (auto val = dyn_cast_or_null<FloatAttr>(Val: adaptor.getOperand())) { |
| 701 | return BoolAttr::get(context: val.getContext(), value: val.getValue().isNaN()); |
| 702 | } |
| 703 | if (auto splat = dyn_cast_or_null<SplatElementsAttr>(Val: adaptor.getOperand())) { |
| 704 | return DenseElementsAttr::get( |
| 705 | type: cast<ShapedType>(Val: getType()), |
| 706 | values: APInt(1, splat.getSplatValue<APFloat>().isNaN())); |
| 707 | } |
| 708 | return {}; |
| 709 | } |
| 710 | |
| 711 | //===----------------------------------------------------------------------===// |
| 712 | // IsNormalOp folder |
| 713 | //===----------------------------------------------------------------------===// |
| 714 | |
| 715 | OpFoldResult math::IsNormalOp::fold(FoldAdaptor adaptor) { |
| 716 | if (auto val = dyn_cast_or_null<FloatAttr>(Val: adaptor.getOperand())) { |
| 717 | return BoolAttr::get(context: val.getContext(), value: val.getValue().isNormal()); |
| 718 | } |
| 719 | if (auto splat = dyn_cast_or_null<SplatElementsAttr>(Val: adaptor.getOperand())) { |
| 720 | return DenseElementsAttr::get( |
| 721 | type: cast<ShapedType>(Val: getType()), |
| 722 | values: APInt(1, splat.getSplatValue<APFloat>().isNormal())); |
| 723 | } |
| 724 | return {}; |
| 725 | } |
| 726 | |
| 727 | //===----------------------------------------------------------------------===// |
| 728 | // TanOp folder |
| 729 | //===----------------------------------------------------------------------===// |
| 730 | |
| 731 | OpFoldResult math::TanOp::fold(FoldAdaptor adaptor) { |
| 732 | return constFoldUnaryOpConditional<FloatAttr>( |
| 733 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 734 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 735 | case APFloat::Semantics::S_IEEEdouble: |
| 736 | return APFloat(tan(x: a.convertToDouble())); |
| 737 | case APFloat::Semantics::S_IEEEsingle: |
| 738 | return APFloat(tanf(x: a.convertToFloat())); |
| 739 | default: |
| 740 | return {}; |
| 741 | } |
| 742 | }); |
| 743 | } |
| 744 | |
| 745 | //===----------------------------------------------------------------------===// |
| 746 | // TanhOp folder |
| 747 | //===----------------------------------------------------------------------===// |
| 748 | |
| 749 | OpFoldResult math::TanhOp::fold(FoldAdaptor adaptor) { |
| 750 | return constFoldUnaryOpConditional<FloatAttr>( |
| 751 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 752 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 753 | case APFloat::Semantics::S_IEEEdouble: |
| 754 | return APFloat(tanh(x: a.convertToDouble())); |
| 755 | case APFloat::Semantics::S_IEEEsingle: |
| 756 | return APFloat(tanhf(x: a.convertToFloat())); |
| 757 | default: |
| 758 | return {}; |
| 759 | } |
| 760 | }); |
| 761 | } |
| 762 | |
| 763 | //===----------------------------------------------------------------------===// |
| 764 | // RoundEvenOp folder |
| 765 | //===----------------------------------------------------------------------===// |
| 766 | |
| 767 | OpFoldResult math::RoundEvenOp::fold(FoldAdaptor adaptor) { |
| 768 | return constFoldUnaryOp<FloatAttr>( |
| 769 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) { |
| 770 | APFloat result(a); |
| 771 | result.roundToIntegral(RM: llvm::RoundingMode::NearestTiesToEven); |
| 772 | return result; |
| 773 | }); |
| 774 | } |
| 775 | |
| 776 | //===----------------------------------------------------------------------===// |
| 777 | // FloorOp folder |
| 778 | //===----------------------------------------------------------------------===// |
| 779 | |
| 780 | OpFoldResult math::FloorOp::fold(FoldAdaptor adaptor) { |
| 781 | return constFoldUnaryOp<FloatAttr>( |
| 782 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) { |
| 783 | APFloat result(a); |
| 784 | result.roundToIntegral(RM: llvm::RoundingMode::TowardNegative); |
| 785 | return result; |
| 786 | }); |
| 787 | } |
| 788 | |
| 789 | //===----------------------------------------------------------------------===// |
| 790 | // RoundOp folder |
| 791 | //===----------------------------------------------------------------------===// |
| 792 | |
| 793 | OpFoldResult math::RoundOp::fold(FoldAdaptor adaptor) { |
| 794 | return constFoldUnaryOpConditional<FloatAttr>( |
| 795 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 796 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 797 | case APFloat::Semantics::S_IEEEdouble: |
| 798 | return APFloat(round(x: a.convertToDouble())); |
| 799 | case APFloat::Semantics::S_IEEEsingle: |
| 800 | return APFloat(roundf(x: a.convertToFloat())); |
| 801 | default: |
| 802 | return {}; |
| 803 | } |
| 804 | }); |
| 805 | } |
| 806 | |
| 807 | //===----------------------------------------------------------------------===// |
| 808 | // TruncOp folder |
| 809 | //===----------------------------------------------------------------------===// |
| 810 | |
| 811 | OpFoldResult math::TruncOp::fold(FoldAdaptor adaptor) { |
| 812 | return constFoldUnaryOpConditional<FloatAttr>( |
| 813 | operands: adaptor.getOperands(), calculate: [](const APFloat &a) -> std::optional<APFloat> { |
| 814 | switch (APFloat::SemanticsToEnum(Sem: a.getSemantics())) { |
| 815 | case APFloat::Semantics::S_IEEEdouble: |
| 816 | return APFloat(trunc(x: a.convertToDouble())); |
| 817 | case APFloat::Semantics::S_IEEEsingle: |
| 818 | return APFloat(truncf(x: a.convertToFloat())); |
| 819 | default: |
| 820 | return {}; |
| 821 | } |
| 822 | }); |
| 823 | } |
| 824 | |
| 825 | //===----------------------------------------------------------------------===// |
| 826 | // FPowIOp folder |
| 827 | //===----------------------------------------------------------------------===// |
| 828 | |
| 829 | OpFoldResult math::FPowIOp::fold(FoldAdaptor adaptor) { |
| 830 | return constFoldBinaryOpConditional<FloatAttr, IntegerAttr>( |
| 831 | operands: adaptor.getOperands(), |
| 832 | calculate: [](const APFloat &base, const APInt &exp) -> std::optional<APFloat> { |
| 833 | switch (APFloat::SemanticsToEnum(Sem: base.getSemantics())) { |
| 834 | case APFloat::Semantics::S_IEEEdouble: |
| 835 | case APFloat::Semantics::S_IEEEsingle: |
| 836 | break; |
| 837 | default: |
| 838 | return {}; |
| 839 | } |
| 840 | |
| 841 | // Square-and-multiply using the base's own semantics, matching the |
| 842 | // multiply sequence ExpandPowI builds in SelectionDAGBuilder.cpp. |
| 843 | const llvm::fltSemantics &sem = base.getSemantics(); |
| 844 | APInt magnitude = exp.abs(); |
| 845 | APFloat res = APFloat::getOne(Sem: sem); |
| 846 | APFloat curSquare = base; |
| 847 | while (!magnitude.isZero()) { |
| 848 | if (magnitude[0]) |
| 849 | res = res * curSquare; |
| 850 | curSquare = curSquare * curSquare; |
| 851 | magnitude.lshrInPlace(ShiftAmt: 1); |
| 852 | } |
| 853 | if (exp.isNegative()) |
| 854 | res = APFloat::getOne(Sem: sem) / res; |
| 855 | return res; |
| 856 | }); |
| 857 | } |
| 858 | |
| 859 | /// Materialize an integer or floating point constant. |
| 860 | Operation *math::MathDialect::materializeConstant(OpBuilder &builder, |
| 861 | Attribute value, Type type, |
| 862 | Location loc) { |
| 863 | if (auto poison = dyn_cast<ub::PoisonAttr>(Val&: value)) |
| 864 | return ub::PoisonOp::create(builder, location: loc, result: type, value: poison); |
| 865 | |
| 866 | return arith::ConstantOp::materialize(builder, value, type, loc); |
| 867 | } |
| 868 | |