[flang-commits] [flang] [flang][MIF] Adding common features related to coarray (PR #215576)

Dan Bonachea via flang-commits flang-commits at lists.llvm.org
Tue Aug 11 18:50:47 PDT 2026


================
@@ -346,6 +346,66 @@ mlir::Value genTerminationOperationWrapper(fir::FirOpBuilder &builder,
   return fir::AddrOfOp::create(builder, loc, funcType, symbolRef);
 }
 
+// Generates the image index relative to the initial team, regardless of which
+// team is selected. Generates a call to the `prif_initial_team_index` function
+// (analogous to `prif_image_index`) if `cosubcripts` contains at least one
+// value; otherwise, it generates a call to `prif_this_image_no_coarray` without
+// the `team` argument.
+[[maybe_unused]] static mlir::Value
+getInitialTeamIndex(fir::FirOpBuilder &builder, mlir::Location loc,
+                    mlir::Value coarrayHandle,
+                    llvm::SmallVector<mlir::Value> cosubscripts) {
+  mlir::Type boxTy = fir::BoxType::get(builder.getNoneType());
+  mlir::Type i32Ty = builder.getI32Type();
+  mlir::Type i64Ty = builder.getI64Type();
+  mlir::Type boxArrTy = genBoxedSequenceType(i64Ty);
+  mlir::Value index = builder.createTemporary(loc, i32Ty);
+
+  // If there are no subscripts, the current image index is used.
+  if (cosubscripts.size() == 0) {
+    mlir::FunctionType ftype = mlir::FunctionType::get(
+        builder.getContext(),
+        /*inputs*/ {boxTy, builder.getRefType(i32Ty)}, /*results*/ {});
+    mlir::Value teamArg = fir::AbsentOp::create(builder, loc, boxTy);
+    mlir::func::FuncOp funcOp = builder.createFunction(
+        loc, getPRIFProcName("this_image_no_coarray"), ftype);
+    llvm::SmallVector<mlir::Value> args =
+        fir::runtime::createArguments(builder, loc, ftype, teamArg, index);
+    fir::CallOp::create(builder, loc, funcOp, args);
+    return index;
+  }
----------------
bonachea wrote:

I don't believe the block above will do what you think it will do.

IIUC the idea here is for lowering a statement like:
```fortran
event post evt  ! `evt` is not coindexed, meaning this references the executing image 
```
which will eventually result in a MIF operation like `mif_EventPostOp` with empty cosubscripts, which I _think_ you're proposing would be lowered to roughly:
```fortran
call prif_this_image_no_coarray(this_image)
call prif_event_post(image_num, evt_coarray_handle, 0)
```
And similarly for invocation of features like atomic subroutines and locks with non-coindexed arguments.
(There's no need to call PRIF at all for put/get on same-image coarray references)

The problem is that `prif_event_post` (as with every PRIF operation) requires `image_num` to be the _initial_ team index. 
`this_image_no_coarray(this_image)` (aka `THIS_IMAGE()`) produces a *current team* index, which in general won't match the initial team index when executed inside a `CHANGE TEAM` construct.

PRIF 0.8 doesn't _currently_ include a procedure to directly retrieve the initial team index of the executing image from an arbitrary point in the code (because Fortran doesn't provide that either). This is something that perhaps makes sense to add in a future PRIF specification, but for now I think the most concise code to retrieve this in a context-free manner is `THIS_IMAGE(GET_TEAM(INITIAL_TEAM))` which would lower to two calls:
```fortran
call prif_get_team(PRIF_INITIAL_TEAM, team)
call prif_this_image_no_coarray(team, this_image)
```

Alternatively, we could instead arrange to call `prif_this_image_no_coarray` during program startup (e.g. right after `prif_init`, before any possible `CHANGE TEAM`) and store the resulting initial team index in a hidden global variable that we can just reference from here. The initial team index never changes, so that would also be a higher-performance solution than calling the library to retrieve it every time. This was our original intent when designing PRIF.





https://github.com/llvm/llvm-project/pull/215576


More information about the flang-commits mailing list