[Flang][MIF] Allocation of NON-ALLOCATABLE SAVE coarray - #205847
Conversation
|
@llvm/pr-subscribers-flang-fir-hlfir Author: Jean-Didier PAILLEUX (JDPailleux) ChangesThe goal of this PR is to add support for allocating NON-allocatable coarrays that are not in the Patch is 22.29 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/205847.diff 14 Files Affected:
diff --git a/flang/include/flang/Lower/MultiImageFortran.h b/flang/include/flang/Lower/MultiImageFortran.h
index 002f156c807f2..c9b9e9f17cf39 100644
--- a/flang/include/flang/Lower/MultiImageFortran.h
+++ b/flang/include/flang/Lower/MultiImageFortran.h
@@ -76,6 +76,11 @@ mlir::Value genAllocateCoarray(
std::nullopt,
mlir::Value errMsg = {}, bool hasStat = false);
+void genAllocateNonAllocatableSaveCoarray(AbstractConverter &converter,
+ mlir::Location loc,
+ const semantics::Symbol &sym,
+ mlir::Value addr);
+
//===----------------------------------------------------------------------===//
// COARRAY expressions
//===----------------------------------------------------------------------===//
diff --git a/flang/include/flang/Optimizer/Builder/MIFCommon.h b/flang/include/flang/Optimizer/Builder/MIFCommon.h
index bde3e6aa80b34..de760b91f7a45 100644
--- a/flang/include/flang/Optimizer/Builder/MIFCommon.h
+++ b/flang/include/flang/Optimizer/Builder/MIFCommon.h
@@ -14,11 +14,20 @@
#include "mlir/IR/BuiltinOps.h"
static constexpr llvm::StringRef coarrayHandleSuffix = "_coarray_handle";
+static constexpr llvm::StringRef mifInitializationName = "__mif_initialization";
+static constexpr llvm::StringRef mifSaveCoarraysAllocName =
+ "__mif_save_coarrays_allocate";
namespace mif {
std::string getFullUniqName(mlir::Value addr);
+mlir::func::FuncOp getOrCreateInitFunc(mlir::OpBuilder &builder,
+ mlir::ModuleOp mod,
+ llvm::StringRef name);
+
+void genMIFInit(fir::FirOpBuilder &, mlir::Location loc);
+
} // namespace mif
#endif // FORTRAN_OPTIMIZER_TRANSFORMS_MIFCOMMON_H_
diff --git a/flang/include/flang/Optimizer/Transforms/Passes.td b/flang/include/flang/Optimizer/Transforms/Passes.td
index e7a83f64acef9..31d3b22e6e470 100644
--- a/flang/include/flang/Optimizer/Transforms/Passes.td
+++ b/flang/include/flang/Optimizer/Transforms/Passes.td
@@ -673,6 +673,11 @@ def MIFOpConversion : Pass<"mif-convert", "mlir::ModuleOp"> {
"mlir::cf::ControlFlowDialect", "mlir::DLTIDialect"];
}
+def EmitMIFGlobalCtors : Pass<"emit-mif-global-ctors", "mlir::ModuleOp"> {
+ let summary = "Generate GlobalCtorOp to init MIF and allocate coarrays.";
+ let dependentDialects = ["fir::FIROpsDialect", "mlir::LLVM::LLVMDialect"];
+}
+
def LoopInvariantCodeMotion : Pass<"flang-licm", "::mlir::func::FuncOp"> {
let summary = "Hoist invariants from loops";
let description = [{
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index ab7c141f11905..dd6d2daad5d68 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -6618,15 +6618,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {
if (sym.name() == "numeric_storage_size" && owner.IsModule() &&
DEREF(owner.symbol()).name() == "iso_fortran_env")
continue;
-
- if (Fortran::evaluate::IsCoarray(sym) &&
- !Fortran::semantics::IsAllocatable(sym) &&
- Fortran::semantics::IsSaved(sym)) {
- mlir::Location loc = toLocation();
- TODO(
- loc,
- "coarray: non-ALLOCATABLE SAVE coarray outside the main program");
- }
}
Fortran::lower::defineModuleVariable(*this, var);
}
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 2834531dccd96..d777ef900d4e4 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -2637,11 +2637,12 @@ void Fortran::lower::mapSymbolAttributes(
assert(!Fortran::semantics::IsAllocatable(sym) &&
"must be a non-ALLOCATABLE coarray");
if (Fortran::semantics::IsSaved(sym) &&
- sym.owner().kind() != Fortran::semantics::Scope::Kind::MainProgram)
- TODO(loc,
- "coarray: non-ALLOCATABLE SAVE coarray outside the main program");
- ;
- Fortran::lower::genAllocateCoarray(converter, loc, sym, addr);
+ (sym.owner().kind() != Fortran::semantics::Scope::Kind::MainProgram ||
+ var.isModuleOrSubmoduleVariable()))
+ Fortran::lower::genAllocateNonAllocatableSaveCoarray(converter, loc, sym,
+ addr);
+ else
+ Fortran::lower::genAllocateCoarray(converter, loc, sym, addr);
::genDeclareSymbol(converter, symMap, sym, addr, len, extents, lbounds,
replace);
return;
diff --git a/flang/lib/Lower/MultiImageFortran.cpp b/flang/lib/Lower/MultiImageFortran.cpp
index dc84a00c29d10..6eb10196f6042 100644
--- a/flang/lib/Lower/MultiImageFortran.cpp
+++ b/flang/lib/Lower/MultiImageFortran.cpp
@@ -20,6 +20,7 @@
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/expression.h"
+#include "mlir/IR/IRMapping.h"
//===----------------------------------------------------------------------===//
// Synchronization statements
@@ -451,6 +452,27 @@ mlir::Value Fortran::lower::genAllocateCoarray(
return stat;
}
+void Fortran::lower::genAllocateNonAllocatableSaveCoarray(
+ Fortran::lower::AbstractConverter &converter, mlir::Location loc,
+ const semantics::Symbol &sym, mlir::Value addr) {
+ fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+ mlir::ModuleOp mod = builder.getModule();
+ mlir::IRMapping mapping;
+
+ auto func = mif::getOrCreateInitFunc(builder, mod, mifSaveCoarraysAllocName);
+
+ mlir::Block &entry = func.getBody().front();
+ auto returnOp = entry.getTerminator();
+
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPoint(returnOp);
+
+ mlir::Operation &op = *addr.getDefiningOp();
+ mlir::Operation *localAddrOp = builder.clone(op, mapping);
+ Fortran::lower::genAllocateCoarray(converter, loc, sym,
+ localAddrOp->getResult(0));
+}
+
//===----------------------------------------------------------------------===//
// COARRAY expressions
//===----------------------------------------------------------------------===//
diff --git a/flang/lib/Optimizer/Builder/MIFCommon.cpp b/flang/lib/Optimizer/Builder/MIFCommon.cpp
index e92d4629609a0..5b007ea937e86 100644
--- a/flang/lib/Optimizer/Builder/MIFCommon.cpp
+++ b/flang/lib/Optimizer/Builder/MIFCommon.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/Builder/MIFCommon.h"
+#include "flang/Lower/MultiImageFortran.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Dialect/MIF/MIFOps.h"
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
@@ -61,3 +62,38 @@ std::string mif::getFullUniqName(mlir::Value addr) {
}
return "";
}
+
+mlir::func::FuncOp mif::getOrCreateInitFunc(mlir::OpBuilder &builder,
+ mlir::ModuleOp mod,
+ llvm::StringRef name) {
+
+ if (auto func = mod.lookupSymbol<mlir::func::FuncOp>(name))
+ return func;
+
+ auto funcType = builder.getFunctionType({}, {});
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPointToEnd(mod.getBody());
+
+ mlir::Location loc = mod.getLoc();
+ auto func = mlir::func::FuncOp::create(builder, loc, name, funcType);
+ func.setPublic();
+
+ func.addEntryBlock();
+ builder.setInsertionPointToEnd(&func.getBody().front());
+ mlir::func::ReturnOp::create(builder, loc);
+
+ return func;
+}
+
+void mif::genMIFInit(fir::FirOpBuilder &builder, mlir::Location loc) {
+ mlir::ModuleOp mod = builder.getModule();
+ auto func = getOrCreateInitFunc(builder, mod, mifInitializationName);
+
+ mlir::Block &entry = func.getBody().front();
+ auto returnOp = entry.getTerminator();
+
+ mlir::OpBuilder::InsertionGuard guard(builder);
+ builder.setInsertionPoint(returnOp);
+
+ mif::InitOp::create(builder, loc);
+}
diff --git a/flang/lib/Optimizer/Builder/Runtime/Main.cpp b/flang/lib/Optimizer/Builder/Runtime/Main.cpp
index 2b748ded039fd..ead469305bee2 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Main.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Main.cpp
@@ -10,6 +10,7 @@
#include "flang/Lower/EnvironmentDefault.h"
#include "flang/Optimizer/Builder/BoxValue.h"
#include "flang/Optimizer/Builder/FIRBuilder.h"
+#include "flang/Optimizer/Builder/MIFCommon.h"
#include "flang/Optimizer/Builder/Runtime/EnvironmentDefaults.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Optimizer/Dialect/FIROps.h"
@@ -71,7 +72,7 @@ void fir::runtime::genMain(
fir::CallOp::create(builder, loc, initFn);
}
if (initCoarrayEnv)
- mif::InitOp::create(builder, loc);
+ mif::genMIFInit(builder, loc);
fir::CallOp::create(builder, loc, qqMainFn);
diff --git a/flang/lib/Optimizer/Passes/Pipelines.cpp b/flang/lib/Optimizer/Passes/Pipelines.cpp
index c677962f30199..5d99e4d7586d3 100644
--- a/flang/lib/Optimizer/Passes/Pipelines.cpp
+++ b/flang/lib/Optimizer/Passes/Pipelines.cpp
@@ -10,6 +10,7 @@
/// common to flang and the test tools.
#include "flang/Optimizer/Passes/Pipelines.h"
+#include "flang/Optimizer/Builder/MIFCommon.h"
#include "flang/Optimizer/OpenACC/Passes.h"
#include "mlir/Conversion/Passes.h"
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h"
@@ -459,6 +460,7 @@ void createDefaultFIRCodeGenPassPipeline(mlir::PassManager &pm,
}
fir::addFIRToLLVMPass(pm, config);
+ pm.addPass(fir::createEmitMIFGlobalCtors());
// Convert applicable OpenMP stack allocations to shared memory allocations
// for GPU targets. This pass must run after any alloca-generating passes to
diff --git a/flang/lib/Optimizer/Transforms/CMakeLists.txt b/flang/lib/Optimizer/Transforms/CMakeLists.txt
index 5a3059ebbd97f..4f3e815e57adf 100644
--- a/flang/lib/Optimizer/Transforms/CMakeLists.txt
+++ b/flang/lib/Optimizer/Transforms/CMakeLists.txt
@@ -27,6 +27,7 @@ add_flang_library(FIRTransforms
ControlFlowConverter.cpp
ConvertComplexPow.cpp
DebugTypeGenerator.cpp
+ EmitMIFGlobalCtors.cpp
ExternalNameConversion.cpp
FIRToSCF.cpp
FIRToMemRef.cpp
diff --git a/flang/lib/Optimizer/Transforms/EmitMIFGlobalCtors.cpp b/flang/lib/Optimizer/Transforms/EmitMIFGlobalCtors.cpp
new file mode 100644
index 0000000000000..cadfd42046ff7
--- /dev/null
+++ b/flang/lib/Optimizer/Transforms/EmitMIFGlobalCtors.cpp
@@ -0,0 +1,109 @@
+//===-- EmitMIFGlobalCtors.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/MIFCommon.h"
+#include "flang/Optimizer/Builder/Runtime/Inquiry.h"
+#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
+#include "flang/Optimizer/Dialect/FIRDialect.h"
+#include "flang/Optimizer/Dialect/FIROps.h"
+#include "flang/Optimizer/Dialect/MIF/MIFOps.h"
+#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/Pass/Pass.h"
+
+namespace fir {
+#define GEN_PASS_DEF_EMITMIFGLOBALCTORS
+#include "flang/Optimizer/Transforms/Passes.h.inc"
+} // namespace fir
+
+using namespace mlir;
+using namespace Fortran::runtime;
+
+namespace {
+
+class EmitMIFGlobalCtors
+ : public fir::impl::EmitMIFGlobalCtorsBase<EmitMIFGlobalCtors> {
+public:
+ void runOnOperation() override {
+ mlir::ModuleOp mod = getOperation();
+ mlir::MLIRContext *ctx = mod.getContext();
+ mlir::OpBuilder builder(ctx);
+ builder.setInsertionPointToEnd(mod.getBody());
+ mlir::Location loc = builder.getUnknownLoc();
+
+ llvm::SmallVector<mlir::Attribute> funcs, priorities, data;
+ mlir::Type i32Ty = mlir::IntegerType::get(ctx, 32);
+ mlir::Attribute zeroAttr = mlir::LLVM::ZeroAttr::get(builder.getContext());
+
+ // Setting priority 0 for the initialization of mif.
+ if (mod.lookupSymbol<mlir::LLVM::LLVMFuncOp>(mifInitializationName)) {
+ funcs.push_back(mlir::FlatSymbolRefAttr::get(ctx, mifInitializationName));
+ priorities.push_back(mlir::IntegerAttr::get(i32Ty, 0));
+ data.push_back(zeroAttr);
+ }
+
+ // We set priority 1 because 0 is used for the function that call
+ // `mif.init`, to ensure that it is called after the parallel environment
+ // initialization.
+ if (mod.lookupSymbol<mlir::LLVM::LLVMFuncOp>(mifSaveCoarraysAllocName)) {
+ funcs.push_back(
+ mlir::FlatSymbolRefAttr::get(ctx, mifSaveCoarraysAllocName));
+ priorities.push_back(mlir::IntegerAttr::get(i32Ty, 1));
+ data.push_back(zeroAttr);
+ }
+
+ if (funcs.empty())
+ return;
+
+ // We check whether a GlobalCtorsOp already exists to avoid having
+ // multiple instances.
+ mlir::LLVM::GlobalCtorsOp globalCtors;
+ mod.walk([&](mlir::LLVM::GlobalCtorsOp op) { globalCtors = op; });
+
+ if (globalCtors) {
+ llvm::SmallVector<mlir::Attribute> mergedFuncs(
+ globalCtors.getCtors().begin(), globalCtors.getCtors().end());
+ llvm::SmallVector<mlir::Attribute> mergedPriorities(
+ globalCtors.getPriorities().begin(),
+ globalCtors.getPriorities().end());
+ llvm::SmallVector<mlir::Attribute> mergedData(
+ globalCtors.getData().begin(), globalCtors.getData().end());
+
+ llvm::SmallDenseSet<llvm::StringRef> existingNames;
+ for (auto attr : globalCtors.getCtors())
+ existingNames.insert(
+ llvm::cast<mlir::FlatSymbolRefAttr>(attr).getValue());
+
+ for (auto [func, priority, d] : llvm::zip(funcs, priorities, data)) {
+ auto name = llvm::cast<mlir::FlatSymbolRefAttr>(func).getValue();
+ if (!existingNames.contains(name)) {
+ mergedFuncs.push_back(func);
+ mergedPriorities.push_back(priority);
+ mergedData.push_back(d);
+ }
+ }
+
+ // Deleting the existing GlobalCtorsOp and creating a new one that
+ // contains the merged attributes.
+ builder.setInsertionPoint(globalCtors);
+ mlir::LLVM::GlobalCtorsOp::create(builder, globalCtors.getLoc(),
+ builder.getArrayAttr(mergedFuncs),
+ builder.getArrayAttr(mergedPriorities),
+ builder.getArrayAttr(mergedData));
+ globalCtors.erase();
+ } else {
+ builder.setInsertionPointToEnd(mod.getBody());
+ mlir::LLVM::GlobalCtorsOp::create(
+ builder, loc, builder.getArrayAttr(funcs),
+ builder.getArrayAttr(priorities), builder.getArrayAttr(data));
+ }
+ }
+};
+
+} // namespace
diff --git a/flang/test/Fir/basic-program.fir b/flang/test/Fir/basic-program.fir
index 2502a2a89a539..dd1bc5e83b601 100644
--- a/flang/test/Fir/basic-program.fir
+++ b/flang/test/Fir/basic-program.fir
@@ -184,6 +184,7 @@ func.func @_QQmain() {
// PASSES-NEXT: LowerNontemporalPass
// PASSES-NEXT: FIRToLLVMLowering
// PASSES-NEXT: ReconcileUnrealizedCasts
+// PASSES-NEXT: EmitMIFGlobalCtors
// PASSES-NEXT: 'llvm.func' Pipeline
// PASSES-NEXT: StackToSharedPass
// PASSES-NEXT: PrepareForOMPOffloadPrivatizationPass
diff --git a/flang/test/Lower/MIF/coarray_allocation4.f90 b/flang/test/Lower/MIF/coarray_allocation4.f90
index f0278ea2cd534..1f10067d651ba 100644
--- a/flang/test/Lower/MIF/coarray_allocation4.f90
+++ b/flang/test/Lower/MIF/coarray_allocation4.f90
@@ -1,8 +1,75 @@
-! RUN: not %flang_fc1 -emit-hlfir -fcoarray %s -o - 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -emit-hlfir -fcoarray %s -o - 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -emit-llvm -fcoarray %s -o - 2>&1 | FileCheck %s --check-prefix=LLVM
-!CHECK: not yet implemented: coarray: non-ALLOCATABLE SAVE coarray outside the main program
+! LLVM: llvm.global_ctors = appending global [2 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 0, ptr @__mif_initialization, ptr null }, { i32, ptr, ptr } { i32 1, ptr @__mif_save_coarrays_allocate, ptr null }]
subroutine test_coarray_save()
implicit none
real, SAVE :: n[*]
+ real, SAVE :: m[*]
+ real, SAVE :: p[*]
end subroutine test_coarray_save
+
+program main
+ call test_coarray_save()
+end program
+
+! CHECK-LABEL: func.func @_QPtest_coarray_save()
+! CHECK: %0 = fir.dummy_scope : !fir.dscope
+! CHECK: %1 = fir.address_of(@_QFtest_coarray_saveEm) : !fir.ref<f32>
+! CHECK: %2:2 = hlfir.declare %1 {uniq_name = "_QFtest_coarray_saveEm"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
+! CHECK: %3 = fir.address_of(@_QFtest_coarray_saveEn) : !fir.ref<f32>
+! CHECK: %4:2 = hlfir.declare %3 {uniq_name = "_QFtest_coarray_saveEn"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
+! CHECK: %5 = fir.address_of(@_QFtest_coarray_saveEp) : !fir.ref<f32>
+! CHECK: %6:2 = hlfir.declare %5 {uniq_name = "_QFtest_coarray_saveEp"} : (!fir.ref<f32>) -> (!fir.ref<f32>, !fir.ref<f32>)
+! CHECK: return
+
+! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "MAIN"}
+! CHECK: %0 = fir.dummy_scope : !fir.dscope
+! CHECK: fir.call @_QPtest_coarray_save() fastmath<contract> : () -> ()
+! CHECK: return
+
+! CHECK: fir.global internal @_QFtest_coarray_saveEm : f32
+
+! CHECK-LABEL: func.func @__mif_save_coarrays_allocate()
+! CHECK: %[[VAL_0:.*]] = fir.alloca !fir.array<0xi64>
+! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.array<1xi64>
+! CHECK: %[[VAL_2:.*]] = fir.alloca !fir.array<0xi64>
+! CHECK: %[[VAL_3:.*]] = fir.alloca !fir.array<1xi64>
+! CHECK: %[[VAL_4:.*]] = fir.alloca !fir.array<0xi64>
+! CHECK: %[[VAL_5:.*]] = fir.alloca !fir.array<1xi64>
+! CHECK: %[[VAL_6:.*]] = fir.address_of(@_QFtest_coarray_saveEm) : !fir.ref<f32>
+! CHECK: %[[C1_I64:.*]] = arith.constant 1 : i64
+! CHECK: %[[C1_I64_0:.*]] = arith.constant 1 : i64
+! CHECK: %[[C0:.*]] = arith.constant 0 : index
+! CHECK: %[[VAL_7:.*]] = fir.coordinate_of %[[VAL_5]], %[[C0]] : (!fir.ref<!fir.array<1xi64>>, index) -> !fir.ref<i64>
+! CHECK: fir.store %[[C1_I64_0]] to %[[VAL_7]] : !fir.ref<i64>
+! CHECK: %[[VAL_8:.*]] = fir.embox %[[VAL_5]] : (!fir.ref<!fir.array<1xi64>>) -> !fir.box<!fir.array<1xi64>>
+! CHECK: %[[C1_I64_1:.*]] = arith.constant 1 : i64
+! CHECK: %[[VAL_9:.*]] = fir.embox %[[VAL_4]] : (!fir.ref<!fir.array<0xi64>>) -> !fir.box<!fir.array<0xi64>>
+! CHECK: mif.alloc_coarray %[[VAL_6]] lcobounds %[[VAL_8]] ucobounds %[[VAL_9]] {uniq_name = "_QFtest_coarray_saveEm"} : (!fir.ref<f32>, !fir.box<!fir.array<1xi64>>, !fir.box<!fir.array<0xi64>>) -> ()
+! CHECK: %[[VAL_10:.*]] = fir.address_of(@_QFtest_coarray_saveEn) : !fir.ref<f32>
+! CHECK: %[[C1_I64_2:.*]] = arith.constant 1 : i64
+! CHECK: %[[C1_I64_3:.*]] = arith.constant 1 : i64
+! CHECK: %[[C0_4:.*]] = arith.constant 0 : index
+! CHECK: %[[VAL_11:.*]] = fir.coordinate_of %[[VAL_3]], %[[C0_4]] : (!fir.ref<!fir.array<1xi64>>, index) -> !fir.ref<i64>
+! CHECK: fir.store %[[C1_I64_3]] to %[[VAL_11]] : !fir.ref<i64>
+! CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_3]] : (!fir.ref<!fir.array<1xi64>>) -> !fir.box<!fir.array<1xi64>>
+! CHECK: %[[C1_I64_5:.*]] = arith.constant 1 : i64
+! CHECK: %[[VAL_13:.*]] = fir.embox %[[VAL_2]] : (!fir.ref<!fir.array<0xi64>>) -> !fir.box<!fir.array<0xi64>>
+! CHECK: mif.alloc_coarray %[[VAL_10]] lcobounds %[[VAL_12]] ucobounds %[[VAL_13]] {uniq_name = "_QFtest_coarray_saveEn"} : (!fir.ref<f32>, !fir.box<!fir.array<1xi64>>, !fir.box<!fir.array<0xi64>>) -> ()
+! CHECK: %[[VAL_14:.*]] = fir.address_of(@_QFtest_coarray_saveEp) : !fir.ref<f32>
+! CHECK: %[[C1_I64_6:.*]] = arith.constant 1 : i64
+! CHECK: %[[C1_I64_7:.*]] = arith.constant 1 : i64
+! CHECK: %[[C0_8:.*]] = arith.constant 0 : index
+! CHECK: %[[VAL_15:.*]] = fir.coordinate_of %[[VAL_1]], %[[C0_8]] : (!fir.ref<!fir.array<1xi64>>, index) -> !fir.ref<i64>
+! CHECK: fir.store %[[C1_I64_7]] to %[[VAL_15]] : !fir.ref<i64>
+! CHECK: %[[VAL_16:.*]] = fir.embox %[[VAL_1]] : (!fir.ref<!fir.array<1xi64>>) -> !fir.box<!fir.array<1xi64>>
+! CHECK: %[[VAL_C1_I64_9:.*]] = arith.constant 1 : i64
+! CHECK: %[[VAL_17:.*]] = fir.embox %[[VAL_0]] : (!fir.ref<!fir.array<0xi64>>) -> !fir.box<!fir.array<0xi64>>
+! CHECK: mif.alloc_coarray %...
[truncated]
|
bonachea
left a comment
There was a problem hiding this comment.
Thanks for this exciting new work!
I have some questions..
| // Setting priority 0 for the initialization of mif. | ||
| if (mod.lookupSymbol<mlir::LLVM::LLVMFuncOp>(mifInitializationName)) { | ||
| funcs.push_back(mlir::FlatSymbolRefAttr::get(ctx, mifInitializationName)); | ||
| priorities.push_back(mlir::IntegerAttr::get(i32Ty, 0)); | ||
| data.push_back(zeroAttr); | ||
| } | ||
|
|
||
| // We set priority 1 because 0 is used for the function that call | ||
| // `mif.init`, to ensure that it is called after the parallel environment | ||
| // initialization. | ||
| if (mod.lookupSymbol<mlir::LLVM::LLVMFuncOp>(mifSaveCoarraysAllocName)) { | ||
| funcs.push_back( | ||
| mlir::FlatSymbolRefAttr::get(ctx, mifSaveCoarraysAllocName)); | ||
| priorities.push_back(mlir::IntegerAttr::get(i32Ty, 1)); | ||
| data.push_back(zeroAttr); | ||
| } |
There was a problem hiding this comment.
I don't really understand the point of registering these as two separate Ctors.
We deliberately designed prif_init to be idempotent, meaning you are permitted to call it multiple times in a process and invocations after the first call are silently ignored. We don't currently say anything either way about whether the mif_InitOp dialect operation provides the same guarantee, but I think we probably need that property to correctly handle separate compilation of different object files that all declare non-allocatable save coarrays (because initialization of the parallel runtime library is a global process property, not per-TU).
Once we specify idempotence for mif_InitOp dialect operation, then I think we don't need two Ctors and the mif_InitOp can just be the first operation inside the allocation Ctor.
I think we may eventually need a second (or third) Ctor to handle the full generality of save coarray initializers, but that's orthogonal to this decision.
|
Thanks @bonachea for this initial feedback. That was actually one of my small question. I've updated the PR based on your comments for mif.init. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
bonachea
left a comment
There was a problem hiding this comment.
Thanks for the improvements!
One other cosmetic nitpick, otherwise LGTM!
|
@jeanPerier @ktras Can we please get your review on this PR? |
|
Hello @JDPailleux. Thanks for the PR! I have compiled a Fortran program with non-allocatable save coarrays with this branch and linked with Caffeine. The program unfortunately hangs and it appears, from dumping the MLIR, that you may be missing a call to |
ktras
left a comment
There was a problem hiding this comment.
Please fix the issue mentioned in previous comment.
@JDPailleux The program that Kate mentions is Caffeine's naive-multi-image test which has just been updated on the tip of Be sure to compile with |
|
@JDPailleux some more details: Here is the crash stack at startup on a Linux/x64_64 build of this PR's 9a2007d commit, using Caffeine's naive-multi-image test: I've independently confirmed (using debugger breakpoints) that In order to help catch such defects in the future, I've just merged a PR to |
bonachea
left a comment
There was a problem hiding this comment.
Need to resolve the missing prif_init defect
jeanPerier
left a comment
There was a problem hiding this comment.
Thanks @JDPailleux, the strategy looks good to me. Minor nit in inline comment.
LGTM provided you address the functional issues raised by other reviewers.
|
Ok thanks for your reviews. I'll fix your functionnal issue ! |
bonachea
left a comment
There was a problem hiding this comment.
Hi @JDPailleux - Thanks for the followup on this!
I've retested your branch at a8ac82f and confirmed that it still compiles Caffeine's native-multi-image test with -DASSERTIONS -DHAVE_SAVE_COARRAY enabled and now executes as expected, without assertion failures.
LGTM!
Co-authored-by: Dan Bonachea <dobonachea@lbl.gov>
a8ac82f to
443ca47
Compare
|
Conflicts has been solved, and if @jeanPerier agrees, I can merge this PR |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Update `flang/docs/FortranStandardsSupport.md` to reflect the current state of multi-image (coarray) feature support. The previous list of multi-image features was incomplete, and several multi-image features added in F2008 were inaccurately listed in the F2018 section. Recent PRs adding functionality now reflected in this document include: * #205847 * #192944 * #182110 Older (pre-22) PRs adding functionality that was not previously mentioned include: * #154081 * #154166
The goal of this PR is to add support for allocating NON-allocatable coarrays that are not in the `Main` program. All allocations will be performed in a function called `__mif_save_coarrays_allocate`. If this function exists, then LLVM::GlobalCtors is created (or modified). The initialization of the MIF with `mif.init` is moved too in a GlobalCtors with a priority of 0 to ensure that the initialization takes place before any MIF allocations. --------- Co-authored-by: Dan Bonachea <dobonachea@lbl.gov>
Update `flang/docs/FortranStandardsSupport.md` to reflect the current state of multi-image (coarray) feature support. The previous list of multi-image features was incomplete, and several multi-image features added in F2008 were inaccurately listed in the F2018 section. Recent PRs adding functionality now reflected in this document include: * llvm#205847 * llvm#192944 * llvm#182110 Older (pre-22) PRs adding functionality that was not previously mentioned include: * llvm#154081 * llvm#154166
The goal of this PR is to add support for allocating NON-allocatable coarrays that are not in the
Mainprogram.All allocations will be performed in a function called
__mif_save_coarrays_allocate. If this function exists, then LLVM::GlobalCtors is created (or modified).The initialization of the MIF with
mif.initis moved too in a GlobalCtors with a priority of 0 to ensure that the initialization takes place before any MIF allocations.