27#include "llvm/ADT/FloatingPointMode.h"
30#define GEN_PASS_DEF_CONVERTMATHTOLLVMPASS
31#include "mlir/Conversion/Passes.h.inc"
38template <
typename SourceOp,
typename TargetOp>
41template <
typename SourceOp,
typename TargetOp,
bool FailOnUnsupportedFP = true>
42using ConvertFMFMathToLLVMPattern =
52template <
typename SourceOp,
typename TargetOp,
bool HasRoundingMode,
53 template <
typename,
typename>
typename AttrConvert =
55 bool FailOnUnsupportedFP =
true>
56struct ConstrainedVectorConvertToLLVMPattern
58 FailOnUnsupportedFP> {
59 using VectorConvertToLLVMPattern<
60 SourceOp, TargetOp, AttrConvert,
61 FailOnUnsupportedFP>::VectorConvertToLLVMPattern;
64 matchAndRewrite(SourceOp op,
typename SourceOp::Adaptor adaptor,
65 ConversionPatternRewriter &rewriter)
const override {
66 if (HasRoundingMode !=
static_cast<bool>(op.getRoundingModeAttr()))
68 return VectorConvertToLLVMPattern<
69 SourceOp, TargetOp, AttrConvert,
70 FailOnUnsupportedFP>::matchAndRewrite(op, adaptor, rewriter);
75 ConvertFMFMathToLLVMPattern<math::AbsFOp, LLVM::FAbsOp,
77using CeilOpLowering = ConvertFMFMathToLLVMPattern<math::CeilOp, LLVM::FCeilOp>;
78using CopySignOpLowering =
79 ConvertFMFMathToLLVMPattern<math::CopySignOp, LLVM::CopySignOp>;
80using CosOpLowering = ConvertFMFMathToLLVMPattern<math::CosOp, LLVM::CosOp>;
81using CoshOpLowering = ConvertFMFMathToLLVMPattern<math::CoshOp, LLVM::CoshOp>;
82using AcosOpLowering = ConvertFMFMathToLLVMPattern<math::AcosOp, LLVM::ACosOp>;
83using CtPopFOpLowering =
87using Exp2OpLowering = ConvertFMFMathToLLVMPattern<math::Exp2Op, LLVM::Exp2Op>;
88using ExpOpLowering = ConvertFMFMathToLLVMPattern<math::ExpOp, LLVM::ExpOp>;
89using FloorOpLowering =
90 ConvertFMFMathToLLVMPattern<math::FloorOp, LLVM::FFloorOp>;
92 ConstrainedVectorConvertToLLVMPattern<math::FmaOp, LLVM::FMAOp,
96using ConstrainedFmaOpLowering = ConstrainedVectorConvertToLLVMPattern<
97 math::FmaOp, LLVM::ConstrainedFMAIntr,
true,
99using Log10OpLowering =
100 ConvertFMFMathToLLVMPattern<math::Log10Op, LLVM::Log10Op>;
101using Log2OpLowering = ConvertFMFMathToLLVMPattern<math::Log2Op, LLVM::Log2Op>;
102using LogOpLowering = ConvertFMFMathToLLVMPattern<math::LogOp, LLVM::LogOp>;
103using PowFOpLowering = ConvertFMFMathToLLVMPattern<math::PowFOp, LLVM::PowOp>;
104using RoundEvenOpLowering =
105 ConvertFMFMathToLLVMPattern<math::RoundEvenOp, LLVM::RoundEvenOp>;
106using RoundOpLowering =
107 ConvertFMFMathToLLVMPattern<math::RoundOp, LLVM::RoundOp>;
108using SinOpLowering = ConvertFMFMathToLLVMPattern<math::SinOp, LLVM::SinOp>;
109using SinhOpLowering = ConvertFMFMathToLLVMPattern<math::SinhOp, LLVM::SinhOp>;
110using ASinOpLowering = ConvertFMFMathToLLVMPattern<math::AsinOp, LLVM::ASinOp>;
111using SqrtOpLowering = ConvertFMFMathToLLVMPattern<math::SqrtOp, LLVM::SqrtOp>;
112using FTruncOpLowering =
113 ConvertFMFMathToLLVMPattern<math::TruncOp, LLVM::FTruncOp>;
114using TanOpLowering = ConvertFMFMathToLLVMPattern<math::TanOp, LLVM::TanOp>;
115using TanhOpLowering = ConvertFMFMathToLLVMPattern<math::TanhOp, LLVM::TanhOp>;
116using ATanOpLowering = ConvertFMFMathToLLVMPattern<math::AtanOp, LLVM::ATanOp>;
117using ATan2OpLowering =
118 ConvertFMFMathToLLVMPattern<math::Atan2Op, LLVM::ATan2Op>;
122template <
typename MathOp,
typename LLVMOp>
123struct IntOpWithFlagLowering
125 using ConvertOpToLLVMPattern<
126 MathOp,
true>::ConvertOpToLLVMPattern;
127 using Super = IntOpWithFlagLowering<MathOp, LLVMOp>;
130 matchAndRewrite(MathOp op,
typename MathOp::Adaptor adaptor,
131 ConversionPatternRewriter &rewriter)
const override {
132 const auto &typeConverter = *this->getTypeConverter();
133 auto operandType = adaptor.getOperand().getType();
134 auto llvmOperandType = typeConverter.convertType(operandType);
135 if (!llvmOperandType)
138 auto loc = op.getLoc();
139 auto resultType = op.getResult().getType();
140 auto llvmResultType = typeConverter.convertType(resultType);
144 if (!isa<LLVM::LLVMArrayType>(llvmOperandType)) {
145 rewriter.replaceOpWithNewOp<LLVMOp>(op, llvmResultType,
146 adaptor.getOperand(),
false);
150 if (!isa<VectorType>(resultType))
154 op.getOperation(), adaptor.getOperands(), typeConverter,
155 [&](Type llvm1DVectorTy,
ValueRange operands) {
156 return LLVMOp::create(rewriter, loc, llvm1DVectorTy, operands[0],
163using CountLeadingZerosOpLowering =
164 IntOpWithFlagLowering<math::CountLeadingZerosOp, LLVM::CountLeadingZerosOp>;
165using CountTrailingZerosOpLowering =
166 IntOpWithFlagLowering<math::CountTrailingZerosOp,
167 LLVM::CountTrailingZerosOp>;
168using AbsIOpLowering = IntOpWithFlagLowering<math::AbsIOp, LLVM::AbsOp>;
171struct SincosOpLowering
175 math::SincosOp,
true>::ConvertOpToLLVMPattern;
179 ConversionPatternRewriter &rewriter)
const override {
181 mlir::Location loc = op.getLoc();
182 mlir::Type operandType = adaptor.getOperand().getType();
183 mlir::Type llvmOperandType = typeConverter.convertType(operandType);
184 mlir::Type sinType = typeConverter.convertType(op.getSin().getType());
185 mlir::Type cosType = typeConverter.convertType(op.getCos().getType());
186 if (!llvmOperandType || !sinType || !cosType)
189 ConvertFastMath<math::SincosOp, LLVM::SincosOp> attrs(op);
191 auto structType = LLVM::LLVMStructType::getLiteral(
192 rewriter.getContext(), {llvmOperandType, llvmOperandType});
194 auto sincosOp = LLVM::SincosOp::create(
196 attrs.getProperties(), attrs.getDiscardableAttrs());
198 auto sinValue = LLVM::ExtractValueOp::create(rewriter, loc, sincosOp, 0);
199 auto cosValue = LLVM::ExtractValueOp::create(rewriter, loc, sincosOp, 1);
201 rewriter.replaceOp(op, {sinValue, cosValue});
207struct ExpM1OpLowering
210 using ConvertOpToLLVMPattern<
211 math::ExpM1Op,
true>::ConvertOpToLLVMPattern;
214 matchAndRewrite(math::ExpM1Op op, OpAdaptor adaptor,
215 ConversionPatternRewriter &rewriter)
const override {
216 const auto &typeConverter = *this->getTypeConverter();
217 auto operandType = adaptor.getOperand().getType();
218 auto llvmOperandType = typeConverter.convertType(operandType);
219 if (!llvmOperandType)
222 auto loc = op.getLoc();
223 auto resultType = op.getResult().getType();
224 auto floatType = cast<FloatType>(
226 auto floatOne = rewriter.getFloatAttr(floatType, 1.0);
227 ConvertFastMath<math::ExpM1Op, LLVM::ExpOp> expAttrs(op);
228 ConvertFastMath<math::ExpM1Op, LLVM::FSubOp> subAttrs(op);
230 if (!isa<LLVM::LLVMArrayType>(llvmOperandType)) {
231 LLVM::ConstantOp one;
233 one = LLVM::ConstantOp::create(
234 rewriter, loc, llvmOperandType,
235 SplatElementsAttr::get(cast<ShapedType>(llvmOperandType),
239 LLVM::ConstantOp::create(rewriter, loc, llvmOperandType, floatOne);
241 auto exp = LLVM::ExpOp::create(rewriter, loc,
TypeRange{llvmOperandType},
243 expAttrs.getProperties(),
244 expAttrs.getDiscardableAttrs());
245 rewriter.replaceOpWithNewOp<LLVM::FSubOp>(
247 subAttrs.getProperties(), subAttrs.getDiscardableAttrs());
251 if (!isa<VectorType>(resultType))
252 return rewriter.notifyMatchFailure(op,
"expected vector result type");
255 op.getOperation(), adaptor.getOperands(), typeConverter,
256 [&](Type llvm1DVectorTy,
ValueRange operands) {
257 auto numElements = LLVM::getVectorNumElements(llvm1DVectorTy);
258 auto splatAttr = SplatElementsAttr::get(
259 mlir::VectorType::get({numElements.getKnownMinValue()}, floatType,
260 {numElements.isScalable()}),
262 auto one = LLVM::ConstantOp::create(rewriter, loc, llvm1DVectorTy,
264 auto exp = LLVM::ExpOp::create(
266 expAttrs.getProperties(), expAttrs.getDiscardableAttrs());
267 return LLVM::FSubOp::create(
269 subAttrs.getProperties(), subAttrs.getDiscardableAttrs());
276struct Log1pOpLowering
279 using ConvertOpToLLVMPattern<
280 math::Log1pOp,
true>::ConvertOpToLLVMPattern;
283 matchAndRewrite(math::Log1pOp op, OpAdaptor adaptor,
284 ConversionPatternRewriter &rewriter)
const override {
285 const auto &typeConverter = *this->getTypeConverter();
286 auto operandType = adaptor.getOperand().getType();
287 auto llvmOperandType = typeConverter.convertType(operandType);
288 if (!llvmOperandType)
289 return rewriter.notifyMatchFailure(op,
"unsupported operand type");
291 auto loc = op.getLoc();
292 auto resultType = op.getResult().getType();
293 auto floatType = cast<FloatType>(
295 auto floatOne = rewriter.getFloatAttr(floatType, 1.0);
296 ConvertFastMath<math::Log1pOp, LLVM::FAddOp> addAttrs(op);
297 ConvertFastMath<math::Log1pOp, LLVM::LogOp> logAttrs(op);
299 if (!isa<LLVM::LLVMArrayType>(llvmOperandType)) {
300 LLVM::ConstantOp one =
301 isa<VectorType>(llvmOperandType)
302 ? LLVM::ConstantOp::create(
303 rewriter, loc, llvmOperandType,
304 SplatElementsAttr::get(cast<ShapedType>(llvmOperandType),
306 : LLVM::ConstantOp::create(rewriter, loc, llvmOperandType,
309 auto add = LLVM::FAddOp::create(rewriter, loc,
TypeRange{llvmOperandType},
311 addAttrs.getProperties(),
312 addAttrs.getDiscardableAttrs());
313 rewriter.replaceOpWithNewOp<LLVM::LogOp>(
315 logAttrs.getProperties(), logAttrs.getDiscardableAttrs());
319 if (!isa<VectorType>(resultType))
320 return rewriter.notifyMatchFailure(op,
"expected vector result type");
323 op.getOperation(), adaptor.getOperands(), typeConverter,
324 [&](Type llvm1DVectorTy,
ValueRange operands) {
325 auto numElements = LLVM::getVectorNumElements(llvm1DVectorTy);
326 auto splatAttr = SplatElementsAttr::get(
327 mlir::VectorType::get({numElements.getKnownMinValue()}, floatType,
328 {numElements.isScalable()}),
330 auto one = LLVM::ConstantOp::create(rewriter, loc, llvm1DVectorTy,
332 auto add = LLVM::FAddOp::create(
333 rewriter, loc,
TypeRange{llvm1DVectorTy},
334 ValueRange{one, operands[0]}, addAttrs.getProperties(),
335 addAttrs.getDiscardableAttrs());
336 return LLVM::LogOp::create(rewriter, loc,
TypeRange{llvm1DVectorTy},
338 logAttrs.getDiscardableAttrs());
345struct RsqrtOpLowering
348 using ConvertOpToLLVMPattern<
349 math::RsqrtOp,
true>::ConvertOpToLLVMPattern;
352 matchAndRewrite(math::RsqrtOp op, OpAdaptor adaptor,
353 ConversionPatternRewriter &rewriter)
const override {
354 const auto &typeConverter = *this->getTypeConverter();
355 auto operandType = adaptor.getOperand().getType();
356 auto llvmOperandType = typeConverter.convertType(operandType);
357 if (!llvmOperandType)
360 auto loc = op.getLoc();
361 auto resultType = op.getResult().getType();
362 auto floatType = cast<FloatType>(
364 auto floatOne = rewriter.getFloatAttr(floatType, 1.0);
365 ConvertFastMath<math::RsqrtOp, LLVM::SqrtOp> sqrtAttrs(op);
366 ConvertFastMath<math::RsqrtOp, LLVM::FDivOp> divAttrs(op);
368 if (!isa<LLVM::LLVMArrayType>(llvmOperandType)) {
369 LLVM::ConstantOp one;
370 if (isa<VectorType>(llvmOperandType)) {
371 one = LLVM::ConstantOp::create(
372 rewriter, loc, llvmOperandType,
373 SplatElementsAttr::get(cast<ShapedType>(llvmOperandType),
377 LLVM::ConstantOp::create(rewriter, loc, llvmOperandType, floatOne);
379 auto sqrt = LLVM::SqrtOp::create(
380 rewriter, loc,
TypeRange{llvmOperandType},
381 ValueRange{adaptor.getOperand()}, sqrtAttrs.getProperties(),
382 sqrtAttrs.getDiscardableAttrs());
383 rewriter.replaceOpWithNewOp<LLVM::FDivOp>(
385 divAttrs.getProperties(), divAttrs.getDiscardableAttrs());
389 if (!isa<VectorType>(resultType))
393 op.getOperation(), adaptor.getOperands(), typeConverter,
394 [&](Type llvm1DVectorTy,
ValueRange operands) {
395 auto numElements = LLVM::getVectorNumElements(llvm1DVectorTy);
396 auto splatAttr = SplatElementsAttr::get(
397 mlir::VectorType::get({numElements.getKnownMinValue()}, floatType,
398 {numElements.isScalable()}),
400 auto one = LLVM::ConstantOp::create(rewriter, loc, llvm1DVectorTy,
402 auto sqrt = LLVM::SqrtOp::create(
404 sqrtAttrs.getProperties(), sqrtAttrs.getDiscardableAttrs());
405 return LLVM::FDivOp::create(
407 divAttrs.getProperties(), divAttrs.getDiscardableAttrs());
413struct FPowIOpLowering
416 using ConvertOpToLLVMPattern<
417 math::FPowIOp,
true>::ConvertOpToLLVMPattern;
420 matchAndRewrite(math::FPowIOp op, OpAdaptor adaptor,
421 ConversionPatternRewriter &rewriter)
const override {
422 const auto &typeConverter = *this->getTypeConverter();
423 auto llvmOperandType = typeConverter.convertType(op.getLhs().getType());
424 if (!llvmOperandType)
427 auto loc = op.getLoc();
428 Value exponent = adaptor.getRhs();
429 if (isa<VectorType>(op.getRhs().getType())) {
430 SplatElementsAttr splatAttr;
432 return rewriter.notifyMatchFailure(op,
"expected a splat exponent");
434 auto exponentType = typeConverter.convertType(
438 exponent = LLVM::ConstantOp::create(
439 rewriter, loc, exponentType,
440 rewriter.getIntegerAttr(exponentType,
444 ConvertFastMath<math::FPowIOp, LLVM::PowIOp> attrs(op);
446 if (!isa<LLVM::LLVMArrayType>(llvmOperandType)) {
447 rewriter.replaceOpWithNewOp<LLVM::PowIOp>(
449 ValueRange{adaptor.getLhs(), exponent}, attrs.getProperties(),
450 attrs.getDiscardableAttrs());
455 op.getOperation(),
ValueRange{adaptor.getLhs()}, typeConverter,
456 [&](Type llvm1DVectorTy,
ValueRange operands) {
457 return LLVM::PowIOp::create(rewriter, loc, TypeRange{llvm1DVectorTy},
459 attrs.getProperties(),
460 attrs.getDiscardableAttrs());
466struct IsNaNOpLowering
469 using ConvertOpToLLVMPattern<
470 math::IsNaNOp,
true>::ConvertOpToLLVMPattern;
473 matchAndRewrite(math::IsNaNOp op, OpAdaptor adaptor,
474 ConversionPatternRewriter &rewriter)
const override {
475 const auto &typeConverter = *this->getTypeConverter();
477 typeConverter.convertType(adaptor.getOperand().getType());
478 auto resultType = typeConverter.convertType(op.getResult().getType());
479 if (!operandType || !resultType)
482 rewriter.replaceOpWithNewOp<LLVM::IsFPClass>(
483 op, resultType, adaptor.getOperand(), llvm::fcNan);
488struct IsFiniteOpLowering
491 using ConvertOpToLLVMPattern<
492 math::IsFiniteOp,
true>::ConvertOpToLLVMPattern;
495 matchAndRewrite(math::IsFiniteOp op, OpAdaptor adaptor,
496 ConversionPatternRewriter &rewriter)
const override {
497 const auto &typeConverter = *this->getTypeConverter();
499 typeConverter.convertType(adaptor.getOperand().getType());
500 auto resultType = typeConverter.convertType(op.getResult().getType());
501 if (!operandType || !resultType)
504 rewriter.replaceOpWithNewOp<LLVM::IsFPClass>(
505 op, resultType, adaptor.getOperand(), llvm::fcFinite);
510struct ConvertMathToLLVMPass
514 void runOnOperation()
override {
519 if (
failed(applyPartialConversion(getOperation(),
target,
520 std::move(patterns))))
529 if (approximateLog1p)
530 patterns.
add<Log1pOpLowering>(converter, benefit);
542 CountLeadingZerosOpLowering,
543 CountTrailingZerosOpLowering,
551 ConstrainedFmaOpLowering,
569 >(converter, benefit);
579struct MathToLLVMDialectInterface :
public ConvertToLLVMPatternInterface {
580 MathToLLVMDialectInterface(
Dialect *dialect)
581 : ConvertToLLVMPatternInterface(dialect) {}
583 void loadDependentDialects(MLIRContext *context)
const final {
584 context->loadDialect<LLVM::LLVMDialect>();
589 void populateConvertToLLVMConversionPatterns(
590 ConversionTarget &
target, LLVMTypeConverter &typeConverter,
591 RewritePatternSet &patterns)
const final {
599 dialect->addInterfaces<MathToLLVMDialectInterface>();
Utility class for operation conversions targeting the LLVM dialect that match exactly one source oper...
ConvertOpToLLVMPattern(const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
const LLVMTypeConverter * getTypeConverter() const
std::enable_if_t<!std::is_base_of< Attribute, T >::value||std::is_same< Attribute, T >::value, T > getSplatValue() const
Return the splat value for this attribute.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Conversion from types to the LLVM IR dialect.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
Basic lowering implementation to rewrite Ops with just one result to the LLVM Dialect.
LogicalResult handleMultidimensionalVectors(Operation *op, ValueRange operands, const LLVMTypeConverter &typeConverter, std::function< Value(Type, ValueRange)> createOperand, ConversionPatternRewriter &rewriter)
bool isCompatibleVectorType(Type type)
Returns true if the given type is a vector type compatible with the LLVM dialect.
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
void populateMathToLLVMConversionPatterns(const LLVMTypeConverter &converter, RewritePatternSet &patterns, bool approximateLog1p=true, PatternBenefit benefit=1)
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
void registerConvertMathToLLVMInterface(DialectRegistry ®istry)
LogicalResult matchAndRewrite(math::SincosOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override