|
12 | 12 | #include "flang/Lower/OpenMP.h" |
13 | 13 | #include "flang/Lower/StatementContext.h" |
14 | 14 | #include "flang/Optimizer/Builder/FIRBuilder.h" |
| 15 | +#include "flang/Optimizer/Builder/Runtime/Coarray.h" |
15 | 16 | #include "flang/Optimizer/Builder/Runtime/RTBuilder.h" |
16 | 17 | #include "flang/Optimizer/Builder/Todo.h" |
17 | 18 | #include "flang/Optimizer/Dialect/FIROpsSupport.h" |
@@ -48,6 +49,50 @@ static void genUnreachable(fir::FirOpBuilder &builder, mlir::Location loc) { |
48 | 49 | builder.setInsertionPointToStart(newBlock); |
49 | 50 | } |
50 | 51 |
|
| 52 | +// Check support of Multi-image features if -fcoarray is provided |
| 53 | +void checkCoarrayEnabled(Fortran::lower::AbstractConverter &converter, |
| 54 | + mlir::Location loc) { |
| 55 | + if (!converter.getFoldingContext().languageFeatures().IsEnabled( |
| 56 | + Fortran::common::LanguageFeature::Coarray)) |
| 57 | + fir::emitFatalError(loc, "Coarrays disabled, use '-fcoarray' to enable.", |
| 58 | + false); |
| 59 | +} |
| 60 | + |
| 61 | +/// Initializes values for STAT and ERRMSG |
| 62 | +static std::pair<mlir::Value, mlir::Value> getStatAndErrmsg( |
| 63 | + Fortran::lower::AbstractConverter &converter, mlir::Location loc, |
| 64 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList) { |
| 65 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 66 | + Fortran::lower::StatementContext stmtCtx; |
| 67 | + |
| 68 | + mlir::Value errMsgExpr, statExpr; |
| 69 | + for (const Fortran::parser::StatOrErrmsg &statOrErr : statOrErrList) { |
| 70 | + std::visit(Fortran::common::visitors{ |
| 71 | + [&](const Fortran::parser::StatVariable &statVar) { |
| 72 | + statExpr = fir::getBase(converter.genExprAddr( |
| 73 | + loc, Fortran::semantics::GetExpr(statVar), stmtCtx)); |
| 74 | + }, |
| 75 | + [&](const Fortran::parser::MsgVariable &errMsgVar) { |
| 76 | + const Fortran::semantics::SomeExpr *expr = |
| 77 | + Fortran::semantics::GetExpr(errMsgVar); |
| 78 | + errMsgExpr = fir::getBase( |
| 79 | + converter.genExprBox(loc, *expr, stmtCtx)); |
| 80 | + }}, |
| 81 | + statOrErr.u); |
| 82 | + } |
| 83 | + |
| 84 | + if (!statExpr) { |
| 85 | + statExpr = builder.create<fir::AbsentOp>( |
| 86 | + loc, builder.getRefType(builder.getI32Type())); |
| 87 | + } |
| 88 | + if (!errMsgExpr) { |
| 89 | + errMsgExpr = builder.create<fir::AbsentOp>( |
| 90 | + loc, fir::BoxType::get(fir::CharacterType::get( |
| 91 | + builder.getContext(), 1, fir::CharacterType::unknownLen()))); |
| 92 | + } |
| 93 | + return {statExpr, errMsgExpr}; |
| 94 | +} |
| 95 | + |
51 | 96 | //===----------------------------------------------------------------------===// |
52 | 97 | // Misc. Fortran statements that lower to runtime calls |
53 | 98 | //===----------------------------------------------------------------------===// |
@@ -170,20 +215,67 @@ void Fortran::lower::genUnlockStatement( |
170 | 215 |
|
171 | 216 | void Fortran::lower::genSyncAllStatement( |
172 | 217 | Fortran::lower::AbstractConverter &converter, |
173 | | - const Fortran::parser::SyncAllStmt &) { |
174 | | - TODO(converter.getCurrentLocation(), "coarray: SYNC ALL runtime"); |
| 218 | + const Fortran::parser::SyncAllStmt &stmt) { |
| 219 | + mlir::Location loc = converter.getCurrentLocation(); |
| 220 | + checkCoarrayEnabled(converter, loc); |
| 221 | + |
| 222 | + // Handle STAT and ERRMSG values |
| 223 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = stmt.v; |
| 224 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 225 | + |
| 226 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 227 | + fir::runtime::genSyncAllStatement(builder, loc, statAddr, errMsgAddr); |
175 | 228 | } |
176 | 229 |
|
177 | 230 | void Fortran::lower::genSyncImagesStatement( |
178 | 231 | Fortran::lower::AbstractConverter &converter, |
179 | | - const Fortran::parser::SyncImagesStmt &) { |
180 | | - TODO(converter.getCurrentLocation(), "coarray: SYNC IMAGES runtime"); |
| 232 | + const Fortran::parser::SyncImagesStmt &stmt) { |
| 233 | + mlir::Location loc = converter.getCurrentLocation(); |
| 234 | + checkCoarrayEnabled(converter, loc); |
| 235 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 236 | + |
| 237 | + // Handle STAT and ERRMSG values |
| 238 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = |
| 239 | + std::get<std::list<Fortran::parser::StatOrErrmsg>>(stmt.t); |
| 240 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 241 | + |
| 242 | + // SYNC_IMAGES(*) is passed as count == -1 while SYNC IMAGES([]) hase count |
| 243 | + // == 0. Note further that SYNC IMAGES(*) is not semantically equivalent to |
| 244 | + // SYNC ALL. |
| 245 | + Fortran::lower::StatementContext stmtCtx; |
| 246 | + mlir::Value imageSet; |
| 247 | + const Fortran::parser::SyncImagesStmt::ImageSet &imgSet = |
| 248 | + std::get<Fortran::parser::SyncImagesStmt::ImageSet>(stmt.t); |
| 249 | + std::visit(Fortran::common::visitors{ |
| 250 | + [&](const Fortran::parser::IntExpr &intExpr) { |
| 251 | + const SomeExpr *expr = Fortran::semantics::GetExpr(intExpr); |
| 252 | + imageSet = |
| 253 | + fir::getBase(converter.genExprBox(loc, *expr, stmtCtx)); |
| 254 | + }, |
| 255 | + [&](const Fortran::parser::Star &) { |
| 256 | + imageSet = builder.create<fir::AbsentOp>( |
| 257 | + loc, fir::BoxType::get(fir::SequenceType::get( |
| 258 | + {fir::SequenceType::getUnknownExtent()}, |
| 259 | + builder.getI32Type()))); |
| 260 | + }}, |
| 261 | + imgSet.u); |
| 262 | + |
| 263 | + fir::runtime::genSyncImagesStatement(builder, loc, imageSet, statAddr, |
| 264 | + errMsgAddr); |
181 | 265 | } |
182 | 266 |
|
183 | 267 | void Fortran::lower::genSyncMemoryStatement( |
184 | 268 | Fortran::lower::AbstractConverter &converter, |
185 | | - const Fortran::parser::SyncMemoryStmt &) { |
186 | | - TODO(converter.getCurrentLocation(), "coarray: SYNC MEMORY runtime"); |
| 269 | + const Fortran::parser::SyncMemoryStmt &stmt) { |
| 270 | + mlir::Location loc = converter.getCurrentLocation(); |
| 271 | + checkCoarrayEnabled(converter, loc); |
| 272 | + |
| 273 | + // Handle STAT and ERRMSG values |
| 274 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = stmt.v; |
| 275 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 276 | + |
| 277 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 278 | + fir::runtime::genSyncMemoryStatement(builder, loc, statAddr, errMsgAddr); |
187 | 279 | } |
188 | 280 |
|
189 | 281 | void Fortran::lower::genSyncTeamStatement( |
|
0 commit comments