|
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"
|
@@ -47,6 +48,42 @@ static void genUnreachable(fir::FirOpBuilder &builder, mlir::Location loc) {
|
47 | 48 | builder.setInsertionPointToStart(newBlock);
|
48 | 49 | }
|
49 | 50 |
|
| 51 | +/// Initializes values for STAT and ERRMSG |
| 52 | +static std::pair<mlir::Value, mlir::Value> getStatAndErrmsg( |
| 53 | + Fortran::lower::AbstractConverter &converter, mlir::Location loc, |
| 54 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList) { |
| 55 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 56 | + Fortran::lower::StatementContext stmtCtx; |
| 57 | + |
| 58 | + mlir::Value errMsgExpr, statExpr; |
| 59 | + for (const Fortran::parser::StatOrErrmsg &statOrErr : statOrErrList) { |
| 60 | + std::visit(Fortran::common::visitors{ |
| 61 | + [&](const Fortran::parser::StatVariable &statVar) { |
| 62 | + statExpr = fir::getBase(converter.genExprAddr( |
| 63 | + loc, Fortran::semantics::GetExpr(statVar), stmtCtx)); |
| 64 | + }, |
| 65 | + [&](const Fortran::parser::MsgVariable &errMsgVar) { |
| 66 | + const Fortran::semantics::SomeExpr *expr = |
| 67 | + Fortran::semantics::GetExpr(errMsgVar); |
| 68 | + errMsgExpr = fir::getBase( |
| 69 | + converter.genExprBox(loc, *expr, stmtCtx)); |
| 70 | + }}, |
| 71 | + statOrErr.u); |
| 72 | + } |
| 73 | + |
| 74 | + if (!statExpr) { |
| 75 | + statExpr = fir::AbsentOp::create(builder, loc, |
| 76 | + builder.getRefType(builder.getI32Type())); |
| 77 | + } |
| 78 | + if (!errMsgExpr) { |
| 79 | + errMsgExpr = fir::AbsentOp::create( |
| 80 | + builder, loc, |
| 81 | + fir::BoxType::get(fir::CharacterType::get( |
| 82 | + builder.getContext(), 1, fir::CharacterType::unknownLen()))); |
| 83 | + } |
| 84 | + return {statExpr, errMsgExpr}; |
| 85 | +} |
| 86 | + |
50 | 87 | //===----------------------------------------------------------------------===//
|
51 | 88 | // Misc. Fortran statements that lower to runtime calls
|
52 | 89 | //===----------------------------------------------------------------------===//
|
@@ -169,20 +206,68 @@ void Fortran::lower::genUnlockStatement(
|
169 | 206 |
|
170 | 207 | void Fortran::lower::genSyncAllStatement(
|
171 | 208 | Fortran::lower::AbstractConverter &converter,
|
172 |
| - const Fortran::parser::SyncAllStmt &) { |
173 |
| - TODO(converter.getCurrentLocation(), "coarray: SYNC ALL runtime"); |
| 209 | + const Fortran::parser::SyncAllStmt &stmt) { |
| 210 | + mlir::Location loc = converter.getCurrentLocation(); |
| 211 | + converter.checkCoarrayEnabled(); |
| 212 | + |
| 213 | + // Handle STAT and ERRMSG values |
| 214 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = stmt.v; |
| 215 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 216 | + |
| 217 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 218 | + fir::runtime::genSyncAllStatement(builder, loc, statAddr, errMsgAddr); |
174 | 219 | }
|
175 | 220 |
|
176 | 221 | void Fortran::lower::genSyncImagesStatement(
|
177 | 222 | Fortran::lower::AbstractConverter &converter,
|
178 |
| - const Fortran::parser::SyncImagesStmt &) { |
179 |
| - TODO(converter.getCurrentLocation(), "coarray: SYNC IMAGES runtime"); |
| 223 | + const Fortran::parser::SyncImagesStmt &stmt) { |
| 224 | + mlir::Location loc = converter.getCurrentLocation(); |
| 225 | + converter.checkCoarrayEnabled(); |
| 226 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 227 | + |
| 228 | + // Handle STAT and ERRMSG values |
| 229 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = |
| 230 | + std::get<std::list<Fortran::parser::StatOrErrmsg>>(stmt.t); |
| 231 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 232 | + |
| 233 | + // SYNC_IMAGES(*) is passed as count == -1 while SYNC IMAGES([]) has count |
| 234 | + // == 0. Note further that SYNC IMAGES(*) is not semantically equivalent to |
| 235 | + // SYNC ALL. |
| 236 | + Fortran::lower::StatementContext stmtCtx; |
| 237 | + mlir::Value imageSet; |
| 238 | + const Fortran::parser::SyncImagesStmt::ImageSet &imgSet = |
| 239 | + std::get<Fortran::parser::SyncImagesStmt::ImageSet>(stmt.t); |
| 240 | + std::visit(Fortran::common::visitors{ |
| 241 | + [&](const Fortran::parser::IntExpr &intExpr) { |
| 242 | + const SomeExpr *expr = Fortran::semantics::GetExpr(intExpr); |
| 243 | + imageSet = |
| 244 | + fir::getBase(converter.genExprBox(loc, *expr, stmtCtx)); |
| 245 | + }, |
| 246 | + [&](const Fortran::parser::Star &) { |
| 247 | + imageSet = fir::AbsentOp::create( |
| 248 | + builder, loc, |
| 249 | + fir::BoxType::get(fir::SequenceType::get( |
| 250 | + {fir::SequenceType::getUnknownExtent()}, |
| 251 | + builder.getI32Type()))); |
| 252 | + }}, |
| 253 | + imgSet.u); |
| 254 | + |
| 255 | + fir::runtime::genSyncImagesStatement(builder, loc, imageSet, statAddr, |
| 256 | + errMsgAddr); |
180 | 257 | }
|
181 | 258 |
|
182 | 259 | void Fortran::lower::genSyncMemoryStatement(
|
183 | 260 | Fortran::lower::AbstractConverter &converter,
|
184 |
| - const Fortran::parser::SyncMemoryStmt &) { |
185 |
| - TODO(converter.getCurrentLocation(), "coarray: SYNC MEMORY runtime"); |
| 261 | + const Fortran::parser::SyncMemoryStmt &stmt) { |
| 262 | + mlir::Location loc = converter.getCurrentLocation(); |
| 263 | + converter.checkCoarrayEnabled(); |
| 264 | + |
| 265 | + // Handle STAT and ERRMSG values |
| 266 | + const std::list<Fortran::parser::StatOrErrmsg> &statOrErrList = stmt.v; |
| 267 | + auto [statAddr, errMsgAddr] = getStatAndErrmsg(converter, loc, statOrErrList); |
| 268 | + |
| 269 | + fir::FirOpBuilder &builder = converter.getFirOpBuilder(); |
| 270 | + fir::runtime::genSyncMemoryStatement(builder, loc, statAddr, errMsgAddr); |
186 | 271 | }
|
187 | 272 |
|
188 | 273 | void Fortran::lower::genSyncTeamStatement(
|
|
0 commit comments