Skip to content

Commit acdbb00

Browse files
authored
[flang] Adding support of -fcoarray flang and init PRIF (#151675)
In relation to the approval and merge of the [PRIF](#76088) specification about multi-image features in Flang, here is a first PR to add support for the `-fcoarray` compilation flag and the initialization of the PRIF environment. Other PRs will follow for adding support of lowering to PRIF.
1 parent 9c4e571 commit acdbb00

File tree

12 files changed

+115
-6
lines changed

12 files changed

+115
-6
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6987,7 +6987,6 @@ def static_libgfortran : Flag<["-"], "static-libgfortran">, Group<gfortran_Group
69876987
// "f" options with values for gfortran.
69886988
def fblas_matmul_limit_EQ : Joined<["-"], "fblas-matmul-limit=">, Group<gfortran_Group>;
69896989
def fcheck_EQ : Joined<["-"], "fcheck=">, Group<gfortran_Group>;
6990-
def fcoarray_EQ : Joined<["-"], "fcoarray=">, Group<gfortran_Group>;
69916990
def ffpe_trap_EQ : Joined<["-"], "ffpe-trap=">, Group<gfortran_Group>;
69926991
def ffree_line_length_VALUE : Joined<["-"], "ffree-line-length-">, Group<gfortran_Group>;
69936992
def finit_character_EQ : Joined<["-"], "finit-character=">, Group<gfortran_Group>;
@@ -8695,6 +8694,15 @@ def fopenmp_host_ir_file_path : Separate<["-"], "fopenmp-host-ir-file-path">,
86958694

86968695
} // let Visibility = [CC1Option, FC1Option]
86978696

8697+
//===----------------------------------------------------------------------===//
8698+
// Coarray Options
8699+
//===----------------------------------------------------------------------===//
8700+
8701+
def fcoarray : Flag<["-"], "fcoarray">,
8702+
Group<f_Group>,
8703+
Visibility<[FlangOption, FC1Option]>,
8704+
HelpText<"Enable Coarray features">;
8705+
86988706
//===----------------------------------------------------------------------===//
86998707
// SYCL Options
87008708
//===----------------------------------------------------------------------===//

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ void Flang::addCodegenOptions(const ArgList &Args,
178178
options::OPT_fstack_repack_arrays, options::OPT_fno_stack_repack_arrays,
179179
options::OPT_ftime_report, options::OPT_ftime_report_EQ,
180180
options::OPT_funroll_loops, options::OPT_fno_unroll_loops});
181+
if (Args.hasArg(clang::driver::options::OPT_fcoarray))
182+
CmdArgs.push_back("-fcoarray");
181183
}
182184

183185
void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===-- Coarray.h -- generate Coarray intrinsics runtime calls --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COARRAY_H
10+
#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COARRAY_H
11+
12+
#include "flang/Lower/AbstractConverter.h"
13+
#include "flang/Optimizer/Support/InternalNames.h"
14+
#include "mlir/Dialect/Func/IR/FuncOps.h"
15+
16+
namespace fir {
17+
class ExtendedValue;
18+
class FirOpBuilder;
19+
} // namespace fir
20+
21+
namespace fir::runtime {
22+
23+
// Get the function type for a prif subroutine with a variable number of
24+
// arguments
25+
#define PRIF_FUNCTYPE(...) \
26+
mlir::FunctionType::get(builder.getContext(), /*inputs*/ {__VA_ARGS__}, \
27+
/*result*/ {})
28+
29+
// Default prefix for subroutines of PRIF compiled with LLVM
30+
#define PRIFNAME_SUB(fmt) \
31+
[]() { \
32+
std::ostringstream oss; \
33+
oss << "prif_" << fmt; \
34+
return fir::NameUniquer::doProcedure({"prif"}, {}, oss.str()); \
35+
}()
36+
37+
/// Generate Call to runtime prif_init
38+
mlir::Value genInitCoarray(fir::FirOpBuilder &builder, mlir::Location loc);
39+
40+
} // namespace fir::runtime
41+
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COARRAY_H

flang/include/flang/Optimizer/Builder/Runtime/Main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace fir::runtime {
2525

2626
void genMain(fir::FirOpBuilder &builder, mlir::Location loc,
2727
const std::vector<Fortran::lower::EnvironmentDefault> &defs,
28-
bool initCuda = false);
28+
bool initCuda = false, bool initCoarrayEnv = false);
2929
}
3030

3131
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_MAIN_H

flang/include/flang/Support/Fortran-features.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ ENUM_CLASS(LanguageFeature, BackslashEscapes, OldDebugLines,
5656
IgnoreIrrelevantAttributes, Unsigned, AmbiguousStructureConstructor,
5757
ContiguousOkForSeqAssociation, ForwardRefExplicitTypeDummy,
5858
InaccessibleDeferredOverride, CudaWarpMatchFunction, DoConcurrentOffload,
59-
TransferBOZ)
59+
TransferBOZ, Coarray)
6060

