Index: flang/lib/Lower/ConvertExprToHLFIR.cpp =================================================================== --- flang/lib/Lower/ConvertExprToHLFIR.cpp +++ flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -1506,16 +1506,26 @@ /*params=*/std::nullopt); } mlir::Value shape = hlfir::genShape(loc, builder, left); + // In case of Parentheses operation we have to apply it + // to the result of the ElementalOp so that further transformations + // (such as elemental inlining) work properly. + constexpr bool isParentheses = + std::is_same_v>; auto genKernel = [&op, &left, &unaryOp]( mlir::Location l, fir::FirOpBuilder &b, mlir::ValueRange oneBasedIndices) -> hlfir::Entity { auto leftElement = hlfir::getElementAt(l, b, left, oneBasedIndices); auto leftVal = hlfir::loadTrivialScalar(l, b, leftElement); + if constexpr (isParentheses) + return leftVal; return unaryOp.gen(l, b, op.derived(), leftVal); }; mlir::Value elemental = hlfir::genElementalOp(loc, builder, elementType, shape, typeParams, genKernel, /*isUnordered=*/true); + if constexpr (isParentheses) + elemental = + unaryOp.gen(loc, builder, op.derived(), hlfir::Entity{elemental}); fir::FirOpBuilder *bldr = &builder; getStmtCtx().attachCleanup( [=]() { bldr->create(loc, elemental); }); Index: flang/test/Lower/HLFIR/array-ctor-index.f90 =================================================================== --- flang/test/Lower/HLFIR/array-ctor-index.f90 +++ flang/test/Lower/HLFIR/array-ctor-index.f90 @@ -34,19 +34,19 @@ ! CHECK: %[[VAL_22:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi64> { ! CHECK: ^bb0(%[[VAL_23:.*]]: index): ! CHECK: %[[VAL_24:.*]] = hlfir.apply %[[VAL_25:.*]], %[[VAL_23]] : (!hlfir.expr<4xi64>, index) -> i64 -! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_24]] : i64 -! CHECK: hlfir.yield_element %[[VAL_26]] : i64 +! CHECK: hlfir.yield_element %[[VAL_24]] : i64 ! CHECK: } +! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_22]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_27:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi8> { ! CHECK: ^bb0(%[[VAL_28:.*]]: index): -! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_30:.*]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 +! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_26]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_29]] : (i64) -> i8 ! CHECK: hlfir.yield_element %[[VAL_31]] : i8 ! CHECK: } -! CHECK: hlfir.assign %[[VAL_32:.*]] to %[[VAL_5]]#0 : !hlfir.expr<4xi8>, !fir.ref> -! CHECK: hlfir.destroy %[[VAL_32]] : !hlfir.expr<4xi8> -! CHECK: hlfir.destroy %[[VAL_33:.*]] : !hlfir.expr<4xi64> -! CHECK: hlfir.destroy %[[VAL_34:.*]] : !hlfir.expr<4xi64> +! CHECK: hlfir.assign %[[VAL_27]] to %[[VAL_5]]#0 : !hlfir.expr<4xi8>, !fir.ref> +! CHECK: hlfir.destroy %[[VAL_27]] : !hlfir.expr<4xi8> +! CHECK: hlfir.destroy %[[VAL_26]] : !hlfir.expr<4xi64> +! CHECK: hlfir.destroy %[[VAL_13]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_35:.*]] = fir.load %[[VAL_5]]#1 : !fir.ref> ! CHECK: return %[[VAL_35]] : !fir.array<4xi8> ! CHECK: } @@ -84,19 +84,19 @@ ! CHECK: %[[VAL_22:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi64> { ! CHECK: ^bb0(%[[VAL_23:.*]]: index): ! CHECK: %[[VAL_24:.*]] = hlfir.apply %[[VAL_25:.*]], %[[VAL_23]] : (!hlfir.expr<4xi64>, index) -> i64 -! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_24]] : i64 -! CHECK: hlfir.yield_element %[[VAL_26]] : i64 +! CHECK: hlfir.yield_element %[[VAL_24]] : i64 ! CHECK: } +! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_22]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_27:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi16> { ! CHECK: ^bb0(%[[VAL_28:.*]]: index): -! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_30:.*]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 +! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_26]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_29]] : (i64) -> i16 ! CHECK: hlfir.yield_element %[[VAL_31]] : i16 ! CHECK: } -! CHECK: hlfir.assign %[[VAL_32:.*]] to %[[VAL_5]]#0 : !hlfir.expr<4xi16>, !fir.ref> -! CHECK: hlfir.destroy %[[VAL_32]] : !hlfir.expr<4xi16> -! CHECK: hlfir.destroy %[[VAL_33:.*]] : !hlfir.expr<4xi64> -! CHECK: hlfir.destroy %[[VAL_34:.*]] : !hlfir.expr<4xi64> +! CHECK: hlfir.assign %[[VAL_27]] to %[[VAL_5]]#0 : !hlfir.expr<4xi16>, !fir.ref> +! CHECK: hlfir.destroy %[[VAL_27]] : !hlfir.expr<4xi16> +! CHECK: hlfir.destroy %[[VAL_26]] : !hlfir.expr<4xi64> +! CHECK: hlfir.destroy %[[VAL_13]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_35:.*]] = fir.load %[[VAL_5]]#1 : !fir.ref> ! CHECK: return %[[VAL_35]] : !fir.array<4xi16> ! CHECK: } @@ -134,19 +134,19 @@ ! CHECK: %[[VAL_22:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi64> { ! CHECK: ^bb0(%[[VAL_23:.*]]: index): ! CHECK: %[[VAL_24:.*]] = hlfir.apply %[[VAL_25:.*]], %[[VAL_23]] : (!hlfir.expr<4xi64>, index) -> i64 -! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_24]] : i64 -! CHECK: hlfir.yield_element %[[VAL_26]] : i64 +! CHECK: hlfir.yield_element %[[VAL_24]] : i64 ! CHECK: } +! CHECK: %[[VAL_26:.*]] = hlfir.no_reassoc %[[VAL_22]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_27:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi32> { ! CHECK: ^bb0(%[[VAL_28:.*]]: index): -! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_30:.*]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 +! CHECK: %[[VAL_29:.*]] = hlfir.apply %[[VAL_26]], %[[VAL_28]] : (!hlfir.expr<4xi64>, index) -> i64 ! CHECK: %[[VAL_31:.*]] = fir.convert %[[VAL_29]] : (i64) -> i32 ! CHECK: hlfir.yield_element %[[VAL_31]] : i32 ! CHECK: } -! CHECK: hlfir.assign %[[VAL_32:.*]] to %[[VAL_5]]#0 : !hlfir.expr<4xi32>, !fir.ref> -! CHECK: hlfir.destroy %[[VAL_32]] : !hlfir.expr<4xi32> -! CHECK: hlfir.destroy %[[VAL_33:.*]] : !hlfir.expr<4xi64> -! CHECK: hlfir.destroy %[[VAL_34:.*]] : !hlfir.expr<4xi64> +! CHECK: hlfir.assign %[[VAL_27]] to %[[VAL_5]]#0 : !hlfir.expr<4xi32>, !fir.ref> +! CHECK: hlfir.destroy %[[VAL_27]] : !hlfir.expr<4xi32> +! CHECK: hlfir.destroy %[[VAL_26]] : !hlfir.expr<4xi64> +! CHECK: hlfir.destroy %[[VAL_13]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_35:.*]] = fir.load %[[VAL_5]]#1 : !fir.ref> ! CHECK: return %[[VAL_35]] : !fir.array<4xi32> ! CHECK: } @@ -183,12 +183,12 @@ ! CHECK: %[[VAL_21:.*]] = hlfir.elemental %[[VAL_7]] unordered : (!fir.shape<1>) -> !hlfir.expr<4xi64> { ! CHECK: ^bb0(%[[VAL_22:.*]]: index): ! CHECK: %[[VAL_23:.*]] = hlfir.apply %[[VAL_24:.*]], %[[VAL_22]] : (!hlfir.expr<4xi64>, index) -> i64 -! CHECK: %[[VAL_25:.*]] = hlfir.no_reassoc %[[VAL_23]] : i64 -! CHECK: hlfir.yield_element %[[VAL_25]] : i64 +! CHECK: hlfir.yield_element %[[VAL_23]] : i64 ! CHECK: } -! CHECK: hlfir.assign %[[VAL_26:.*]] to %[[VAL_5]]#0 : !hlfir.expr<4xi64>, !fir.ref> -! CHECK: hlfir.destroy %[[VAL_26]] : !hlfir.expr<4xi64> -! CHECK: hlfir.destroy %[[VAL_27:.*]] : !hlfir.expr<4xi64> +! CHECK: %[[VAL_25:.*]] = hlfir.no_reassoc %[[VAL_21]] : !hlfir.expr<4xi64> +! CHECK: hlfir.assign %[[VAL_25]] to %[[VAL_5]]#0 : !hlfir.expr<4xi64>, !fir.ref> +! CHECK: hlfir.destroy %[[VAL_25]] : !hlfir.expr<4xi64> +! CHECK: hlfir.destroy %[[VAL_13]] : !hlfir.expr<4xi64> ! CHECK: %[[VAL_28:.*]] = fir.load %[[VAL_5]]#1 : !fir.ref> ! CHECK: return %[[VAL_28]] : !fir.array<4xi64> ! CHECK: } Index: flang/test/Lower/HLFIR/elemental-array-ops.f90 =================================================================== --- flang/test/Lower/HLFIR/elemental-array-ops.f90 +++ flang/test/Lower/HLFIR/elemental-array-ops.f90 @@ -85,11 +85,11 @@ ! CHECK: %[[VAL_7:.*]] = hlfir.elemental %[[VAL_5]] typeparams %[[VAL_2]] unordered : (!fir.shape<1>, index) -> !hlfir.expr<20x!fir.char<1,?>> { ! CHECK: ^bb0(%[[VAL_8:.*]]: index): ! CHECK: %[[VAL_9:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_8]]) typeparams %[[VAL_2]] : (!fir.ref>>, index, index) -> !fir.ref> -! CHECK: %[[VAL_10:.*]] = hlfir.as_expr %[[VAL_9]] : (!fir.ref>) -> !hlfir.expr> -! CHECK: hlfir.yield_element %[[VAL_10]] : !hlfir.expr> +! CHECK: hlfir.yield_element %[[VAL_9]] : !fir.ref> ! CHECK: } +! CHECK: %[[VAL_10:.*]] = hlfir.no_reassoc %[[VAL_7]] : !hlfir.expr<20x!fir.char<1,?>> ! CHECK: fir.call -! CHECK: hlfir.destroy %[[VAL_7]] +! CHECK: hlfir.destroy %[[VAL_10]] subroutine chained_elemental(x, y, z) integer :: x(100), y(100), z(100) @@ -136,8 +136,8 @@ ! CHECK: %[[VAL_10:.*]] = arith.addi %[[VAL_7]], %[[VAL_9]] : index ! CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_10]]) : (!fir.box>, index) -> !fir.ref ! CHECK: %[[VAL_12:.*]] = fir.load %[[VAL_11]] : !fir.ref -! CHECK: %[[VAL_13:.*]] = hlfir.no_reassoc %[[VAL_12]] : i32 -! CHECK: hlfir.yield_element %[[VAL_13]] : i32 +! CHECK: hlfir.yield_element %[[VAL_12]] : i32 ! CHECK: } +! CHECK: %[[VAL_13:.*]] = hlfir.no_reassoc %[[VAL_6]] : !hlfir.expr<100xi32> ! CHECK: fir.call -! CHECK: hlfir.destroy %[[VAL_6]] +! CHECK: hlfir.destroy %[[VAL_13]]