[flang-commits] [flang] [Flang][MIF] Adding support of intrinsics with coarray argument (PR #192944)

via flang-commits flang-commits at lists.llvm.org
Thu Apr 23 06:11:42 PDT 2026


================
@@ -118,6 +120,124 @@ def mif_ThisImageOp : mif_Op<"this_image", [AttrSizedOperandSegments]> {
   }];
 }
 
+//===----------------------------------------------------------------------===//
+// Coarray Queries
+//===----------------------------------------------------------------------===//
+
+def mif_ImageIndexOp : mif_Op<"image_index", [AttrSizedOperandSegments]> {
+  let summary = "Image index from cosubscripts.";
+  let description = [{
+    Arguments:
+      - `coarray`: Shall be a coarray of any type.
+      - `sub`: rank-one integer array of size equal to the corank of `coarray`.
+      - `team`: Shall be a scalar of type `team_type` from ISO_FORTRAN_ENV.
+      - `team_number`: It shall identify the initial team or a sibling team 
+        of the current team.
+
+    Usage:
+    - Case(1) : `call image_index(coarray, sub)`
+    - Case(2) : `call image_index(coarray, sub, team)`
+    - Case(3) : `call image_index(coarray, sub, team_number)`
+
+    Result: If the value of `sub` is a valid sequence of cosubscripts for `coarray` in the
+      team specified by `team` or `team_number`, or the current team if neither `team` nor
+      `team_number` appears, the result is the index of the corresponding image in that team.
+      Otherwise, the result is zero.
+  }];
+
+  let arguments = (ins AnyType:$coarray, fir_BoxType:$sub,
+      Optional<AnyRefOrBoxType>:$team, Optional<AnyInteger>:$team_number);
+  let builders = [OpBuilder<(ins "mlir::Value":$coarray, "mlir::Value":$sub,
+      "mlir::Value":$team)>];
+
+  let results = (outs I32);
+
+  let hasVerifier = 1;
+  let assemblyFormat = [{
+    `coarray` $coarray `sub` $sub 
+    ( `team` $team^ )? 
+    ( `team_number` $team_number^ )? 
+    attr-dict `:` functional-type(operands, results)
+  }];
+}
+
+def mif_LcoboundOp : mif_Op<"lcobound", [NoMemoryEffect]> {
+  let summary = "Returns the lower cobound(s) associated with a coarray.";
+  let description = [{
+    This operation returns the lower cobound(s) associated with a coarray.
+    Arguments: 
+      - `coarray`: Shall be a coarray of any type.
+      - `dim`(optional) : Shall be an integer scalar. Its value shall be in the range of
+          `1 <= DIM <= N`, where `N` is the corank of the coarray.
+    Results:
+      - Case(1): If `dim` is present, the result is an integer scalar equal to
+        the lower cobound for codimension `dim`.
+      - Case(2): `dim` is absent, so the result is an array whose size matches
+        the corank of the indicated coarray.
+  }];
+
+  let arguments = (ins AnyType:$coarray, Optional<AnyInteger>:$dim);
+  let results = (outs AnyType);
----------------
jeanPerier wrote:

The result type will brings too much complexity in the IR analysis IMHO (returning new memory means that this is an allocation like operation, which is BTW not compatible with the NoMemoryEffect attribute).

Even if this leads to more IR being generated, I think the IR will be easier to manipulate if this is broken down.
I would recommend either always breaking down lcobound to lcobound with dims and allocate/store into a fir.ref<fir.array<>> created in lowering, or to return a tuple<> and to loop/store into memory in lowering, or to have an operation that takes an output argument and writes to it.

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


More information about the flang-commits mailing list