| 1 | //===- MathToSPIRV.cpp - Math to SPIR-V Patterns --------------------------===// |
| 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 | // This file implements patterns to convert Math dialect to SPIR-V dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "../SPIRVCommon/Pattern.h" |
| 14 | #include "mlir/Dialect/Math/IR/Math.h" |
| 15 | #include "mlir/Dialect/SPIRV/IR/SPIRVOps.h" |
| 16 | #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h" |
| 17 | #include "mlir/IR/BuiltinTypes.h" |
| 18 | #include "mlir/IR/Matchers.h" |
| 19 | #include "mlir/IR/TypeUtilities.h" |
| 20 | #include "mlir/Transforms/DialectConversion.h" |
| 21 | #include "llvm/ADT/STLExtras.h" |
| 22 | #include "llvm/ADT/TypeSwitch.h" |
| 23 | #include "llvm/Support/FormatVariadic.h" |
| 24 | |
| 25 | #define DEBUG_TYPE "math-to-spirv-pattern" |
| 26 | |
| 27 | using namespace mlir; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Utility functions |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | /// Creates a 32-bit scalar/vector integer constant. Returns nullptr if the |
| 34 | /// given type is not a 32-bit scalar/vector type. |
| 35 | static Value getScalarOrVectorI32Constant(Type type, int value, |
| 36 | OpBuilder &builder, Location loc) { |
| 37 | if (auto vectorType = dyn_cast<VectorType>(Val&: type)) { |
| 38 | if (!vectorType.getElementType().isInteger(width: 32)) |
| 39 | return nullptr; |
| 40 | SmallVector<int> values(vectorType.getNumElements(), value); |
| 41 | return spirv::ConstantOp::create(builder, location: loc, constant: type, |
| 42 | value: builder.getI32VectorAttr(values)); |
| 43 | } |
| 44 | if (type.isInteger(width: 32)) |
| 45 | return spirv::ConstantOp::create(builder, location: loc, constant: type, |
| 46 | value: builder.getI32IntegerAttr(value)); |
| 47 | |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | /// Check if the type is supported by math-to-spirv conversion. We expect to |
| 52 | /// only see scalars and vectors at this point, with higher-level types already |
| 53 | /// lowered. |
| 54 | static bool isSupportedSourceType(Type originalType) { |
| 55 | if (originalType.isIntOrIndexOrFloat()) |
| 56 | return true; |
| 57 | |
| 58 | if (auto vecTy = dyn_cast<VectorType>(Val&: originalType)) { |
| 59 | if (!vecTy.getElementType().isIntOrIndexOrFloat()) |
| 60 | return false; |
| 61 | if (vecTy.isScalable()) |
| 62 | return false; |
| 63 | if (vecTy.getRank() > 1) |
| 64 | return false; |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | /// Check if all `sourceOp` types are supported by math-to-spirv conversion. |
| 73 | /// Notify of a match failure othwerise and return a `failure` result. |
| 74 | /// This is intended to simplify type checks in `OpConversionPattern`s. |
| 75 | static LogicalResult checkSourceOpTypes(ConversionPatternRewriter &rewriter, |
| 76 | Operation *sourceOp) { |
| 77 | auto allTypes = llvm::to_vector(Range: sourceOp->getOperandTypes()); |
| 78 | llvm::append_range(C&: allTypes, R: sourceOp->getResultTypes()); |
| 79 | |
| 80 | for (Type ty : allTypes) { |
| 81 | if (!isSupportedSourceType(originalType: ty)) { |
| 82 | return rewriter.notifyMatchFailure( |
| 83 | arg&: sourceOp, |
| 84 | msg: llvm::formatv( |
| 85 | Fmt: "unsupported source type for Math to SPIR-V conversion: {0}" , |
| 86 | Vals&: ty)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return success(); |
| 91 | } |
| 92 | |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | // Operation conversion |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | |
| 97 | // Note that DRR cannot be used for the patterns in this file: we may need to |
| 98 | // convert type along the way, which requires ConversionPattern. DRR generates |
| 99 | // normal RewritePattern. |
| 100 | |
| 101 | namespace { |
| 102 | /// Converts elementwise unary, binary, and ternary standard operations to |
| 103 | /// SPIR-V operations. Checks that source `Op` types are supported. |
| 104 | template <typename Op, typename SPIRVOp> |
| 105 | struct CheckedElementwiseOpPattern final |
| 106 | : public spirv::ElementwiseOpPattern<Op, SPIRVOp> { |
| 107 | using BasePattern = typename spirv::ElementwiseOpPattern<Op, SPIRVOp>; |
| 108 | using BasePattern::BasePattern; |
| 109 | |
| 110 | LogicalResult |
| 111 | matchAndRewrite(Op op, typename Op::Adaptor adaptor, |
| 112 | ConversionPatternRewriter &rewriter) const override { |
| 113 | if (LogicalResult res = checkSourceOpTypes(rewriter, op); failed(Result: res)) |
| 114 | return res; |
| 115 | |
| 116 | return BasePattern::matchAndRewrite(op, adaptor, rewriter); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | /// Converts math.copysign to SPIR-V ops. |
| 121 | struct CopySignPattern final : public OpConversionPattern<math::CopySignOp> { |
| 122 | using Base::Base; |
| 123 | |
| 124 | LogicalResult |
| 125 | matchAndRewrite(math::CopySignOp copySignOp, OpAdaptor adaptor, |
| 126 | ConversionPatternRewriter &rewriter) const override { |
| 127 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: copySignOp); |
| 128 | failed(Result: res)) |
| 129 | return res; |
| 130 | |
| 131 | // Defer to the CL copysign op on Kernel targets. |
| 132 | auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>(); |
| 133 | if (typeConverter.getTargetEnv().allows(spirv::Capability::Kernel)) |
| 134 | return rewriter.notifyMatchFailure(arg&: copySignOp, |
| 135 | msg: "Kernel target has native CL op" ); |
| 136 | |
| 137 | Type type = getTypeConverter()->convertType(t: copySignOp.getType()); |
| 138 | if (!type) |
| 139 | return failure(); |
| 140 | |
| 141 | FloatType floatType; |
| 142 | if (auto scalarType = dyn_cast<FloatType>(Val: copySignOp.getType())) { |
| 143 | floatType = scalarType; |
| 144 | } else if (auto vectorType = dyn_cast<VectorType>(Val: copySignOp.getType())) { |
| 145 | floatType = cast<FloatType>(Val: vectorType.getElementType()); |
| 146 | } else { |
| 147 | return failure(); |
| 148 | } |
| 149 | |
| 150 | Location loc = copySignOp.getLoc(); |
| 151 | int bitwidth = floatType.getWidth(); |
| 152 | Type intType = rewriter.getIntegerType(width: bitwidth); |
| 153 | uint64_t intValue = uint64_t(1) << (bitwidth - 1); |
| 154 | |
| 155 | Value signMask = spirv::ConstantOp::create( |
| 156 | builder&: rewriter, location: loc, constant: intType, value: rewriter.getIntegerAttr(type: intType, value: intValue)); |
| 157 | Value valueMask = spirv::ConstantOp::create( |
| 158 | builder&: rewriter, location: loc, constant: intType, |
| 159 | value: rewriter.getIntegerAttr(type: intType, value: intValue - 1u)); |
| 160 | |
| 161 | if (auto vectorType = dyn_cast<VectorType>(Val&: type)) { |
| 162 | assert(vectorType.getRank() == 1); |
| 163 | int count = vectorType.getNumElements(); |
| 164 | intType = VectorType::get(shape: count, elementType: intType); |
| 165 | |
| 166 | Repeated<Value> signSplat(count, signMask); |
| 167 | signMask = spirv::CompositeConstructOp::create(builder&: rewriter, location: loc, result: intType, |
| 168 | constituents: signSplat); |
| 169 | |
| 170 | Repeated<Value> valueSplat(count, valueMask); |
| 171 | valueMask = spirv::CompositeConstructOp::create(builder&: rewriter, location: loc, result: intType, |
| 172 | constituents: valueSplat); |
| 173 | } |
| 174 | |
| 175 | Value lhsCast = |
| 176 | spirv::BitcastOp::create(builder&: rewriter, location: loc, result: intType, operand: adaptor.getLhs()); |
| 177 | Value rhsCast = |
| 178 | spirv::BitcastOp::create(builder&: rewriter, location: loc, result: intType, operand: adaptor.getRhs()); |
| 179 | |
| 180 | Value value = spirv::BitwiseAndOp::create(builder&: rewriter, location: loc, resultTypes: intType, |
| 181 | operands: ValueRange{lhsCast, valueMask}); |
| 182 | Value sign = spirv::BitwiseAndOp::create(builder&: rewriter, location: loc, resultTypes: intType, |
| 183 | operands: ValueRange{rhsCast, signMask}); |
| 184 | |
| 185 | Value result = spirv::BitwiseOrOp::create(builder&: rewriter, location: loc, resultTypes: intType, |
| 186 | operands: ValueRange{value, sign}); |
| 187 | rewriter.replaceOpWithNewOp<spirv::BitcastOp>(op: copySignOp, args&: type, args&: result); |
| 188 | return success(); |
| 189 | } |
| 190 | }; |
| 191 | |
| 192 | /// Converts math.ctlz to SPIR-V ops. |
| 193 | /// |
| 194 | /// OpenCL targets lower math.ctlz directly to OpenCL.std clz via the generic |
| 195 | /// elementwise pattern. This pattern handles the shader fallback. |
| 196 | /// |
| 197 | /// SPIR-V does not have a direct operations for counting leading zeros for |
| 198 | /// glsl. If Shader capability is supported, we can leverage GL FindUMsb to |
| 199 | /// calculate it. |
| 200 | struct CountLeadingZerosPattern final |
| 201 | : public OpConversionPattern<math::CountLeadingZerosOp> { |
| 202 | using Base::Base; |
| 203 | |
| 204 | LogicalResult |
| 205 | matchAndRewrite(math::CountLeadingZerosOp countOp, OpAdaptor adaptor, |
| 206 | ConversionPatternRewriter &rewriter) const override { |
| 207 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: countOp); failed(Result: res)) |
| 208 | return res; |
| 209 | |
| 210 | Type type = getTypeConverter()->convertType(t: countOp.getType()); |
| 211 | if (!type) |
| 212 | return failure(); |
| 213 | |
| 214 | auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>(); |
| 215 | if (!typeConverter.getTargetEnv().allows(spirv::Capability::Shader)) |
| 216 | return rewriter.notifyMatchFailure(arg&: countOp, msg: "requires Shader capability" ); |
| 217 | |
| 218 | // The GL FindUMsb fallback only supports 32-bit integer types for now. |
| 219 | unsigned bitwidth = 0; |
| 220 | if (isa<IntegerType>(Val: type)) |
| 221 | bitwidth = type.getIntOrFloatBitWidth(); |
| 222 | if (auto vectorType = dyn_cast<VectorType>(Val&: type)) |
| 223 | bitwidth = vectorType.getElementTypeBitWidth(); |
| 224 | if (bitwidth != 32) |
| 225 | return failure(); |
| 226 | |
| 227 | Location loc = countOp.getLoc(); |
| 228 | Value input = adaptor.getOperand(); |
| 229 | Value val1 = getScalarOrVectorI32Constant(type, value: 1, builder&: rewriter, loc); |
| 230 | Value val31 = getScalarOrVectorI32Constant(type, value: 31, builder&: rewriter, loc); |
| 231 | Value val32 = getScalarOrVectorI32Constant(type, value: 32, builder&: rewriter, loc); |
| 232 | |
| 233 | Value msb = spirv::GLFindUMsbOp::create(builder&: rewriter, location: loc, operand: input); |
| 234 | // We need to subtract from 31 given that the index returned by GLSL |
| 235 | // FindUMsb is counted from the least significant bit. Theoretically this |
| 236 | // also gives the correct result even if the integer has all zero bits, in |
| 237 | // which case GL FindUMsb would return -1. |
| 238 | Value subMsb = spirv::ISubOp::create(builder&: rewriter, location: loc, operand1: val31, operand2: msb); |
| 239 | // However, certain Vulkan implementations have driver bugs for the corner |
| 240 | // case where the input is zero. And.. it can be smart to optimize a select |
| 241 | // only involving the corner case. So separately compute the result when the |
| 242 | // input is either zero or one. |
| 243 | Value subInput = spirv::ISubOp::create(builder&: rewriter, location: loc, operand1: val32, operand2: input); |
| 244 | Value cmp = spirv::ULessThanEqualOp::create(builder&: rewriter, location: loc, operand1: input, operand2: val1); |
| 245 | rewriter.replaceOpWithNewOp<spirv::SelectOp>(op: countOp, args&: cmp, args&: subInput, |
| 246 | args&: subMsb); |
| 247 | return success(); |
| 248 | } |
| 249 | }; |
| 250 | |
| 251 | /// Converts math.cttz to GL FindILsb. GL FindILsb returns -1 for a zero |
| 252 | /// input while math.cttz must return the bitwidth, so the zero case is |
| 253 | /// patched up with a select. |
| 254 | /// |
| 255 | /// OpenCL targets use OpenCL.std ctz. This pattern handles the shader fallback. |
| 256 | struct CountTrailingZerosPattern final |
| 257 | : public OpConversionPattern<math::CountTrailingZerosOp> { |
| 258 | using Base::Base; |
| 259 | |
| 260 | LogicalResult |
| 261 | matchAndRewrite(math::CountTrailingZerosOp countOp, OpAdaptor adaptor, |
| 262 | ConversionPatternRewriter &rewriter) const override { |
| 263 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: countOp); failed(Result: res)) |
| 264 | return res; |
| 265 | |
| 266 | Type type = getTypeConverter()->convertType(t: countOp.getType()); |
| 267 | if (!type) |
| 268 | return failure(); |
| 269 | |
| 270 | auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>(); |
| 271 | if (!typeConverter.getTargetEnv().allows(spirv::Capability::Shader)) |
| 272 | return rewriter.notifyMatchFailure(arg&: countOp, msg: "requires Shader capability" ); |
| 273 | |
| 274 | unsigned bitwidth = 0; |
| 275 | if (isa<IntegerType>(Val: type)) |
| 276 | bitwidth = type.getIntOrFloatBitWidth(); |
| 277 | else if (auto vectorType = dyn_cast<VectorType>(Val&: type)) |
| 278 | bitwidth = vectorType.getElementTypeBitWidth(); |
| 279 | |
| 280 | Location loc = countOp.getLoc(); |
| 281 | Value input = adaptor.getOperand(); |
| 282 | Value val0 = spirv::ConstantOp::getZero(type, loc, builder&: rewriter); |
| 283 | Type elemType = getElementTypeOrSelf(type); |
| 284 | Attribute bwAttr = IntegerAttr::get(type: elemType, value: bitwidth); |
| 285 | if (auto vecType = dyn_cast<VectorType>(Val&: type)) |
| 286 | bwAttr = SplatElementsAttr::get(type: vecType, values: bwAttr); |
| 287 | Value valBitwidth = spirv::ConstantOp::create(builder&: rewriter, location: loc, constant: type, value: bwAttr); |
| 288 | |
| 289 | Value lsb = spirv::GLFindILsbOp::create(builder&: rewriter, location: loc, operand: input); |
| 290 | Value isZero = spirv::IEqualOp::create(builder&: rewriter, location: loc, operand1: input, operand2: val0); |
| 291 | rewriter.replaceOpWithNewOp<spirv::SelectOp>(op: countOp, args&: isZero, args&: valBitwidth, |
| 292 | args&: lsb); |
| 293 | return success(); |
| 294 | } |
| 295 | }; |
| 296 | |
| 297 | /// Converts math.cttz to OpenCL.std ctz, preserving the source bit width when |
| 298 | /// narrow integers are emulated using wider integers. |
| 299 | struct CountTrailingZerosOpenCLPattern final |
| 300 | : public OpConversionPattern<math::CountTrailingZerosOp> { |
| 301 | using Base::Base; |
| 302 | |
| 303 | LogicalResult |
| 304 | matchAndRewrite(math::CountTrailingZerosOp op, OpAdaptor adaptor, |
| 305 | ConversionPatternRewriter &rewriter) const override { |
| 306 | if (failed(Result: checkSourceOpTypes(rewriter, sourceOp: op))) |
| 307 | return failure(); |
| 308 | |
| 309 | auto &typeConverter = *getTypeConverter<SPIRVTypeConverter>(); |
| 310 | if (!typeConverter.getTargetEnv().allows(spirv::Capability::Kernel)) |
| 311 | return rewriter.notifyMatchFailure(arg&: op, msg: "requires Kernel capability" ); |
| 312 | Type type = typeConverter.convertType(t: op.getType()); |
| 313 | if (!type) |
| 314 | return failure(); |
| 315 | |
| 316 | Value input = adaptor.getOperand(); |
| 317 | Type elementType = getElementTypeOrSelf(type); |
| 318 | if (elementType.isInteger(width: 1)) { |
| 319 | // cttz(false) is true and cttz(true) is false. |
| 320 | rewriter.replaceOpWithNewOp<spirv::LogicalNotOp>(op, args&: type, args&: input); |
| 321 | return success(); |
| 322 | } |
| 323 | |
| 324 | Type sourceElementType = getElementTypeOrSelf(type: op.getType()); |
| 325 | unsigned targetWidth = elementType.getIntOrFloatBitWidth(); |
| 326 | // Index values use the bit width chosen by the type converter. |
| 327 | unsigned sourceWidth = sourceElementType.isIndex() |
| 328 | ? targetWidth |
| 329 | : sourceElementType.getIntOrFloatBitWidth(); |
| 330 | if (sourceWidth < targetWidth) { |
| 331 | // Set the bit immediately above the original value. This bounds the |
| 332 | // count by sourceWidth, even when the emulated value has nonzero high |
| 333 | // bits, and gives the correct result for a zero source value. |
| 334 | Attribute sentinel = IntegerAttr::get( |
| 335 | type: elementType, value: APInt::getOneBitSet(numBits: targetWidth, BitNo: sourceWidth)); |
| 336 | if (auto vectorType = dyn_cast<VectorType>(Val&: type)) |
| 337 | sentinel = SplatElementsAttr::get(type: vectorType, values: sentinel); |
| 338 | Value mask = |
| 339 | spirv::ConstantOp::create(builder&: rewriter, location: op.getLoc(), constant: type, value: sentinel); |
| 340 | input = spirv::BitwiseOrOp::create(builder&: rewriter, location: op.getLoc(), operand1: input, operand2: mask); |
| 341 | } |
| 342 | rewriter.replaceOpWithNewOp<spirv::CLCtzOp>(op, args&: type, args&: input); |
| 343 | return success(); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | /// Converts math.expm1 to SPIR-V ops. |
| 348 | /// |
| 349 | /// SPIR-V does not have a direct operations for exp(x)-1. Explicitly lower to |
| 350 | /// these operations. |
| 351 | template <typename ExpOp> |
| 352 | struct ExpM1OpPattern final : public OpConversionPattern<math::ExpM1Op> { |
| 353 | using Base::Base; |
| 354 | |
| 355 | LogicalResult |
| 356 | matchAndRewrite(math::ExpM1Op operation, OpAdaptor adaptor, |
| 357 | ConversionPatternRewriter &rewriter) const override { |
| 358 | assert(adaptor.getOperands().size() == 1); |
| 359 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: operation); |
| 360 | failed(Result: res)) |
| 361 | return res; |
| 362 | |
| 363 | Location loc = operation.getLoc(); |
| 364 | Type type = this->getTypeConverter()->convertType(operation.getType()); |
| 365 | if (!type) |
| 366 | return failure(); |
| 367 | |
| 368 | Value exp = ExpOp::create(rewriter, loc, type, adaptor.getOperand()); |
| 369 | auto one = spirv::ConstantOp::getOne(type, loc, builder&: rewriter); |
| 370 | rewriter.replaceOpWithNewOp<spirv::FSubOp>(op: operation, args&: exp, args&: one); |
| 371 | return success(); |
| 372 | } |
| 373 | }; |
| 374 | |
| 375 | /// Converts math.log1p to SPIR-V ops. |
| 376 | /// |
| 377 | /// SPIR-V does not have a direct operations for log(1+x). Explicitly lower to |
| 378 | /// these operations. |
| 379 | template <typename LogOp> |
| 380 | struct Log1pOpPattern final : public OpConversionPattern<math::Log1pOp> { |
| 381 | using Base::Base; |
| 382 | |
| 383 | LogicalResult |
| 384 | matchAndRewrite(math::Log1pOp operation, OpAdaptor adaptor, |
| 385 | ConversionPatternRewriter &rewriter) const override { |
| 386 | assert(adaptor.getOperands().size() == 1); |
| 387 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: operation); |
| 388 | failed(Result: res)) |
| 389 | return res; |
| 390 | |
| 391 | Location loc = operation.getLoc(); |
| 392 | Type type = this->getTypeConverter()->convertType(operation.getType()); |
| 393 | if (!type) |
| 394 | return failure(); |
| 395 | |
| 396 | auto one = spirv::ConstantOp::getOne(type, loc: operation.getLoc(), builder&: rewriter); |
| 397 | Value onePlus = |
| 398 | spirv::FAddOp::create(builder&: rewriter, location: loc, operand1: one, operand2: adaptor.getOperand()); |
| 399 | rewriter.replaceOpWithNewOp<LogOp>(operation, type, onePlus); |
| 400 | return success(); |
| 401 | } |
| 402 | }; |
| 403 | |
| 404 | /// Converts math.log10 to GLSL SPIR-V ops. |
| 405 | /// |
| 406 | /// GLSL.std.450 has no Log10 instruction. Lower it as: |
| 407 | /// log10(x) = log(x) * 1/log(10) |
| 408 | struct Log10OpPattern final : public OpConversionPattern<math::Log10Op> { |
| 409 | using Base::Base; |
| 410 | |
| 411 | static constexpr double log10Reciprocal = |
| 412 | 0.4342944819032518276511289189166050822943970058036665661144537832; |
| 413 | |
| 414 | LogicalResult |
| 415 | matchAndRewrite(math::Log10Op operation, OpAdaptor adaptor, |
| 416 | ConversionPatternRewriter &rewriter) const override { |
| 417 | assert(adaptor.getOperands().size() == 1); |
| 418 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: operation); |
| 419 | failed(Result: res)) |
| 420 | return res; |
| 421 | |
| 422 | Location loc = operation.getLoc(); |
| 423 | Type type = this->getTypeConverter()->convertType(t: operation.getType()); |
| 424 | if (!type) |
| 425 | return rewriter.notifyMatchFailure(arg&: operation, msg: "type conversion failed" ); |
| 426 | |
| 427 | auto getConstantValue = [&](double value) { |
| 428 | if (auto floatType = dyn_cast<FloatType>(Val&: type)) { |
| 429 | return spirv::ConstantOp::create( |
| 430 | builder&: rewriter, location: loc, constant: type, value: rewriter.getFloatAttr(type: floatType, value)); |
| 431 | } |
| 432 | if (auto vectorType = dyn_cast<VectorType>(Val&: type)) { |
| 433 | Type elemType = vectorType.getElementType(); |
| 434 | |
| 435 | if (isa<FloatType>(Val: elemType)) { |
| 436 | return spirv::ConstantOp::create( |
| 437 | builder&: rewriter, location: loc, constant: type, |
| 438 | value: DenseFPElementsAttr::get( |
| 439 | type: vectorType, arg: FloatAttr::get(type: elemType, value).getValue())); |
| 440 | } |
| 441 | } |
| 442 | llvm_unreachable("unimplemented type for log10" ); |
| 443 | }; |
| 444 | |
| 445 | Value constantValue = getConstantValue(log10Reciprocal); |
| 446 | Value log = spirv::GLLogOp::create(builder&: rewriter, location: loc, operand: adaptor.getOperand()); |
| 447 | rewriter.replaceOpWithNewOp<spirv::FMulOp>(op: operation, args&: type, args&: log, |
| 448 | args&: constantValue); |
| 449 | return success(); |
| 450 | } |
| 451 | }; |
| 452 | |
| 453 | /// Converts math.powf to SPIRV-Ops. |
| 454 | struct PowFOpPattern final : public OpConversionPattern<math::PowFOp> { |
| 455 | using Base::Base; |
| 456 | |
| 457 | LogicalResult |
| 458 | matchAndRewrite(math::PowFOp powfOp, OpAdaptor adaptor, |
| 459 | ConversionPatternRewriter &rewriter) const override { |
| 460 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: powfOp); failed(Result: res)) |
| 461 | return res; |
| 462 | |
| 463 | Type dstType = getTypeConverter()->convertType(t: powfOp.getType()); |
| 464 | if (!dstType) |
| 465 | return failure(); |
| 466 | |
| 467 | Location loc = powfOp.getLoc(); |
| 468 | Type operandType = adaptor.getRhs().getType(); |
| 469 | |
| 470 | // Parity-based lowering requires an integer-valued constant exponent. |
| 471 | // Otherwise fall back to exp(y*log(x)), which yields NaN for x<0 (matches |
| 472 | // C). |
| 473 | auto isOdd = [](const APFloat &v) { |
| 474 | APSInt i(/*BitWidth=*/64, /*isUnsigned=*/false); |
| 475 | bool ignored; |
| 476 | v.convertToInteger(Result&: i, RM: APFloat::rmTowardZero, IsExact: &ignored); |
| 477 | return i[0]; |
| 478 | }; |
| 479 | |
| 480 | SmallVector<bool> oddMask; |
| 481 | Attribute rhsAttr; |
| 482 | if (matchPattern(value: adaptor.getRhs(), pattern: m_Constant(bind_value: &rhsAttr))) { |
| 483 | TypeSwitch<Attribute>(rhsAttr) |
| 484 | .Case(caseFn: [&](FloatAttr a) { |
| 485 | if (a.getValue().isInteger()) |
| 486 | oddMask.push_back(Elt: isOdd(a.getValue())); |
| 487 | }) |
| 488 | .Case(caseFn: [&](SplatElementsAttr a) { |
| 489 | APFloat splat = a.getSplatValue<APFloat>(); |
| 490 | if (splat.isInteger()) |
| 491 | oddMask.push_back(Elt: isOdd(splat)); |
| 492 | }) |
| 493 | .Case(caseFn: [&](DenseElementsAttr a) { |
| 494 | SmallVector<bool> mask; |
| 495 | for (const APFloat &elt : a.getValues<APFloat>()) { |
| 496 | if (!elt.isInteger()) |
| 497 | return; |
| 498 | mask.push_back(Elt: isOdd(elt)); |
| 499 | } |
| 500 | oddMask = std::move(mask); |
| 501 | }); |
| 502 | } |
| 503 | |
| 504 | if (oddMask.empty()) { |
| 505 | Value log = spirv::GLLogOp::create(builder&: rewriter, location: loc, operand: adaptor.getLhs()); |
| 506 | Value mul = spirv::FMulOp::create(builder&: rewriter, location: loc, operand1: adaptor.getRhs(), operand2: log); |
| 507 | rewriter.replaceOpWithNewOp<spirv::GLExpOp>(op: powfOp, args&: mul); |
| 508 | return success(); |
| 509 | } |
| 510 | |
| 511 | // GL.Pow is undefined for x < 0; take abs and conditionally negate the |
| 512 | // result for lanes whose exponent is odd. |
| 513 | Value abs = spirv::GLFAbsOp::create(builder&: rewriter, location: loc, operand: adaptor.getLhs()); |
| 514 | Value pow = spirv::GLPowOp::create(builder&: rewriter, location: loc, lhs: abs, rhs: adaptor.getRhs()); |
| 515 | |
| 516 | // No odd-parity element: result has the same sign as |lhs|^rhs >= 0. |
| 517 | if (llvm::none_of(Range&: oddMask, P: [](bool b) { return b; })) { |
| 518 | rewriter.replaceOp(op: powfOp, newValues: pow); |
| 519 | return success(); |
| 520 | } |
| 521 | |
| 522 | Value zero = spirv::ConstantOp::getZero(type: operandType, loc, builder&: rewriter); |
| 523 | Value lessThan = |
| 524 | spirv::FOrdLessThanOp::create(builder&: rewriter, location: loc, operand1: adaptor.getLhs(), operand2: zero); |
| 525 | Value negate = spirv::FNegateOp::create(builder&: rewriter, location: loc, operand: pow); |
| 526 | |
| 527 | Value shouldNegate; |
| 528 | if (llvm::all_equal(Range&: oddMask)) { |
| 529 | // Every lane has odd exponent: negate iff lhs < 0. |
| 530 | shouldNegate = lessThan; |
| 531 | } else { |
| 532 | // Mixed parity (non-splat dense vector): AND lhs<0 with a per-element |
| 533 | // constant odd-mask. |
| 534 | auto vecType = cast<VectorType>(Val&: operandType); |
| 535 | auto maskType = VectorType::get(shape: vecType.getShape(), elementType: rewriter.getI1Type()); |
| 536 | Value oddConst = spirv::ConstantOp::create( |
| 537 | builder&: rewriter, location: loc, constant: maskType, value: DenseElementsAttr::get(type: maskType, values: oddMask)); |
| 538 | shouldNegate = |
| 539 | spirv::LogicalAndOp::create(builder&: rewriter, location: loc, operand1: lessThan, operand2: oddConst); |
| 540 | } |
| 541 | |
| 542 | rewriter.replaceOpWithNewOp<spirv::SelectOp>(op: powfOp, args&: shouldNegate, args&: negate, |
| 543 | args&: pow); |
| 544 | return success(); |
| 545 | } |
| 546 | }; |
| 547 | |
| 548 | /// Converts math.fpowi to spirv.CL.pown. |
| 549 | struct PowIOpPattern final : public OpConversionPattern<math::FPowIOp> { |
| 550 | using Base::Base; |
| 551 | |
| 552 | LogicalResult |
| 553 | matchAndRewrite(math::FPowIOp op, OpAdaptor adaptor, |
| 554 | ConversionPatternRewriter &rewriter) const override { |
| 555 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: op); failed(Result: res)) |
| 556 | return res; |
| 557 | |
| 558 | Type dstType = getTypeConverter()->convertType(t: op.getType()); |
| 559 | if (!dstType) |
| 560 | return failure(); |
| 561 | |
| 562 | rewriter.replaceOpWithNewOp<spirv::CLPownOp>(op, args&: dstType, args: adaptor.getLhs(), |
| 563 | args: adaptor.getRhs()); |
| 564 | return success(); |
| 565 | } |
| 566 | }; |
| 567 | |
| 568 | /// Converts math.fpowi to GLSL SPIR-V ops. GL has no integer-power op, so the |
| 569 | /// exponent is converted to float and lowered through spirv.GL.Pow. As GL.Pow |
| 570 | /// is undefined for a negative base, the base is made positive and the result |
| 571 | /// is negated when the base is negative and the exponent is odd. |
| 572 | struct PowIOpGLPattern final : public OpConversionPattern<math::FPowIOp> { |
| 573 | using Base::Base; |
| 574 | |
| 575 | LogicalResult |
| 576 | matchAndRewrite(math::FPowIOp op, OpAdaptor adaptor, |
| 577 | ConversionPatternRewriter &rewriter) const override { |
| 578 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: op); failed(Result: res)) |
| 579 | return res; |
| 580 | |
| 581 | Type dstType = getTypeConverter()->convertType(t: op.getType()); |
| 582 | if (!dstType) |
| 583 | return failure(); |
| 584 | |
| 585 | Location loc = op.getLoc(); |
| 586 | Value base = adaptor.getLhs(); |
| 587 | Value power = adaptor.getRhs(); |
| 588 | |
| 589 | Value expFloat = |
| 590 | spirv::ConvertSToFOp::create(builder&: rewriter, location: loc, result: dstType, operand: power); |
| 591 | Value abs = spirv::GLFAbsOp::create(builder&: rewriter, location: loc, operand: base); |
| 592 | Value pow = spirv::GLPowOp::create(builder&: rewriter, location: loc, lhs: abs, rhs: expFloat); |
| 593 | |
| 594 | Value zeroF = spirv::ConstantOp::getZero(type: dstType, loc, builder&: rewriter); |
| 595 | Value lessThan = spirv::FOrdLessThanOp::create(builder&: rewriter, location: loc, operand1: base, operand2: zeroF); |
| 596 | |
| 597 | Type powerType = power.getType(); |
| 598 | Value oneI = spirv::ConstantOp::getOne(type: powerType, loc, builder&: rewriter); |
| 599 | Value lowBit = spirv::BitwiseAndOp::create(builder&: rewriter, location: loc, operand1: power, operand2: oneI); |
| 600 | Value isOdd = spirv::IEqualOp::create(builder&: rewriter, location: loc, operand1: lowBit, operand2: oneI); |
| 601 | |
| 602 | Value shouldNegate = |
| 603 | spirv::LogicalAndOp::create(builder&: rewriter, location: loc, operand1: lessThan, operand2: isOdd); |
| 604 | Value negate = spirv::FNegateOp::create(builder&: rewriter, location: loc, operand: pow); |
| 605 | rewriter.replaceOpWithNewOp<spirv::SelectOp>(op, args&: shouldNegate, args&: negate, args&: pow); |
| 606 | return success(); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | /// Converts math.sincos to SPIR-V ops. |
| 611 | /// |
| 612 | /// SPIR-V has no fused sincos instruction, so emit separate sin and cos ops |
| 613 | /// sharing the same operand. |
| 614 | template <typename SinOp, typename CosOp> |
| 615 | struct SincosOpPattern final : public OpConversionPattern<math::SincosOp> { |
| 616 | using Base::Base; |
| 617 | |
| 618 | LogicalResult |
| 619 | matchAndRewrite(math::SincosOp operation, OpAdaptor adaptor, |
| 620 | ConversionPatternRewriter &rewriter) const override { |
| 621 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: operation); |
| 622 | failed(Result: res)) |
| 623 | return res; |
| 624 | |
| 625 | Type type = |
| 626 | getTypeConverter()->convertType(operation.getOperand().getType()); |
| 627 | if (!type) |
| 628 | return failure(); |
| 629 | |
| 630 | Location loc = operation.getLoc(); |
| 631 | Value sin = SinOp::create(rewriter, loc, type, adaptor.getOperand()); |
| 632 | Value cos = CosOp::create(rewriter, loc, type, adaptor.getOperand()); |
| 633 | rewriter.replaceOp(op: operation, newValues: {sin, cos}); |
| 634 | return success(); |
| 635 | } |
| 636 | }; |
| 637 | |
| 638 | /// Converts math.round to GLSL SPIRV extended ops. |
| 639 | struct RoundOpPattern final : public OpConversionPattern<math::RoundOp> { |
| 640 | using Base::Base; |
| 641 | |
| 642 | LogicalResult |
| 643 | matchAndRewrite(math::RoundOp roundOp, OpAdaptor adaptor, |
| 644 | ConversionPatternRewriter &rewriter) const override { |
| 645 | if (LogicalResult res = checkSourceOpTypes(rewriter, sourceOp: roundOp); failed(Result: res)) |
| 646 | return res; |
| 647 | |
| 648 | Location loc = roundOp.getLoc(); |
| 649 | auto ty = getTypeConverter()->convertType(t: adaptor.getOperand().getType()); |
| 650 | if (!ty) { |
| 651 | return rewriter.notifyMatchFailure( |
| 652 | arg: roundOp->getLoc(), |
| 653 | msg: llvm::formatv(Fmt: "failed to convert type {0} for SPIR-V" , |
| 654 | Vals: roundOp.getType())); |
| 655 | } |
| 656 | |
| 657 | Type ety = getElementTypeOrSelf(type: ty); |
| 658 | |
| 659 | auto zero = spirv::ConstantOp::getZero(type: ty, loc, builder&: rewriter); |
| 660 | auto one = spirv::ConstantOp::getOne(type: ty, loc, builder&: rewriter); |
| 661 | Value half; |
| 662 | if (VectorType vty = dyn_cast<VectorType>(Val&: ty)) { |
| 663 | half = spirv::ConstantOp::create( |
| 664 | builder&: rewriter, location: loc, constant: vty, |
| 665 | value: DenseElementsAttr::get(type: vty, |
| 666 | values: rewriter.getFloatAttr(type: ety, value: 0.5).getValue())); |
| 667 | } else { |
| 668 | half = spirv::ConstantOp::create(builder&: rewriter, location: loc, constant: ty, |
| 669 | value: rewriter.getFloatAttr(type: ety, value: 0.5)); |
| 670 | } |
| 671 | |
| 672 | auto abs = spirv::GLFAbsOp::create(builder&: rewriter, location: loc, operand: adaptor.getOperand()); |
| 673 | auto floor = spirv::GLFloorOp::create(builder&: rewriter, location: loc, operand: abs); |
| 674 | auto sub = spirv::FSubOp::create(builder&: rewriter, location: loc, operand1: abs, operand2: floor); |
| 675 | auto greater = |
| 676 | spirv::FOrdGreaterThanEqualOp::create(builder&: rewriter, location: loc, operand1: sub, operand2: half); |
| 677 | auto select = spirv::SelectOp::create(builder&: rewriter, location: loc, condition: greater, true_value: one, false_value: zero); |
| 678 | auto add = spirv::FAddOp::create(builder&: rewriter, location: loc, operand1: floor, operand2: select); |
| 679 | rewriter.replaceOpWithNewOp<math::CopySignOp>(op: roundOp, args&: add, |
| 680 | args: adaptor.getOperand()); |
| 681 | return success(); |
| 682 | } |
| 683 | }; |
| 684 | } // namespace |
| 685 | |
| 686 | //===----------------------------------------------------------------------===// |
| 687 | // Pattern population |
| 688 | //===----------------------------------------------------------------------===// |
| 689 | |
| 690 | namespace mlir { |
| 691 | void populateMathToSPIRVPatterns(const SPIRVTypeConverter &typeConverter, |
| 692 | RewritePatternSet &patterns) { |
| 693 | // Core patterns |
| 694 | patterns |
| 695 | .add<CopySignPattern, |
| 696 | CheckedElementwiseOpPattern<math::CtPopOp, spirv::BitCountOp>, |
| 697 | CheckedElementwiseOpPattern<math::IsInfOp, spirv::IsInfOp>, |
| 698 | CheckedElementwiseOpPattern<math::IsNaNOp, spirv::IsNanOp>, |
| 699 | CheckedElementwiseOpPattern<math::IsFiniteOp, spirv::IsFiniteOp>, |
| 700 | CheckedElementwiseOpPattern<math::IsNormalOp, spirv::IsNormalOp>>( |
| 701 | arg: typeConverter, args: patterns.getContext()); |
| 702 | |
| 703 | // GLSL patterns |
| 704 | patterns |
| 705 | .add<CountLeadingZerosPattern, CountTrailingZerosPattern, |
| 706 | Log1pOpPattern<spirv::GLLogOp>, Log10OpPattern, |
| 707 | ExpM1OpPattern<spirv::GLExpOp>, PowFOpPattern, PowIOpGLPattern, |
| 708 | RoundOpPattern, SincosOpPattern<spirv::GLSinOp, spirv::GLCosOp>, |
| 709 | CheckedElementwiseOpPattern<math::AbsFOp, spirv::GLFAbsOp>, |
| 710 | CheckedElementwiseOpPattern<math::AbsIOp, spirv::GLSAbsOp>, |
| 711 | CheckedElementwiseOpPattern<math::AtanOp, spirv::GLAtanOp>, |
| 712 | CheckedElementwiseOpPattern<math::Atan2Op, spirv::GLAtan2Op>, |
| 713 | CheckedElementwiseOpPattern<math::CeilOp, spirv::GLCeilOp>, |
| 714 | CheckedElementwiseOpPattern<math::ClampFOp, spirv::GLFClampOp>, |
| 715 | CheckedElementwiseOpPattern<math::CosOp, spirv::GLCosOp>, |
| 716 | CheckedElementwiseOpPattern<math::ExpOp, spirv::GLExpOp>, |
| 717 | CheckedElementwiseOpPattern<math::Exp2Op, spirv::GLExp2Op>, |
| 718 | CheckedElementwiseOpPattern<math::FloorOp, spirv::GLFloorOp>, |
| 719 | CheckedElementwiseOpPattern<math::FmaOp, spirv::GLFmaOp>, |
| 720 | CheckedElementwiseOpPattern<math::LogOp, spirv::GLLogOp>, |
| 721 | CheckedElementwiseOpPattern<math::Log2Op, spirv::GLLog2Op>, |
| 722 | CheckedElementwiseOpPattern<math::RoundEvenOp, spirv::GLRoundEvenOp>, |
| 723 | CheckedElementwiseOpPattern<math::RsqrtOp, spirv::GLInverseSqrtOp>, |
| 724 | CheckedElementwiseOpPattern<math::SinOp, spirv::GLSinOp>, |
| 725 | CheckedElementwiseOpPattern<math::SqrtOp, spirv::GLSqrtOp>, |
| 726 | CheckedElementwiseOpPattern<math::TanhOp, spirv::GLTanhOp>, |
| 727 | CheckedElementwiseOpPattern<math::TanOp, spirv::GLTanOp>, |
| 728 | CheckedElementwiseOpPattern<math::TruncOp, spirv::GLTruncOp>, |
| 729 | CheckedElementwiseOpPattern<math::AsinOp, spirv::GLAsinOp>, |
| 730 | CheckedElementwiseOpPattern<math::AcosOp, spirv::GLAcosOp>, |
| 731 | CheckedElementwiseOpPattern<math::SinhOp, spirv::GLSinhOp>, |
| 732 | CheckedElementwiseOpPattern<math::CoshOp, spirv::GLCoshOp>, |
| 733 | CheckedElementwiseOpPattern<math::AsinhOp, spirv::GLAsinhOp>, |
| 734 | CheckedElementwiseOpPattern<math::AcoshOp, spirv::GLAcoshOp>, |
| 735 | CheckedElementwiseOpPattern<math::AtanhOp, spirv::GLAtanhOp>>( |
| 736 | arg: typeConverter, args: patterns.getContext()); |
| 737 | |
| 738 | // OpenCL patterns |
| 739 | patterns.add< |
| 740 | Log1pOpPattern<spirv::CLLogOp>, ExpM1OpPattern<spirv::CLExpOp>, |
| 741 | SincosOpPattern<spirv::CLSinOp, spirv::CLCosOp>, |
| 742 | CheckedElementwiseOpPattern<math::AbsFOp, spirv::CLFAbsOp>, |
| 743 | CheckedElementwiseOpPattern<math::AbsIOp, spirv::CLSAbsOp>, |
| 744 | CheckedElementwiseOpPattern<math::CountLeadingZerosOp, spirv::CLClzOp>, |
| 745 | CountTrailingZerosOpenCLPattern, |
| 746 | CheckedElementwiseOpPattern<math::AtanOp, spirv::CLAtanOp>, |
| 747 | CheckedElementwiseOpPattern<math::Atan2Op, spirv::CLAtan2Op>, |
| 748 | CheckedElementwiseOpPattern<math::CbrtOp, spirv::CLCbrtOp>, |
| 749 | CheckedElementwiseOpPattern<math::CeilOp, spirv::CLCeilOp>, |
| 750 | CheckedElementwiseOpPattern<math::CopySignOp, spirv::CLCopysignOp>, |
| 751 | CheckedElementwiseOpPattern<math::CosOp, spirv::CLCosOp>, |
| 752 | CheckedElementwiseOpPattern<math::ErfOp, spirv::CLErfOp>, |
| 753 | CheckedElementwiseOpPattern<math::ErfcOp, spirv::CLErfcOp>, |
| 754 | CheckedElementwiseOpPattern<math::ExpOp, spirv::CLExpOp>, |
| 755 | CheckedElementwiseOpPattern<math::Exp2Op, spirv::CLExp2Op>, |
| 756 | CheckedElementwiseOpPattern<math::FloorOp, spirv::CLFloorOp>, |
| 757 | CheckedElementwiseOpPattern<math::FmaOp, spirv::CLFmaOp>, |
| 758 | CheckedElementwiseOpPattern<math::LogOp, spirv::CLLogOp>, |
| 759 | CheckedElementwiseOpPattern<math::Log2Op, spirv::CLLog2Op>, |
| 760 | CheckedElementwiseOpPattern<math::Log10Op, spirv::CLLog10Op>, |
| 761 | CheckedElementwiseOpPattern<math::PowFOp, spirv::CLPowOp>, PowIOpPattern, |
| 762 | CheckedElementwiseOpPattern<math::RoundEvenOp, spirv::CLRintOp>, |
| 763 | CheckedElementwiseOpPattern<math::RoundOp, spirv::CLRoundOp>, |
| 764 | CheckedElementwiseOpPattern<math::RsqrtOp, spirv::CLRsqrtOp>, |
| 765 | CheckedElementwiseOpPattern<math::SinOp, spirv::CLSinOp>, |
| 766 | CheckedElementwiseOpPattern<math::SqrtOp, spirv::CLSqrtOp>, |
| 767 | CheckedElementwiseOpPattern<math::TanhOp, spirv::CLTanhOp>, |
| 768 | CheckedElementwiseOpPattern<math::TanOp, spirv::CLTanOp>, |
| 769 | CheckedElementwiseOpPattern<math::TruncOp, spirv::CLTruncOp>, |
| 770 | CheckedElementwiseOpPattern<math::AsinOp, spirv::CLAsinOp>, |
| 771 | CheckedElementwiseOpPattern<math::AcosOp, spirv::CLAcosOp>, |
| 772 | CheckedElementwiseOpPattern<math::SinhOp, spirv::CLSinhOp>, |
| 773 | CheckedElementwiseOpPattern<math::CoshOp, spirv::CLCoshOp>, |
| 774 | CheckedElementwiseOpPattern<math::AsinhOp, spirv::CLAsinhOp>, |
| 775 | CheckedElementwiseOpPattern<math::AcoshOp, spirv::CLAcoshOp>, |
| 776 | CheckedElementwiseOpPattern<math::AtanhOp, spirv::CLAtanhOp>>( |
| 777 | arg: typeConverter, args: patterns.getContext()); |
| 778 | } |
| 779 | } // namespace mlir |
| 780 | |