|
| 1 | +// Copyright 2022 The IREE Authors |
| 2 | +// |
| 3 | +// Licensed 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 | +#include "iree/compiler/Codegen/Common/Passes.h" |
| 8 | +#include "mlir/Dialect/Math/Transforms/Approximation.h" |
| 9 | +#include "mlir/Dialect/Math/Transforms/Passes.h" |
| 10 | +#include "mlir/IR/PatternMatch.h" |
| 11 | +#include "mlir/Pass/Pass.h" |
| 12 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 13 | + |
| 14 | +namespace mlir::iree_compiler { |
| 15 | + |
| 16 | +/// Deprecated! This flag had buggy/unintentional semantics. |
| 17 | +/// Its original comment said: |
| 18 | +/// ""use native hardware operations instead of polynomial approximation". |
| 19 | +static llvm::cl::opt<bool> clNativeMathPrecision( |
| 20 | + "iree-codegen-gpu-native-math-precision", |
| 21 | + llvm::cl::desc("Deprecated! This flag had buggy/unintentional semantics. " |
| 22 | + "Its original description said: \"Skip polynomial lowering " |
| 23 | + "for math op natively available on GPU.\""), |
| 24 | + llvm::cl::init(false)); |
| 25 | + |
| 26 | +#define GEN_PASS_DEF_MATHTRANSFORMPASS |
| 27 | +#include "iree/compiler/Codegen/Common/Passes.h.inc" |
| 28 | + |
| 29 | +static void populateMathFunctionsRewritePatterns( |
| 30 | + RewritePatternSet &patterns, |
| 31 | + const std::function<bool(StringRef)> &predicate) { |
| 32 | + if (predicate(math::TanOp::getOperationName())) { |
| 33 | + populateExpandTanPattern(patterns); |
| 34 | + } |
| 35 | + if (predicate(math::SinhOp::getOperationName())) { |
| 36 | + populateExpandSinhPattern(patterns); |
| 37 | + } |
| 38 | + if (predicate(math::CoshOp::getOperationName())) { |
| 39 | + populateExpandCoshPattern(patterns); |
| 40 | + } |
| 41 | + if (predicate(math::AsinhOp::getOperationName())) { |
| 42 | + populateExpandAsinhPattern(patterns); |
| 43 | + } |
| 44 | + if (predicate(math::AcoshOp::getOperationName())) { |
| 45 | + populateExpandAcoshPattern(patterns); |
| 46 | + } |
| 47 | + if (predicate(math::AtanhOp::getOperationName())) { |
| 48 | + populateExpandAtanhPattern(patterns); |
| 49 | + } |
| 50 | + if (predicate(math::PowFOp::getOperationName())) { |
| 51 | + populateExpandPowFPattern(patterns); |
| 52 | + } |
| 53 | + if (predicate(math::FPowIOp::getOperationName())) { |
| 54 | + populateExpandFPowIPattern(patterns); |
| 55 | + } |
| 56 | + if (predicate(math::Exp2Op::getOperationName())) { |
| 57 | + populateExpandExp2FPattern(patterns); |
| 58 | + } |
| 59 | + if (predicate(math::RoundEvenOp::getOperationName())) { |
| 60 | + populateExpandRoundEvenPattern(patterns); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +static bool predicateRewrite(StringRef name, |
| 65 | + IREE::HAL::ExecutableTargetAttr target) { |
| 66 | + (void)target; // Currently unused. |
| 67 | + if (clNativeMathPrecision) { // Legacy. |
| 68 | + if (name == math::Exp2Op::getOperationName() || |
| 69 | + name == math::RoundEvenOp::getOperationName()) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + // Currently enable all non-approximative rewrites. |
| 74 | + return true; |
| 75 | +} |
| 76 | + |
| 77 | +static bool predicateF32Cast(StringRef name, |
| 78 | + IREE::HAL::ExecutableTargetAttr target) { |
| 79 | + (void)target; // Currently unused. |
| 80 | + if (clNativeMathPrecision) { // Legacy. |
| 81 | + return false; |
| 82 | + } |
| 83 | + StringRef atan = math::AtanOp::getOperationName(); |
| 84 | + StringRef atan2 = math::Atan2Op::getOperationName(); |
| 85 | + StringRef cos = math::CosOp::getOperationName(); |
| 86 | + StringRef sin = math::SinOp::getOperationName(); |
| 87 | + StringRef tanh = math::TanhOp::getOperationName(); |
| 88 | + StringRef log = math::LogOp::getOperationName(); |
| 89 | + StringRef log2 = math::Log2Op::getOperationName(); |
| 90 | + StringRef log1p = math::Log1pOp::getOperationName(); |
| 91 | + StringRef exp = math::ExpOp::getOperationName(); |
| 92 | + StringRef expm1 = math::ExpM1Op::getOperationName(); |
| 93 | + StringRef cbrt = math::CbrtOp::getOperationName(); |
| 94 | + StringRef erf = math::ErfOp::getOperationName(); |
| 95 | + return llvm::is_contained( |
| 96 | + {atan, atan2, tanh, log, log2, log1p, erf, exp, expm1, cbrt, sin, cos}, |
| 97 | + name); |
| 98 | +} |
| 99 | + |
| 100 | +static bool predicateApprox(StringRef name, |
| 101 | + IREE::HAL::ExecutableTargetAttr target) { |
| 102 | + (void)target; // Currently unused. |
| 103 | + if (clNativeMathPrecision) { // Legacy. |
| 104 | + if (name == math::ErfOp::getOperationName()) { |
| 105 | + // The legacy implementation had a bug: it always applied polynomial |
| 106 | + // approximation of math.erf, even when clNativeMathPrecision was passed. |
| 107 | + // We actually have CI tests that rely on that bug: they pass |
| 108 | + // clNativeMathPrecision but fail unless math.erf is approximated. |
| 109 | + return true; |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + StringRef acos = math::AcosOp::getOperationName(); |
| 114 | + StringRef asin = math::AsinOp::getOperationName(); |
| 115 | + StringRef atan = math::AtanOp::getOperationName(); |
| 116 | + StringRef atan2 = math::Atan2Op::getOperationName(); |
| 117 | + StringRef cos = math::CosOp::getOperationName(); |
| 118 | + StringRef sin = math::SinOp::getOperationName(); |
| 119 | + StringRef tanh = math::TanhOp::getOperationName(); |
| 120 | + StringRef log = math::LogOp::getOperationName(); |
| 121 | + StringRef log2 = math::Log2Op::getOperationName(); |
| 122 | + StringRef log1p = math::Log1pOp::getOperationName(); |
| 123 | + StringRef exp = math::ExpOp::getOperationName(); |
| 124 | + StringRef expm1 = math::ExpM1Op::getOperationName(); |
| 125 | + StringRef cbrt = math::CbrtOp::getOperationName(); |
| 126 | + StringRef erf = math::ErfOp::getOperationName(); |
| 127 | + return llvm::is_contained({atan, atan2, tanh, log, log2, log1p, erf, asin, |
| 128 | + acos, exp, expm1, cbrt, sin, cos}, |
| 129 | + name); |
| 130 | +} |
| 131 | + |
| 132 | +namespace { |
| 133 | + |
| 134 | +class MathTransformPass final |
| 135 | + : public impl::MathTransformPassBase<MathTransformPass> { |
| 136 | +public: |
| 137 | + using Base::Base; |
| 138 | + |
| 139 | + void runOnOperation() override { |
| 140 | + RewritePatternSet patterns(&getContext()); |
| 141 | + auto target = IREE::HAL::ExecutableTargetAttr::lookup(getOperation()); |
| 142 | + if (!target) { |
| 143 | + return signalPassFailure(); |
| 144 | + } |
| 145 | + populateMathFunctionsRewritePatterns(patterns, [target](StringRef name) { |
| 146 | + return predicateRewrite(name, target); |
| 147 | + }); |
| 148 | + |
| 149 | + populateMathF32ExpansionPatterns(patterns, [target](StringRef name) { |
| 150 | + return predicateF32Cast(name, target); |
| 151 | + }); |
| 152 | + |
| 153 | + populateMathPolynomialApproximationPatterns( |
| 154 | + patterns, |
| 155 | + [target](StringRef name) { return predicateApprox(name, target); }); |
| 156 | + |
| 157 | + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) { |
| 158 | + return signalPassFailure(); |
| 159 | + } |
| 160 | + } |
| 161 | +}; |
| 162 | + |
| 163 | +} // namespace |
| 164 | +} // namespace mlir::iree_compiler |
0 commit comments