3333#include " mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
3434#include " mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
3535#include " mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h"
36+ #include " mlir/Dialect/Arith/IR/Arith.h"
3637#include " mlir/Dialect/LLVMIR/LLVMDialect.h"
3738#include " mlir/Dialect/LLVMIR/Transforms/AddComdats.h"
3839#include " mlir/Dialect/OpenACC/OpenACC.h"
4445#include " mlir/Target/LLVMIR/ModuleTranslation.h"
4546#include " llvm/ADT/ArrayRef.h"
4647#include " llvm/ADT/TypeSwitch.h"
48+ #include < mlir/Dialect/LLVMIR/LLVMAttrs.h>
4749
4850namespace fir {
4951#define GEN_PASS_DEF_FIRTOLLVMLOWERING
@@ -3496,12 +3498,20 @@ struct AbsentOpConversion : public FIROpConversion<fir::AbsentOp> {
34963498// Primitive operations on Complex types
34973499//
34983500
3501+ template <typename OPTY>
3502+ static inline mlir::LLVM::FastmathFlagsAttr getLLVMFMFAttr (OPTY op) {
3503+ return mlir::LLVM::FastmathFlagsAttr::get (
3504+ op.getContext (),
3505+ mlir::arith::convertArithFastMathFlagsToLLVM (op.getFastmath ()));
3506+ }
3507+
34993508// / Generate inline code for complex addition/subtraction
35003509template <typename LLVMOP, typename OPTY>
35013510static mlir::LLVM::InsertValueOp
35023511complexSum (OPTY sumop, mlir::ValueRange opnds,
35033512 mlir::ConversionPatternRewriter &rewriter,
35043513 const fir::LLVMTypeConverter &lowering) {
3514+ mlir::LLVM::FastmathFlagsAttr fmf = getLLVMFMFAttr (sumop);
35053515 mlir::Value a = opnds[0 ];
35063516 mlir::Value b = opnds[1 ];
35073517 auto loc = sumop.getLoc ();
@@ -3511,8 +3521,8 @@ complexSum(OPTY sumop, mlir::ValueRange opnds,
35113521 auto y0 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, a, 1 );
35123522 auto x1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 0 );
35133523 auto y1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 1 );
3514- auto rx = rewriter.create <LLVMOP>(loc, eleTy, x0, x1);
3515- auto ry = rewriter.create <LLVMOP>(loc, eleTy, y0, y1);
3524+ auto rx = rewriter.create <LLVMOP>(loc, eleTy, x0, x1, fmf );
3525+ auto ry = rewriter.create <LLVMOP>(loc, eleTy, y0, y1, fmf );
35163526 auto r0 = rewriter.create <mlir::LLVM::UndefOp>(loc, ty);
35173527 auto r1 = rewriter.create <mlir::LLVM::InsertValueOp>(loc, r0, rx, 0 );
35183528 return rewriter.create <mlir::LLVM::InsertValueOp>(loc, r1, ry, 1 );
@@ -3560,6 +3570,7 @@ struct MulcOpConversion : public FIROpConversion<fir::MulcOp> {
35603570 // TODO: Can we use a call to __muldc3 ?
35613571 // given: (x + iy) * (x' + iy')
35623572 // result: (xx'-yy')+i(xy'+yx')
3573+ mlir::LLVM::FastmathFlagsAttr fmf = getLLVMFMFAttr (mulc);
35633574 mlir::Value a = adaptor.getOperands ()[0 ];
35643575 mlir::Value b = adaptor.getOperands ()[1 ];
35653576 auto loc = mulc.getLoc ();
@@ -3569,12 +3580,12 @@ struct MulcOpConversion : public FIROpConversion<fir::MulcOp> {
35693580 auto y0 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, a, 1 );
35703581 auto x1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 0 );
35713582 auto y1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 1 );
3572- auto xx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, x1);
3573- auto yx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, x1);
3574- auto xy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, y1);
3575- auto ri = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, xy, yx);
3576- auto yy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, y1);
3577- auto rr = rewriter.create <mlir::LLVM::FSubOp>(loc, eleTy, xx, yy);
3583+ auto xx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, x1, fmf );
3584+ auto yx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, x1, fmf );
3585+ auto xy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, y1, fmf );
3586+ auto ri = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, xy, yx, fmf );
3587+ auto yy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, y1, fmf );
3588+ auto rr = rewriter.create <mlir::LLVM::FSubOp>(loc, eleTy, xx, yy, fmf );
35783589 auto ra = rewriter.create <mlir::LLVM::UndefOp>(loc, ty);
35793590 auto r1 = rewriter.create <mlir::LLVM::InsertValueOp>(loc, ra, rr, 0 );
35803591 auto r0 = rewriter.create <mlir::LLVM::InsertValueOp>(loc, r1, ri, 1 );
@@ -3594,6 +3605,7 @@ struct DivcOpConversion : public FIROpConversion<fir::DivcOp> {
35943605 // Just generate inline code for now.
35953606 // given: (x + iy) / (x' + iy')
35963607 // result: ((xx'+yy')/d) + i((yx'-xy')/d) where d = x'x' + y'y'
3608+ mlir::LLVM::FastmathFlagsAttr fmf = getLLVMFMFAttr (divc);
35973609 mlir::Value a = adaptor.getOperands ()[0 ];
35983610 mlir::Value b = adaptor.getOperands ()[1 ];
35993611 auto loc = divc.getLoc ();
@@ -3603,17 +3615,17 @@ struct DivcOpConversion : public FIROpConversion<fir::DivcOp> {
36033615 auto y0 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, a, 1 );
36043616 auto x1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 0 );
36053617 auto y1 = rewriter.create <mlir::LLVM::ExtractValueOp>(loc, b, 1 );
3606- auto xx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, x1);
3607- auto x1x1 = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x1, x1);
3608- auto yx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, x1);
3609- auto xy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, y1);
3610- auto yy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, y1);
3611- auto y1y1 = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y1, y1);
3612- auto d = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, x1x1, y1y1);
3613- auto rrn = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, xx, yy);
3614- auto rin = rewriter.create <mlir::LLVM::FSubOp>(loc, eleTy, yx, xy);
3615- auto rr = rewriter.create <mlir::LLVM::FDivOp>(loc, eleTy, rrn, d);
3616- auto ri = rewriter.create <mlir::LLVM::FDivOp>(loc, eleTy, rin, d);
3618+ auto xx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, x1, fmf );
3619+ auto x1x1 = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x1, x1, fmf );
3620+ auto yx = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, x1, fmf );
3621+ auto xy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, x0, y1, fmf );
3622+ auto yy = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y0, y1, fmf );
3623+ auto y1y1 = rewriter.create <mlir::LLVM::FMulOp>(loc, eleTy, y1, y1, fmf );
3624+ auto d = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, x1x1, y1y1, fmf );
3625+ auto rrn = rewriter.create <mlir::LLVM::FAddOp>(loc, eleTy, xx, yy, fmf );
3626+ auto rin = rewriter.create <mlir::LLVM::FSubOp>(loc, eleTy, yx, xy, fmf );
3627+ auto rr = rewriter.create <mlir::LLVM::FDivOp>(loc, eleTy, rrn, d, fmf );
3628+ auto ri = rewriter.create <mlir::LLVM::FDivOp>(loc, eleTy, rin, d, fmf );
36173629 auto ra = rewriter.create <mlir::LLVM::UndefOp>(loc, ty);
36183630 auto r1 = rewriter.create <mlir::LLVM::InsertValueOp>(loc, ra, rr, 0 );
36193631 auto r0 = rewriter.create <mlir::LLVM::InsertValueOp>(loc, r1, ri, 1 );
0 commit comments