Skip to content

Commit d6ed678

Browse files
Initial change from Alexey to turn off FMF
1 parent 21d006c commit d6ed678

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

flang/lib/Lower/ConvertExprToHLFIR.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1689,12 +1689,24 @@ class HlfirBuilder {
16891689
BinaryOp<D> binaryOp;
16901690
auto left = hlfir::loadTrivialScalar(loc, builder, gen(op.left()));
16911691
auto right = hlfir::loadTrivialScalar(loc, builder, gen(op.right()));
1692+
mlir::Value exprl = left;
1693+
mlir::Value exprr = right;
1694+
1695+
bool noReassoc = exprl.getDefiningOp<hlfir::NoReassocOp>() ||
1696+
exprr.getDefiningOp<hlfir::NoReassocOp>();
16921697
llvm::SmallVector<mlir::Value, 1> typeParams;
16931698
if constexpr (R::category == Fortran::common::TypeCategory::Character) {
16941699
binaryOp.genResultTypeParams(loc, builder, left, right, typeParams);
16951700
}
1696-
if (rank == 0)
1697-
return binaryOp.gen(loc, builder, op.derived(), left, right);
1701+
if (rank == 0) {
1702+
auto fmfBackup = builder.getFastMathFlags();
1703+
if (noReassoc)
1704+
builder.setFastMathFlags(fmfBackup &
1705+
~mlir::arith::FastMathFlags::reassoc);
1706+
auto res = binaryOp.gen(loc, builder, op.derived(), left, right);
1707+
builder.setFastMathFlags(fmfBackup);
1708+
return res;
1709+
}
16981710

16991711
// Elemental expression.
17001712
mlir::Type elementType =
@@ -1721,9 +1733,14 @@ class HlfirBuilder {
17211733
// nsw is never added to operations on vector subscripts
17221734
// even if -fno-wrapv is enabled.
17231735
builder.setIntegerOverflowFlags(mlir::arith::IntegerOverflowFlags::none);
1736+
auto fmfBackup = builder.getFastMathFlags();
1737+
if (noReassoc)
1738+
builder.setFastMathFlags(fmfBackup &
1739+
~mlir::arith::FastMathFlags::reassoc);
17241740
mlir::Value elemental = hlfir::genElementalOp(loc, builder, elementType,
17251741
shape, typeParams, genKernel,
17261742
/*isUnordered=*/true);
1743+
builder.setFastMathFlags(fmfBackup);
17271744
builder.setIntegerOverflowFlags(iofBackup);
17281745
fir::FirOpBuilder *bldr = &builder;
17291746
getStmtCtx().attachCleanup(

0 commit comments

Comments
 (0)