[flang-commits] [flang] [flang] Delete the array-value-copy pass (PR #212643)
Eugene Epshteyn via flang-commits
flang-commits at lists.llvm.org
Tue Jul 28 15:50:43 PDT 2026
https://github.com/eugeneepshteyn created https://github.com/llvm/llvm-project/pull/212643
Since the removal of the legacy expression lowering (#210385, #210621, #210639, #210873) nothing in flang produces the array-value operations this pass legalizes, and #211816 already removed it from the default pipeline. Delete the pass and the pieces that existed solely for it:
- ArrayValueCopy.cpp, its Passes.td definition, createArrayValueCopyPass, and the (duplicated) CMake source entries.
- Optimizer/Builder/Factory.h in its entirety: the pass was its only includer, and its contents (attrFortranArrayOffsets, getExtents/getOrigins on shape values, originateIndices) have no other users.
- The getTypeParams(fir::ArrayLoadOp) overload (sole caller was the pass) and the ArrayLoadOp forward declaration in BoxValue.h.
- The pass LIT tests (array-value-copy*.fir, array-copies-pointers.fir, array-modify.fir); stack-arrays-hlfir.f90 drops the --array-value-copy stage from its RUN pipeline.
- The pass-describing paragraphs in fstack-arrays.md and the stale ArrayValueCopy TODO entry in ParameterizedDerivedTypes.md.
The array-value operations themselves (fir.array_load and friends; not fir.array_coor) remain in the dialect and will be deleted in a follow-up.
Assisted-by: AI
>From 018cabde5fe5ecafbdb8a4fc96920761b7db7d04 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 28 Jul 2026 11:22:52 -0400
Subject: [PATCH] [flang] Delete the array-value-copy pass
Since the removal of the legacy expression lowering (#210385, #210621,
#210639, #210873) nothing in flang produces the array-value operations this
pass legalizes, and #211816 already removed it from the default pipeline.
Delete the pass and the pieces that existed solely for it:
- ArrayValueCopy.cpp, its Passes.td definition, createArrayValueCopyPass,
and the (duplicated) CMake source entries.
- Optimizer/Builder/Factory.h in its entirety: the pass was its only
includer, and its contents (attrFortranArrayOffsets, getExtents/getOrigins
on shape values, originateIndices) have no other users.
- The getTypeParams(fir::ArrayLoadOp) overload (sole caller was the pass)
and the ArrayLoadOp forward declaration in BoxValue.h.
- The pass LIT tests (array-value-copy*.fir, array-copies-pointers.fir,
array-modify.fir); stack-arrays-hlfir.f90 drops the --array-value-copy
stage from its RUN pipeline.
- The pass-describing paragraphs in fstack-arrays.md and the stale
ArrayValueCopy TODO entry in ParameterizedDerivedTypes.md.
The array-value operations themselves (fir.array_load and friends; not
fir.array_coor) remain in the dialect and will be deleted in a follow-up.
---
flang/docs/ParameterizedDerivedTypes.md | 1 -
flang/docs/fstack-arrays.md | 17 -
.../flang/Optimizer/Builder/BoxValue.h | 6 -
.../include/flang/Optimizer/Builder/Factory.h | 114 --
.../flang/Optimizer/Transforms/Passes.h | 2 -
.../flang/Optimizer/Transforms/Passes.td | 26 -
flang/lib/Optimizer/Builder/FIRBuilder.cpp | 9 -
.../Optimizer/Transforms/ArrayValueCopy.cpp | 1434 -----------------
flang/lib/Optimizer/Transforms/CMakeLists.txt | 2 -
flang/test/Fir/array-copies-pointers.fir | 290 ----
flang/test/Fir/array-modify.fir | 131 --
flang/test/Fir/array-value-copy-2.fir | 87 -
flang/test/Fir/array-value-copy-3.fir | 56 -
flang/test/Fir/array-value-copy-4.fir | 58 -
flang/test/Fir/array-value-copy-cam4.fir | 102 --
flang/test/Fir/array-value-copy.fir | 533 ------
flang/test/Transforms/stack-arrays-hlfir.f90 | 1 -
17 files changed, 2869 deletions(-)
delete mode 100644 flang/include/flang/Optimizer/Builder/Factory.h
delete mode 100644 flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
delete mode 100644 flang/test/Fir/array-copies-pointers.fir
delete mode 100644 flang/test/Fir/array-modify.fir
delete mode 100644 flang/test/Fir/array-value-copy-2.fir
delete mode 100644 flang/test/Fir/array-value-copy-3.fir
delete mode 100644 flang/test/Fir/array-value-copy-4.fir
delete mode 100644 flang/test/Fir/array-value-copy-cam4.fir
delete mode 100644 flang/test/Fir/array-value-copy.fir
diff --git a/flang/docs/ParameterizedDerivedTypes.md b/flang/docs/ParameterizedDerivedTypes.md
index be0cec7b55114..398bb9243a1f5 100644
--- a/flang/docs/ParameterizedDerivedTypes.md
+++ b/flang/docs/ParameterizedDerivedTypes.md
@@ -1009,7 +1009,6 @@ Current list of TODOs in code generation:
Current list of TODOs in optimizations:
-- `flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp:1007` not yet implemented: unhandled dynamic type parameters
---
diff --git a/flang/docs/fstack-arrays.md b/flang/docs/fstack-arrays.md
index 8eaa5d3f70e67..9f3f385b4834a 100644
--- a/flang/docs/fstack-arrays.md
+++ b/flang/docs/fstack-arrays.md
@@ -24,12 +24,6 @@ be always converted to stack allocations. It is much easier to swap heap
allocations for stack allocations when they are first generated because the
lifetime information is conveniently available.
-For example, to rewrite the heap allocation in the `array-value-copy` pass with
-a stack allocation using the first approach would require analysis to ensure
-that the heap allocation is always freed before the function returns. This is
-much more complex than never generating a heap allocation (and free) in the
-first place (the second approach).
-
The plan is to take the more complex first approach so that newly added changes
to lowering code do not need to be made to support the stack arrays option. The
general problem of determining heap allocation lifetimes can be simplified in
@@ -47,7 +41,6 @@ understand the situations in which Flang will generate heap allocations.
### Known Heap Array Allocations
Flang allocates most arrays on the stack by default, but there are a few cases
where temporary arrays are allocated on the heap:
-- `flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp`
- `flang/lib/Optimizer/Transforms/MemoryAllocation.cpp`
- `flang/lib/Lower/IntrinsicCall.cpp`
- `flang/lib/Lower/ConvertVariable.cpp`
@@ -56,16 +49,6 @@ Lowering code is being updated and in the future, temporaries for expressions
will be created in the HLFIR bufferization pass in
`flang/lib/Optimizer/HLFIR/Trnasforms/BufferizeHLFIR.cpp`.
-#### `ArrayValueCopy.cpp`
-Memory is allocated for a temporary array in `allocateArrayTemp()`. This
-temporary array is used to ensure that assignments of one array to itself
-produce the required value. E.g.
-
-```
-integer, dimension(5), intent(inout) :: x
-x(3,4) = x(1,2)
-```
-
#### `MemoryAllocation.cpp`
The default options for the Memory Allocation transformation ensure that no
array allocations, no matter how large, are moved from the stack to the heap.
diff --git a/flang/include/flang/Optimizer/Builder/BoxValue.h b/flang/include/flang/Optimizer/Builder/BoxValue.h
index 22e8d39a69b6d..417e26c46855c 100644
--- a/flang/include/flang/Optimizer/Builder/BoxValue.h
+++ b/flang/include/flang/Optimizer/Builder/BoxValue.h
@@ -25,7 +25,6 @@
namespace fir {
class FirOpBuilder;
-class ArrayLoadOp;
class ArrayBoxValue;
class BoxValue;
@@ -454,11 +453,6 @@ llvm::SmallVector<mlir::Value> getTypeParams(mlir::Location loc,
FirOpBuilder &builder,
const ExtendedValue &exv);
-/// Specialization of get type parameters for an ArrayLoadOp. An array load must
-/// either have all type parameters given as arguments or be a boxed value.
-llvm::SmallVector<mlir::Value>
-getTypeParams(mlir::Location loc, FirOpBuilder &builder, ArrayLoadOp load);
-
// The generalized function to get a vector of extents is
/// Get extents from \p box. For fir::BoxValue and
/// fir::MutableBoxValue, this will generate code to read the extents.
diff --git a/flang/include/flang/Optimizer/Builder/Factory.h b/flang/include/flang/Optimizer/Builder/Factory.h
deleted file mode 100644
index 8b6eba95c9671..0000000000000
--- a/flang/include/flang/Optimizer/Builder/Factory.h
+++ /dev/null
@@ -1,114 +0,0 @@
-//===-- Optimizer/Builder/Factory.h -----------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-//
-// Templates to generate more complex code patterns in transformation passes.
-// In transformation passes, front-end information such as is available in
-// lowering is not available.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef FORTRAN_OPTIMIZER_BUILDER_FACTORY_H
-#define FORTRAN_OPTIMIZER_BUILDER_FACTORY_H
-
-#include "flang/Optimizer/Dialect/FIROps.h"
-#include "flang/Optimizer/Dialect/FIRType.h"
-
-namespace mlir {
-class Location;
-class Value;
-} // namespace mlir
-
-namespace fir::factory {
-
-constexpr llvm::StringRef attrFortranArrayOffsets() {
- return "Fortran.offsets";
-}
-
-/// Get extents from fir.shape/fir.shape_shift op. Empty result if
-/// \p shapeVal is empty or is a fir.shift.
-inline llvm::SmallVector<mlir::Value> getExtents(mlir::Value shapeVal) {
- if (shapeVal)
- if (auto *shapeOp = shapeVal.getDefiningOp()) {
- if (auto shOp = mlir::dyn_cast<fir::ShapeOp>(shapeOp)) {
- auto operands = shOp.getExtents();
- return {operands.begin(), operands.end()};
- }
- if (auto shOp = mlir::dyn_cast<fir::ShapeShiftOp>(shapeOp)) {
- auto operands = shOp.getExtents();
- return {operands.begin(), operands.end()};
- }
- }
- return {};
-}
-
-/// Get origins from fir.shape_shift/fir.shift op. Empty result if
-/// \p shapeVal is empty or is a fir.shape.
-inline llvm::SmallVector<mlir::Value> getOrigins(mlir::Value shapeVal) {
- if (shapeVal)
- if (auto *shapeOp = shapeVal.getDefiningOp()) {
- if (auto shOp = mlir::dyn_cast<fir::ShapeShiftOp>(shapeOp)) {
- auto operands = shOp.getOrigins();
- return {operands.begin(), operands.end()};
- }
- if (auto shOp = mlir::dyn_cast<fir::ShiftOp>(shapeOp)) {
- auto operands = shOp.getOrigins();
- return {operands.begin(), operands.end()};
- }
- }
- return {};
-}
-
-/// Convert the normalized indices on array_fetch and array_update to the
-/// dynamic (and non-zero) origin required by array_coor.
-/// Do not adjust any trailing components in the path as they specify a
-/// particular path into the array value and must already correspond to the
-/// structure of an element.
-template <typename B>
-llvm::SmallVector<mlir::Value>
-originateIndices(mlir::Location loc, B &builder, mlir::Type memTy,
- mlir::Value shapeVal, mlir::ValueRange indices) {
- llvm::SmallVector<mlir::Value> result;
- auto origins = getOrigins(shapeVal);
- if (origins.empty()) {
- assert(!shapeVal || mlir::isa<fir::ShapeOp>(shapeVal.getDefiningOp()));
- auto ty = fir::dyn_cast_ptrOrBoxEleTy(memTy);
- assert(ty && mlir::isa<fir::SequenceType>(ty));
- auto seqTy = mlir::cast<fir::SequenceType>(ty);
- auto one = mlir::arith::ConstantIndexOp::create(builder, loc, 1);
- const auto dimension = seqTy.getDimension();
- if (shapeVal) {
- assert(dimension == mlir::cast<fir::ShapeOp>(shapeVal.getDefiningOp())
- .getType()
- .getRank());
- }
- for (auto i : llvm::enumerate(indices)) {
- if (i.index() < dimension) {
- assert(fir::isa_integer(i.value().getType()));
- result.push_back(
- mlir::arith::AddIOp::create(builder, loc, i.value(), one));
- } else {
- result.push_back(i.value());
- }
- }
- return result;
- }
- const auto dimension = origins.size();
- unsigned origOff = 0;
- for (auto i : llvm::enumerate(indices)) {
- if (i.index() < dimension)
- result.push_back(mlir::arith::AddIOp::create(builder, loc, i.value(),
- origins[origOff++]));
- else
- result.push_back(i.value());
- }
- return result;
-}
-
-} // namespace fir::factory
-
-#endif // FORTRAN_OPTIMIZER_BUILDER_FACTORY_H
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.h b/flang/include/flang/Optimizer/Transforms/Passes.h
index 0dc6182187aa3..a655c6919a7f7 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.h
+++ b/flang/include/flang/Optimizer/Transforms/Passes.h
@@ -42,8 +42,6 @@ enum class LICMNestedHoistingMode {
#include "flang/Optimizer/Transforms/Passes.h.inc"
std::unique_ptr<mlir::Pass> createAffineDemotionPass();
-std::unique_ptr<mlir::Pass>
-createArrayValueCopyPass(fir::ArrayValueCopyOptions options = {});
std::unique_ptr<mlir::Pass> createMemDataFlowOptPass();
std::unique_ptr<mlir::Pass> createPromoteToAffinePass();
std::unique_ptr<mlir::Pass>
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index 8573c0e4f3f00..3d95f154b0473 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -110,32 +110,6 @@ def AnnotateConstantOperands : Pass<"annotate-constant"> {
let dependentDialects = [ "fir::FIROpsDialect" ];
}
-def ArrayValueCopy : Pass<"array-value-copy", "::mlir::func::FuncOp"> {
- let summary = "Convert array value operations to memory operations.";
- let description = [{
- Transform the set of array value primitives to a memory-based array
- representation.
-
- The Ops `array_load`, `array_store`, `array_fetch`, and `array_update` are
- used to manage abstract aggregate array values. A simple analysis is done
- to determine if there are potential dependences between these operations.
- If not, these array operations can be lowered to work directly on the memory
- representation. If there is a potential conflict, a temporary is created
- along with appropriate copy-in/copy-out operations. Here, a more refined
- analysis might be deployed, such as using the affine framework.
-
- This pass is required before code gen to the LLVM IR dialect.
- }];
- let constructor = "::fir::createArrayValueCopyPass()";
- let dependentDialects = [ "fir::FIROpsDialect" ];
- let options = [
- Option<"optimizeConflicts", "optimize-conflicts", "bool",
- /*default=*/"false",
- "do more detailed conflict analysis to reduce the number "
- "of temporaries">
- ];
-}
-
def CharacterConversion : Pass<"character-conversion"> {
let summary = "Convert CHARACTER entities with different KINDs";
let description = [{
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index b7be26729e1d0..98bdbf52e68cb 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1203,15 +1203,6 @@ fir::factory::getTypeParams(mlir::Location loc, fir::FirOpBuilder &builder,
[&](const auto &) { return fir::getTypeParams(exv); });
}
-llvm::SmallVector<mlir::Value>
-fir::factory::getTypeParams(mlir::Location loc, fir::FirOpBuilder &builder,
- fir::ArrayLoadOp load) {
- mlir::Type memTy = load.getMemref().getType();
- if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(memTy))
- return getFromBox(loc, builder, boxTy, load.getMemref());
- return load.getTypeparams();
-}
-
std::string fir::factory::uniqueCGIdent(llvm::StringRef prefix,
llvm::StringRef name) {
// For "long" identifiers use a hash value
diff --git a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp b/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
deleted file mode 100644
index 5bf783db92bf3..0000000000000
--- a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp
+++ /dev/null
@@ -1,1434 +0,0 @@
-//===-- ArrayValueCopy.cpp ------------------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "flang/Optimizer/Builder/BoxValue.h"
-#include "flang/Optimizer/Builder/FIRBuilder.h"
-#include "flang/Optimizer/Builder/Factory.h"
-#include "flang/Optimizer/Builder/Runtime/Derived.h"
-#include "flang/Optimizer/Builder/Todo.h"
-#include "flang/Optimizer/Dialect/FIRDialect.h"
-#include "flang/Optimizer/Dialect/FIROpsSupport.h"
-#include "flang/Optimizer/Dialect/Support/FIRContext.h"
-#include "flang/Optimizer/Transforms/Passes.h"
-#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
-#include "mlir/Dialect/SCF/IR/SCF.h"
-#include "mlir/Transforms/DialectConversion.h"
-#include "llvm/Support/Debug.h"
-
-namespace fir {
-#define GEN_PASS_DEF_ARRAYVALUECOPY
-#include "flang/Optimizer/Transforms/Passes.h.inc"
-} // namespace fir
-
-#define DEBUG_TYPE "flang-array-value-copy"
-
-using namespace fir;
-using namespace mlir;
-
-using OperationUseMapT = llvm::DenseMap<mlir::Operation *, mlir::Operation *>;
-
-namespace {
-
-/// Array copy analysis.
-/// Perform an interference analysis between array values.
-///
-/// Lowering will generate a sequence of the following form.
-/// ```mlir
-/// %a_1 = fir.array_load %array_1(%shape) : ...
-/// ...
-/// %a_j = fir.array_load %array_j(%shape) : ...
-/// ...
-/// %a_n = fir.array_load %array_n(%shape) : ...
-/// ...
-/// %v_i = fir.array_fetch %a_i, ...
-/// %a_j1 = fir.array_update %a_j, ...
-/// ...
-/// fir.array_merge_store %a_j, %a_jn to %array_j : ...
-/// ```
-///
-/// The analysis is to determine if there are any conflicts. A conflict is when
-/// one the following cases occurs.
-///
-/// 1. There is an `array_update` to an array value, a_j, such that a_j was
-/// loaded from the same array memory reference (array_j) but with a different
-/// shape as the other array values a_i, where i != j. [Possible overlapping
-/// arrays.]
-///
-/// 2. There is either an array_fetch or array_update of a_j with a different
-/// set of index values. [Possible loop-carried dependence.]
-///
-/// If none of the array values overlap in storage and the accesses are not
-/// loop-carried, then the arrays are conflict-free and no copies are required.
-class ArrayCopyAnalysisBase {
-public:
- using ConflictSetT = llvm::SmallPtrSet<mlir::Operation *, 16>;
- using UseSetT = llvm::SmallPtrSet<mlir::OpOperand *, 8>;
- using LoadMapSetsT = llvm::DenseMap<mlir::Operation *, UseSetT>;
- using AmendAccessSetT = llvm::SmallPtrSet<mlir::Operation *, 4>;
-
- ArrayCopyAnalysisBase(mlir::Operation *op, bool optimized)
- : operation{op}, optimizeConflicts(optimized) {
- construct(op);
- }
- virtual ~ArrayCopyAnalysisBase() = default;
-
- mlir::Operation *getOperation() const { return operation; }
-
- /// Return true iff the `array_merge_store` has potential conflicts.
- bool hasPotentialConflict(mlir::Operation *op) const {
- LLVM_DEBUG(llvm::dbgs()
- << "looking for a conflict on " << *op
- << " and the set has a total of " << conflicts.size() << '\n');
- return conflicts.contains(op);
- }
-
- /// Return the use map.
- /// The use map maps array access, amend, fetch and update operations back to
- /// the array load that is the original source of the array value.
- /// It maps an array_load to an array_merge_store, if and only if the loaded
- /// array value has pending modifications to be merged.
- const OperationUseMapT &getUseMap() const { return useMap; }
-
- /// Return the set of array_access ops directly associated with array_amend
- /// ops.
- bool inAmendAccessSet(mlir::Operation *op) const {
- return amendAccesses.count(op);
- }
-
- /// For ArrayLoad `load`, return the transitive set of all OpOperands.
- UseSetT getLoadUseSet(mlir::Operation *load) const {
- assert(loadMapSets.count(load) && "analysis missed an array load?");
- return loadMapSets.lookup(load);
- }
-
- void arrayMentions(llvm::SmallVectorImpl<mlir::Operation *> &mentions,
- ArrayLoadOp load);
-
-private:
- void construct(mlir::Operation *topLevelOp);
-
- mlir::Operation *operation; // operation that analysis ran upon
- ConflictSetT conflicts; // set of conflicts (loads and merge stores)
- OperationUseMapT useMap;
- LoadMapSetsT loadMapSets;
- // Set of array_access ops associated with array_amend ops.
- AmendAccessSetT amendAccesses;
- bool optimizeConflicts;
-};
-
-// Optimized array copy analysis that takes into account Fortran
-// variable attributes to prove that no conflict is possible
-// and reduce the number of temporary arrays.
-class ArrayCopyAnalysisOptimized : public ArrayCopyAnalysisBase {
-public:
- MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ArrayCopyAnalysisOptimized)
-
- ArrayCopyAnalysisOptimized(mlir::Operation *op)
- : ArrayCopyAnalysisBase(op, /*optimized=*/true) {}
-};
-
-// Unoptimized array copy analysis used at O0.
-class ArrayCopyAnalysis : public ArrayCopyAnalysisBase {
-public:
- MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ArrayCopyAnalysis)
-
- ArrayCopyAnalysis(mlir::Operation *op)
- : ArrayCopyAnalysisBase(op, /*optimized=*/false) {}
-};
-} // namespace
-
-namespace {
-/// Helper class to collect all array operations that produced an array value.
-class ReachCollector {
-public:
- ReachCollector(llvm::SmallVectorImpl<mlir::Operation *> &reach,
- mlir::Region *loopRegion)
- : reach{reach}, loopRegion{loopRegion} {}
-
- void collectArrayMentionFrom(mlir::Operation *op, mlir::ValueRange range) {
- if (range.empty()) {
- collectArrayMentionFrom(op, mlir::Value{});
- return;
- }
- for (mlir::Value v : range)
- collectArrayMentionFrom(v);
- }
-
- // Collect all the array_access ops in `block`. This recursively looks into
- // blocks in ops with regions.
- // FIXME: This is temporarily relying on the array_amend appearing in a
- // do_loop Region. This phase ordering assumption can be eliminated by using
- // dominance information to find the array_access ops or by scanning the
- // transitive closure of the amending array_access's users and the defs that
- // reach them.
- void collectAccesses(llvm::SmallVector<ArrayAccessOp> &result,
- mlir::Block *block) {
- for (auto &op : *block) {
- if (auto access = mlir::dyn_cast<ArrayAccessOp>(op)) {
- LLVM_DEBUG(llvm::dbgs() << "adding access: " << access << '\n');
- result.push_back(access);
- continue;
- }
- for (auto ®ion : op.getRegions())
- for (auto &bb : region.getBlocks())
- collectAccesses(result, &bb);
- }
- }
-
- void collectArrayMentionFrom(mlir::Operation *op, mlir::Value val) {
- // `val` is defined by an Op, process the defining Op.
- // If `val` is defined by a region containing Op, we want to drill down
- // and through that Op's region(s).
- LLVM_DEBUG(llvm::dbgs() << "popset: " << *op << '\n');
- auto popFn = [&](auto rop) {
- assert(val && "op must have a result value");
- auto resNum = mlir::cast<mlir::OpResult>(val).getResultNumber();
- llvm::SmallVector<mlir::Value> results;
- rop.resultToSourceOps(results, resNum);
- for (auto u : results)
- collectArrayMentionFrom(u);
- };
- if (auto rop = mlir::dyn_cast<DoLoopOp>(op)) {
- popFn(rop);
- return;
- }
- if (auto rop = mlir::dyn_cast<IterWhileOp>(op)) {
- popFn(rop);
- return;
- }
- if (auto rop = mlir::dyn_cast<fir::IfOp>(op)) {
- popFn(rop);
- return;
- }
- if (auto box = mlir::dyn_cast<EmboxOp>(op)) {
- for (auto *user : box.getMemref().getUsers())
- if (user != op)
- collectArrayMentionFrom(user, user->getResults());
- return;
- }
- if (auto mergeStore = mlir::dyn_cast<ArrayMergeStoreOp>(op)) {
- if (opIsInsideLoops(mergeStore))
- collectArrayMentionFrom(mergeStore.getSequence());
- return;
- }
-
- if (mlir::isa<AllocaOp, AllocMemOp>(op)) {
- // Look for any stores inside the loops, and collect an array operation
- // that produced the value being stored to it.
- for (auto *user : op->getUsers())
- if (auto store = mlir::dyn_cast<fir::StoreOp>(user))
- if (opIsInsideLoops(store))
- collectArrayMentionFrom(store.getValue());
- return;
- }
-
- // Scan the uses of amend's memref
- if (auto amend = mlir::dyn_cast<ArrayAmendOp>(op)) {
- reach.push_back(op);
- llvm::SmallVector<ArrayAccessOp> accesses;
- collectAccesses(accesses, op->getBlock());
- for (auto access : accesses)
- collectArrayMentionFrom(access.getResult());
- }
-
- // Otherwise, Op does not contain a region so just chase its operands.
- if (mlir::isa<ArrayAccessOp, ArrayLoadOp, ArrayUpdateOp, ArrayModifyOp,
- ArrayFetchOp>(op)) {
- LLVM_DEBUG(llvm::dbgs() << "add " << *op << " to reachable set\n");
- reach.push_back(op);
- }
-
- // Include all array_access ops using an array_load.
- if (auto arrLd = mlir::dyn_cast<ArrayLoadOp>(op))
- for (auto *user : arrLd.getResult().getUsers())
- if (mlir::isa<ArrayAccessOp>(user)) {
- LLVM_DEBUG(llvm::dbgs() << "add " << *user << " to reachable set\n");
- reach.push_back(user);
- }
-
- // Array modify assignment is performed on the result. So the analysis must
- // look at the what is done with the result.
- if (mlir::isa<ArrayModifyOp>(op))
- for (auto *user : op->getResult(0).getUsers())
- followUsers(user);
-
- if (mlir::isa<fir::CallOp>(op)) {
- LLVM_DEBUG(llvm::dbgs() << "add " << *op << " to reachable set\n");
- reach.push_back(op);
- }
-
- for (auto u : op->getOperands())
- collectArrayMentionFrom(u);
- }
-
- void collectArrayMentionFrom(mlir::BlockArgument ba) {
- auto *parent = ba.getOwner()->getParentOp();
- // If inside an Op holding a region, the block argument corresponds to an
- // argument passed to the containing Op.
- auto popFn = [&](auto rop) {
- collectArrayMentionFrom(rop.blockArgToSourceOp(ba.getArgNumber()));
- };
- if (auto rop = mlir::dyn_cast<DoLoopOp>(parent)) {
- popFn(rop);
- return;
- }
- if (auto rop = mlir::dyn_cast<IterWhileOp>(parent)) {
- popFn(rop);
- return;
- }
- // Otherwise, a block argument is provided via the pred blocks.
- for (auto *pred : ba.getOwner()->getPredecessors()) {
- auto u = pred->getTerminator()->getOperand(ba.getArgNumber());
- collectArrayMentionFrom(u);
- }
- }
-
- // Recursively trace operands to find all array operations relating to the
- // values merged.
- void collectArrayMentionFrom(mlir::Value val) {
- if (!val || visited.contains(val))
- return;
- visited.insert(val);
-
- // Process a block argument.
- if (auto ba = mlir::dyn_cast<mlir::BlockArgument>(val)) {
- collectArrayMentionFrom(ba);
- return;
- }
-
- // Process an Op.
- if (auto *op = val.getDefiningOp()) {
- collectArrayMentionFrom(op, val);
- return;
- }
-
- emitFatalError(val.getLoc(), "unhandled value");
- }
-
- /// Return all ops that produce the array value that is stored into the
- /// `array_merge_store`.
- static void reachingValues(llvm::SmallVectorImpl<mlir::Operation *> &reach,
- mlir::Value seq) {
- reach.clear();
- mlir::Region *loopRegion = nullptr;
- if (auto doLoop = mlir::dyn_cast_or_null<DoLoopOp>(seq.getDefiningOp()))
- loopRegion = &doLoop->getRegion(0);
- ReachCollector collector(reach, loopRegion);
- collector.collectArrayMentionFrom(seq);
- }
-
-private:
- /// Is \op inside the loop nest region ?
- /// FIXME: replace this structural dependence with graph properties.
- bool opIsInsideLoops(mlir::Operation *op) const {
- auto *region = op->getParentRegion();
- while (region) {
- if (region == loopRegion)
- return true;
- region = region->getParentRegion();
- }
- return false;
- }
-
- /// Recursively trace the use of an operation results, calling
- /// collectArrayMentionFrom on the direct and indirect user operands.
- void followUsers(mlir::Operation *op) {
- for (auto userOperand : op->getOperands())
- collectArrayMentionFrom(userOperand);
- // Go through potential converts/coordinate_op.
- for (auto indirectUser : op->getUsers())
- followUsers(indirectUser);
- }
-
- llvm::SmallVectorImpl<mlir::Operation *> &reach;
- llvm::SmallPtrSet<mlir::Value, 16> visited;
- /// Region of the loops nest that produced the array value.
- mlir::Region *loopRegion;
-};
-} // namespace
-
-/// Find all the array operations that access the array value that is loaded by
-/// the array load operation, `load`.
-void ArrayCopyAnalysisBase::arrayMentions(
- llvm::SmallVectorImpl<mlir::Operation *> &mentions, ArrayLoadOp load) {
- mentions.clear();
- auto lmIter = loadMapSets.find(load);
- if (lmIter != loadMapSets.end()) {
- for (auto *opnd : lmIter->second) {
- auto *owner = opnd->getOwner();
- if (mlir::isa<ArrayAccessOp, ArrayAmendOp, ArrayFetchOp, ArrayUpdateOp,
- ArrayModifyOp>(owner))
- mentions.push_back(owner);
- }
- return;
- }
-
- UseSetT visited;
- llvm::SmallVector<mlir::OpOperand *> queue; // uses of ArrayLoad[orig]
-
- auto appendToQueue = [&](mlir::Value val) {
- for (auto &use : val.getUses())
- if (!visited.count(&use)) {
- visited.insert(&use);
- queue.push_back(&use);
- }
- };
-
- // Build the set of uses of `original`.
- // let USES = { uses of original fir.load }
- appendToQueue(load);
-
- // Process the worklist until done.
- while (!queue.empty()) {
- mlir::OpOperand *operand = queue.pop_back_val();
- mlir::Operation *owner = operand->getOwner();
- if (!owner)
- continue;
- auto structuredLoop = [&](auto ro) {
- if (auto blockArg = ro.iterArgToBlockArg(operand->get())) {
- int64_t arg = blockArg.getArgNumber();
- mlir::Value output = ro.getResult(ro.getFinalValue() ? arg : arg - 1);
- appendToQueue(output);
- appendToQueue(blockArg);
- }
- };
- // TODO: this need to be updated to use the control-flow interface.
- auto branchOp = [&](mlir::Block *dest, OperandRange operands) {
- if (operands.empty())
- return;
-
- // Check if this operand is within the range.
- unsigned operandIndex = operand->getOperandNumber();
- unsigned operandsStart = operands.getBeginOperandIndex();
- if (operandIndex < operandsStart ||
- operandIndex >= (operandsStart + operands.size()))
- return;
-
- // Index the successor.
- unsigned argIndex = operandIndex - operandsStart;
- appendToQueue(dest->getArgument(argIndex));
- };
- // Thread uses into structured loop bodies and return value uses.
- if (auto ro = mlir::dyn_cast<DoLoopOp>(owner)) {
- structuredLoop(ro);
- } else if (auto ro = mlir::dyn_cast<IterWhileOp>(owner)) {
- structuredLoop(ro);
- } else if (auto rs = mlir::dyn_cast<ResultOp>(owner)) {
- // Thread any uses of fir.if that return the marked array value.
- mlir::Operation *parent = rs->getParentRegion()->getParentOp();
- if (auto ifOp = mlir::dyn_cast<fir::IfOp>(parent))
- appendToQueue(ifOp.getResult(operand->getOperandNumber()));
- } else if (mlir::isa<ArrayFetchOp>(owner)) {
- // Keep track of array value fetches.
- LLVM_DEBUG(llvm::dbgs()
- << "add fetch {" << *owner << "} to array value set\n");
- mentions.push_back(owner);
- } else if (auto update = mlir::dyn_cast<ArrayUpdateOp>(owner)) {
- // Keep track of array value updates and thread the return value uses.
- LLVM_DEBUG(llvm::dbgs()
- << "add update {" << *owner << "} to array value set\n");
- mentions.push_back(owner);
- appendToQueue(update.getResult());
- } else if (auto update = mlir::dyn_cast<ArrayModifyOp>(owner)) {
- // Keep track of array value modification and thread the return value
- // uses.
- LLVM_DEBUG(llvm::dbgs()
- << "add modify {" << *owner << "} to array value set\n");
- mentions.push_back(owner);
- appendToQueue(update.getResult(1));
- } else if (auto mention = mlir::dyn_cast<ArrayAccessOp>(owner)) {
- mentions.push_back(owner);
- } else if (auto amend = mlir::dyn_cast<ArrayAmendOp>(owner)) {
- mentions.push_back(owner);
- appendToQueue(amend.getResult());
- } else if (auto br = mlir::dyn_cast<mlir::cf::BranchOp>(owner)) {
- branchOp(br.getDest(), br.getDestOperands());
- } else if (auto br = mlir::dyn_cast<mlir::cf::CondBranchOp>(owner)) {
- branchOp(br.getTrueDest(), br.getTrueOperands());
- branchOp(br.getFalseDest(), br.getFalseOperands());
- } else if (mlir::isa<ArrayMergeStoreOp>(owner)) {
- // do nothing
- } else {
- llvm::report_fatal_error("array value reached unexpected op");
- }
- }
- loadMapSets.insert({load, visited});
-}
-
-static bool hasPointerType(mlir::Type type) {
- if (auto boxTy = mlir::dyn_cast<BoxType>(type))
- type = boxTy.getEleTy();
- return mlir::isa<fir::PointerType>(type);
-}
-
-// This is a NF performance hack. It makes a simple test that the slices of the
-// load, \p ld, and the merge store, \p st, are trivially mutually exclusive.
-static bool mutuallyExclusiveSliceRange(ArrayLoadOp ld, ArrayMergeStoreOp st) {
- // If the same array_load, then no further testing is warranted.
- if (ld.getResult() == st.getOriginal())
- return false;
-
- auto getSliceOp = [](mlir::Value val) -> SliceOp {
- if (!val)
- return {};
- auto sliceOp = mlir::dyn_cast_or_null<SliceOp>(val.getDefiningOp());
- if (!sliceOp)
- return {};
- return sliceOp;
- };
-
- auto ldSlice = getSliceOp(ld.getSlice());
- auto stSlice = getSliceOp(st.getSlice());
- if (!ldSlice || !stSlice)
- return false;
-
- // Resign on subobject slices.
- if (!ldSlice.getFields().empty() || !stSlice.getFields().empty() ||
- !ldSlice.getSubstr().empty() || !stSlice.getSubstr().empty())
- return false;
-
- // Crudely test that the two slices do not overlap by looking for the
- // following general condition. If the slices look like (i:j) and (j+1:k) then
- // these ranges do not overlap. The addend must be a constant.
- auto ldTriples = ldSlice.getTriples();
- auto stTriples = stSlice.getTriples();
- const auto size = ldTriples.size();
- if (size != stTriples.size())
- return false;
-
- auto displacedByConstant = [](mlir::Value v1, mlir::Value v2) {
- auto removeConvert = [](mlir::Value v) -> mlir::Operation * {
- auto *op = v.getDefiningOp();
- while (auto conv = mlir::dyn_cast_or_null<ConvertOp>(op))
- op = conv.getValue().getDefiningOp();
- return op;
- };
-
- auto isPositiveConstant = [](mlir::Value v) -> bool {
- if (auto conOp =
- mlir::dyn_cast<mlir::arith::ConstantOp>(v.getDefiningOp()))
- if (auto iattr = mlir::dyn_cast<mlir::IntegerAttr>(conOp.getValue()))
- return iattr.getInt() > 0;
- return false;
- };
-
- auto *op1 = removeConvert(v1);
- auto *op2 = removeConvert(v2);
- if (!op1 || !op2)
- return false;
- if (auto addi = mlir::dyn_cast<mlir::arith::AddIOp>(op2))
- if ((addi.getLhs().getDefiningOp() == op1 &&
- isPositiveConstant(addi.getRhs())) ||
- (addi.getRhs().getDefiningOp() == op1 &&
- isPositiveConstant(addi.getLhs())))
- return true;
- if (auto subi = mlir::dyn_cast<mlir::arith::SubIOp>(op1))
- if (subi.getLhs().getDefiningOp() == op2 &&
- isPositiveConstant(subi.getRhs()))
- return true;
- return false;
- };
-
- for (std::remove_const_t<decltype(size)> i = 0; i < size; i += 3) {
- // If both are loop invariant, skip to the next triple.
- if (mlir::isa_and_nonnull<fir::UndefOp>(ldTriples[i + 1].getDefiningOp()) &&
- mlir::isa_and_nonnull<fir::UndefOp>(stTriples[i + 1].getDefiningOp())) {
- // Unless either is a vector index, then be conservative.
- if (mlir::isa_and_nonnull<fir::UndefOp>(ldTriples[i].getDefiningOp()) ||
- mlir::isa_and_nonnull<fir::UndefOp>(stTriples[i].getDefiningOp()))
- return false;
- continue;
- }
- // If identical, skip to the next triple.
- if (ldTriples[i] == stTriples[i] && ldTriples[i + 1] == stTriples[i + 1] &&
- ldTriples[i + 2] == stTriples[i + 2])
- continue;
- // If ubound and lbound are the same with a constant offset, skip to the
- // next triple.
- if (displacedByConstant(ldTriples[i + 1], stTriples[i]) ||
- displacedByConstant(stTriples[i + 1], ldTriples[i]))
- continue;
- return false;
- }
- LLVM_DEBUG(llvm::dbgs() << "detected non-overlapping slice ranges on " << ld
- << " and " << st << ", which is not a conflict\n");
- return true;
-}
-
-/// Is there a conflict between the array value that was updated and to be
-/// stored to `st` and the set of arrays loaded (`reach`) and used to compute
-/// the updated value?
-/// If `optimize` is true, use the variable attributes to prove that
-/// there is no conflict.
-static bool conflictOnLoad(llvm::ArrayRef<mlir::Operation *> reach,
- ArrayMergeStoreOp st, bool optimize) {
- mlir::Value load;
- mlir::Value addr = st.getMemref();
- const bool storeHasPointerType = hasPointerType(addr.getType());
- for (auto *op : reach)
- if (auto ld = mlir::dyn_cast<ArrayLoadOp>(op)) {
- mlir::Type ldTy = ld.getMemref().getType();
- auto globalOpName = mlir::OperationName(fir::GlobalOp::getOperationName(),
- ld.getContext());
- if (ld.getMemref() == addr) {
- if (mutuallyExclusiveSliceRange(ld, st))
- continue;
- if (ld.getResult() != st.getOriginal())
- return true;
- if (load) {
- // TODO: extend this to allow checking if the first `load` and this
- // `ld` are mutually exclusive accesses but not identical.
- return true;
- }
- load = ld;
- } else if (storeHasPointerType) {
- if (optimize && !hasPointerType(ldTy) &&
- !valueMayHaveFirAttributes(
- ld.getMemref(),
- {getTargetAttrName(),
- fir::GlobalOp::getTargetAttrName(globalOpName).strref()}))
- continue;
-
- return true;
- } else if (hasPointerType(ldTy)) {
- if (optimize && !storeHasPointerType &&
- !valueMayHaveFirAttributes(
- addr,
- {getTargetAttrName(),
- fir::GlobalOp::getTargetAttrName(globalOpName).strref()}))
- continue;
-
- return true;
- }
- // TODO: Check if types can also allow ruling out some cases. For now,
- // the fact that equivalences is using pointer attribute to enforce
- // aliasing is preventing any attempt to do so, and in general, it may
- // be wrong to use this if any of the types is a complex or a derived
- // for which it is possible to create a pointer to a part with a
- // different type than the whole, although this deserve some more
- // investigation because existing compiler behavior seem to diverge
- // here.
- }
- return false;
-}
-
-/// Is there an access vector conflict on the array being merged into? If the
-/// access vectors diverge, then assume that there are potentially overlapping
-/// loop-carried references.
-static bool conflictOnMerge(llvm::ArrayRef<mlir::Operation *> mentions) {
- if (mentions.size() < 2)
- return false;
- llvm::SmallVector<mlir::Value> indices;
- LLVM_DEBUG(llvm::dbgs() << "check merge conflict on with " << mentions.size()
- << " mentions on the list\n");
- bool valSeen = false;
- bool refSeen = false;
- for (auto *op : mentions) {
- llvm::SmallVector<mlir::Value> compareVector;
- if (auto u = mlir::dyn_cast<ArrayUpdateOp>(op)) {
- valSeen = true;
- if (indices.empty()) {
- indices = u.getIndices();
- continue;
- }
- compareVector = u.getIndices();
- } else if (auto f = mlir::dyn_cast<ArrayModifyOp>(op)) {
- valSeen = true;
- if (indices.empty()) {
- indices = f.getIndices();
- continue;
- }
- compareVector = f.getIndices();
- } else if (auto f = mlir::dyn_cast<ArrayFetchOp>(op)) {
- valSeen = true;
- if (indices.empty()) {
- indices = f.getIndices();
- continue;
- }
- compareVector = f.getIndices();
- } else if (auto f = mlir::dyn_cast<ArrayAccessOp>(op)) {
- refSeen = true;
- if (indices.empty()) {
- indices = f.getIndices();
- continue;
- }
- compareVector = f.getIndices();
- } else if (mlir::isa<ArrayAmendOp>(op)) {
- refSeen = true;
- continue;
- } else {
- mlir::emitError(op->getLoc(), "unexpected operation in analysis");
- }
- if (compareVector.size() != indices.size() ||
- llvm::any_of(llvm::zip(compareVector, indices), [&](auto pair) {
- return std::get<0>(pair) != std::get<1>(pair);
- }))
- return true;
- LLVM_DEBUG(llvm::dbgs() << "vectors compare equal\n");
- }
- return valSeen && refSeen;
-}
-
-/// With element-by-reference semantics, an amended array with more than once
-/// access to the same loaded array are conservatively considered a conflict.
-/// Note: the array copy can still be eliminated in subsequent optimizations.
-static bool conflictOnReference(llvm::ArrayRef<mlir::Operation *> mentions) {
- LLVM_DEBUG(llvm::dbgs() << "checking reference semantics " << mentions.size()
- << '\n');
- if (mentions.size() < 3)
- return false;
- unsigned amendCount = 0;
- unsigned accessCount = 0;
- for (auto *op : mentions) {
- if (mlir::isa<ArrayAmendOp>(op) && ++amendCount > 1) {
- LLVM_DEBUG(llvm::dbgs() << "conflict: multiple amends of array value\n");
- return true;
- }
- if (mlir::isa<ArrayAccessOp>(op) && ++accessCount > 1) {
- LLVM_DEBUG(llvm::dbgs()
- << "conflict: multiple accesses of array value\n");
- return true;
- }
- if (mlir::isa<ArrayFetchOp, ArrayUpdateOp, ArrayModifyOp>(op)) {
- LLVM_DEBUG(llvm::dbgs()
- << "conflict: array value has both uses by-value and uses "
- "by-reference. conservative assumption.\n");
- return true;
- }
- }
- return false;
-}
-
-static mlir::Operation *
-amendingAccess(llvm::ArrayRef<mlir::Operation *> mentions) {
- for (auto *op : mentions)
- if (auto amend = mlir::dyn_cast<ArrayAmendOp>(op))
- return amend.getMemref().getDefiningOp();
- return {};
-}
-
-// Are any conflicts present? The conflicts detected here are described above.
-static bool conflictDetected(llvm::ArrayRef<mlir::Operation *> reach,
- llvm::ArrayRef<mlir::Operation *> mentions,
- ArrayMergeStoreOp st, bool optimize) {
- return conflictOnLoad(reach, st, optimize) || conflictOnMerge(mentions);
-}
-
-// Assume that any call to a function that uses host-associations will be
-// modifying the output array.
-static bool
-conservativeCallConflict(llvm::ArrayRef<mlir::Operation *> reaches) {
- return llvm::any_of(reaches, [](mlir::Operation *op) {
- if (auto call = mlir::dyn_cast<fir::CallOp>(op))
- if (auto callee = mlir::dyn_cast<mlir::SymbolRefAttr>(
- call.getCallableForCallee())) {
- auto module = op->getParentOfType<mlir::ModuleOp>();
- return isInternalProcedure(
- module.lookupSymbol<mlir::func::FuncOp>(callee));
- }
- return false;
- });
-}
-
-/// Constructor of the array copy analysis.
-/// This performs the analysis and saves the intermediate results.
-void ArrayCopyAnalysisBase::construct(mlir::Operation *topLevelOp) {
- topLevelOp->walk([&](Operation *op) {
- if (auto st = mlir::dyn_cast<fir::ArrayMergeStoreOp>(op)) {
- llvm::SmallVector<mlir::Operation *> values;
- ReachCollector::reachingValues(values, st.getSequence());
- bool callConflict = conservativeCallConflict(values);
- llvm::SmallVector<mlir::Operation *> mentions;
- arrayMentions(mentions,
- mlir::cast<ArrayLoadOp>(st.getOriginal().getDefiningOp()));
- bool conflict = conflictDetected(values, mentions, st, optimizeConflicts);
- bool refConflict = conflictOnReference(mentions);
- if (callConflict || conflict || refConflict) {
- LLVM_DEBUG(llvm::dbgs()
- << "CONFLICT: copies required for " << st << '\n'
- << " adding conflicts on: " << *op << " and "
- << st.getOriginal() << '\n');
- conflicts.insert(op);
- conflicts.insert(st.getOriginal().getDefiningOp());
- if (auto *access = amendingAccess(mentions))
- amendAccesses.insert(access);
- }
- auto *ld = st.getOriginal().getDefiningOp();
- LLVM_DEBUG(llvm::dbgs()
- << "map: adding {" << *ld << " -> " << st << "}\n");
- useMap.insert({ld, op});
- } else if (auto load = mlir::dyn_cast<ArrayLoadOp>(op)) {
- llvm::SmallVector<mlir::Operation *> mentions;
- arrayMentions(mentions, load);
- LLVM_DEBUG(llvm::dbgs() << "process load: " << load
- << ", mentions: " << mentions.size() << '\n');
- for (auto *acc : mentions) {
- LLVM_DEBUG(llvm::dbgs() << " mention: " << *acc << '\n');
- if (mlir::isa<ArrayAccessOp, ArrayAmendOp, ArrayFetchOp, ArrayUpdateOp,
- ArrayModifyOp>(acc)) {
- if (useMap.count(acc)) {
- mlir::emitError(
- load.getLoc(),
- "The parallel semantics of multiple array_merge_stores per "
- "array_load are not supported.");
- continue;
- }
- LLVM_DEBUG(llvm::dbgs()
- << "map: adding {" << *acc << "} -> {" << load << "}\n");
- useMap.insert({acc, op});
- }
- }
- }
- });
-}
-
-//===----------------------------------------------------------------------===//
-// Conversions for converting out of array value form.
-//===----------------------------------------------------------------------===//
-
-namespace {
-class ArrayLoadConversion : public mlir::OpRewritePattern<ArrayLoadOp> {
-public:
- using OpRewritePattern::OpRewritePattern;
-
- llvm::LogicalResult
- matchAndRewrite(ArrayLoadOp load,
- mlir::PatternRewriter &rewriter) const override {
- LLVM_DEBUG(llvm::dbgs() << "replace load " << load << " with undef.\n");
- rewriter.replaceOpWithNewOp<UndefOp>(load, load.getType());
- return mlir::success();
- }
-};
-
-class ArrayMergeStoreConversion
- : public mlir::OpRewritePattern<ArrayMergeStoreOp> {
-public:
- using OpRewritePattern::OpRewritePattern;
-
- llvm::LogicalResult
- matchAndRewrite(ArrayMergeStoreOp store,
- mlir::PatternRewriter &rewriter) const override {
- LLVM_DEBUG(llvm::dbgs() << "marking store " << store << " as dead.\n");
- rewriter.eraseOp(store);
- return mlir::success();
- }
-};
-} // namespace
-
-static mlir::Type getEleTy(mlir::Type ty) {
- auto eleTy = unwrapSequenceType(unwrapPassByRefType(ty));
- // FIXME: keep ptr/heap/ref information.
- return ReferenceType::get(eleTy);
-}
-
-// This is an unsafe way to deduce this (won't be true in internal
-// procedure or inside select-rank for assumed-size). Only here to satisfy
-// legacy code until removed.
-static bool isAssumedSize(llvm::SmallVectorImpl<mlir::Value> &extents) {
- if (extents.empty())
- return false;
- return llvm::isa_and_nonnull<fir::AssumedSizeExtentOp>(
- extents.back().getDefiningOp());
-}
-
-// Extract extents from the ShapeOp/ShapeShiftOp into the result vector.
-static bool getAdjustedExtents(mlir::Location loc,
- mlir::PatternRewriter &rewriter,
- ArrayLoadOp arrLoad,
- llvm::SmallVectorImpl<mlir::Value> &result,
- mlir::Value shape) {
- bool copyUsingSlice = false;
- auto *shapeOp = shape.getDefiningOp();
- if (auto s = mlir::dyn_cast_or_null<ShapeOp>(shapeOp)) {
- auto e = s.getExtents();
- result.insert(result.end(), e.begin(), e.end());
- } else if (auto s = mlir::dyn_cast_or_null<ShapeShiftOp>(shapeOp)) {
- auto e = s.getExtents();
- result.insert(result.end(), e.begin(), e.end());
- } else {
- emitFatalError(loc, "not a fir.shape/fir.shape_shift op");
- }
- auto idxTy = rewriter.getIndexType();
- if (isAssumedSize(result)) {
- // Use slice information to compute the extent of the column.
- auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1);
- mlir::Value size = one;
- if (mlir::Value sliceArg = arrLoad.getSlice()) {
- if (auto sliceOp =
- mlir::dyn_cast_or_null<SliceOp>(sliceArg.getDefiningOp())) {
- auto triples = sliceOp.getTriples();
- const std::size_t tripleSize = triples.size();
- auto module = arrLoad->getParentOfType<mlir::ModuleOp>();
- FirOpBuilder builder(rewriter, module);
- size = builder.genExtentFromTriplet(loc, triples[tripleSize - 3],
- triples[tripleSize - 2],
- triples[tripleSize - 1], idxTy);
- copyUsingSlice = true;
- }
- }
- result[result.size() - 1] = size;
- }
- return copyUsingSlice;
-}
-
-/// Place the extents of the array load, \p arrLoad, into \p result and
-/// return a ShapeOp or ShapeShiftOp with the same extents. If \p arrLoad is
-/// loading a `!fir.box`, code will be generated to read the extents from the
-/// boxed value, and the retunred shape Op will be built with the extents read
-/// from the box. Otherwise, the extents will be extracted from the ShapeOp (or
-/// ShapeShiftOp) argument of \p arrLoad. \p copyUsingSlice will be set to true
-/// if slicing of the output array is to be done in the copy-in/copy-out rather
-/// than in the elemental computation step.
-static mlir::Value getOrReadExtentsAndShapeOp(
- mlir::Location loc, mlir::PatternRewriter &rewriter, ArrayLoadOp arrLoad,
- llvm::SmallVectorImpl<mlir::Value> &result, bool ©UsingSlice) {
- assert(result.empty());
- if (arrLoad->hasAttr(fir::getOptionalAttrName()))
- fir::emitFatalError(
- loc, "shapes from array load of OPTIONAL arrays must not be used");
- if (auto boxTy = mlir::dyn_cast<BoxType>(arrLoad.getMemref().getType())) {
- auto rank =
- mlir::cast<SequenceType>(dyn_cast_ptrOrBoxEleTy(boxTy)).getDimension();
- auto idxTy = rewriter.getIndexType();
- for (decltype(rank) dim = 0; dim < rank; ++dim) {
- auto dimVal = mlir::arith::ConstantIndexOp::create(rewriter, loc, dim);
- auto dimInfo = BoxDimsOp::create(rewriter, loc, idxTy, idxTy, idxTy,
- arrLoad.getMemref(), dimVal);
- result.emplace_back(dimInfo.getResult(1));
- }
- if (!arrLoad.getShape()) {
- auto shapeType = ShapeType::get(rewriter.getContext(), rank);
- return ShapeOp::create(rewriter, loc, shapeType, result);
- }
- auto shiftOp = arrLoad.getShape().getDefiningOp<ShiftOp>();
- auto shapeShiftType = ShapeShiftType::get(rewriter.getContext(), rank);
- llvm::SmallVector<mlir::Value> shapeShiftOperands;
- for (auto [lb, extent] : llvm::zip(shiftOp.getOrigins(), result)) {
- shapeShiftOperands.push_back(lb);
- shapeShiftOperands.push_back(extent);
- }
- return ShapeShiftOp::create(rewriter, loc, shapeShiftType,
- shapeShiftOperands);
- }
- copyUsingSlice =
- getAdjustedExtents(loc, rewriter, arrLoad, result, arrLoad.getShape());
- return arrLoad.getShape();
-}
-
-static mlir::Type toRefType(mlir::Type ty) {
- if (fir::isa_ref_type(ty))
- return ty;
- return fir::ReferenceType::get(ty);
-}
-
-static llvm::SmallVector<mlir::Value>
-getTypeParamsIfRawData(mlir::Location loc, FirOpBuilder &builder,
- ArrayLoadOp arrLoad, mlir::Type ty) {
- if (mlir::isa<BoxType>(ty))
- return {};
- return fir::factory::getTypeParams(loc, builder, arrLoad);
-}
-
-static mlir::Value genCoorOp(mlir::PatternRewriter &rewriter,
- mlir::Location loc, mlir::Type eleTy,
- mlir::Type resTy, mlir::Value alloc,
- mlir::Value shape, mlir::Value slice,
- mlir::ValueRange indices, ArrayLoadOp load,
- bool skipOrig = false) {
- llvm::SmallVector<mlir::Value> originated;
- if (skipOrig)
- originated.assign(indices.begin(), indices.end());
- else
- originated = factory::originateIndices(loc, rewriter, alloc.getType(),
- shape, indices);
- auto seqTy = dyn_cast_ptrOrBoxEleTy(alloc.getType());
- assert(seqTy && mlir::isa<SequenceType>(seqTy));
- const auto dimension = mlir::cast<SequenceType>(seqTy).getDimension();
- auto module = load->getParentOfType<mlir::ModuleOp>();
- FirOpBuilder builder(rewriter, module);
- auto typeparams = getTypeParamsIfRawData(loc, builder, load, alloc.getType());
- mlir::Value result = ArrayCoorOp::create(
- rewriter, loc, eleTy, alloc, shape, slice,
- llvm::ArrayRef<mlir::Value>{originated}.take_front(dimension),
- typeparams);
- if (dimension < originated.size())
- result = fir::CoordinateOp::create(
- rewriter, loc, resTy, result,
- llvm::ArrayRef<mlir::Value>{originated}.drop_front(dimension));
- return result;
-}
-
-static mlir::Value getCharacterLen(mlir::Location loc, FirOpBuilder &builder,
- ArrayLoadOp load, CharacterType charTy) {
- auto charLenTy = builder.getCharacterLengthType();
- if (charTy.hasDynamicLen()) {
- if (mlir::isa<BoxType>(load.getMemref().getType())) {
- // The loaded array is an emboxed value. Get the CHARACTER length from
- // the box value.
- auto eleSzInBytes =
- BoxEleSizeOp::create(builder, loc, charLenTy, load.getMemref());
- auto kindSize =
- builder.getKindMap().getCharacterBitsize(charTy.getFKind());
- auto kindByteSize =
- builder.createIntegerConstant(loc, charLenTy, kindSize / 8);
- return mlir::arith::DivSIOp::create(builder, loc, eleSzInBytes,
- kindByteSize);
- }
- // The loaded array is a (set of) unboxed values. If the CHARACTER's
- // length is not a constant, it must be provided as a type parameter to
- // the array_load.
- auto typeparams = load.getTypeparams();
- assert(typeparams.size() > 0 && "expected type parameters on array_load");
- return typeparams.back();
- }
- // The typical case: the length of the CHARACTER is a compile-time
- // constant that is encoded in the type information.
- return builder.createIntegerConstant(loc, charLenTy, charTy.getLen());
-}
-/// Generate a shallow array copy. This is used for both copy-in and copy-out.
-template <bool CopyIn>
-void genArrayCopy(mlir::Location loc, mlir::PatternRewriter &rewriter,
- mlir::Value dst, mlir::Value src, mlir::Value shapeOp,
- mlir::Value sliceOp, ArrayLoadOp arrLoad) {
- auto insPt = rewriter.saveInsertionPoint();
- llvm::SmallVector<mlir::Value> indices;
- llvm::SmallVector<mlir::Value> extents;
- bool copyUsingSlice =
- getAdjustedExtents(loc, rewriter, arrLoad, extents, shapeOp);
- auto idxTy = rewriter.getIndexType();
- // Build loop nest from column to row.
- for (auto sh : llvm::reverse(extents)) {
- auto ubi = ConvertOp::create(rewriter, loc, idxTy, sh);
- auto zero = mlir::arith::ConstantIndexOp::create(rewriter, loc, 0);
- auto one = mlir::arith::ConstantIndexOp::create(rewriter, loc, 1);
- auto ub = mlir::arith::SubIOp::create(rewriter, loc, idxTy, ubi, one);
- auto loop = DoLoopOp::create(rewriter, loc, zero, ub, one);
- rewriter.setInsertionPointToStart(loop.getBody());
- indices.push_back(loop.getInductionVar());
- }
- // Reverse the indices so they are in column-major order.
- std::reverse(indices.begin(), indices.end());
- auto module = arrLoad->getParentOfType<mlir::ModuleOp>();
- FirOpBuilder builder(rewriter, module);
- auto fromAddr = ArrayCoorOp::create(
- rewriter, loc, getEleTy(src.getType()), src, shapeOp,
- CopyIn && copyUsingSlice ? sliceOp : mlir::Value{},
- factory::originateIndices(loc, rewriter, src.getType(), shapeOp, indices),
- getTypeParamsIfRawData(loc, builder, arrLoad, src.getType()));
- auto toAddr = ArrayCoorOp::create(
- rewriter, loc, getEleTy(dst.getType()), dst, shapeOp,
- !CopyIn && copyUsingSlice ? sliceOp : mlir::Value{},
- factory::originateIndices(loc, rewriter, dst.getType(), shapeOp, indices),
- getTypeParamsIfRawData(loc, builder, arrLoad, dst.getType()));
- auto eleTy = unwrapSequenceType(unwrapPassByRefType(dst.getType()));
- // Copy from (to) object to (from) temp copy of same object.
- if (auto charTy = mlir::dyn_cast<CharacterType>(eleTy)) {
- auto len = getCharacterLen(loc, builder, arrLoad, charTy);
- CharBoxValue toChar(toAddr, len);
- CharBoxValue fromChar(fromAddr, len);
- factory::genScalarAssignment(builder, loc, toChar, fromChar);
- } else {
- if (hasDynamicSize(eleTy))
- TODO(loc, "copy element of dynamic size");
- factory::genScalarAssignment(builder, loc, toAddr, fromAddr);
- }
- rewriter.restoreInsertionPoint(insPt);
-}
-
-/// The array load may be either a boxed or unboxed value. If the value is
-/// boxed, we read the type parameters from the boxed value.
-static llvm::SmallVector<mlir::Value>
-genArrayLoadTypeParameters(mlir::Location loc, mlir::PatternRewriter &rewriter,
- ArrayLoadOp load) {
- if (load.getTypeparams().empty()) {
- auto eleTy =
- unwrapSequenceType(unwrapPassByRefType(load.getMemref().getType()));
- if (hasDynamicSize(eleTy)) {
- if (auto charTy = mlir::dyn_cast<CharacterType>(eleTy)) {
- assert(mlir::isa<BoxType>(load.getMemref().getType()));
- auto module = load->getParentOfType<mlir::ModuleOp>();
- FirOpBuilder builder(rewriter, module);
- return {getCharacterLen(loc, builder, load, charTy)};
- }
- TODO(loc, "unhandled dynamic type parameters");
- }
- return {};
- }
- return load.getTypeparams();
-}
-
-static llvm::SmallVector<mlir::Value>
-findNonconstantExtents(mlir::Type memrefTy,
- llvm::ArrayRef<mlir::Value> extents) {
- llvm::SmallVector<mlir::Value> nce;
- auto arrTy = unwrapPassByRefType(memrefTy);
- auto seqTy = mlir::cast<SequenceType>(arrTy);
- for (auto [s, x] : llvm::zip(seqTy.getShape(), extents))
- if (s == SequenceType::getUnknownExtent())
- nce.emplace_back(x);
- if (extents.size() > seqTy.getShape().size())
- for (auto x : extents.drop_front(seqTy.getShape().size()))
- nce.emplace_back(x);
- return nce;
-}
-
-/// Allocate temporary storage for an ArrayLoadOp \load and initialize any
-/// allocatable direct components of the array elements with an unallocated
-/// status. Returns the temporary address as well as a callback to generate the
-/// temporary clean-up once it has been used. The clean-up will take care of
-/// deallocating all the element allocatable components that may have been
-/// allocated while using the temporary.
-static std::pair<mlir::Value,
- std::function<void(mlir::PatternRewriter &rewriter)>>
-allocateArrayTemp(mlir::Location loc, mlir::PatternRewriter &rewriter,
- ArrayLoadOp load, llvm::ArrayRef<mlir::Value> extents,
- mlir::Value shape) {
- mlir::Type baseType = load.getMemref().getType();
- llvm::SmallVector<mlir::Value> nonconstantExtents =
- findNonconstantExtents(baseType, extents);
- llvm::SmallVector<mlir::Value> typeParams =
- genArrayLoadTypeParameters(loc, rewriter, load);
- mlir::Value allocmem =
- AllocMemOp::create(rewriter, loc, dyn_cast_ptrOrBoxEleTy(baseType),
- typeParams, nonconstantExtents);
- mlir::Type eleType =
- fir::unwrapSequenceType(fir::unwrapPassByRefType(baseType));
- if (fir::isRecordWithAllocatableMember(eleType)) {
- // The allocatable component descriptors need to be set to a clean
- // deallocated status before anything is done with them.
- mlir::Value box = fir::EmboxOp::create(
- rewriter, loc, fir::BoxType::get(allocmem.getType()), allocmem, shape,
- /*slice=*/mlir::Value{}, typeParams);
- auto module = load->getParentOfType<mlir::ModuleOp>();
- FirOpBuilder builder(rewriter, module);
- runtime::genDerivedTypeInitialize(builder, loc, box);
- // Any allocatable component that may have been allocated must be
- // deallocated during the clean-up.
- auto cleanup = [=](mlir::PatternRewriter &r) {
- FirOpBuilder builder(r, module);
- runtime::genDerivedTypeDestroy(builder, loc, box);
- FreeMemOp::create(r, loc, allocmem);
- };
- return {allocmem, cleanup};
- }
- auto cleanup = [=](mlir::PatternRewriter &r) {
- FreeMemOp::create(r, loc, allocmem);
- };
- return {allocmem, cleanup};
-}
-
-namespace {
-/// Conversion of fir.array_update and fir.array_modify Ops.
-/// If there is a conflict for the update, then we need to perform a
-/// copy-in/copy-out to preserve the original values of the array. If there is
-/// no conflict, then it is save to eschew making any copies.
-template <typename ArrayOp>
-class ArrayUpdateConversionBase : public mlir::OpRewritePattern<ArrayOp> {
-public:
- // TODO: Implement copy/swap semantics?
- explicit ArrayUpdateConversionBase(mlir::MLIRContext *ctx,
- const ArrayCopyAnalysisBase &a,
- const OperationUseMapT &m)
- : mlir::OpRewritePattern<ArrayOp>{ctx}, analysis{a}, useMap{m} {}
-
- /// The array_access, \p access, is to be to a cloned copy due to a potential
- /// conflict. Uses copy-in/copy-out semantics and not copy/swap.
- mlir::Value referenceToClone(mlir::Location loc,
- mlir::PatternRewriter &rewriter,
- ArrayOp access) const {
- LLVM_DEBUG(llvm::dbgs()
- << "generating copy-in/copy-out loops for " << access << '\n');
- auto *op = access.getOperation();
- auto *loadOp = useMap.lookup(op);
- auto load = mlir::cast<ArrayLoadOp>(loadOp);
- auto eleTy = access.getType();
- rewriter.setInsertionPoint(loadOp);
- // Copy in.
- llvm::SmallVector<mlir::Value> extents;
- bool copyUsingSlice = false;
- auto shapeOp = getOrReadExtentsAndShapeOp(loc, rewriter, load, extents,
- copyUsingSlice);
- auto [allocmem, genTempCleanUp] =
- allocateArrayTemp(loc, rewriter, load, extents, shapeOp);
- genArrayCopy</*copyIn=*/true>(load.getLoc(), rewriter, allocmem,
- load.getMemref(), shapeOp, load.getSlice(),
- load);
- // Generate the reference for the access.
- rewriter.setInsertionPoint(op);
- auto coor = genCoorOp(
- rewriter, loc, getEleTy(load.getType()), eleTy, allocmem, shapeOp,
- copyUsingSlice ? mlir::Value{} : load.getSlice(), access.getIndices(),
- load, access->hasAttr(factory::attrFortranArrayOffsets()));
- // Copy out.
- auto *storeOp = useMap.lookup(loadOp);
- auto store = mlir::cast<ArrayMergeStoreOp>(storeOp);
- rewriter.setInsertionPoint(storeOp);
- // Copy out.
- genArrayCopy</*copyIn=*/false>(store.getLoc(), rewriter, store.getMemref(),
- allocmem, shapeOp, store.getSlice(), load);
- genTempCleanUp(rewriter);
- return coor;
- }
-
- /// Copy the RHS element into the LHS and insert copy-in/copy-out between a
- /// temp and the LHS if the analysis found potential overlaps between the RHS
- /// and LHS arrays. The element copy generator must be provided in \p
- /// assignElement. \p update must be the ArrayUpdateOp or the ArrayModifyOp.
- /// Returns the address of the LHS element inside the loop and the LHS
- /// ArrayLoad result.
- std::pair<mlir::Value, mlir::Value>
- materializeAssignment(mlir::Location loc, mlir::PatternRewriter &rewriter,
- ArrayOp update,
- const std::function<void(mlir::Value)> &assignElement,
- mlir::Type lhsEltRefType) const {
- auto *op = update.getOperation();
- auto *loadOp = useMap.lookup(op);
- auto load = mlir::cast<ArrayLoadOp>(loadOp);
- LLVM_DEBUG(llvm::outs() << "does " << load << " have a conflict?\n");
- if (analysis.hasPotentialConflict(loadOp)) {
- // If there is a conflict between the arrays, then we copy the lhs array
- // to a temporary, update the temporary, and copy the temporary back to
- // the lhs array. This yields Fortran's copy-in copy-out array semantics.
- LLVM_DEBUG(llvm::outs() << "Yes, conflict was found\n");
- rewriter.setInsertionPoint(loadOp);
- // Copy in.
- llvm::SmallVector<mlir::Value> extents;
- bool copyUsingSlice = false;
- auto shapeOp = getOrReadExtentsAndShapeOp(loc, rewriter, load, extents,
- copyUsingSlice);
- auto [allocmem, genTempCleanUp] =
- allocateArrayTemp(loc, rewriter, load, extents, shapeOp);
-
- genArrayCopy</*copyIn=*/true>(load.getLoc(), rewriter, allocmem,
- load.getMemref(), shapeOp, load.getSlice(),
- load);
- rewriter.setInsertionPoint(op);
- auto coor = genCoorOp(
- rewriter, loc, getEleTy(load.getType()), lhsEltRefType, allocmem,
- shapeOp, copyUsingSlice ? mlir::Value{} : load.getSlice(),
- update.getIndices(), load,
- update->hasAttr(factory::attrFortranArrayOffsets()));
- assignElement(coor);
- auto *storeOp = useMap.lookup(loadOp);
- auto store = mlir::cast<ArrayMergeStoreOp>(storeOp);
- rewriter.setInsertionPoint(storeOp);
- // Copy out.
- genArrayCopy</*copyIn=*/false>(store.getLoc(), rewriter,
- store.getMemref(), allocmem, shapeOp,
- store.getSlice(), load);
- genTempCleanUp(rewriter);
- return {coor, load.getResult()};
- }
- // Otherwise, when there is no conflict (a possible loop-carried
- // dependence), the lhs array can be updated in place.
- LLVM_DEBUG(llvm::outs() << "No, conflict wasn't found\n");
- rewriter.setInsertionPoint(op);
- auto coorTy = getEleTy(load.getType());
- auto coor =
- genCoorOp(rewriter, loc, coorTy, lhsEltRefType, load.getMemref(),
- load.getShape(), load.getSlice(), update.getIndices(), load,
- update->hasAttr(factory::attrFortranArrayOffsets()));
- assignElement(coor);
- return {coor, load.getResult()};
- }
-
-protected:
- const ArrayCopyAnalysisBase &analysis;
- const OperationUseMapT &useMap;
-};
-
-class ArrayUpdateConversion : public ArrayUpdateConversionBase<ArrayUpdateOp> {
-public:
- explicit ArrayUpdateConversion(mlir::MLIRContext *ctx,
- const ArrayCopyAnalysisBase &a,
- const OperationUseMapT &m)
- : ArrayUpdateConversionBase{ctx, a, m} {}
-
- llvm::LogicalResult
- matchAndRewrite(ArrayUpdateOp update,
- mlir::PatternRewriter &rewriter) const override {
- auto loc = update.getLoc();
- auto assignElement = [&](mlir::Value coor) {
- auto input = update.getMerge();
- if (auto inEleTy = dyn_cast_ptrEleTy(input.getType())) {
- emitFatalError(loc, "array_update on references not supported");
- } else {
- fir::StoreOp::create(rewriter, loc, input, coor);
- }
- };
- auto lhsEltRefType = toRefType(update.getMerge().getType());
- auto [_, lhsLoadResult] = materializeAssignment(
- loc, rewriter, update, assignElement, lhsEltRefType);
- rewriter.replaceOp(update, lhsLoadResult);
- return mlir::success();
- }
-};
-
-class ArrayModifyConversion : public ArrayUpdateConversionBase<ArrayModifyOp> {
-public:
- explicit ArrayModifyConversion(mlir::MLIRContext *ctx,
- const ArrayCopyAnalysisBase &a,
- const OperationUseMapT &m)
- : ArrayUpdateConversionBase{ctx, a, m} {}
-
- llvm::LogicalResult
- matchAndRewrite(ArrayModifyOp modify,
- mlir::PatternRewriter &rewriter) const override {
- auto loc = modify.getLoc();
- auto assignElement = [](mlir::Value) {
- // Assignment already materialized by lowering using lhs element address.
- };
- auto lhsEltRefType = modify.getResult(0).getType();
- auto [lhsEltCoor, lhsLoadResult] = materializeAssignment(
- loc, rewriter, modify, assignElement, lhsEltRefType);
- rewriter.replaceOp(modify, mlir::ValueRange{lhsEltCoor, lhsLoadResult});
- return mlir::success();
- }
-};
-
-class ArrayFetchConversion : public mlir::OpRewritePattern<ArrayFetchOp> {
-public:
- explicit ArrayFetchConversion(mlir::MLIRContext *ctx,
- const OperationUseMapT &m)
- : OpRewritePattern{ctx}, useMap{m} {}
-
- llvm::LogicalResult
- matchAndRewrite(ArrayFetchOp fetch,
- mlir::PatternRewriter &rewriter) const override {
- auto *op = fetch.getOperation();
- rewriter.setInsertionPoint(op);
- auto load = mlir::cast<ArrayLoadOp>(useMap.lookup(op));
- auto loc = fetch.getLoc();
- auto coor = genCoorOp(
- rewriter, loc, getEleTy(load.getType()), toRefType(fetch.getType()),
- load.getMemref(), load.getShape(), load.getSlice(), fetch.getIndices(),
- load, fetch->hasAttr(factory::attrFortranArrayOffsets()));
- if (isa_ref_type(fetch.getType()))
- rewriter.replaceOp(fetch, coor);
- else
- rewriter.replaceOpWithNewOp<fir::LoadOp>(fetch, coor);
- return mlir::success();
- }
-
-private:
- const OperationUseMapT &useMap;
-};
-
-/// As array_access op is like an array_fetch op, except that it does not imply
-/// a load op. (It operates in the reference domain.)
-class ArrayAccessConversion : public ArrayUpdateConversionBase<ArrayAccessOp> {
-public:
- explicit ArrayAccessConversion(mlir::MLIRContext *ctx,
- const ArrayCopyAnalysisBase &a,
- const OperationUseMapT &m)
- : ArrayUpdateConversionBase{ctx, a, m} {}
-
- llvm::LogicalResult
- matchAndRewrite(ArrayAccessOp access,
- mlir::PatternRewriter &rewriter) const override {
- auto *op = access.getOperation();
- auto loc = access.getLoc();
- if (analysis.inAmendAccessSet(op)) {
- // This array_access is associated with an array_amend and there is a
- // conflict. Make a copy to store into.
- auto result = referenceToClone(loc, rewriter, access);
- rewriter.replaceOp(access, result);
- return mlir::success();
- }
- rewriter.setInsertionPoint(op);
- auto load = mlir::cast<ArrayLoadOp>(useMap.lookup(op));
- auto coor = genCoorOp(
- rewriter, loc, getEleTy(load.getType()), toRefType(access.getType()),
- load.getMemref(), load.getShape(), load.getSlice(), access.getIndices(),
- load, access->hasAttr(factory::attrFortranArrayOffsets()));
- rewriter.replaceOp(access, coor);
- return mlir::success();
- }
-};
-
-/// An array_amend op is a marker to record which array access is being used to
-/// update an array value. After this pass runs, an array_amend has no
-/// semantics. We rewrite these to undefined values here to remove them while
-/// preserving SSA form.
-class ArrayAmendConversion : public mlir::OpRewritePattern<ArrayAmendOp> {
-public:
- explicit ArrayAmendConversion(mlir::MLIRContext *ctx)
- : OpRewritePattern{ctx} {}
-
- llvm::LogicalResult
- matchAndRewrite(ArrayAmendOp amend,
- mlir::PatternRewriter &rewriter) const override {
- auto *op = amend.getOperation();
- rewriter.setInsertionPoint(op);
- auto loc = amend.getLoc();
- auto undef = UndefOp::create(rewriter, loc, amend.getType());
- rewriter.replaceOp(amend, undef.getResult());
- return mlir::success();
- }
-};
-
-class ArrayValueCopyConverter
- : public fir::impl::ArrayValueCopyBase<ArrayValueCopyConverter> {
-public:
- ArrayValueCopyConverter() = default;
- ArrayValueCopyConverter(const fir::ArrayValueCopyOptions &options)
- : Base(options) {}
-
- void runOnOperation() override {
- auto func = getOperation();
- LLVM_DEBUG(llvm::dbgs() << "\n\narray-value-copy pass on function '"
- << func.getName() << "'\n");
- auto *context = &getContext();
-
- // Perform the conflict analysis.
- const ArrayCopyAnalysisBase *analysis;
- if (optimizeConflicts)
- analysis = &getAnalysis<ArrayCopyAnalysisOptimized>();
- else
- analysis = &getAnalysis<ArrayCopyAnalysis>();
-
- const auto &useMap = analysis->getUseMap();
-
- mlir::RewritePatternSet patterns1(context);
- patterns1.insert<ArrayFetchConversion>(context, useMap);
- patterns1.insert<ArrayUpdateConversion>(context, *analysis, useMap);
- patterns1.insert<ArrayModifyConversion>(context, *analysis, useMap);
- patterns1.insert<ArrayAccessConversion>(context, *analysis, useMap);
- patterns1.insert<ArrayAmendConversion>(context);
- mlir::ConversionTarget target(*context);
- target
- .addLegalDialect<FIROpsDialect, mlir::scf::SCFDialect,
- mlir::arith::ArithDialect, mlir::func::FuncDialect>();
- target.addIllegalOp<ArrayAccessOp, ArrayAmendOp, ArrayFetchOp,
- ArrayUpdateOp, ArrayModifyOp>();
- // Rewrite the array fetch and array update ops.
- if (mlir::failed(
- mlir::applyPartialConversion(func, target, std::move(patterns1)))) {
- mlir::emitError(mlir::UnknownLoc::get(context),
- "failure in array-value-copy pass, phase 1");
- signalPassFailure();
- }
-
- mlir::RewritePatternSet patterns2(context);
- patterns2.insert<ArrayLoadConversion>(context);
- patterns2.insert<ArrayMergeStoreConversion>(context);
- target.addIllegalOp<ArrayLoadOp, ArrayMergeStoreOp>();
- if (mlir::failed(
- mlir::applyPartialConversion(func, target, std::move(patterns2)))) {
- mlir::emitError(mlir::UnknownLoc::get(context),
- "failure in array-value-copy pass, phase 2");
- signalPassFailure();
- }
- }
-};
-} // namespace
-
-std::unique_ptr<mlir::Pass>
-fir::createArrayValueCopyPass(fir::ArrayValueCopyOptions options) {
- return std::make_unique<ArrayValueCopyConverter>(options);
-}
diff --git a/flang/lib/Optimizer/Transforms/CMakeLists.txt b/flang/lib/Optimizer/Transforms/CMakeLists.txt
index 1a4940af95d3e..6d1885ae0b26d 100644
--- a/flang/lib/Optimizer/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/Transforms/CMakeLists.txt
@@ -6,8 +6,6 @@ add_flang_library(FIRTransforms
AffinePromotion.cpp
AlgebraicSimplification.cpp
AnnotateConstant.cpp
- ArrayValueCopy.cpp
- ArrayValueCopy.cpp
AssumedRankOpConversion.cpp
CUDA/CUFAddConstructor.cpp
CUDA/CUFAllocationConversion.cpp
diff --git a/flang/test/Fir/array-copies-pointers.fir b/flang/test/Fir/array-copies-pointers.fir
deleted file mode 100644
index a304e5f84b9e1..0000000000000
--- a/flang/test/Fir/array-copies-pointers.fir
+++ /dev/null
@@ -1,290 +0,0 @@
-// Test array-copy-value pass (copy elision) with array assignment
-// involving Fortran pointers. Focus in only on wether copy ellision
-// is made or not.
-// RUN: fir-opt %s --array-value-copy -split-input-file | FileCheck --check-prefixes=ALL,NOOPT %s
-// RUN: fir-opt %s --array-value-copy="optimize-conflicts=true" -split-input-file | FileCheck --check-prefixes=ALL,OPT %s
-
-// Test `pointer(:) = array(:)`
-// ALL-LABEL: func @maybe_overlap
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-func.func @maybe_overlap(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1 : !fir.ref<!fir.array<100xf32>> {fir.target}) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %3, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %4 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ptr<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `pointer(:) = array(:)`
-// ALL-LABEL: func @no_overlap1
-// OPT-NOT: fir.allocmem
-// NOOPT: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-func.func @no_overlap1(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1 : !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %3, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %4 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ptr<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `pointer(:) = pointer(:)`
-// ALL-LABEL: func @no_overlap
-// ALL-NOT: fir.allocmem
-// ALL: fir.do_loop
-// ALL: fir.array_coor
-// ALL: fir.array_coor
-// ALL: fir.store
-func.func @no_overlap(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
- %4 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %5 = fir.array_update %arg3, %4, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %5 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %3 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ptr<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `array(:) = pointer(:)`
-// ALL-LABEL: func @maybe_overlap_2
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-func.func @maybe_overlap_2(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>> {fir.target}) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %3, %4 to %arg1 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `array(:) = pointer(:)`
-// ALL-LABEL: func @no_overlap_2
-// OPT-NOT: fir.allocmem
-// NOOPT: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-func.func @no_overlap_2(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %3, %4 to %arg1 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `pointer1(:) = pointer2(:)`
-// ALL-LABEL: func @maybe_overlap_3
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-func.func @maybe_overlap_3(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ptr<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %3, %4 to %arg1 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ptr<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test derived_target(:)%i = integer_pointer(:)
-// The integer pointer may be aliasing the derived target component.
-// ALL-LABEL: func @derived_whose_component_may_be_aliased
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<4x!fir.type<some_type{i:i32}>>
-// ALL-COUNT-3: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<4x!fir.type<some_type{i:i32}>>>
-func.func @derived_whose_component_may_be_aliased(%arg0: !fir.box<!fir.array<4x!fir.type<some_type{i:i32}>>> {fir.target}, %arg1: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) {
- %c4 = arith.constant 4 : index
- %0 = fir.field_index i, !fir.type<some_type{i:i32}>
- %c1 = arith.constant 1 : index
- %1 = fir.slice %c1, %c4, %c1 path %0 : (index, index, index, !fir.field) -> !fir.slice<1>
- %2 = fir.array_load %arg0 [%1] : (!fir.box<!fir.array<4x!fir.type<some_type{i:i32}>>>, !fir.slice<1>) -> !fir.array<4xi32>
- %3 = fir.load %arg1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
- %c0 = arith.constant 0 : index
- %4:3 = fir.box_dims %3, %c0 : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, index) -> (index, index, index)
- %5 = fir.shift %4#0 : (index) -> !fir.shift<1>
- %6 = fir.array_load %3(%5) : (!fir.box<!fir.ptr<!fir.array<?xi32>>>, !fir.shift<1>) -> !fir.array<?xi32>
- %7 = arith.subi %c4, %c1 : index
- %8 = fir.do_loop %arg2 = %c0 to %7 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<4xi32>) {
- %9 = fir.array_fetch %6, %arg2 : (!fir.array<?xi32>, index) -> i32
- %10 = fir.array_update %arg3, %9, %arg2 : (!fir.array<4xi32>, i32, index) -> !fir.array<4xi32>
- fir.result %10 : !fir.array<4xi32>
- }
- fir.array_merge_store %2, %8 to %arg0[%1] : !fir.array<4xi32>, !fir.array<4xi32>, !fir.box<!fir.array<4x!fir.type<some_type{i:i32}>>>, !fir.slice<1>
- return
-}
-
-// -----
-
-// Test real_target = complex_target(:)%re
-// The real pointer may be aliasing the complex real part.
-// ALL-LABEL: func @complex_real_aliasing
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<?xf32>
-// ALL-COUNT-3: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<?xf32>>
-func.func @complex_real_aliasing(%arg0: !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>, %arg1: !fir.ref<!fir.array<4xcomplex<f32>>> {fir.target}) {
- %c4 = arith.constant 4 : index
- %0 = fir.load %arg0 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
- %c0 = arith.constant 0 : index
- %1:3 = fir.box_dims %0, %c0 : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, index) -> (index, index, index)
- %2 = fir.shift %1#0 : (index) -> !fir.shift<1>
- %3 = fir.array_load %0(%2) : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, !fir.shift<1>) -> !fir.array<?xf32>
- %c0_i32 = arith.constant 0 : i32
- %4 = fir.shape %c4 : (index) -> !fir.shape<1>
- %c1 = arith.constant 1 : index
- %5 = fir.slice %c1, %c4, %c1 path %c0_i32 : (index, index, index, i32) -> !fir.slice<1>
- %6 = fir.array_load %arg1(%4) [%5] : (!fir.ref<!fir.array<4xcomplex<f32>>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<4xf32>
- %7 = arith.subi %c4, %c1 : index
- %8 = fir.do_loop %arg2 = %c0 to %7 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<?xf32>) {
- %9 = fir.array_fetch %6, %arg2 : (!fir.array<4xf32>, index) -> f32
- %10 = fir.array_update %arg3, %9, %arg2 : (!fir.array<?xf32>, f32, index) -> !fir.array<?xf32>
- fir.result %10 : !fir.array<?xf32>
- }
- fir.array_merge_store %3, %8 to %0 : !fir.array<?xf32>, !fir.array<?xf32>, !fir.box<!fir.ptr<!fir.array<?xf32>>>
- return
-}
-
-// -----
-
-// Test `array(:) = pointer(:)`
-// ALL-LABEL: func @maybe_overlap_3
-// ALL: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.do_loop
-// ALL: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-fir.global @_QMdataEglob target : !fir.array<100xf32> {
- %0 = fir.undefined !fir.array<100xf32>
- fir.has_value %0 : !fir.array<100xf32>
-}
-
-func.func @maybe_overlap_3(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>> {fir.target}) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.address_of(@_QMdataEglob) : !fir.ref<!fir.array<100xf32>>
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %3, %4 to %0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-// -----
-
-// Test `array(:) = pointer(:)`
-// ALL-LABEL: func @no_overlap_3
-// OPT-NOT: fir.allocmem
-// NOOPT: %[[ALLOC:.*]] = fir.allocmem !fir.array<100xf32>
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.do_loop
-// NOOPT: fir.freemem %[[ALLOC]] : !fir.heap<!fir.array<100xf32>>
-fir.global @_QMdataEglob : !fir.array<100xf32> {
- %0 = fir.undefined !fir.array<100xf32>
- fir.has_value %0 : !fir.array<100xf32>
-}
-
-func.func @no_overlap_3(%arg0: !fir.ptr<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>> {fir.target}) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.address_of(@_QMdataEglob) : !fir.ref<!fir.array<100xf32>>
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ptr<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %3) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %2, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6 = fir.array_update %arg3, %5, %arg2 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %6 : !fir.array<100xf32>
- }
- fir.array_merge_store %3, %4 to %0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
diff --git a/flang/test/Fir/array-modify.fir b/flang/test/Fir/array-modify.fir
deleted file mode 100644
index 09c9516ec57d3..0000000000000
--- a/flang/test/Fir/array-modify.fir
+++ /dev/null
@@ -1,131 +0,0 @@
-// Test array-copy-value pass (copy elision) with fir.array_modify
-// RUN: fir-opt %s --array-value-copy | FileCheck %s
-// RUN: fir-opt %s --array-value-copy="optimize-conflicts=true" | FileCheck %s
-
-// Test user_defined_assignment(arg0(:), arg1(:))
-func.func @no_overlap(%arg0: !fir.ref<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %3, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6:2 = fir.array_modify %arg3, %arg2 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
- fir.store %5 to %0 : !fir.ref<f32>
- fir.call @user_defined_assignment(%6#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
- fir.result %6#1 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %4 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-// CHECK-LABEL: func @no_overlap(
-// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<100xf32>>,
-// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.array<100xf32>>) {
-// CHECK-DAG: %[[VAL_2:.*]] = arith.constant 100 : index
-// CHECK-DAG: %[[VAL_3:.*]] = arith.constant 99 : index
-// CHECK-DAG: %[[VAL_4:.*]] = arith.constant 1 : index
-// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 0 : index
-// CHECK: %[[VAL_6:.*]] = fir.alloca f32
-// CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
-// CHECK: %[[VAL_8:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[VAL_9:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[VAL_10:.*]] = fir.do_loop %[[VAL_11:.*]] = %[[VAL_5]] to %[[VAL_3]] step %[[VAL_4]] unordered iter_args(%[[VAL_12:.*]] = %[[VAL_8]]) -> (!fir.array<100xf32>) {
-// CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_14:.*]] = arith.addi %[[VAL_11]], %[[VAL_13]] : index
-// CHECK: %[[VAL_15:.*]] = fir.array_coor %[[VAL_1]](%[[VAL_7]]) %[[VAL_14]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_15]] : !fir.ref<f32>
-// CHECK: %[[VAL_17:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_18:.*]] = arith.addi %[[VAL_11]], %[[VAL_17]] : index
-// CHECK: %[[VAL_19:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_18]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: fir.store %[[VAL_16]] to %[[VAL_6]] : !fir.ref<f32>
-// CHECK: fir.call @user_defined_assignment(%[[VAL_19]], %[[VAL_6]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
-// CHECK: fir.result %[[VAL_8]] : !fir.array<100xf32>
-// CHECK: }
-// CHECK: return
-// CHECK: }
-
-
-// Test user_defined_assignment(arg0(:), arg0(100:1:-1))
-func.func @overlap(%arg0: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c-1 = arith.constant -1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.slice %c100, %c1, %c-1 : (index, index, index) -> !fir.slice<1>
- %4 = fir.array_load %arg0(%1) [%3] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<100xf32>
- %5 = fir.do_loop %arg1 = %c0 to %c99 step %c1 unordered iter_args(%arg2 = %2) -> (!fir.array<100xf32>) {
- %6 = fir.array_fetch %4, %arg1 : (!fir.array<100xf32>, index) -> f32
- %7:2 = fir.array_modify %arg2, %arg1 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
- fir.store %6 to %0 : !fir.ref<f32>
- fir.call @user_defined_assignment(%7#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
- fir.result %7#1 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %5 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-// CHECK-LABEL: func @overlap(
-// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<100xf32>>) {
-// CHECK-DAG: %[[VAL_1:.*]] = arith.constant 100 : index
-// CHECK-DAG: %[[VAL_2:.*]] = arith.constant 99 : index
-// CHECK-DAG: %[[VAL_3:.*]] = arith.constant 1 : index
-// CHECK-DAG: %[[VAL_4:.*]] = arith.constant -1 : index
-// CHECK-DAG: %[[VAL_5:.*]] = arith.constant 0 : index
-// CHECK: %[[VAL_6:.*]] = fir.alloca f32
-// CHECK: %[[VAL_7:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1>
-// CHECK: %[[VAL_8:.*]] = fir.allocmem !fir.array<100xf32>{{$}}
-// CHECK: %[[VAL_9:.*]] = fir.convert %[[VAL_1]] : (index) -> index
-// CHECK: %[[VAL_10:.*]] = arith.constant 0 : index
-// CHECK: %[[VAL_11:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_12:.*]] = arith.subi %[[VAL_9]], %[[VAL_11]] : index
-// CHECK: fir.do_loop %[[VAL_13:.*]] = %[[VAL_10]] to %[[VAL_12]] step %[[VAL_11]] {
-// CHECK: %[[VAL_14:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_15:.*]] = arith.addi %[[VAL_13]], %[[VAL_14]] : index
-// CHECK: %[[VAL_16:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_15]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_18:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_19:.*]] = arith.addi %[[VAL_13]], %[[VAL_18]] : index
-// CHECK: %[[VAL_20:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_19]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_17:.*]] = fir.load %[[VAL_16]] : !fir.ref<f32>
-// CHECK: fir.store %[[VAL_17]] to %[[VAL_20]] : !fir.ref<f32>
-// CHECK: }
-// CHECK: %[[VAL_21:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[VAL_22:.*]] = fir.slice %[[VAL_1]], %[[VAL_3]], %[[VAL_4]] : (index, index, index) -> !fir.slice<1>
-// CHECK: %[[VAL_23:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[VAL_24:.*]] = fir.do_loop %[[VAL_25:.*]] = %[[VAL_5]] to %[[VAL_2]] step %[[VAL_3]] unordered iter_args(%[[VAL_26:.*]] = %[[VAL_21]]) -> (!fir.array<100xf32>) {
-// CHECK: %[[VAL_27:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_28:.*]] = arith.addi %[[VAL_25]], %[[VAL_27]] : index
-// CHECK: %[[VAL_29:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) {{\[}}%[[VAL_22]]] %[[VAL_28]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_30:.*]] = fir.load %[[VAL_29]] : !fir.ref<f32>
-// CHECK: %[[VAL_31:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_32:.*]] = arith.addi %[[VAL_25]], %[[VAL_31]] : index
-// CHECK: %[[VAL_33:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_32]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: fir.store %[[VAL_30]] to %[[VAL_6]] : !fir.ref<f32>
-// CHECK: fir.call @user_defined_assignment(%[[VAL_33]], %[[VAL_6]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
-// CHECK: fir.result %[[VAL_21]] : !fir.array<100xf32>
-// CHECK: }
-// CHECK: %[[VAL_34:.*]] = fir.convert %[[VAL_1]] : (index) -> index
-// CHECK: %[[VAL_35:.*]] = arith.constant 0 : index
-// CHECK: %[[VAL_36:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_37:.*]] = arith.subi %[[VAL_34]], %[[VAL_36]] : index
-// CHECK: fir.do_loop %[[VAL_38:.*]] = %[[VAL_35]] to %[[VAL_37]] step %[[VAL_36]] {
-// CHECK: %[[VAL_39:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_40:.*]] = arith.addi %[[VAL_38]], %[[VAL_39]] : index
-// CHECK: %[[VAL_41:.*]] = fir.array_coor %[[VAL_8]](%[[VAL_7]]) %[[VAL_40]] : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_43:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_44:.*]] = arith.addi %[[VAL_38]], %[[VAL_43]] : index
-// CHECK: %[[VAL_45:.*]] = fir.array_coor %[[VAL_0]](%[[VAL_7]]) %[[VAL_44]] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[VAL_42:.*]] = fir.load %[[VAL_41]] : !fir.ref<f32>
-// CHECK: fir.store %[[VAL_42]] to %[[VAL_45]] : !fir.ref<f32>
-// CHECK: }
-// CHECK: fir.freemem %[[VAL_8]] : !fir.heap<!fir.array<100xf32>>
-// CHECK: return
-// CHECK: }
-
-func.func private @user_defined_assignment(!fir.ref<f32>, !fir.ref<f32>)
diff --git a/flang/test/Fir/array-value-copy-2.fir b/flang/test/Fir/array-value-copy-2.fir
deleted file mode 100644
index 21b340af10c6b..0000000000000
--- a/flang/test/Fir/array-value-copy-2.fir
+++ /dev/null
@@ -1,87 +0,0 @@
-// RUN: fir-opt --array-value-copy --cfg-conversion %s | FileCheck %s
-// RUN: fir-opt --array-value-copy="optimize-conflicts=true" --cfg-conversion %s | FileCheck %s
-
-// CHECK-LABEL: func @_QPslice1(
-// CHECK-NOT: fir.allocmem
-// CHECK: ^bb{{[0-9]+}}(%{{[0-9]+}}: index,
-// CHECK-NOT: ^bb{{[0-9]+}}(%{{[0-9]+}}: index,
-// CHECK: return
-func.func @_QPslice1(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.ref<i32>, %arg2: !fir.ref<i32>) {
- %c1_i64 = arith.constant 1 : i64
- %0 = fir.convert %c1_i64 : (i64) -> index
- %1 = fir.load %arg1 : !fir.ref<i32>
- %2 = fir.convert %1 : (i32) -> i64
- %3 = fir.convert %2 : (i64) -> index
- %c0 = arith.constant 0 : index
- %4 = arith.subi %3, %0 : index
- %5 = arith.addi %4, %0 : index
- %6 = arith.divsi %5, %0 : index
- %7 = arith.cmpi sgt, %6, %c0 : index
- %8 = arith.select %7, %6, %c0 : index
- %9 = fir.slice %0, %3, %0 : (index, index, index) -> !fir.slice<1>
- %10 = fir.array_load %arg0 [%9] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.array<?xf32>
- %c1_i32 = arith.constant 1 : i32
- %11 = arith.addi %1, %c1_i32 : i32
- %12 = fir.convert %11 : (i32) -> i64
- %13 = fir.convert %12 : (i64) -> index
- %14 = fir.load %arg2 : !fir.ref<i32>
- %15 = fir.convert %14 : (i32) -> i64
- %16 = fir.convert %15 : (i64) -> index
- %17 = fir.slice %13, %16, %0 : (index, index, index) -> !fir.slice<1>
- %18 = fir.array_load %arg0 [%17] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.array<?xf32>
- %c1 = arith.constant 1 : index
- %19 = arith.subi %8, %c1 : index
- %20 = fir.do_loop %arg3 = %c0 to %19 step %c1 unordered iter_args(%arg4 = %10) -> (!fir.array<?xf32>) {
- %21 = fir.array_fetch %18, %arg3 : (!fir.array<?xf32>, index) -> f32
- %22 = fir.array_update %arg4, %21, %arg3 : (!fir.array<?xf32>, f32, index) -> !fir.array<?xf32>
- fir.result %22 : !fir.array<?xf32>
- }
- fir.array_merge_store %10, %20 to %arg0[%9] : !fir.array<?xf32>, !fir.array<?xf32>, !fir.box<!fir.array<?xf32>>, !fir.slice<1>
- return
-}
-
-// CHECK-LABEL: func @_QPslice2(
-// CHECK: fir.allocmem
-// CHECK: ^bb{{[0-9]+}}(%{{[0-9]+}}: index,
-// CHECK: ^bb{{[0-9]+}}(%{{[0-9]+}}: index,
-// CHECK: fir.freemem
-// CHECK: return
-func.func @_QPslice2(%arg0: !fir.box<!fir.array<?xf32>>, %arg1: !fir.ref<i32>, %arg2: !fir.ref<i32>) {
- %c1_i64 = arith.constant 1 : i64
- %0 = fir.convert %c1_i64 : (i64) -> index
- %c1_i64_0 = arith.constant 1 : i64
- %1 = fir.convert %c1_i64_0 : (i64) -> index
- %2 = fir.load %arg1 : !fir.ref<i32>
- %3 = fir.convert %2 : (i32) -> i64
- %4 = fir.convert %3 : (i64) -> index
- %c0 = arith.constant 0 : index
- %5 = arith.subi %4, %0 : index
- %6 = arith.addi %5, %1 : index
- %7 = arith.divsi %6, %1 : index
- %8 = arith.cmpi sgt, %7, %c0 : index
- %9 = arith.select %8, %7, %c0 : index
- %10 = fir.slice %0, %4, %1 : (index, index, index) -> !fir.slice<1>
- %11 = fir.array_load %arg0 [%10] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.array<?xf32>
- %12 = fir.load %arg1 : !fir.ref<i32>
- %c4_i32 = arith.constant 4 : i32
- %13 = arith.subi %12, %c4_i32 : i32
- %14 = fir.convert %13 : (i32) -> i64
- %15 = fir.convert %14 : (i64) -> index
- %c1_i64_1 = arith.constant 1 : i64
- %16 = fir.convert %c1_i64_1 : (i64) -> index
- %17 = fir.load %arg2 : !fir.ref<i32>
- %18 = fir.convert %17 : (i32) -> i64
- %19 = fir.convert %18 : (i64) -> index
- %20 = fir.slice %15, %19, %16 : (index, index, index) -> !fir.slice<1>
- %21 = fir.array_load %arg0 [%20] : (!fir.box<!fir.array<?xf32>>, !fir.slice<1>) -> !fir.array<?xf32>
- %c1 = arith.constant 1 : index
- %c0_2 = arith.constant 0 : index
- %22 = arith.subi %9, %c1 : index
- %23 = fir.do_loop %arg3 = %c0_2 to %22 step %c1 unordered iter_args(%arg4 = %11) -> (!fir.array<?xf32>) {
- %24 = fir.array_fetch %21, %arg3 : (!fir.array<?xf32>, index) -> f32
- %25 = fir.array_update %arg4, %24, %arg3 : (!fir.array<?xf32>, f32, index) -> !fir.array<?xf32>
- fir.result %25 : !fir.array<?xf32>
- }
- fir.array_merge_store %11, %23 to %arg0[%10] : !fir.array<?xf32>, !fir.array<?xf32>, !fir.box<!fir.array<?xf32>>, !fir.slice<1>
- return
-}
diff --git a/flang/test/Fir/array-value-copy-3.fir b/flang/test/Fir/array-value-copy-3.fir
deleted file mode 100644
index 945a857e46669..0000000000000
--- a/flang/test/Fir/array-value-copy-3.fir
+++ /dev/null
@@ -1,56 +0,0 @@
-// Test overlapping assignment of derived type arrays with allocatable components.
-// This requires initializing the allocatable components to an unallocated status
-// before they can be used in component assignments, and to deallocate the components
-// that may have been allocated in the end.
-
-// RUN: fir-opt --array-value-copy %s | FileCheck %s
-// RUN: fir-opt --array-value-copy="optimize-conflicts=true" %s | FileCheck %s
-
-
-!t_with_alloc_comp = !fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>
-func.func private @custom_assign(!fir.ref<!t_with_alloc_comp>, !fir.ref<!t_with_alloc_comp>)
-func.func @test_overlap_with_alloc_components(%arg0: !fir.ref<!fir.array<10x!t_with_alloc_comp>>) {
- %0 = fir.alloca !fir.box<!t_with_alloc_comp>
- %c10 = arith.constant 10 : index
- %c9 = arith.constant 9 : index
- %c1 = arith.constant 1 : index
- %c-1 = arith.constant -1 : index
- %c0 = arith.constant 0 : index
- %1 = fir.shape %c10 : (index) -> !fir.shape<1>
- %6 = fir.slice %c10, %c1, %c-1 : (index, index, index) -> !fir.slice<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<10x!t_with_alloc_comp>>, !fir.shape<1>) -> !fir.array<10x!t_with_alloc_comp>
- %7 = fir.array_load %arg0(%1) [%6] : (!fir.ref<!fir.array<10x!t_with_alloc_comp>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10x!t_with_alloc_comp>
- %9 = fir.do_loop %arg1 = %c0 to %c9 step %c1 unordered iter_args(%arg2 = %2) -> (!fir.array<10x!t_with_alloc_comp>) {
- %10 = fir.array_access %7, %arg1 : (!fir.array<10x!t_with_alloc_comp>, index) -> !fir.ref<!t_with_alloc_comp>
- %11 = fir.array_access %arg2, %arg1 : (!fir.array<10x!t_with_alloc_comp>, index) -> !fir.ref<!t_with_alloc_comp>
- fir.call @custom_assign(%11, %10) : (!fir.ref<!t_with_alloc_comp>, !fir.ref<!t_with_alloc_comp>) -> ()
- %19 = fir.array_amend %arg2, %11 : (!fir.array<10x!t_with_alloc_comp>, !fir.ref<!t_with_alloc_comp>) -> !fir.array<10x!t_with_alloc_comp>
- fir.result %19 : !fir.array<10x!t_with_alloc_comp>
- }
- fir.array_merge_store %2, %9 to %arg0 : !fir.array<10x!t_with_alloc_comp>, !fir.array<10x!t_with_alloc_comp>, !fir.ref<!fir.array<10x!t_with_alloc_comp>>
- return
-}
-
-// CHECK-LABEL: func @test_overlap_with_alloc_components(
-// CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>) {
-// CHECK: %[[VAL_4:.*]] = arith.constant 10 : index
-// CHECK: %[[VAL_6:.*]] = arith.constant 1 : index
-// CHECK: %[[VAL_7:.*]] = arith.constant -1 : index
-// CHECK: %[[VAL_9:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
-// CHECK: %[[VAL_10:.*]] = fir.slice %[[VAL_4]], %[[VAL_6]], %[[VAL_7]] : (index, index, index) -> !fir.slice<1>
-// CHECK: %[[VAL_11:.*]] = fir.allocmem !fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
-// CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_11]](%[[VAL_9]]) : (!fir.heap<!fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>>
-// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_12]] : (!fir.box<!fir.heap<!fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>>) -> !fir.box<none>
-// CHECK: fir.call @_FortranAInitialize(%[[VAL_16]], %{{.*}}, %{{.*}}) : (!fir.box<none>, !fir.ref<i8>, i32) -> ()
-// CHECK: fir.do_loop {{.*}} {
-// CHECK: fir.call @_FortranAAssign
-// CHECK: }
-// CHECK: fir.do_loop {{.*}} {
-// CHECK: fir.call @custom_assign
-// CHECK: }
-// CHECK: fir.do_loop %{{.*}} {
-// CHECK: fir.call @_FortranAAssign
-// CHECK: }
-// CHECK: %[[VAL_72:.*]] = fir.convert %[[VAL_12]] : (!fir.box<!fir.heap<!fir.array<10x!fir.type<t{i:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>>) -> !fir.box<none>
-// CHECK: fir.call @_FortranADestroy(%[[VAL_72]]) : (!fir.box<none>) -> ()
-// CHECK: fir.freemem %[[VAL_11]]
diff --git a/flang/test/Fir/array-value-copy-4.fir b/flang/test/Fir/array-value-copy-4.fir
deleted file mode 100644
index 85def88e6887c..0000000000000
--- a/flang/test/Fir/array-value-copy-4.fir
+++ /dev/null
@@ -1,58 +0,0 @@
-// Test conversion of array_load with array of derived-type.
-// Conversion was previously crashing as reported in
-// https://github.com/llvm/llvm-project/issues/59342.
-
-// RUN: fir-opt --array-value-copy %s | FileCheck %s
-// RUN: fir-opt --array-value-copy="optimize-conflicts=true" %s | FileCheck %s
-
-func.func @_QMmodPsub1(%arg0: !fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>> {fir.bindc_name = "x"}) {
- %0 = fir.alloca !fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %c3_i64 = arith.constant 3 : i64
- %1 = fir.convert %c3_i64 : (i64) -> index
- %c1_i64 = arith.constant 1 : i64
- %2 = fir.convert %c1_i64 : (i64) -> index
- %c4_i64 = arith.constant 4 : i64
- %3 = fir.convert %c4_i64 : (i64) -> index
- %c0 = arith.constant 0 : index
- %4 = arith.subi %3, %1 : index
- %5 = arith.addi %4, %2 : index
- %6 = arith.divsi %5, %2 : index
- %7 = arith.cmpi sgt, %6, %c0 : index
- %8 = arith.select %7, %6, %c0 : index
- %9 = fir.slice %1, %3, %2 : (index, index, index) -> !fir.slice<1>
- %10 = fir.array_load %arg0 [%9] : (!fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, !fir.slice<1>) -> !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %c1_i64_0 = arith.constant 1 : i64
- %11 = fir.convert %c1_i64_0 : (i64) -> index
- %c1_i64_1 = arith.constant 1 : i64
- %12 = fir.convert %c1_i64_1 : (i64) -> index
- %c2_i64 = arith.constant 2 : i64
- %13 = fir.convert %c2_i64 : (i64) -> index
- %14 = fir.slice %11, %13, %12 : (index, index, index) -> !fir.slice<1>
- %15 = fir.array_load %arg0 [%14] : (!fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, !fir.slice<1>) -> !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %c1 = arith.constant 1 : index
- %c0_2 = arith.constant 0 : index
- %16 = arith.subi %8, %c1 : index
- %17 = fir.do_loop %arg1 = %c0_2 to %16 step %c1 unordered iter_args(%arg2 = %10) -> (!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) {
- %18 = fir.array_access %15, %arg1 : (!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %19 = fir.array_access %arg2, %arg1 : (!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, index) -> !fir.ref<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %20 = fir.embox %19 : (!fir.ref<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- %21 = fir.embox %18 : (!fir.ref<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- fir.store %20 to %0 : !fir.ref<!fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>
- %22 = fir.address_of(@_QQclX2E2F64756D6D792E66393000) : !fir.ref<!fir.char<1,12>>
- %c9_i32 = arith.constant 9 : i32
- %23 = fir.convert %0 : (!fir.ref<!fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>) -> !fir.ref<!fir.box<none>>
- %24 = fir.convert %21 : (!fir.box<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.box<none>
- %25 = fir.convert %22 : (!fir.ref<!fir.char<1,12>>) -> !fir.ref<i8>
- fir.call @_FortranAAssign(%23, %24, %25, %c9_i32) fastmath<contract> : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> ()
- %27 = fir.array_amend %arg2, %19 : (!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.ref<!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>) -> !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- fir.result %27 : !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>
- }
- fir.array_merge_store %10, %17 to %arg0[%9] : !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, !fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, !fir.slice<1>
- return
-}
-
-// CHECK-LABEL: func.func @_QMmodPsub1(
-// CHECK-SAME: %[[ARG0:.*]]: !fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>> {fir.bindc_name = "x"}) {
-// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[ARG0]], %c0{{.*}} : (!fir.box<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, index) -> (index, index, index)
-// CHECK: %[[ALLOCMEM:.*]] = fir.allocmem !fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>, %[[BOX_DIMS]]#1
-// CHECK: %{{.*}} = fir.embox %[[ALLOCMEM]](%{{.*}}) : (!fir.heap<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>, !fir.shape<1>) -> !fir.box<!fir.heap<!fir.array<?x!fir.type<_QMmodTrec1{dat:!fir.box<!fir.heap<!fir.array<?xi32>>>}>>>>
diff --git a/flang/test/Fir/array-value-copy-cam4.fir b/flang/test/Fir/array-value-copy-cam4.fir
deleted file mode 100644
index 3bb465cfaa59f..0000000000000
--- a/flang/test/Fir/array-value-copy-cam4.fir
+++ /dev/null
@@ -1,102 +0,0 @@
-// RUN: fir-opt --array-value-copy %s | FileCheck --check-prefix=NOOPT %s
-// RUN: fir-opt --array-value-copy="optimize-conflicts=true" %s | FileCheck --check-prefix=OPT %s
-
-// Reproducer from SPEC CPU2017/527.cam4_r:
-// module cam4
-// type, public :: pbuf_fld
-// real*8, pointer, dimension(:,:,:,:,:) :: fld_ptr
-// end type pbuf_fld
-// contains
-// subroutine test(pbuf, ncol, lchnk, time_index, kvh_idx)
-// implicit none
-// interface
-// subroutine init(kvh)
-// real*8, intent(out) :: kvh(4,27)
-// end subroutine init
-// end interface
-// type(pbuf_fld), intent(inout), dimension(1000) :: pbuf
-// real*8 :: kvh(4,27)
-// integer kvh_idx, ncol, lchnk, time_index
-// call init(kvh)
-// pbuf(kvh_idx)%fld_ptr(1,1:ncol,1:27,lchnk,time_index) = kvh(:ncol,:)
-// end subroutine test
-// end module cam4
-
-// Verify that no memory is allocated for the temporary array,
-// when compiling with optimizations:
-// OPT-NOT: fir.allocmem
-// NOOPT: fir.allocmem
-
-module {
- func.func @_QMcam4Ptest(%arg0: !fir.ref<!fir.array<1000x!fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>>> {fir.bindc_name = "pbuf"}, %arg1: !fir.ref<i32> {fir.bindc_name = "ncol"}, %arg2: !fir.ref<i32> {fir.bindc_name = "lchnk"}, %arg3: !fir.ref<i32> {fir.bindc_name = "time_index"}, %arg4: !fir.ref<i32> {fir.bindc_name = "kvh_idx"}) {
- %c4 = arith.constant 4 : index
- %c27 = arith.constant 27 : index
- %0 = fir.alloca !fir.array<4x27xf64> {bindc_name = "kvh", uniq_name = "_QMcam4FtestEkvh"}
- fir.call @_QPinit(%0) fastmath<contract> : (!fir.ref<!fir.array<4x27xf64>>) -> ()
- %1 = fir.load %arg4 : !fir.ref<i32>
- %2 = fir.convert %1 : (i32) -> i64
- %c1_i64 = arith.constant 1 : i64
- %3 = arith.subi %2, %c1_i64 : i64
- %4 = fir.coordinate_of %arg0, %3 : (!fir.ref<!fir.array<1000x!fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>>>, i64) -> !fir.ref<!fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>>
- %5 = fir.field_index fld_ptr, !fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>
- %6 = fir.coordinate_of %4, %5 : (!fir.ref<!fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>>, !fir.field) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>>
- %7 = fir.load %6 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>>
- %c0 = arith.constant 0 : index
- %8:3 = fir.box_dims %7, %c0 : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, index) -> (index, index, index)
- %c1 = arith.constant 1 : index
- %9:3 = fir.box_dims %7, %c1 : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, index) -> (index, index, index)
- %c2 = arith.constant 2 : index
- %10:3 = fir.box_dims %7, %c2 : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, index) -> (index, index, index)
- %c3 = arith.constant 3 : index
- %11:3 = fir.box_dims %7, %c3 : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, index) -> (index, index, index)
- %12:3 = fir.box_dims %7, %c4 : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, index) -> (index, index, index)
- %13 = fir.undefined index
- %14 = fir.convert %c1_i64 : (i64) -> index
- %15 = arith.subi %14, %8#0 : index
- %16 = fir.load %arg1 : !fir.ref<i32>
- %17 = fir.convert %16 : (i32) -> i64
- %18 = fir.convert %17 : (i64) -> index
- %19 = arith.subi %18, %14 : index
- %20 = arith.addi %19, %14 : index
- %21 = arith.divsi %20, %14 : index
- %22 = arith.cmpi sgt, %21, %c0 : index
- %23 = arith.select %22, %21, %c0 : index
- %c27_i64 = arith.constant 27 : i64
- %24 = fir.convert %c27_i64 : (i64) -> index
- %25 = arith.subi %24, %14 : index
- %26 = arith.addi %25, %14 : index
- %27 = arith.divsi %26, %14 : index
- %28 = arith.cmpi sgt, %27, %c0 : index
- %29 = arith.select %28, %27, %c0 : index
- %30 = fir.load %arg2 : !fir.ref<i32>
- %31 = fir.convert %30 : (i32) -> i64
- %32 = fir.convert %31 : (i64) -> index
- %33 = arith.subi %32, %11#0 : index
- %34 = fir.load %arg3 : !fir.ref<i32>
- %35 = fir.convert %34 : (i32) -> i64
- %36 = fir.convert %35 : (i64) -> index
- %37 = arith.subi %36, %12#0 : index
- %38 = fir.shift %8#0, %9#0, %10#0, %11#0, %12#0 : (index, index, index, index, index) -> !fir.shift<5>
- %39 = fir.slice %c1_i64, %13, %13, %14, %18, %14, %14, %24, %14, %31, %13, %13, %35, %13, %13 : (i64, index, index, index, index, index, index, index, index, i64, index, index, i64, index, index) -> !fir.slice<5>
- %40 = fir.array_load %7(%38) [%39] : (!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, !fir.shift<5>, !fir.slice<5>) -> !fir.array<?x?x?x?x?xf64>
- %41 = arith.addi %c1, %c27 : index
- %42 = arith.subi %41, %c1 : index
- %43 = fir.shape %c4, %c27 : (index, index) -> !fir.shape<2>
- %44 = fir.slice %c1, %18, %14, %c1, %42, %14 : (index, index, index, index, index, index) -> !fir.slice<2>
- %45 = fir.array_load %0(%43) [%44] : (!fir.ref<!fir.array<4x27xf64>>, !fir.shape<2>, !fir.slice<2>) -> !fir.array<4x27xf64>
- %46 = arith.subi %23, %c1 : index
- %47 = arith.subi %29, %c1 : index
- %48 = fir.do_loop %arg5 = %c0 to %47 step %c1 unordered iter_args(%arg6 = %40) -> (!fir.array<?x?x?x?x?xf64>) {
- %49 = fir.do_loop %arg7 = %c0 to %46 step %c1 unordered iter_args(%arg8 = %arg6) -> (!fir.array<?x?x?x?x?xf64>) {
- %50 = fir.array_fetch %45, %arg7, %arg5 : (!fir.array<4x27xf64>, index, index) -> f64
- %51 = fir.array_update %arg8, %50, %15, %arg7, %arg5, %33, %37 : (!fir.array<?x?x?x?x?xf64>, f64, index, index, index, index, index) -> !fir.array<?x?x?x?x?xf64>
- fir.result %51 : !fir.array<?x?x?x?x?xf64>
- }
- fir.result %49 : !fir.array<?x?x?x?x?xf64>
- }
- fir.array_merge_store %40, %48 to %7[%39] : !fir.array<?x?x?x?x?xf64>, !fir.array<?x?x?x?x?xf64>, !fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>, !fir.slice<5>
- return
- }
- func.func private @_QPinit(!fir.ref<!fir.array<4x27xf64>>)
- fir.type_info @_QMcam4Tpbuf_fld : !fir.type<_QMcam4Tpbuf_fld{fld_ptr:!fir.box<!fir.ptr<!fir.array<?x?x?x?x?xf64>>>}>
-}
diff --git a/flang/test/Fir/array-value-copy.fir b/flang/test/Fir/array-value-copy.fir
deleted file mode 100644
index 3d44407b5fcf8..0000000000000
--- a/flang/test/Fir/array-value-copy.fir
+++ /dev/null
@@ -1,533 +0,0 @@
-// Test for the array-value-copy pass
-// RUN: fir-opt --split-input-file --array-value-copy %s | FileCheck %s
-
-// Test simple fir.array_load/fir.array_fetch conversion to fir.array_coor
-func.func @array_fetch_conversion(%arr1 : !fir.ref<!fir.array<?x?xf32>>, %m: index, %n: index) {
- %c10 = arith.constant 10 : index
- %c20 = arith.constant 20 : index
- %s = fir.shape %m, %n : (index, index) -> !fir.shape<2>
- %av1 = fir.array_load %arr1(%s) : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.array<?x?xf32>
- %f = fir.array_fetch %av1, %c10, %c20 : (!fir.array<?x?xf32>, index, index) -> f32
- return
-}
-
-// CHECK-LABEL: func @array_fetch_conversion(
-// CHECK-SAME: %[[ARRAY:.*]]: !fir.ref<!fir.array<?x?xf32>>,
-// CHECK-SAME: %[[ARG1:.*]]: index,
-// CHECK-SAME: %[[ARG2:.*]]: index) {
-// CHECK: %{{.*}} = fir.shape %[[ARG1]], %[[ARG2]] : (index, index) -> !fir.shape<2>
-// CHECK: %{{.*}} = fir.undefined !fir.array<?x?xf32>
-// CHECK: %[[VAL_0:.*]] = arith.addi %{{.*}}, %{{.*}} : index
-// CHECK: %[[VAL_1:.*]] = arith.addi %{{.*}}, %{{.*}} : index
-// CHECK-NOT: fir.array_load
-// CHECK-NOT: fir.array_fetch
-// CHECK: %{{.*}} = fir.array_coor %arg0(%0) %[[VAL_0]], %[[VAL_1]] : (!fir.ref<!fir.array<?x?xf32>>, !fir.shape<2>, index, index) -> !fir.ref<f32>
-// CHECK: %{{.*}} = fir.load %4 : !fir.ref<f32>
-
-// -----
-
-// Test simple fir.array_load/fir.array_update conversion without copy-in/copy-out
-func.func @array_update_conversion(%arr1 : !fir.box<!fir.array<?x?xf32>>, %m: index, %n: index) {
- %c10 = arith.constant 10 : index
- %c20 = arith.constant 20 : index
- %c1 = arith.constant 1 : index
- %f = arith.constant 2.0 : f32
- %s = fir.shape %m, %n : (index, index) -> !fir.shape<2>
- %av1 = fir.array_load %arr1(%s) : (!fir.box<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.array<?x?xf32>
- %av2 = fir.array_update %av1, %f, %c1, %c1 : (!fir.array<?x?xf32>, f32, index, index) -> !fir.array<?x?xf32>
- return
-}
-
-// CHECK-LABEL: func @array_update_conversion
-// CHECK-NOT: fir.array_load
-// CHECK-NOT: fir.array_update
-// CHECK: %{{.*}} = arith.addi %{{.*}}, %{{.*}} : index
-// CHECK: %{{.*}} = arith.addi %{{.*}}, %{{.*}} : index
-// CHECK: %[[ARRAY_COOR:.*]] = fir.array_coor{{.*}}-> !fir.ref<f32>
-// CHECK: fir.store %{{.*}} to %[[ARRAY_COOR]] : !fir.ref<f32>
-
-// -----
-
-// Test simple fir.array_load/fir.array_update conversion without copy-in/copy-out
-func.func @array_update_conversion(%arr1 : !fir.box<!fir.array<?x?xf32>>, %m: index, %n: index, %cond: i1) {
- %c10 = arith.constant 10 : index
- %c20 = arith.constant 20 : index
- %c1 = arith.constant 1 : index
- %f = arith.constant 2.0 : f32
- %g = arith.constant 4.0 : f32
- %s = fir.shape %m, %n : (index, index) -> !fir.shape<2>
- %av1 = fir.array_load %arr1(%s) : (!fir.box<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.array<?x?xf32>
- fir.if %cond {
- %av2 = fir.array_update %av1, %f, %c1, %c1 : (!fir.array<?x?xf32>, f32, index, index) -> !fir.array<?x?xf32>
- } else {
- %av2 = fir.array_update %av1, %g, %c1, %c1 : (!fir.array<?x?xf32>, f32, index, index) -> !fir.array<?x?xf32>
- }
- return
-}
-
-// -----
-
-// Test fir.array_load/fir.array_fetch/fir.array_update conversion with
-// an introduced copy-in/copy-out.
-//
-// This test corresponds to a simplified FIR version of the following Fortran
-// code.
-// ```
-// integer :: i(10)
-// i = i(10:1:-1)
-// end
-// ```
-
-func.func @conversion_with_temporary(%arr0 : !fir.ref<!fir.array<10xi32>>) {
- %c10 = arith.constant 10 : index
- %1 = fir.shape %c10 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arr0(%1) : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>) -> !fir.array<10xi32>
- %c10_i64 = arith.constant 10 : i64
- %3 = fir.convert %c10_i64 : (i64) -> index
- %c1_i64 = arith.constant 1 : i64
- %c-1_i64 = arith.constant -1 : i64
- %4 = fir.shape %c10 : (index) -> !fir.shape<1>
- %5 = fir.slice %c10_i64, %c1_i64, %c-1_i64 : (i64, i64, i64) -> !fir.slice<1>
- %6 = fir.array_load %arr0(%4) [%5] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %7 = arith.subi %3, %c1 : index
- %8 = fir.do_loop %arg0 = %c0 to %7 step %c1 unordered iter_args(%arg1 = %2) -> (!fir.array<10xi32>) {
- %9 = fir.array_fetch %6, %arg0 : (!fir.array<10xi32>, index) -> i32
- %10 = fir.array_update %arg1, %9, %arg0 : (!fir.array<10xi32>, i32, index) -> !fir.array<10xi32>
- fir.result %10 : !fir.array<10xi32>
- }
- fir.array_merge_store %2, %8 to %arr0 : !fir.array<10xi32>, !fir.array<10xi32>, !fir.ref<!fir.array<10xi32>>
- return
-}
-
-// CHECK-LABEL: func @conversion_with_temporary(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.ref<!fir.array<10xi32>>)
-// Allocation of temporary array.
-// CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<10xi32>
-// Copy of original array to temp.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: }
-// Perform the assignment i = i(10:1:-1) using the temporary array.
-// CHECK: %{{.*}} = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<10xi32>) {
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK-NOT: %{{.*}} = fir.array_update
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) [%{{.*}}] %{{.*}} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: fir.result %{{.*}} : !fir.array<10xi32>
-// CHECK: }
-// Copy the result back to the original array.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0:.*]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: }
-// Free temporary array.
-// CHECK: fir.freemem %[[TEMP]] : !fir.heap<!fir.array<10xi32>>
-
-// -----
-
-// Test fir.array_load/fir.array_fetch/fir.array_update conversion with
-// an introduced copy-in/copy-out on a multidimensional array.
-
-func.func @conversion_with_temporary_multidim(%0: !fir.ref<!fir.array<10x5xi32>>) {
- %c10 = arith.constant 10 : index
- %c5 = arith.constant 5 : index
- %1 = fir.shape %c10, %c5 : (index, index) -> !fir.shape<2>
- %2 = fir.array_load %0(%1) : (!fir.ref<!fir.array<10x5xi32>>, !fir.shape<2>) -> !fir.array<10x5xi32>
- %c10_i64 = arith.constant 10 : i64
- %3 = fir.convert %c10_i64 : (i64) -> index
- %c5_i64 = arith.constant 5 : i64
- %4 = fir.convert %c5_i64 : (i64) -> index
- %c1 = arith.constant 1 : index
- %c10_i64_0 = arith.constant 10 : i64
- %c1_i64 = arith.constant 1 : i64
- %c-1_i64 = arith.constant -1 : i64
- %5 = arith.addi %c1, %c5 : index
- %6 = arith.subi %5, %c1 : index
- %c1_i64_1 = arith.constant 1 : i64
- %7 = fir.shape %c10, %c5 : (index, index) -> !fir.shape<2>
- %8 = fir.slice %c10_i64_0, %c1_i64, %c-1_i64, %c1, %6, %c1_i64_1 : (i64, i64, i64, index, index, i64) -> !fir.slice<2>
- %9 = fir.array_load %0(%7) [%8] : (!fir.ref<!fir.array<10x5xi32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.array<10x5xi32>
- %c1_2 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %10 = arith.subi %3, %c1_2 : index
- %11 = arith.subi %4, %c1_2 : index
- %12 = fir.do_loop %arg0 = %c0 to %11 step %c1_2 unordered iter_args(%arg1 = %2) -> (!fir.array<10x5xi32>) {
- %13 = fir.do_loop %arg2 = %c0 to %10 step %c1_2 unordered iter_args(%arg3 = %arg1) -> (!fir.array<10x5xi32>) {
- %14 = fir.array_fetch %9, %arg2, %arg0 : (!fir.array<10x5xi32>, index, index) -> i32
- %15 = fir.array_update %arg3, %14, %arg2, %arg0 : (!fir.array<10x5xi32>, i32, index, index) -> !fir.array<10x5xi32>
- fir.result %15 : !fir.array<10x5xi32>
- }
- fir.result %13 : !fir.array<10x5xi32>
- }
- fir.array_merge_store %2, %12 to %0 : !fir.array<10x5xi32>, !fir.array<10x5xi32>, !fir.ref<!fir.array<10x5xi32>>
- return
-}
-
-// CHECK-LABEL: func @conversion_with_temporary_multidim(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.ref<!fir.array<10x5xi32>>) {
-// CHECK: %[[CST10:.*]] = arith.constant 10 : index
-// CHECK: %[[CST5:.*]] = arith.constant 5 : index
-// CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<10x5xi32>
-// CHECK: %[[IDX5:.*]] = fir.convert %[[CST5]] : (index) -> index
-// CHECK: %[[UB5:.*]] = arith.subi %[[IDX5]], %{{.*}} : index
-// CHECK: fir.do_loop %[[INDUC0:.*]] = %{{.*}} to %[[UB5]] step %{{.*}} {
-// CHECK: %[[IDX10:.*]] = fir.convert %[[CST10]] : (index) -> index
-// CHECK: %[[UB10:.*]] = arith.subi %[[IDX10]], %{{.*}} : index
-// CHECK: fir.do_loop %[[INDUC1:.*]] = %{{.*}} to %[[UB10]] step %{{.*}} {
-// CHECK: %[[IDX1:.*]] = arith.addi %[[INDUC1]], %{{.*}} : index
-// CHECK: %[[IDX2:.*]] = arith.addi %[[INDUC0]], %{{.*}} : index
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %[[IDX1:.*]], %[[IDX2:.*]] : (!fir.ref<!fir.array<10x5xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}}, %{{.*}} : (!fir.heap<!fir.array<10x5xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: %{{.*}} = fir.do_loop %[[INDUC0:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<10x5xi32>) {
-// CHECK: %{{.*}} = fir.do_loop %[[INDUC1:.*]] = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<10x5xi32>) {
-// CHECK: %[[IDX1:.*]] = arith.addi %[[INDUC1]], %{{.*}} : index
-// CHECK: %[[IDX2:.*]] = arith.addi %[[INDUC0]], %{{.*}} : index
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK-NOT: %{{.*}} = fir.array_update
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) [%{{.*}}] %[[IDX1]], %[[IDX2]] : (!fir.ref<!fir.array<10x5xi32>>, !fir.shape<2>, !fir.slice<2>, index, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}}, %{{.*}} : (!fir.heap<!fir.array<10x5xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: %[[IDX5:.*]] = fir.convert %[[CST5]] : (index) -> index
-// CHECK: %[[UB5:.*]] = arith.subi %[[IDX5]], %{{.*}} : index
-// CHECK: fir.do_loop %[[INDUC0:.*]] = %{{.*}} to %[[UB5]] step %{{.*}} {
-// CHECK: %[[IDX10:.*]] = fir.convert %[[CST10]] : (index) -> index
-// CHECK: %[[UB10:.*]] = arith.subi %[[IDX10]], %{{.*}} : index
-// CHECK: fir.do_loop %[[INDUC1:.*]] = %{{.*}} to %[[UB10]] step %{{.*}} {
-// CHECK: %[[IDX1:.*]] = arith.addi %[[INDUC1]], %{{.*}} : index
-// CHECK: %[[IDX2:.*]] = arith.addi %[[INDUC0]], %{{.*}} : index
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %[[IDX1]], %[[IDX2]] : (!fir.heap<!fir.array<10x5xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}}, %{{.*}} : (!fir.ref<!fir.array<10x5xi32>>, !fir.shape<2>, index, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: fir.freemem %[[TEMP]] : !fir.heap<!fir.array<10x5xi32>>
-
-// -----
-
-// Test fir.array_modify conversion with no overlap.
-func.func @array_modify_no_overlap(%arg0: !fir.ref<!fir.array<100xf32>>, %arg1: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.array_load %arg1(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %4 = fir.do_loop %arg2 = %c0 to %c99 step %c1 unordered iter_args(%arg3 = %2) -> (!fir.array<100xf32>) {
- %5 = fir.array_fetch %3, %arg2 : (!fir.array<100xf32>, index) -> f32
- %6:2 = fir.array_modify %arg3, %arg2 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
- fir.store %5 to %0 : !fir.ref<f32>
- fir.call @user_defined_assignment(%6#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
- fir.result %6#1 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %4 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-func.func private @user_defined_assignment(!fir.ref<f32>, !fir.ref<f32>)
-
-// CHECK-LABEL: func @array_modify_no_overlap(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.ref<!fir.array<100xf32>>,
-// CHECK-SAME: %[[ARR1:.*]]: !fir.ref<!fir.array<100xf32>>) {
-// CHECK: %[[VAR0:.*]] = fir.alloca f32
-// CHECK-COUNT-1: %{{.*}} = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<100xf32>) {
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK-NOT: %{{.*}} = fir.array_modify
-// CHECK: %[[COOR0:.*]] = fir.array_coor %arg1(%1) %5 : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<f32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: fir.store %[[LOAD0]] to %[[VAR0]] : !fir.ref<f32>
-// CHECK: fir.call @{{.*}}(%[[COOR1]], %[[VAR0]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
-
-// -----
-
-// Test fir.array_modify conversion with an overlap.
-// Test user_defined_assignment(arg0(:), arg0(100:1:-1))
-func.func @array_modify_overlap(%arg0: !fir.ref<!fir.array<100xf32>>) {
- %c100 = arith.constant 100 : index
- %c99 = arith.constant 99 : index
- %c1 = arith.constant 1 : index
- %c-1 = arith.constant -1 : index
- %c0 = arith.constant 0 : index
- %0 = fir.alloca f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %3 = fir.slice %c100, %c1, %c-1 : (index, index, index) -> !fir.slice<1>
- %4 = fir.array_load %arg0(%1) [%3] : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<100xf32>
- %5 = fir.do_loop %arg1 = %c0 to %c99 step %c1 unordered iter_args(%arg2 = %2) -> (!fir.array<100xf32>) {
- %6 = fir.array_fetch %4, %arg1 : (!fir.array<100xf32>, index) -> f32
- %7:2 = fir.array_modify %arg2, %arg1 : (!fir.array<100xf32>, index) -> (!fir.ref<f32>, !fir.array<100xf32>)
- fir.store %6 to %0 : !fir.ref<f32>
- fir.call @user_defined_assignment(%7#0, %0) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
- fir.result %7#1 : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %5 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-func.func private @user_defined_assignment(!fir.ref<f32>, !fir.ref<f32>)
-
-// CHECK-LABEL: func @array_modify_overlap(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.ref<!fir.array<100xf32>>) {
-// CHECK: %[[VAR0:.*]] = fir.alloca f32
-// Allocate the temporary array.
-// CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<100xf32>
-// Copy original array to temp.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<f32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<f32>
-// CHECK: }
-// CHECK: %[[VAL_21:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[VAL_23:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK-NOT: %{{.*}} = fir.array_modify
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) {{\[}}%{{.*}}] %{{.*}} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<f32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<f32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: fir.store %[[LOAD0]] to %[[VAR0]] : !fir.ref<f32>
-// CHECK: fir.call @user_defined_assignment(%[[COOR1]], %[[VAR0]]) : (!fir.ref<f32>, !fir.ref<f32>) -> ()
-// CHECK: }
-// Copy back result to original array from temp.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<f32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<f32>
-// CHECK: }
-// Free the temporary array.
-// CHECK: fir.freemem %[[TEMP]] : !fir.heap<!fir.array<100xf32>>
-// CHECK: return
-// CHECK: }
-
-// -----
-
-// Test array of types with no overlap
-func.func @array_of_types() {
- %0 = fir.alloca i32 {bindc_name = "j", uniq_name = "_QEj"}
- %1 = fir.address_of(@_QEtypes) : !fir.ref<!fir.array<10x!fir.type<_QTd{i:!fir.array<10xi32>}>>>
- %c1_i32 = arith.constant 1 : i32
- %2 = fir.convert %c1_i32 : (i32) -> index
- %c10_i32 = arith.constant 10 : i32
- %3 = fir.convert %c10_i32 : (i32) -> index
- %c1 = arith.constant 1 : index
- %4 = fir.do_loop %arg0 = %2 to %3 step %c1 -> index {
- %6 = fir.convert %arg0 : (index) -> i32
- fir.store %6 to %0 : !fir.ref<i32>
- %c1_0 = arith.constant 1 : index
- %7 = fir.load %0 : !fir.ref<i32>
- %8 = fir.convert %7 : (i32) -> i64
- %c1_i64 = arith.constant 1 : i64
- %9 = arith.subi %8, %c1_i64 : i64
- %10 = fir.coordinate_of %1, %9 : (!fir.ref<!fir.array<10x!fir.type<_QTd{i:!fir.array<10xi32>}>>>, i64) -> !fir.ref<!fir.type<_QTd{i:!fir.array<10xi32>}>>
- %12 = fir.coordinate_of %10, i : (!fir.ref<!fir.type<_QTd{i:!fir.array<10xi32>}>>) -> !fir.ref<!fir.array<10xi32>>
- %c10 = arith.constant 10 : index
- %13 = arith.addi %c1_0, %c10 : index
- %14 = arith.subi %13, %c1_0 : index
- %c1_i64_1 = arith.constant 1 : i64
- %15 = fir.shape %c10 : (index) -> !fir.shape<1>
- %16 = fir.slice %c1_0, %14, %c1_i64_1 : (index, index, i64) -> !fir.slice<1>
- %17 = fir.array_load %12(%15) [%16] : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
- %c10_i64 = arith.constant 10 : i64
- %18 = fir.convert %c10_i64 : (i64) -> index
- %c0_i32 = arith.constant 0 : i32
- %c1_2 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %19 = arith.subi %18, %c1_2 : index
- %20 = fir.do_loop %arg1 = %c0 to %19 step %c1_2 unordered iter_args(%arg2 = %17) -> (!fir.array<10xi32>) {
- %22 = fir.array_update %arg2, %c0_i32, %arg1 : (!fir.array<10xi32>, i32, index) -> !fir.array<10xi32>
- fir.result %22 : !fir.array<10xi32>
- }
- fir.array_merge_store %17, %20 to %12[%16] : !fir.array<10xi32>, !fir.array<10xi32>, !fir.ref<!fir.array<10xi32>>, !fir.slice<1>
- %21 = arith.addi %arg0, %c1 : index
- fir.result %21 : index
- }
- %5 = fir.convert %4 : (index) -> i32
- fir.store %5 to %0 : !fir.ref<i32>
- return
-}
-
-// CHECK-LABEL: func @array_of_types() {
-// CHECK: %{{.*}} = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} -> index {
-// CHECK: %{{.*}} = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<10xi32>) {
-// CHECK-NOT: %{{.*}} = fir.array_update
-// CHECK: %[[COOR0:.*]] = fir.array_coor %{{.*}}(%{{.*}}) [%{{.*}}] %{{.*}} : (!fir.ref<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<i32>
-// CHECK: fir.store %{{.*}} to %[[COOR0]] : !fir.ref<i32>
-// CHECK-NOT: fir.array_merge_store
-
-// -----
-
-// Test fir.array_load/boxed array
-func.func @conversion_with_temporary_boxed_array(%arr0 : !fir.box<!fir.array<10xi32>>) {
- %c10 = arith.constant 10 : index
- %1:3 = fir.box_dims %arr0, %c10 : (!fir.box<!fir.array<10xi32>>, index) -> (index, index, index)
- %shift = fir.shift %1#0 : (index) -> !fir.shift<1>
- %2 = fir.array_load %arr0(%shift) : (!fir.box<!fir.array<10xi32>>, !fir.shift<1>) -> !fir.array<10xi32>
- %c10_i64 = arith.constant 10 : i64
- %3 = fir.convert %c10_i64 : (i64) -> index
- %c1_i64 = arith.constant 1 : i64
- %c-1_i64 = arith.constant -1 : i64
- %4 = fir.shape %c10 : (index) -> !fir.shape<1>
- %5 = fir.slice %c10_i64, %c1_i64, %c-1_i64 : (i64, i64, i64) -> !fir.slice<1>
- %6 = fir.array_load %arr0(%4) [%5] : (!fir.box<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.array<10xi32>
- %c1 = arith.constant 1 : index
- %c0 = arith.constant 0 : index
- %7 = arith.subi %3, %c1 : index
- %8 = fir.do_loop %arg0 = %c0 to %7 step %c1 unordered iter_args(%arg1 = %2) -> (!fir.array<10xi32>) {
- %9 = fir.array_fetch %6, %arg0 : (!fir.array<10xi32>, index) -> i32
- %10 = fir.array_update %arg1, %9, %arg0 : (!fir.array<10xi32>, i32, index) -> !fir.array<10xi32>
- fir.result %10 : !fir.array<10xi32>
- }
- fir.array_merge_store %2, %8 to %arr0 : !fir.array<10xi32>, !fir.array<10xi32>, !fir.box<!fir.array<10xi32>>
- return
-}
-
-// CHECK-LABEL: func @conversion_with_temporary_boxed_array(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.box<!fir.array<10xi32>>)
-// Allocation of temporary array.
-// CHECK: %[[TEMP:.*]] = fir.allocmem !fir.array<10xi32>
-// Copy of original array to temp.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.box<!fir.array<10xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: }
-// Perform the assignment i = i(10:1:-1) using the temporary array.
-// CHECK: %{{.*}} = fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} unordered iter_args(%{{.*}} = %{{.*}}) -> (!fir.array<10xi32>) {
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK-NOT: %{{.*}} = fir.update
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) [%{{.*}}] %{{.*}} : (!fir.box<!fir.array<10xi32>>, !fir.shape<1>, !fir.slice<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0]] : !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: fir.result %{{.*}} : !fir.array<10xi32>
-// CHECK: }
-// Copy the result back to the original array.
-// CHECK: fir.do_loop %{{.*}} = %{{.*}} to %{{.*}} step %{{.*}} {
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[TEMP]](%{{.*}}) %{{.*}} : (!fir.heap<!fir.array<10xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
-// CHECK: %[[COOR1:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.box<!fir.array<10xi32>>, !fir.shapeshift<1>, index) -> !fir.ref<i32>
-// CHECK: %[[LOAD0:.*]] = fir.load %[[COOR0:.*]] : !fir.ref<i32>
-// CHECK: fir.store %[[LOAD0]] to %[[COOR1]] : !fir.ref<i32>
-// CHECK: }
-// Free temporary array.
-// CHECK: fir.freemem %[[TEMP]] : !fir.heap<!fir.array<10xi32>>
-
-// -----
-
-// Test simple fir.array_update with Fortran.offsets attribute.
-func.func @array_update_conversion(%arr1 : !fir.box<!fir.array<?x?xf32>>, %m: index, %n: index) {
- %c10 = arith.constant 10 : index
- %c20 = arith.constant 20 : index
- %c1 = arith.constant 1 : index
- %f = arith.constant 2.0 : f32
- %s = fir.shape %m, %n : (index, index) -> !fir.shape<2>
- %av1 = fir.array_load %arr1(%s) : (!fir.box<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.array<?x?xf32>
- %av2 = fir.array_update %av1, %f, %c1, %c1 {Fortran.offsets} : (!fir.array<?x?xf32>, f32, index, index) -> !fir.array<?x?xf32>
- return
-}
-
-// CHECK-LABEL: func @array_update_conversion
-// CHECK-NOT: fir.array_update
-// CHECK-NOT: %{{.*}} = arith.addi %{{.*}}, %{{.*}} : index
-// CHECK: %[[ARRAY_COOR:.*]] = fir.array_coor{{.*}}-> !fir.ref<f32>
-// CHECK: fir.store %{{.*}} to %[[ARRAY_COOR]] : !fir.ref<f32>
-
-// -----
-
-// Test fir.array_fetch on derived type members in an array of derived types.
-func.func @array_fetch_derived_type(%0 : !fir.ref<!fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>>) {
- %1 = fir.alloca i32 {bindc_name = "i", uniq_name = "_QEi"}
- %c1_i32 = arith.constant 1 : i32
- %2 = fir.convert %c1_i32 : (i32) -> index
- %c10_i32 = arith.constant 10 : i32
- %3 = fir.convert %c10_i32 : (i32) -> index
- %c1 = arith.constant 1 : index
- %shape = fir.shape %2 : (index) -> !fir.shape<1>
- %arr0 = fir.array_load %0(%shape) : (!fir.ref<!fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>>, !fir.shape<1>) -> !fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>
- %4 = fir.do_loop %arg0 = %2 to %3 step %c1 -> index {
- %6 = fir.convert %arg0 : (index) -> i32
- fir.store %6 to %1 : !fir.ref<i32>
- %c1_i32_0 = arith.constant 1 : i32
- %7 = fir.load %1 : !fir.ref<i32>
- %8 = fir.convert %7 : (i32) -> i64
- %c1_i64 = arith.constant 1 : i64
- %9 = arith.subi %8, %c1_i64 : i64
- %11 = fir.field_index mt, !fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>
- %12 = fir.field_index mem, !fir.type<_QTt{mem:i32}>
- %idx = fir.convert %9 : (i64) -> index
- %res = fir.array_fetch %arr0, %idx, %11, %12 : (!fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>, index, !fir.field, !fir.field) -> i32
- %14 = arith.addi %arg0, %c1 : index
- fir.result %14 : index
- }
- %5 = fir.convert %4 : (index) -> i32
- fir.store %5 to %1 : !fir.ref<i32>
- return
-}
-
-// CHECK-LABEL: func @array_fetch_derived_type(
-// CHECK-SAME: %[[ARR0:.*]]: !fir.ref<!fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>>) {
-// CHECK: %{{.*}} = fir.do_loop
-// CHECK-NOT: %{{.*}} = fir.array_fetch
-// CHECK: %[[COOR0:.*]] = fir.array_coor %[[ARR0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<10x!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>>, !fir.shape<1>, index) -> !fir.ref<!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>
-// CHECK: %[[COOR_OF:.*]] = fir.coordinate_of %[[COOR0]], mt, mem : (!fir.ref<!fir.type<_QTu{mt:!fir.type<_QTt{mem:i32}>}>>) -> !fir.ref<i32>
-// CHECK: %{{.*}} = fir.load %[[COOR_OF]] : !fir.ref<i32>
-
-// -----
-
-// Test simple fir.array_load/fir.array_update conversion without copy-in/copy-out with a `fir.box`
-func.func @array_update_conversion(%arr1 : !fir.box<!fir.array<?x?xf32>>, %m: index, %n: index) {
- %c10 = arith.constant 10 : index
- %c20 = arith.constant 20 : index
- %c1 = arith.constant 1 : index
- %f = arith.constant 2.0 : f32
- %s = fir.shape %m, %n : (index, index) -> !fir.shape<2>
- %av1 = fir.array_load %arr1(%s) : (!fir.box<!fir.array<?x?xf32>>, !fir.shape<2>) -> !fir.array<?x?xf32>
- %av2 = fir.array_update %av1, %f, %c1, %c1 : (!fir.array<?x?xf32>, f32, index, index) -> !fir.array<?x?xf32>
- return
-}
-
-// -----
-
-// Test array operation with conditional update.
-
-func.func @array_operation_with_cond_update(%arg0: !fir.ref<!fir.array<100xf32>>, %cond1: i1) {
- %c100 = arith.constant 100 : index
- %c1 = arith.constant 1 : index
- %c-1 = arith.constant -1 : index
- %f = arith.constant 2.0 : f32
- %1 = fir.shape %c100 : (index) -> !fir.shape<1>
- %2 = fir.array_load %arg0(%1) : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>) -> !fir.array<100xf32>
- %arg2 = fir.if %cond1 -> !fir.array<100xf32> {
- fir.result %2 : !fir.array<100xf32>
- } else {
- %r = fir.array_update %2, %f, %c1 : (!fir.array<100xf32>, f32, index) -> !fir.array<100xf32>
- fir.result %r : !fir.array<100xf32>
- }
- fir.array_merge_store %2, %arg2 to %arg0 : !fir.array<100xf32>, !fir.array<100xf32>, !fir.ref<!fir.array<100xf32>>
- return
-}
-
-// CHECK-LABEL: func @array_operation_with_cond_update(
-// CHECK-SAME: %[[ARG0:.*]]: !fir.ref<!fir.array<100xf32>>, %[[COND:.*]]: i1) {
-// CHECK: %[[ARRAY_LOAD:.*]] = fir.undefined !fir.array<100xf32>
-// CHECK: %[[IF_RES:.*]] = fir.if %[[COND]] -> (!fir.array<100xf32>) {
-// CHECK: fir.result %[[ARRAY_LOAD]] : !fir.array<100xf32>
-// CHECK: } else {
-// CHECK: %[[UPDATE0:.*]] = fir.array_coor %[[ARG0]](%{{.*}}) %{{.*}} : (!fir.ref<!fir.array<100xf32>>, !fir.shape<1>, index) -> !fir.ref<f32>
-// CHECK: fir.store %{{.*}} to %{{.*}} : !fir.ref<f32>
-// CHECK: fir.result %[[ARRAY_LOAD]] : !fir.array<100xf32>
-// CHECK: }
diff --git a/flang/test/Transforms/stack-arrays-hlfir.f90 b/flang/test/Transforms/stack-arrays-hlfir.f90
index e70a1d9b89216..35237a5deeed3 100644
--- a/flang/test/Transforms/stack-arrays-hlfir.f90
+++ b/flang/test/Transforms/stack-arrays-hlfir.f90
@@ -9,7 +9,6 @@
! RUN: | fir-opt --lower-hlfir-ordered-assignments \
! RUN: --bufferize-hlfir \
! RUN: --convert-hlfir-to-fir \
-! RUN: --array-value-copy \
! RUN: --stack-arrays \
! RUN: | FileCheck %s
More information about the flang-commits
mailing list