diff --git a/flang/include/flang/Optimizer/HLFIR/Passes.h b/flang/include/flang/Optimizer/HLFIR/Passes.h --- a/flang/include/flang/Optimizer/HLFIR/Passes.h +++ b/flang/include/flang/Optimizer/HLFIR/Passes.h @@ -28,6 +28,7 @@ std::unique_ptr createSimplifyHLFIRIntrinsicsPass(); std::unique_ptr createInlineElementalsPass(); std::unique_ptr createLowerHLFIROrderedAssignmentsPass(); +std::unique_ptr createOptimizedBufferizationPass(); #define GEN_PASS_REGISTRATION #include "flang/Optimizer/HLFIR/Passes.h.inc" diff --git a/flang/include/flang/Optimizer/HLFIR/Passes.td b/flang/include/flang/Optimizer/HLFIR/Passes.td --- a/flang/include/flang/Optimizer/HLFIR/Passes.td +++ b/flang/include/flang/Optimizer/HLFIR/Passes.td @@ -20,6 +20,11 @@ let constructor = "hlfir::createBufferizeHLFIRPass()"; } +def OptimizedBufferization : Pass<"opt-bufferization", "::mlir::func::FuncOp"> { + let summary = "Special cases for hlfir.expr bufferization where we can avoid a temporary which would be created by the generic bufferization pass"; + let constructor = "hlfir::createOptimizedBufferizationPass()"; +} + def LowerHLFIRIntrinsics : Pass<"lower-hlfir-intrinsics", "::mlir::ModuleOp"> { let summary = "Lower HLFIR transformational intrinsic operations"; let constructor = "hlfir::createLowerHLFIRIntrinsicsPass()"; diff --git a/flang/include/flang/Tools/CLOptions.inc b/flang/include/flang/Tools/CLOptions.inc --- a/flang/include/flang/Tools/CLOptions.inc +++ b/flang/include/flang/Tools/CLOptions.inc @@ -243,6 +243,11 @@ pm.addPass(hlfir::createSimplifyHLFIRIntrinsicsPass()); } pm.addPass(hlfir::createInlineElementalsPass()); + if (optLevel.isOptimizingForSpeed()) { + addCanonicalizerPassWithoutRegionSimplification(pm); + pm.addPass(mlir::createCSEPass()); + pm.addPass(hlfir::createOptimizedBufferizationPass()); + } pm.addPass(hlfir::createLowerHLFIROrderedAssignmentsPass()); pm.addPass(hlfir::createLowerHLFIRIntrinsicsPass()); pm.addPass(hlfir::createBufferizeHLFIRPass()); diff --git a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt --- a/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt +++ b/flang/lib/Optimizer/HLFIR/Transforms/CMakeLists.txt @@ -8,6 +8,7 @@ LowerHLFIROrderedAssignments.cpp ScheduleOrderedAssignments.cpp SimplifyHLFIRIntrinsics.cpp + OptimizedBufferization.cpp DEPENDS FIRDialect diff --git a/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp new file mode 100644 --- /dev/null +++ b/flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp @@ -0,0 +1,311 @@ +//===- OptimizedBufferization.cpp - special cases for bufferization -------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// In some special cases we can bufferize hlfir expressions in a more optimal +// way so as to avoid creating temporaries. This pass handles these. It should +// be run before the catch-all bufferization pass. +// +// This requires constant subexpression elimination to have already been run. +//===----------------------------------------------------------------------===// + +#include "flang/Optimizer/Analysis/AliasAnalysis.h" +#include "flang/Optimizer/Builder/FIRBuilder.h" +#include "flang/Optimizer/Builder/HLFIRTools.h" +#include "flang/Optimizer/Builder/Todo.h" +#include "flang/Optimizer/Dialect/FIRType.h" +#include "flang/Optimizer/HLFIR/HLFIRDialect.h" +#include "flang/Optimizer/HLFIR/HLFIROps.h" +#include "flang/Optimizer/HLFIR/Passes.h" +#include "mlir/Dialect/Func/IR/FuncOps.h" +#include "mlir/IR/Dominance.h" +#include "mlir/IR/PatternMatch.h" +#include "mlir/Interfaces/SideEffectInterfaces.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Support/LLVM.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" +#include "llvm/ADT/TypeSwitch.h" +#include +#include +#include + +namespace hlfir { +#define GEN_PASS_DEF_OPTIMIZEDBUFFERIZATION +#include "flang/Optimizer/HLFIR/Passes.h.inc" +} // namespace hlfir + +#define DEBUG_TYPE "opt-bufferization" + +namespace { + +/// This transformation should match in place modification of arrays. +/// It should match code of the form +/// %array = some.operation // array has shape %shape +/// %expr = hlfir.elemental %shape : [...] { +/// bb0(%arg0: index) +/// %0 = hlfir.designate %array(%arg0) +/// [...] // no other reads or writes to %array +/// hlfir.yield_element %element +/// } +/// hlfir.assign %expr to %array +/// hlfir.destroy %expr +/// +/// In this case, it is safe to turn the elemental into a do loop and modify +/// elements of %array in place without creating an extra temporary for the +/// elemental. Note that %array should dominate %expr, with the same value +/// used for the assignment. This indicates that CSE was able to prove there +/// were no modifications to the array between %array and the assignment +/// (including during the elemental). We must check that there are no reads +/// from the array at indexes which might conflict with the assignment. For +/// now we will keep that strict and say that all reads must be at the +/// elemental index (it is probably safe to read from higher indices if +/// lowering to an ordered loop). +class ElementalAssignBufferization + : public mlir::OpRewritePattern { +private: + struct MatchInfo { + mlir::Value array; + hlfir::AssignOp assign; + hlfir::DestroyOp destroy; + }; + /// determines if the transformation can be applied to this elemental + static std::optional findMatch(hlfir::ElementalOp elemental); + +public: + using mlir::OpRewritePattern::OpRewritePattern; + + mlir::LogicalResult + matchAndRewrite(hlfir::ElementalOp elemental, + mlir::PatternRewriter &rewriter) const override; +}; + +std::optional +ElementalAssignBufferization::findMatch(hlfir::ElementalOp elemental) { + mlir::Operation::user_range users = elemental->getUsers(); + // the only uses of the elemental should be the assignment and the destroy + if (std::distance(users.begin(), users.end()) != 2) + return std::nullopt; + + MatchInfo match; + for (mlir::Operation *user : users) + mlir::TypeSwitch(user) + .Case([&](hlfir::AssignOp op) { match.assign = op; }) + .Case([&](hlfir::DestroyOp op) { match.destroy = op; }); + + if (!match.assign || !match.destroy) + return std::nullopt; + + // the array is what the elemental is assigned into + // TODO: this could be extended to also allow hlfir.expr by first bufferizing + // the incoming expression + match.array = match.assign.getLhs(); + mlir::Type arrayType = mlir::dyn_cast( + fir::unwrapPassByRefType(match.array.getType())); + if (!arrayType) + return std::nullopt; + + // require that the array elements are trivial + // TODO: this is just to make the pass easier to think about. Not an inherent + // limitation + mlir::Type eleTy = hlfir::getFortranElementType(arrayType); + if (!fir::isa_trivial(eleTy)) { + return std::nullopt; + } + + // does the array dominate the elmemental? + mlir::DominanceInfo domInfo; + // the array value can have no defining op if it is a block argument + if (match.array.getDefiningOp()) + if (!domInfo.properlyDominates(match.array.getDefiningOp(), elemental)) + return std::nullopt; + + // the array must have the same shape as the elemental. CSE should have + // deduplicated the fir.shape operations where they are provably the same + // so we just have to check for the same ssa value + // TODO: add more ways of getting the shape of the array + mlir::Value arrayShape; + if (match.array.getDefiningOp()) + arrayShape = + mlir::TypeSwitch( + match.array.getDefiningOp()) + .Case([](hlfir::DesignateOp designate) { + return designate.getShape(); + }) + .Case([](hlfir::DeclareOp declare) { return declare.getShape(); }) + .Default([](mlir::Operation *) { return mlir::Value{}; }); + if (!arrayShape) { + LLVM_DEBUG(llvm::dbgs() << "Can't get shape of " << match.array << " at " + << elemental->getLoc() << "\n"); + return std::nullopt; + } + if (arrayShape != elemental.getShape()) + return std::nullopt; + + // check that there are no reads from the array other than at the elemental + // index + for (mlir::Operation *user : match.array.getUsers()) { + if (user == match.assign) + continue; + if (domInfo.properlyDominates(user, elemental)) + // this use comes before the elemental so we don't care + continue; + if (domInfo.properlyDominates(match.assign, user)) + // this use comes after the assign so we don't care + continue; + if (user->getParentOp() == elemental) { + // use inside of the elemental. The only thing that is okay is a + // hlfir.designate using the elemental index + hlfir::DesignateOp designate = mlir::dyn_cast(user); + if (!designate) { + LLVM_DEBUG(llvm::dbgs() << "Non-designate use of match.array: " << *user + << " for " << elemental.getLoc() << "\n"); + return std::nullopt; + } + auto indices = designate.getIndices(); + auto elementalIndices = elemental.getIndices(); + if (indices.size() != elementalIndices.size()) { + LLVM_DEBUG(llvm::dbgs() << "Mismatched designate indices: " << designate + << " for " << elemental.getLoc() << "\n"); + return std::nullopt; + } + if (!std::equal(indices.begin(), indices.end(), elementalIndices.begin(), + elementalIndices.end())) { + LLVM_DEBUG(llvm::dbgs() << "Mismatched designate indices: " << designate + << " for " << elemental.getLoc() << "\n"); + return std::nullopt; + } + continue; + } + return std::nullopt; + } + + fir::AliasAnalysis aliasAnalysis; + // check for other memory accesses inside the elemental body + for (mlir::Operation &op : *elemental.getBody()) { + std::optional> + effects = mlir::getEffectsRecursively(&op); + // if we can't get effects for that operation then assume the worst case + if (!effects) { + LLVM_DEBUG(llvm::dbgs() << "Unknown effects for " << op << " for " + << elemental.getLoc() << "\n"); + return std::nullopt; + } + + for (const mlir::MemoryEffects::EffectInstance &effect : *effects) { + bool error = + mlir::TypeSwitch( + effect.getEffect()) + .Case([](auto _) { return false; }) + .Case([](auto _) { return false; }) + // are there any write effects which might alias with the array? + .Case([&](auto write) -> bool { + LLVM_DEBUG(llvm::dbgs() << "Found write in " << op << " for " + << elemental.getLoc() << "\n"); + return !aliasAnalysis.alias(match.array, effect.getValue()) + .isNo(); + }) + // don't allow aliased reads + .Case([&](auto read) -> bool { + mlir::Value val = effect.getValue(); + if (val == match.array) + // already checked all uses of array + return false; + + // allow: + // %ref = hlfir.designate %array(%index) + // %val = fir.load %ref + if (auto designate = val.getDefiningOp()) { + // already checked all uses of array + if (designate.getMemref() == match.array) + return false; + + // if the designate is into an array that definately doesn't + // alias match.array, then this should be safe. Alias analysis + // doesn't currently follow fir.load or understand + // hlfir.designate so it can't do this itself + if (aliasAnalysis.alias(match.array, designate.getMemref()) + .isNo()) + return false; + } + + LLVM_DEBUG(llvm::dbgs() << "Found read in " << op << " for " + << elemental.getLoc() << "\n"); + return !aliasAnalysis.alias(match.array, val).isNo(); + }) + .Default([](auto _) { + llvm_unreachable("Unknown memory effect"); + return true; + }); + if (error) + return std::nullopt; + } + } + + return match; +} + +mlir::LogicalResult ElementalAssignBufferization::matchAndRewrite( + hlfir::ElementalOp elemental, mlir::PatternRewriter &rewriter) const { + std::optional match = findMatch(elemental); + if (!match) + return rewriter.notifyMatchFailure( + elemental, "cannot prove safety of ElementalAssignBufferization"); + + mlir::Location loc = elemental->getLoc(); + fir::FirOpBuilder builder(rewriter, elemental.getOperation()); + auto extents = hlfir::getIndexExtents(loc, builder, elemental.getShape()); + + // Generate a loop nest looping around the hlfir.elemental shape and clone + // hlfir.elemental region inside the inner loop + hlfir::LoopNest loopNest = + hlfir::genLoopNest(loc, builder, extents, !elemental.isOrdered()); + builder.setInsertionPointToStart(loopNest.innerLoop.getBody()); + auto yield = hlfir::inlineElementalOp(loc, builder, elemental, + loopNest.oneBasedIndices); + hlfir::Entity elementValue{yield.getElementValue()}; + rewriter.eraseOp(yield); + + // Assign the element value to the array element for this iteration. + auto arrayElement = hlfir::getElementAt( + loc, builder, hlfir::Entity{match->array}, loopNest.oneBasedIndices); + builder.create( + loc, elementValue, arrayElement, /*realloc=*/false, + /*keep_lhs_length_if_realloc=*/false, /*temporary_lhs=*/true); + + rewriter.eraseOp(match->assign); + rewriter.eraseOp(match->destroy); + rewriter.eraseOp(elemental); + return mlir::success(); +} + +class OptimizedBufferizationPass + : public hlfir::impl::OptimizedBufferizationBase< + OptimizedBufferizationPass> { +public: + void runOnOperation() override { + mlir::func::FuncOp func = getOperation(); + mlir::MLIRContext *context = &getContext(); + + mlir::GreedyRewriteConfig config; + // Prevent the pattern driver from merging blocks + config.enableRegionSimplification = false; + + mlir::RewritePatternSet patterns(context); + patterns.insert(context); + + if (mlir::failed(mlir::applyPatternsAndFoldGreedily( + func, std::move(patterns), config))) { + mlir::emitError(func.getLoc(), + "failure in HLFIR optimized bufferization"); + signalPassFailure(); + } + } +}; +} // namespace + +std::unique_ptr hlfir::createOptimizedBufferizationPass() { + return std::make_unique(); +} diff --git a/flang/test/Fir/basic-program.fir b/flang/test/Fir/basic-program.fir --- a/flang/test/Fir/basic-program.fir +++ b/flang/test/Fir/basic-program.fir @@ -20,6 +20,12 @@ // PASSES-NEXT: 'func.func' Pipeline // PASSES-NEXT: SimplifyHLFIRIntrinsics // PASSES-NEXT: InlineElementals +// PASSES-NEXT: Canonicalizer +// PASSES-NEXT: CSE +// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd +// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd +// PASSES-NEXT: 'func.func' Pipeline +// PASSES-NEXT: OptimizedBufferization // PASSES-NEXT: LowerHLFIROrderedAssignments // PASSES-NEXT: LowerHLFIRIntrinsics // PASSES-NEXT: BufferizeHLFIR diff --git a/flang/test/HLFIR/opt-bufferization.fir b/flang/test/HLFIR/opt-bufferization.fir new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/opt-bufferization.fir @@ -0,0 +1,409 @@ +// RUN: fir-opt --opt-bufferization %s | FileCheck %s + +// simplified example +func.func @simple(%arg: !fir.ref>) { + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return +} +// CHECK-LABEL: func.func @simple( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_3:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_4:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_4]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: fir.do_loop %[[VAL_6:.*]] = %[[VAL_1]] to %[[VAL_2]] step %[[VAL_1]] unordered { +// CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref +// CHECK: %[[VAL_9:.*]] = arith.subi %[[VAL_8]], %[[VAL_3]] : i32 +// CHECK: %[[VAL_10:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: hlfir.assign %[[VAL_9]] to %[[VAL_10]] temporary_lhs : i32, !fir.ref +// CHECK: } +// CHECK: return +// CHECK: } + +// check we support reads that don't alias the transformed array +func.func @read_no_alias(%arg: !fir.ref>, %arg1: !fir.ref>) { + %c42 = arith.constant 42 : index + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %other:2 = hlfir.declare %arg1(%shape) {uniq_name = "other"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %other_ref = hlfir.designate %other#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %other_val = fir.load %other_ref : !fir.ref + %sub = arith.subi %val, %other_val : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return +} +// CHECK-LABEL: func.func @read_no_alias( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref>) { +// CHECK: %[[VAL_2:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_3:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_4:.*]] = fir.shape %[[VAL_3]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_4]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_1]](%[[VAL_4]]) {uniq_name = "other"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: fir.do_loop %[[VAL_7:.*]] = %[[VAL_2]] to %[[VAL_3]] step %[[VAL_2]] unordered { +// CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_7]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_9:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_7]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_8]] : !fir.ref +// CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_9]] : !fir.ref +// CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_10]], %[[VAL_11]] : i32 +// CHECK: %[[VAL_13:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_7]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: hlfir.assign %[[VAL_12]] to %[[VAL_13]] temporary_lhs : i32, !fir.ref +// CHECK: } +// CHECK: return +// CHECK: } + + +// check we don't transform when there is another use of the elemental expr +func.func @two_uses(%arg: !fir.ref>) -> i32 { + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + %bad = hlfir.apply %elemental, %c42 : (!hlfir.expr<42xi32>, index) -> i32 + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return %bad : i32 +} +// CHECK-LABEL: func.func @two_uses( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) -> i32 { +// CHECK: %[[VAL_1:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_3]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_5:.*]] = hlfir.elemental %[[VAL_3]] unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { +// CHECK: ^bb0(%[[VAL_6:.*]]: index): +// CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref +// CHECK: %[[VAL_9:.*]] = arith.subi %[[VAL_8]], %[[VAL_2]] : i32 +// CHECK: hlfir.yield_element %[[VAL_9]] : i32 +// CHECK: } +// CHECK: hlfir.assign %[[VAL_10:.*]] to %[[VAL_4]]#0 : !hlfir.expr<42xi32>, !fir.ref> +// CHECK: %[[VAL_11:.*]] = hlfir.apply %[[VAL_10]], %[[VAL_1]] : (!hlfir.expr<42xi32>, index) -> i32 +// CHECK: hlfir.destroy %[[VAL_10]] : !hlfir.expr<42xi32> +// CHECK: return %[[VAL_11]] : i32 +// CHECK: } + +// two dimensional array +func.func @two_dimensional(%arg: !fir.ref>) { + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42, %c42 : (index, index) -> !fir.shape<2> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<2>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<2>) -> !hlfir.expr<42x42xi32> { + ^bb0(%i: index, %j: index): + %ref = hlfir.designate %array#0 (%i, %j) : (!fir.ref>, index, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42x42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42x42xi32> + return +} +// CHECK-LABEL: func.func @two_dimensional( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_3:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_4:.*]] = fir.shape %[[VAL_2]], %[[VAL_2]] : (index, index) -> !fir.shape<2> +// CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_4]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<2>) -> (!fir.ref>, !fir.ref>) +// CHECK: fir.do_loop %[[VAL_6:.*]] = %[[VAL_1]] to %[[VAL_2]] step %[[VAL_1]] unordered { +// CHECK: fir.do_loop %[[VAL_7:.*]] = %[[VAL_1]] to %[[VAL_2]] step %[[VAL_1]] unordered { +// CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_7]], %[[VAL_6]]) : (!fir.ref>, index, index) -> !fir.ref +// CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_8]] : !fir.ref +// CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_3]] : i32 +// CHECK: %[[VAL_11:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_7]], %[[VAL_6]]) : (!fir.ref>, index, index) -> !fir.ref +// CHECK: hlfir.assign %[[VAL_10]] to %[[VAL_11]] temporary_lhs : i32, !fir.ref +// CHECK: } +// CHECK: } +// CHECK: return +// CHECK: } + +// don't transform when elements are accessessed out of order (e.g. transposed) +func.func @transposed(%arg: !fir.ref>) { + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42, %c42 : (index, index) -> !fir.shape<2> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<2>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<2>) -> !hlfir.expr<42x42xi32> { + ^bb0(%i: index, %j: index): + %ref = hlfir.designate %array#0 (%j, %i) : (!fir.ref>, index, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42x42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42x42xi32> + return +} +// CHECK-LABEL: func.func @transposed( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_1]], %[[VAL_1]] : (index, index) -> !fir.shape<2> +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_3]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<2>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_5:.*]] = hlfir.elemental %[[VAL_3]] unordered : (!fir.shape<2>) -> !hlfir.expr<42x42xi32> { +// CHECK: ^bb0(%[[VAL_6:.*]]: index, %[[VAL_7:.*]]: index): +// CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_7]], %[[VAL_6]]) : (!fir.ref>, index, index) -> !fir.ref +// CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_8]] : !fir.ref +// CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_2]] : i32 +// CHECK: hlfir.yield_element %[[VAL_10]] : i32 +// CHECK: } +// CHECK: hlfir.assign %[[VAL_11:.*]] to %[[VAL_4]]#0 : !hlfir.expr<42x42xi32>, !fir.ref> +// CHECK: hlfir.destroy %[[VAL_11]] : !hlfir.expr<42x42xi32> +// CHECK: return +// CHECK: } + +// don't transform when there's an operation with unknown effects +func.func @unknown(%arg: !fir.ref>) { + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + %res = fir.call @impure(%sub) : (i32) -> i32 + hlfir.yield_element %res : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return +} +// CHECK-LABEL: func.func @unknown( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_2:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_3]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_5:.*]] = hlfir.elemental %[[VAL_3]] unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { +// CHECK: ^bb0(%[[VAL_6:.*]]: index): +// CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref +// CHECK: %[[VAL_9:.*]] = arith.subi %[[VAL_8]], %[[VAL_2]] : i32 +// CHECK: %[[VAL_10:.*]] = fir.call @impure(%[[VAL_9]]) : (i32) -> i32 +// CHECK: hlfir.yield_element %[[VAL_10]] : i32 +// CHECK: } +// CHECK: hlfir.assign %[[VAL_11:.*]] to %[[VAL_4]]#0 : !hlfir.expr<42xi32>, !fir.ref> +// CHECK: hlfir.destroy %[[VAL_11]] : !hlfir.expr<42xi32> +// CHECK: return +// CHECK: } + +// don't transform when there's an operation with write effects +func.func @write(%arg: !fir.ref>, %arg1: !fir.ref>) { + %alloc = fir.alloca i32 + %c42 = arith.constant 42 : index + %c1_i32 = arith.constant 1 : i32 + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %array2:2 = hlfir.declare %arg1(%shape) {uniq_name = "array2"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + hlfir.assign %array2#0 to %array#0 : !fir.ref>, !fir.ref> + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %sub = arith.subi %val, %c1_i32 : i32 + fir.store %sub to %alloc : !fir.ref + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return +} +// CHECK-LABEL: func.func @write( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>, +// CHECK-SAME: %[[ARG_1:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 1 : i32 +// CHECK: %[[VAL_2:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_3:.*]] = fir.alloca i32 +// CHECK: %[[VAL_4:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_5:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_4]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_5B:.*]]:2 = hlfir.declare %[[ARG_1]](%[[VAL_4]]) {uniq_name = "array2"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_6:.*]] = hlfir.elemental %[[VAL_4]] unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { +// CHECK: ^bb0(%[[VAL_7:.*]]: index): +// CHECK: hlfir.assign %[[VAL_5B]]#0 to %[[VAL_5]]#0 : !fir.ref>, !fir.ref> +// CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_5]]#0 (%[[VAL_7]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_8]] : !fir.ref +// CHECK: %[[VAL_10:.*]] = arith.subi %[[VAL_9]], %[[VAL_1]] : i32 +// CHECK: fir.store %[[VAL_10]] to %[[VAL_3]] : !fir.ref +// CHECK: hlfir.yield_element %[[VAL_10]] : i32 +// CHECK: } +// CHECK: hlfir.assign %[[VAL_11:.*]] to %[[VAL_5]]#0 : !hlfir.expr<42xi32>, !fir.ref> +// CHECK: hlfir.destroy %[[VAL_11]] : !hlfir.expr<42xi32> +// CHECK: return +// CHECK: } + +// don't transform when there is an aliasing read +func.func @readAlias(%arg: !fir.ref>) { + %c42 = arith.constant 42 : index + %shape = fir.shape %c42 : (index) -> !fir.shape<1> + %array:2 = hlfir.declare %arg(%shape) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %arrayDup:2 = hlfir.declare %arg(%shape) {uniq_name = "arrayDup"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) + %elemental = hlfir.elemental %shape unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { + ^bb0(%i: index): + %ref = hlfir.designate %array#0 (%i) : (!fir.ref>, index) -> !fir.ref + %refDup = hlfir.designate %arrayDup#0 (%i) : (!fir.ref>, index) -> !fir.ref + %val = fir.load %ref : !fir.ref + %valDup = fir.load %refDup : !fir.ref + %sub = arith.subi %val, %valDup : i32 + hlfir.yield_element %sub : i32 + } + hlfir.assign %elemental to %array#0 : !hlfir.expr<42xi32>, !fir.ref> + hlfir.destroy %elemental : !hlfir.expr<42xi32> + return +} +// CHECK-LABEL: func.func @readAlias( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { +// CHECK: %[[VAL_1:.*]] = arith.constant 42 : index +// CHECK: %[[VAL_2:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_2]]) {uniq_name = "array"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_2]]) {uniq_name = "arrayDup"} : (!fir.ref>, !fir.shape<1>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_5:.*]] = hlfir.elemental %[[VAL_2]] unordered : (!fir.shape<1>) -> !hlfir.expr<42xi32> { +// CHECK: ^bb0(%[[VAL_6:.*]]: index): +// CHECK: %[[VAL_7:.*]] = hlfir.designate %[[VAL_3]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_8:.*]] = hlfir.designate %[[VAL_4]]#0 (%[[VAL_6]]) : (!fir.ref>, index) -> !fir.ref +// CHECK: %[[VAL_9:.*]] = fir.load %[[VAL_7]] : !fir.ref +// CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_8]] : !fir.ref +// CHECK: %[[VAL_11:.*]] = arith.subi %[[VAL_9]], %[[VAL_10]] : i32 +// CHECK: hlfir.yield_element %[[VAL_11]] : i32 +// CHECK: } +// CHECK: hlfir.assign %[[VAL_12:.*]] to %[[VAL_3]]#0 : !hlfir.expr<42xi32>, !fir.ref> +// CHECK: hlfir.destroy %[[VAL_12]] : !hlfir.expr<42xi32> +// CHECK: return +// CHECK: } + +fir.global @_QMmEblock : !fir.array<9x9x9xi32> { + %0 = fir.undefined !fir.array<9x9x9xi32> + fir.has_value %0 : !fir.array<9x9x9xi32> +} +fir.global @_QMmECr constant : i32 { + %c9_i32 = arith.constant 9 : i32 + fir.has_value %c9_i32 : i32 +} + +// does it work for the intended case? +func.func @_QMmPrepro(%arg0: !fir.ref {fir.bindc_name = "imin"}, %arg1: !fir.ref {fir.bindc_name = "imax"}, %arg2: !fir.ref {fir.bindc_name = "row"}) { + %c10_i32 = arith.constant 10 : i32 + %c8 = arith.constant 8 : index + %c2 = arith.constant 2 : index + %c1 = arith.constant 1 : index + %c9 = arith.constant 9 : index + %0 = fir.address_of(@_QMmEblock) : !fir.ref> + %1 = fir.shape %c9, %c9, %c9 : (index, index, index) -> !fir.shape<3> + %2:2 = hlfir.declare %0(%1) {uniq_name = "_QMmEblock"} : (!fir.ref>, !fir.shape<3>) -> (!fir.ref>, !fir.ref>) + %3 = fir.address_of(@_QMmECr) : !fir.ref + %4:2 = hlfir.declare %3 {fortran_attrs = #fir.var_attrs, uniq_name = "_QMmECr"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %5 = fir.alloca i32 {bindc_name = "i1", uniq_name = "_QMmFreproEi1"} + %6:2 = hlfir.declare %5 {uniq_name = "_QMmFreproEi1"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %7:2 = hlfir.declare %arg1 {uniq_name = "_QMmFreproEimax"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %8:2 = hlfir.declare %arg0 {uniq_name = "_QMmFreproEimin"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %9:2 = hlfir.declare %arg2 {uniq_name = "_QMmFreproErow"} : (!fir.ref) -> (!fir.ref, !fir.ref) + %10 = fir.load %8#0 : !fir.ref + %11 = fir.convert %10 : (i32) -> index + %12 = fir.load %7#0 : !fir.ref + %13 = fir.convert %12 : (i32) -> index + %14 = fir.convert %11 : (index) -> i32 + %15:2 = fir.do_loop %arg3 = %11 to %13 step %c1 iter_args(%arg4 = %14) -> (index, i32) { + fir.store %arg4 to %6#1 : !fir.ref + %16 = fir.load %9#0 : !fir.ref + %17 = fir.convert %16 : (i32) -> i64 + %18 = fir.load %6#0 : !fir.ref + %19 = fir.convert %18 : (i32) -> i64 + %20 = fir.shape %c8 : (index) -> !fir.shape<1> + %21 = hlfir.designate %2#0 (%17, %c2:%c9:%c1, %19) shape %20 : (!fir.ref>, i64, index, index, index, i64, !fir.shape<1>) -> !fir.box> + %22 = hlfir.elemental %20 unordered : (!fir.shape<1>) -> !hlfir.expr<8xi32> { + ^bb0(%arg5: index): + %27 = hlfir.designate %21 (%arg5) : (!fir.box>, index) -> !fir.ref + %28 = fir.load %27 : !fir.ref + %29 = arith.subi %28, %c10_i32 : i32 + hlfir.yield_element %29 : i32 + } + hlfir.assign %22 to %21 : !hlfir.expr<8xi32>, !fir.box> + hlfir.destroy %22 : !hlfir.expr<8xi32> + %23 = arith.addi %arg3, %c1 : index + %24 = fir.convert %c1 : (index) -> i32 + %25 = fir.load %6#1 : !fir.ref + %26 = arith.addi %25, %24 : i32 + fir.result %23, %26 : index, i32 + } + fir.store %15#1 to %6#1 : !fir.ref + return +} +// CHECK-LABEL: func.func @_QMmPrepro( +// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref {fir.bindc_name = "imin"}, +// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref {fir.bindc_name = "imax"}, +// CHECK-SAME: %[[VAL_2:.*]]: !fir.ref {fir.bindc_name = "row"}) { +// CHECK: %[[VAL_3:.*]] = arith.constant 10 : i32 +// CHECK: %[[VAL_4:.*]] = arith.constant 8 : index +// CHECK: %[[VAL_5:.*]] = arith.constant 2 : index +// CHECK: %[[VAL_6:.*]] = arith.constant 1 : index +// CHECK: %[[VAL_7:.*]] = arith.constant 9 : index +// CHECK: %[[VAL_8:.*]] = fir.address_of(@_QMmEblock) : !fir.ref> +// CHECK: %[[VAL_9:.*]] = fir.shape %[[VAL_7]], %[[VAL_7]], %[[VAL_7]] : (index, index, index) -> !fir.shape<3> +// CHECK: %[[VAL_10:.*]]:2 = hlfir.declare %[[VAL_8]](%[[VAL_9]]) {uniq_name = "_QMmEblock"} : (!fir.ref>, !fir.shape<3>) -> (!fir.ref>, !fir.ref>) +// CHECK: %[[VAL_11:.*]] = fir.address_of(@_QMmECr) : !fir.ref +// CHECK: %[[VAL_12:.*]]:2 = hlfir.declare %[[VAL_11]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QMmECr"} : (!fir.ref) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_13:.*]] = fir.alloca i32 {bindc_name = "i1", uniq_name = "_QMmFreproEi1"} +// CHECK: %[[VAL_14:.*]]:2 = hlfir.declare %[[VAL_13]] {uniq_name = "_QMmFreproEi1"} : (!fir.ref) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_15:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = "_QMmFreproEimax"} : (!fir.ref) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_16:.*]]:2 = hlfir.declare %[[VAL_0]] {uniq_name = "_QMmFreproEimin"} : (!fir.ref) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_17:.*]]:2 = hlfir.declare %[[VAL_2]] {uniq_name = "_QMmFreproErow"} : (!fir.ref) -> (!fir.ref, !fir.ref) +// CHECK: %[[VAL_18:.*]] = fir.load %[[VAL_16]]#0 : !fir.ref +// CHECK: %[[VAL_19:.*]] = fir.convert %[[VAL_18]] : (i32) -> index +// CHECK: %[[VAL_20:.*]] = fir.load %[[VAL_15]]#0 : !fir.ref +// CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (i32) -> index +// CHECK: %[[VAL_22:.*]] = fir.convert %[[VAL_19]] : (index) -> i32 +// CHECK: %[[VAL_23:.*]]:2 = fir.do_loop %[[VAL_24:.*]] = %[[VAL_19]] to %[[VAL_21]] step %[[VAL_6]] iter_args(%[[VAL_25:.*]] = %[[VAL_22]]) -> (index, i32) { +// CHECK: fir.store %[[VAL_25]] to %[[VAL_14]]#1 : !fir.ref +// CHECK: %[[VAL_26:.*]] = fir.load %[[VAL_17]]#0 : !fir.ref +// CHECK: %[[VAL_27:.*]] = fir.convert %[[VAL_26]] : (i32) -> i64 +// CHECK: %[[VAL_28:.*]] = fir.load %[[VAL_14]]#0 : !fir.ref +// CHECK: %[[VAL_29:.*]] = fir.convert %[[VAL_28]] : (i32) -> i64 +// CHECK: %[[VAL_30:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1> +// CHECK: %[[VAL_31:.*]] = hlfir.designate %[[VAL_10]]#0 (%[[VAL_27]], %[[VAL_5]]:%[[VAL_7]]:%[[VAL_6]], %[[VAL_29]]) shape %[[VAL_30]] : (!fir.ref>, i64, index, index, index, i64, !fir.shape<1>) -> !fir.box> +// CHECK: fir.do_loop %[[VAL_32:.*]] = %[[VAL_6]] to %[[VAL_4]] step %[[VAL_6]] unordered { +// CHECK: %[[VAL_33:.*]] = hlfir.designate %[[VAL_31]] (%[[VAL_32]]) : (!fir.box>, index) -> !fir.ref +// CHECK: %[[VAL_34:.*]] = fir.load %[[VAL_33]] : !fir.ref +// CHECK: %[[VAL_35:.*]] = arith.subi %[[VAL_34]], %[[VAL_3]] : i32 +// CHECK: %[[VAL_36:.*]] = hlfir.designate %[[VAL_31]] (%[[VAL_32]]) : (!fir.box>, index) -> !fir.ref +// CHECK: hlfir.assign %[[VAL_35]] to %[[VAL_36]] temporary_lhs : i32, !fir.ref +// CHECK: } +// CHECK: %[[VAL_37:.*]] = arith.addi %[[VAL_24]], %[[VAL_6]] : index +// CHECK: %[[VAL_38:.*]] = fir.convert %[[VAL_6]] : (index) -> i32 +// CHECK: %[[VAL_39:.*]] = fir.load %[[VAL_14]]#1 : !fir.ref +// CHECK: %[[VAL_40:.*]] = arith.addi %[[VAL_39]], %[[VAL_38]] : i32 +// CHECK: fir.result %[[VAL_37]], %[[VAL_40]] : index, i32 +// CHECK: } +// CHECK: fir.store %[[VAL_41:.*]]#1 to %[[VAL_14]]#1 : !fir.ref +// CHECK: return +// CHECK: }