6161
// Portability and suspicious usage warnings
6262
ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,17 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
11521152
diags.Report(diagID);
11531153
}
11541154
}
1155+
// -fcoarray
1156+
if (args.hasArg(clang::driver::options::OPT_fcoarray)) {
1157+
res.getFrontendOpts().features.Enable(
1158+
Fortran::common::LanguageFeature::Coarray);
1159+
const unsigned diagID =
1160+
diags.getCustomDiagID(clang::DiagnosticsEngine::Warning,
1161+
"Support for multi image Fortran features is "
1162+
"still experimental and in development.");
1163+
diags.Report(diagID);
1164+
}
1165+
11551166
return diags.getNumErrors() == numErrorsBefore;
11561167
}
11571168

flang/lib/Lower/Bridge.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
475475
fir::runtime::genMain(*builder, toLocation(),
476476
bridge.getEnvironmentDefaults(),
477477
getFoldingContext().languageFeatures().IsEnabled(
478-
Fortran::common::LanguageFeature::CUDA));
478+
Fortran::common::LanguageFeature::CUDA),
479+
getFoldingContext().languageFeatures().IsEnabled(
480+
Fortran::common::LanguageFeature::Coarray));
479481
});
480482

481483
finalizeOpenMPLowering(globalOmpRequiresSymbol);

flang/lib/Optimizer/Builder/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_flang_library(FIRBuilder
1616
Runtime/Allocatable.cpp
1717
Runtime/ArrayConstructor.cpp
1818
Runtime/Assign.cpp
19+
Runtime/Coarray.cpp
1920
Runtime/Character.cpp
2021
Runtime/Command.cpp
2122
Runtime/CUDA/Descriptor.cpp
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- Coarray.cpp -- runtime API for coarray intrinsics -----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "flang/Optimizer/Builder/Runtime/Coarray.h"
10+
#include "flang/Optimizer/Builder/FIRBuilder.h"
11+
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12+
#include "mlir/Dialect/Func/IR/FuncOps.h"
13+
14+
using namespace Fortran::runtime;
15+
using namespace Fortran::semantics;
16+
17+
/// Generate Call to runtime prif_init
18+
mlir::Value fir::runtime::genInitCoarray(fir::FirOpBuilder &builder,
19+
mlir::Location loc) {
20+
mlir::Type i32Ty = builder.getI32Type();
21+
mlir::Value result = builder.createTemporary(loc, i32Ty);
22+
mlir::FunctionType ftype = PRIF_FUNCTYPE(builder.getRefType(i32Ty));
23+
mlir::func::FuncOp funcOp =
24+
builder.createFunction(loc, PRIFNAME_SUB("init"), ftype);
25+
llvm::SmallVector<mlir::Value> args =
26+
fir::runtime::createArguments(builder, loc, ftype, result);
27+
builder.create<fir::CallOp>(loc, funcOp, args);
28+
return builder.create<fir::LoadOp>(loc, result);
29+
}

flang/lib/Optimizer/Builder/Runtime/Main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "flang/Lower/EnvironmentDefault.h"
1111
#include "flang/Optimizer/Builder/BoxValue.h"
1212
#include "flang/Optimizer/Builder/FIRBuilder.h"
13+
#include "flang/Optimizer/Builder/Runtime/Coarray.h"
1314
#include "flang/Optimizer/Builder/Runtime/EnvironmentDefaults.h"
1415
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
1516
#include "flang/Optimizer/Dialect/FIROps.h"
@@ -23,8 +24,8 @@ using namespace Fortran::runtime;
2324
/// Create a `int main(...)` that calls the Fortran entry point
2425
void fir::runtime::genMain(
2526
fir::FirOpBuilder &builder, mlir::Location loc,
26-
const std::vector<Fortran::lower::EnvironmentDefault> &defs,
27-
bool initCuda) {
27+
const std::vector<Fortran::lower::EnvironmentDefault> &defs, bool initCuda,
28+
bool initCoarrayEnv) {
2829
auto *context = builder.getContext();
2930
auto argcTy = builder.getDefaultIntegerType();
3031
auto ptrTy = mlir::LLVM::LLVMPointerType::get(context);
@@ -69,6 +70,8 @@ void fir::runtime::genMain(
6970
loc, RTNAME_STRING(CUFInit), mlir::FunctionType::get(context, {}, {}));
7071
fir::CallOp::create(builder, loc, initFn);
7172
}
73+
if (initCoarrayEnv)
74+
fir::runtime::genInitCoarray(builder, loc);
7275

7376
fir::CallOp::create(builder, loc, qqMainFn);
7477
fir::CallOp::create(builder, loc, stopFn);

0 commit comments

Comments
 (0)