1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
//===- ExpandTanh.cpp - Code to perform expanding tanh op -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements expansion of tanh op.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/Math/Transforms/Passes.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Vector/IR/VectorOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/ImplicitLocOpBuilder.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Transforms/DialectConversion.h"
using namespace mlir;
/// Create a float constant.
static Value createFloatConst(Location loc, Type type, double value,
OpBuilder &b) {
auto attr = b.getFloatAttr(getElementTypeOrSelf(type), value);
if (auto shapedTy = dyn_cast<ShapedType>(type)) {
return b.create<arith::ConstantOp>(loc,
DenseElementsAttr::get(shapedTy, attr));
}
return b.create<arith::ConstantOp>(loc, attr);
}
/// Create a float constant.
static Value createIntConst(Location loc, Type type, int64_t value,
OpBuilder &b) {
auto attr = b.getIntegerAttr(getElementTypeOrSelf(type), value);
if (auto shapedTy = dyn_cast<ShapedType>(type)) {
return b.create<arith::ConstantOp>(loc,
DenseElementsAttr::get(shapedTy, attr));
}
return b.create<arith::ConstantOp>(loc, attr);
}
static Value createTruncatedFPValue(Value operand, ImplicitLocOpBuilder &b) {
Type opType = operand.getType();
Type i64Ty = b.getI64Type();
if (auto shapedTy = dyn_cast<ShapedType>(opType))
i64Ty = shapedTy.clone(i64Ty);
Value fixedConvert = b.create<arith::FPToSIOp>(i64Ty, operand);
Value fpFixedConvert = b.create<arith::SIToFPOp>(opType, fixedConvert);
// The truncation does not preserve the sign when the truncated
// value is -0. So here the sign is copied again.
return b.create<math::CopySignOp>(fpFixedConvert, operand);
}
/// Expands tanh op into
/// 1) 1-exp^{-2x} / 1+exp^{-2x}, if x => 0
/// 2) exp^{2x}-1 / exp^{2x}+1 , if x < 0
static LogicalResult convertTanhOp(math::TanhOp op, PatternRewriter &rewriter) {
auto floatType = op.getOperand().getType();
Location loc = op.getLoc();
Value one = createFloatConst(loc, floatType, 1.0, rewriter);
Value two = createFloatConst(loc, floatType, 2.0, rewriter);
Value doubledX = rewriter.create<arith::MulFOp>(loc, op.getOperand(), two);
// Case 1: tanh(x) = 1-exp^{-2x} / 1+exp^{-2x}
Value negDoubledX = rewriter.create<arith::NegFOp>(loc, doubledX);
Value exp2x = rewriter.create<math::ExpOp>(loc, negDoubledX);
Value dividend = rewriter.create<arith::SubFOp>(loc, one, exp2x);
Value divisor = rewriter.create<arith::AddFOp>(loc, one, exp2x);
Value positiveRes = rewriter.create<arith::DivFOp>(loc, dividend, divisor);
// Case 2: tanh(x) = exp^{2x}-1 / exp^{2x}+1
exp2x = rewriter.create<math::ExpOp>(loc, doubledX);
dividend = rewriter.create<arith::SubFOp>(loc, exp2x, one);
divisor = rewriter.create<arith::AddFOp>(loc, exp2x, one);
Value negativeRes = rewriter.create<arith::DivFOp>(loc, dividend, divisor);
// tanh(x) = x >= 0 ? positiveRes : negativeRes
Value zero = createFloatConst(loc, floatType, 0.0, rewriter);
Value cmpRes = rewriter.create<arith::CmpFOp>(loc, arith::CmpFPredicate::OGE,
op.getOperand(), zero);
rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cmpRes, positiveRes,
negativeRes);
return success();
}
// Converts math.tan to math.sin, math.cos, and arith.divf.
static LogicalResult convertTanOp(math::TanOp op, PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operand = op.getOperand();
Type type = operand.getType();
Value sin = b.create<math::SinOp>(type, operand);
Value cos = b.create<math::CosOp>(type, operand);
Value div = b.create<arith::DivFOp>(type, sin, cos);
rewriter.replaceOp(op, div);
return success();
}
static LogicalResult convertFmaFOp(math::FmaOp op, PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operandA = op.getOperand(0);
Value operandB = op.getOperand(1);
Value operandC = op.getOperand(2);
Type type = op.getType();
Value mult = b.create<arith::MulFOp>(type, operandA, operandB);
Value add = b.create<arith::AddFOp>(type, mult, operandC);
rewriter.replaceOp(op, add);
return success();
}
// Converts a floorf() function to the following:
// floorf(float x) ->
// y = (float)(int) x
// if (x < 0) then incr = -1 else incr = 0
// y = y + incr <= replace this op with the floorf op.
static LogicalResult convertFloorOp(math::FloorOp op,
PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operand = op.getOperand();
Type opType = operand.getType();
Value fpFixedConvert = createTruncatedFPValue(operand, b);
// Creating constants for later use.
Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
Value negOne = createFloatConst(op->getLoc(), opType, -1.00, rewriter);
Value negCheck =
b.create<arith::CmpFOp>(arith::CmpFPredicate::OLT, operand, zero);
Value incrValue =
b.create<arith::SelectOp>(op->getLoc(), negCheck, negOne, zero);
Value ret = b.create<arith::AddFOp>(opType, fpFixedConvert, incrValue);
rewriter.replaceOp(op, ret);
return success();
}
// Converts a ceilf() function to the following:
// ceilf(float x) ->
// y = (float)(int) x
// if (x > y) then incr = 1 else incr = 0
// y = y + incr <= replace this op with the ceilf op.
static LogicalResult convertCeilOp(math::CeilOp op, PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operand = op.getOperand();
Type opType = operand.getType();
Value fpFixedConvert = createTruncatedFPValue(operand, b);
// Creating constants for later use.
Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
Value one = createFloatConst(op->getLoc(), opType, 1.00, rewriter);
Value gtCheck = b.create<arith::CmpFOp>(arith::CmpFPredicate::OGT, operand,
fpFixedConvert);
Value incrValue = b.create<arith::SelectOp>(op->getLoc(), gtCheck, one, zero);
Value ret = b.create<arith::AddFOp>(opType, fpFixedConvert, incrValue);
rewriter.replaceOp(op, ret);
return success();
}
// Converts Powf(float a, float b) (meaning a^b) to exp^(b * ln(a))
static LogicalResult convertPowfOp(math::PowFOp op, PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operandA = op.getOperand(0);
Value operandB = op.getOperand(1);
Type opType = operandA.getType();
Value zero = createFloatConst(op->getLoc(), opType, 0.00, rewriter);
Value two = createFloatConst(op->getLoc(), opType, 2.00, rewriter);
Value negOne = createFloatConst(op->getLoc(), opType, -1.00, rewriter);
Value opASquared = b.create<arith::MulFOp>(opType, operandA, operandA);
Value opBHalf = b.create<arith::DivFOp>(opType, operandB, two);
Value logA = b.create<math::LogOp>(opType, opASquared);
Value mult = b.create<arith::MulFOp>(opType, opBHalf, logA);
Value expResult = b.create<math::ExpOp>(opType, mult);
Value negExpResult = b.create<arith::MulFOp>(opType, expResult, negOne);
Value remainder = b.create<arith::RemFOp>(opType, operandB, two);
Value negCheck =
b.create<arith::CmpFOp>(arith::CmpFPredicate::OLT, operandA, zero);
Value oddPower =
b.create<arith::CmpFOp>(arith::CmpFPredicate::ONE, remainder, zero);
Value oddAndNeg = b.create<arith::AndIOp>(op->getLoc(), oddPower, negCheck);
Value res = b.create<arith::SelectOp>(op->getLoc(), oddAndNeg, negExpResult,
expResult);
rewriter.replaceOp(op, res);
return success();
}
// exp2f(float x) -> exp(x * ln(2))
// Proof: Let's say 2^x = y
// ln(2^x) = ln(y)
// x * ln(2) = ln(y) => e ^(x*ln(2)) = y
static LogicalResult convertExp2fOp(math::Exp2Op op,
PatternRewriter &rewriter) {
ImplicitLocOpBuilder b(op->getLoc(), rewriter);
Value operand = op.getOperand();
Type opType = operand.getType();
Value ln2 = createFloatConst(op->getLoc(), opType, llvm::numbers::ln2, b);
Value mult = b.create<arith::MulFOp>(opType, operand, ln2);
Value exp = b.create<math::ExpOp>(op->getLoc(), mult);
rewriter.replaceOp(op, exp);
return success();
}
static LogicalResult convertRoundOp(math::RoundOp op,
PatternRewriter &rewriter) {
Location loc = op.getLoc();
ImplicitLocOpBuilder b(loc, rewriter);
Value operand = op.getOperand();
Type opType = operand.getType();
Type opEType = getElementTypeOrSelf(opType);
if (!opEType.isF32()) {
return rewriter.notifyMatchFailure(op, "not a round of f32.");
}
Type i32Ty = b.getI32Type();
if (auto shapedTy = dyn_cast<ShapedType>(opType))
i32Ty = shapedTy.clone(i32Ty);
Value half = createFloatConst(loc, opType, 0.5, b);
Value c23 = createIntConst(loc, i32Ty, 23, b);
Value c127 = createIntConst(loc, i32Ty, 127, b);
Value expMask = createIntConst(loc, i32Ty, (1 << 8) - 1, b);
Value incrValue = b.create<math::CopySignOp>(half, operand);
Value add = b.create<arith::AddFOp>(opType, operand, incrValue);
Value fpFixedConvert = createTruncatedFPValue(add, b);
// There are three cases where adding 0.5 to the value and truncating by
// converting to an i64 does not result in the correct behavior:
//
// 1. Special values: +-inf and +-nan
// Casting these special values to i64 has undefined behavior. To identify
// these values, we use the fact that these values are the only float
// values with the maximum possible biased exponent.
//
// 2. Large values: 2^23 <= |x| <= INT_64_MAX
// Adding 0.5 to a float larger than or equal to 2^23 results in precision
// errors that sometimes round the value up and sometimes round the value
// down. For example:
// 8388608.0 + 0.5 = 8388608.0
// 8388609.0 + 0.5 = 8388610.0
//
// 3. Very large values: |x| > INT_64_MAX
// Casting to i64 a value greater than the max i64 value will overflow the
// i64 leading to wrong outputs.
//
// All three cases satisfy the property `biasedExp >= 23`.
Value operandBitcast = b.create<arith::BitcastOp>(i32Ty, operand);
Value operandExp = b.create<arith::AndIOp>(
b.create<arith::ShRUIOp>(operandBitcast, c23), expMask);
Value operandBiasedExp = b.create<arith::SubIOp>(operandExp, c127);
Value isSpecialValOrLargeVal =
b.create<arith::CmpIOp>(arith::CmpIPredicate::sge, operandBiasedExp, c23);
Value result = b.create<arith::SelectOp>(isSpecialValOrLargeVal, operand,
fpFixedConvert);
rewriter.replaceOp(op, result);
return success();
}
// Converts math.ctlz to scf and arith operations. This is done
// by performing a binary search on the bits.
static LogicalResult convertCtlzOp(math::CountLeadingZerosOp op,
PatternRewriter &rewriter) {
auto operand = op.getOperand();
auto operandTy = operand.getType();
auto eTy = getElementTypeOrSelf(operandTy);
Location loc = op.getLoc();
int32_t bitwidth = eTy.getIntOrFloatBitWidth();
if (bitwidth > 64)
return failure();
uint64_t allbits = -1;
if (bitwidth < 64) {
allbits = allbits >> (64 - bitwidth);
}
Value x = operand;
Value count = createIntConst(loc, operandTy, 0, rewriter);
for (int32_t bw = bitwidth; bw > 1; bw = bw / 2) {
auto half = bw / 2;
auto bits = createIntConst(loc, operandTy, half, rewriter);
auto mask = createIntConst(loc, operandTy, allbits >> half, rewriter);
Value pred =
rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::ule, x, mask);
Value add = rewriter.create<arith::AddIOp>(loc, count, bits);
Value shift = rewriter.create<arith::ShLIOp>(loc, x, bits);
x = rewriter.create<arith::SelectOp>(loc, pred, shift, x);
count = rewriter.create<arith::SelectOp>(loc, pred, add, count);
}
Value zero = createIntConst(loc, operandTy, 0, rewriter);
Value pred = rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
operand, zero);
Value bwval = createIntConst(loc, operandTy, bitwidth, rewriter);
Value sel = rewriter.create<arith::SelectOp>(loc, pred, bwval, count);
rewriter.replaceOp(op, sel);
return success();
}
// Convert `math.roundeven` into `math.round` + arith ops
static LogicalResult convertRoundEvenOp(math::RoundEvenOp op,
PatternRewriter &rewriter) {
Location loc = op.getLoc();
ImplicitLocOpBuilder b(loc, rewriter);
auto operand = op.getOperand();
Type operandTy = operand.getType();
Type resultTy = op.getType();
Type operandETy = getElementTypeOrSelf(operandTy);
Type resultETy = getElementTypeOrSelf(resultTy);
if (!operandETy.isF32() || !resultETy.isF32()) {
return rewriter.notifyMatchFailure(op, "not a roundeven of f32.");
}
Type i32Ty = b.getI32Type();
Type f32Ty = b.getF32Type();
if (auto shapedTy = dyn_cast<ShapedType>(operandTy)) {
i32Ty = shapedTy.clone(i32Ty);
f32Ty = shapedTy.clone(f32Ty);
}
Value c1Float = createFloatConst(loc, f32Ty, 1.0, b);
Value c0 = createIntConst(loc, i32Ty, 0, b);
Value c1 = createIntConst(loc, i32Ty, 1, b);
Value cNeg1 = createIntConst(loc, i32Ty, -1, b);
Value c23 = createIntConst(loc, i32Ty, 23, b);
Value c31 = createIntConst(loc, i32Ty, 31, b);
Value c127 = createIntConst(loc, i32Ty, 127, b);
Value c2To22 = createIntConst(loc, i32Ty, 1 << 22, b);
Value c23Mask = createIntConst(loc, i32Ty, (1 << 23) - 1, b);
Value expMask = createIntConst(loc, i32Ty, (1 << 8) - 1, b);
Value operandBitcast = b.create<arith::BitcastOp>(i32Ty, operand);
Value round = b.create<math::RoundOp>(operand);
Value roundBitcast = b.create<arith::BitcastOp>(i32Ty, round);
// Get biased exponents for operand and round(operand)
Value operandExp = b.create<arith::AndIOp>(
b.create<arith::ShRUIOp>(operandBitcast, c23), expMask);
Value operandBiasedExp = b.create<arith::SubIOp>(operandExp, c127);
Value roundExp = b.create<arith::AndIOp>(
b.create<arith::ShRUIOp>(roundBitcast, c23), expMask);
Value roundBiasedExp = b.create<arith::SubIOp>(roundExp, c127);
auto safeShiftRight = [&](Value x, Value shift) -> Value {
// Clamp shift to valid range [0, 31] to avoid undefined behavior
Value clampedShift = b.create<arith::MaxSIOp>(shift, c0);
clampedShift = b.create<arith::MinSIOp>(clampedShift, c31);
return b.create<arith::ShRUIOp>(x, clampedShift);
};
auto maskMantissa = [&](Value mantissa,
Value mantissaMaskRightShift) -> Value {
Value shiftedMantissaMask = safeShiftRight(c23Mask, mantissaMaskRightShift);
return b.create<arith::AndIOp>(mantissa, shiftedMantissaMask);
};
// A whole number `x`, such that `|x| != 1`, is even if the mantissa, ignoring
// the leftmost `clamp(biasedExp - 1, 0, 23)` bits, is zero. Large numbers
// with `biasedExp > 23` (numbers where there is not enough precision to store
// decimals) are always even, and they satisfy the even condition trivially
// since the mantissa without all its bits is zero. The even condition
// is also true for +-0, since they have `biasedExp = -127` and the entire
// mantissa is zero. The case of +-1 has to be handled separately. Here
// we identify these values by noting that +-1 are the only whole numbers with
// `biasedExp == 0`.
//
// The special values +-inf and +-nan also satisfy the same property that
// whole non-unit even numbers satisfy. In particular, the special values have
// `biasedExp > 23`, so they get treated as large numbers with no room for
// decimals, which are always even.
Value roundBiasedExpEq0 =
b.create<arith::CmpIOp>(arith::CmpIPredicate::eq, roundBiasedExp, c0);
Value roundBiasedExpMinus1 = b.create<arith::SubIOp>(roundBiasedExp, c1);
Value roundMaskedMantissa = maskMantissa(roundBitcast, roundBiasedExpMinus1);
Value roundIsNotEvenOrSpecialVal = b.create<arith::CmpIOp>(
arith::CmpIPredicate::ne, roundMaskedMantissa, c0);
roundIsNotEvenOrSpecialVal =
b.create<arith::OrIOp>(roundIsNotEvenOrSpecialVal, roundBiasedExpEq0);
// A value `x` with `0 <= biasedExp < 23`, is halfway between two consecutive
// integers if the bit at index `biasedExp` starting from the left in the
// mantissa is 1 and all the bits to the right are zero. Values with
// `biasedExp >= 23` don't have decimals, so they are never halfway. The
// values +-0.5 are the only halfway values that have `biasedExp == -1 < 0`,
// so these are handled separately. In particular, if `biasedExp == -1`, the
// value is halfway if the entire mantissa is zero.
Value operandBiasedExpEqNeg1 = b.create<arith::CmpIOp>(
arith::CmpIPredicate::eq, operandBiasedExp, cNeg1);
Value expectedOperandMaskedMantissa = b.create<arith::SelectOp>(
operandBiasedExpEqNeg1, c0, safeShiftRight(c2To22, operandBiasedExp));
Value operandMaskedMantissa = maskMantissa(operandBitcast, operandBiasedExp);
Value operandIsHalfway =
b.create<arith::CmpIOp>(arith::CmpIPredicate::eq, operandMaskedMantissa,
expectedOperandMaskedMantissa);
// Ensure `biasedExp` is in the valid range for half values.
Value operandBiasedExpGeNeg1 = b.create<arith::CmpIOp>(
arith::CmpIPredicate::sge, operandBiasedExp, cNeg1);
Value operandBiasedExpLt23 =
b.create<arith::CmpIOp>(arith::CmpIPredicate::slt, operandBiasedExp, c23);
operandIsHalfway =
b.create<arith::AndIOp>(operandIsHalfway, operandBiasedExpLt23);
operandIsHalfway =
b.create<arith::AndIOp>(operandIsHalfway, operandBiasedExpGeNeg1);
// Adjust rounded operand with `round(operand) - sign(operand)` to correct the
// case where `round` rounded in the opposite direction of `roundeven`.
Value sign = b.create<math::CopySignOp>(c1Float, operand);
Value roundShifted = b.create<arith::SubFOp>(round, sign);
// If the rounded value is even or a special value, we default to the behavior
// of `math.round`.
Value needsShift =
b.create<arith::AndIOp>(roundIsNotEvenOrSpecialVal, operandIsHalfway);
Value result = b.create<arith::SelectOp>(needsShift, roundShifted, round);
// The `x - sign` adjustment does not preserve the sign when we are adjusting
// the value -1 to -0. So here the sign is copied again to ensure that -0.5 is
// rounded to -0.0.
result = b.create<math::CopySignOp>(result, operand);
rewriter.replaceOp(op, result);
return success();
}
void mlir::populateExpandCtlzPattern(RewritePatternSet &patterns) {
patterns.add(convertCtlzOp);
}
void mlir::populateExpandTanPattern(RewritePatternSet &patterns) {
patterns.add(convertTanOp);
}
void mlir::populateExpandTanhPattern(RewritePatternSet &patterns) {
patterns.add(convertTanhOp);
}
void mlir::populateExpandFmaFPattern(RewritePatternSet &patterns) {
patterns.add(convertFmaFOp);
}
void mlir::populateExpandCeilFPattern(RewritePatternSet &patterns) {
patterns.add(convertCeilOp);
}
void mlir::populateExpandExp2FPattern(RewritePatternSet &patterns) {
patterns.add(convertExp2fOp);
}
void mlir::populateExpandPowFPattern(RewritePatternSet &patterns) {
patterns.add(convertPowfOp);
}
void mlir::populateExpandRoundFPattern(RewritePatternSet &patterns) {
patterns.add(convertRoundOp);
}
void mlir::populateExpandFloorFPattern(RewritePatternSet &patterns) {
patterns.add(convertFloorOp);
}
void mlir::populateExpandRoundEvenPattern(RewritePatternSet &patterns) {
patterns.add(convertRoundEvenOp);
}
|