Skip to content

Commit 18eeadc

Browse files
committed
[flang] Add support of -fcoarray and init PRIF
1 parent 4745637 commit 18eeadc

File tree

11 files changed

+124
-5
lines changed

11 files changed

+124
-5
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6962,7 +6962,6 @@ def static_libgfortran : Flag<["-"], "static-libgfortran">, Group<gfortran_Group
69626962
// "f" options with values for gfortran.
69636963
def fblas_matmul_limit_EQ : Joined<["-"], "fblas-matmul-limit=">, Group<gfortran_Group>;
69646964
def fcheck_EQ : Joined<["-"], "fcheck=">, Group<gfortran_Group>;
6965-
def fcoarray_EQ : Joined<["-"], "fcoarray=">, Group<gfortran_Group>;
69666965
def ffpe_trap_EQ : Joined<["-"], "ffpe-trap=">, Group<gfortran_Group>;
69676966
def ffree_line_length_VALUE : Joined<["-"], "ffree-line-length-">, Group<gfortran_Group>;
69686967
def finit_character_EQ : Joined<["-"], "finit-character=">, Group<gfortran_Group>;
@@ -8670,6 +8669,15 @@ def fopenmp_host_ir_file_path : Separate<["-"], "fopenmp-host-ir-file-path">,
86708669

86718670
} // let Visibility = [CC1Option, FC1Option]
86728671

8672+
//===----------------------------------------------------------------------===//
8673+
// Coarray Options
8674+
//===----------------------------------------------------------------------===//
8675+
8676+
def fcoarray : Flag<["-"], "fcoarray">,
8677+
Group<f_Group>,
8678+
Visibility<[FlangOption, FC1Option]>,
8679+
HelpText<"Enable Coarray features">;
8680+
86738681
//===----------------------------------------------------------------------===//
86748682
// SYCL Options
86758683
//===----------------------------------------------------------------------===//

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 {

flang/include/flang/Lower/LoweringOptions.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@ ENUM_LOWERINGOPT(SkipExternalRttiDefinition, unsigned, 1, 0)
7474
/// If false, lower to the complex dialect of MLIR.
7575
/// On by default.
7676
ENUM_LOWERINGOPT(ComplexDivisionToRuntime, unsigned, 1, 1)
77+
78+
/// Enable coarrays feature.
79+
ENUM_LOWERINGOPT(CoarrayFeature, unsigned, 1, 0)
80+
7781
#undef LOWERINGOPT
7882
#undef ENUM_LOWERINGOPT
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 "mlir/Dialect/Func/IR/FuncOps.h"
14+
15+
namespace fir {
16+
class ExtendedValue;
17+
class FirOpBuilder;
18+
} // namespace fir
19+
20+
namespace fir::runtime {
21+
22+
// Get the function type for a prif subroutine with a variable number of
23+
// arguments
24+
#define PRIF_FUNCTYPE(...) \
25+
mlir::FunctionType::get(builder.getContext(), /*inputs*/ {__VA_ARGS__}, \
26+
/*result*/ {})
27+
28+
// Default prefix for type of PRIF compiled with LLVM
29+
#define PRIFTYPE_PREFIX "_QM__prifT"
30+
#define PRIFTYPE(fmt) \
31+
[]() { \
32+
std::ostringstream oss; \
33+
oss << PRIFTYPE_PREFIX << fmt; \
34+
return oss.str(); \
35+
}()
36+
37+
// Default prefix for subroutines of PRIF compiled with LLVM
38+
#define PRIFSUB_PREFIX "_QMprifPprif_"
39+
#define PRIFNAME_SUB(fmt) \
40+
[]() { \
41+
std::ostringstream oss; \
42+
oss << PRIFSUB_PREFIX << fmt; \
43+
return oss.str(); \
44+
}()
45+
46+
/// Generate Call to runtime prif_init
47+
mlir::Value genInitCoarray(fir::FirOpBuilder &builder, mlir::Location loc);
48+
49+
} // namespace fir::runtime
50+
#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/lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,10 @@ 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.getLoweringOpts().setCoarrayFeature(1);
1158+
11551159
return diags.getNumErrors() == numErrorsBefore;
11561160
}
11571161

flang/lib/Lower/Bridge.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,8 @@ 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+
getLoweringOptions().getCoarrayFeature());
479480
});
480481

481482
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 "flang/Optimizer/HLFIR/HLFIROps.h"
13+
#include "flang/Optimizer/Support/InternalNames.h"
14+
#include "flang/Semantics/scope.h"
15+
#include "flang/Semantics/tools.h"
16+
#include "mlir/Dialect/Func/IR/FuncOps.h"
17+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18+
#include "llvm/ADT/TypeSwitch.h"
19+
20+
using namespace Fortran::runtime;
21+
using namespace Fortran::semantics;
22+
23+
/// Generate Call to runtime prif_init
24+
mlir::Value fir::runtime::genInitCoarray(fir::FirOpBuilder &builder,
25+
mlir::Location loc) {
26+
mlir::Type i32Ty = builder.getI32Type();
27+
mlir::Value result = builder.createTemporary(loc, i32Ty);
28+
mlir::FunctionType ftype = PRIF_FUNCTYPE(builder.getRefType(i32Ty));
29+
mlir::func::FuncOp funcOp =
30+
builder.createFunction(loc, PRIFNAME_SUB("init"), ftype);
31+
llvm::SmallVector<mlir::Value> args =
32+
fir::runtime::createArguments(builder, loc, ftype, result);
33+
builder.create<fir::CallOp>(loc, funcOp, args);
34+
return builder.create<fir::LoadOp>(loc, result);
35+
}

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)