[flang-commits] [flang] [flang][Multi-Image] Moving Mutli-image lowering to PRIF into the MIF dialect (PR #161179)
Dan Bonachea via flang-commits
flang-commits at lists.llvm.org
Wed Oct 8 08:39:42 PDT 2025
================
@@ -0,0 +1,263 @@
+//===-- MIFOps.td - MIF operation definitions --------------*- tablegen -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of the MIF dialect operations
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_DIALECT_MIF_MIF_OPS
+#define FORTRAN_DIALECT_MIF_MIF_OPS
+
+include "flang/Optimizer/Dialect/MIF/MIFDialect.td"
+include "flang/Optimizer/Dialect/FIRTypes.td"
+include "flang/Optimizer/Dialect/FIRAttr.td"
+
+class mif_Op<string mnemonic, list<Trait> traits>
+ : Op<MIFDialect, mnemonic, traits>;
+
+//===----------------------------------------------------------------------===//
+// Initialization and Finalization
+//===----------------------------------------------------------------------===//
+
+def mif_InitOp : mif_Op<"init", []> {
+ let summary = "Initialize the parallel environment";
+ let description = [{This operation will initialize the parallel environment}];
+
+ let results = (outs I32:$stat);
+ let assemblyFormat = "`->` type($stat) attr-dict";
+}
+
+//===----------------------------------------------------------------------===//
+// Image Queries
+//===----------------------------------------------------------------------===//
+
+def mif_NumImagesOp : mif_Op<"num_images", [AttrSizedOperandSegments]> {
+ let summary = "Query the number of images in the specified or current team";
+ let description = [{
+ This operation query the number of images in the specified or current
+ team and can be called with 3 differents way :
+ - `num_images()`
+ - `num_images(team)`
+ - `num_images(team_number)`
+
+ Arguments:
+ - `team` : Shall be a scalar of type `team_type` from the `ISO_FORTRAN_ENV`
+ module with a value that identifies the current or ancestor team.
+ - `team_number` : Shall be an integer scalar. It shall identify the
+ initial team or a sibling team of the current team.
+
+ Result Value: The number of images in the specified team, or in the current
+ team if no team is specified.
+ }];
+
+ let arguments = (ins Optional<AnyInteger>:$team_number,
+ Optional<AnyRefOrBoxType>:$team);
+ let results = (outs I32:$res);
+
+ let builders = [OpBuilder<(ins CArg<"mlir::Value", "{}">:$teamArg)>];
+
+ let hasVerifier = 1;
+ let assemblyFormat = [{
+ ( `team_number` $team_number^ )?
+ ( `team` $team^ )?
+ attr-dict `:` functional-type(operands, results)
+ }];
+}
+
+def mif_ThisImageOp : mif_Op<"this_image", [AttrSizedOperandSegments]> {
+ let summary = "Determine the image index of the current image";
+ let description = [{
+ Arguments:
+ - `coarray` : Shall be a coarray of any type.
+ - `dim` : Shall be an integer scalar. Its value shall be in the range of
+ 1 <= DIM <= N, where N is the corank of the coarray.
+ - `team`(optional) : Shall be a scalar of type `team_type` from
+ ISO_FORTRAN_ENV. If the `coarray` is present, it shall be
+ established in that team.
+
+ Results:
+ - Case(1) : The result of `this_image([team])` is a scalar with a value
+ equal to the index of the image in the current or specified team.
+ - Case(2) : The result of `this_image(coarray [,team])` is the sequence of
+ cosubscript values for `coarray`.
+ - Case(3) : The result of `this_image(coarray, dim [,team])` is the value of
+ cosubscript `dim` in the sequence of cosubscript values for `coarray`.
+
+ Example:
+ ```fortran
+ REAL :: A[10, 0:9, 0:*]
+ ```
+ If we take a look on the example and we are on image 5, `this_image` has the
+ value 5, `this_image(A)` has the value [5, 0, 0].
+ }];
+
+ let arguments = (ins Optional<fir_BoxType>:$coarray,
+ Optional<AnyInteger>:$dim, Optional<AnyRefOrBoxType>:$team);
+ let results = (outs I32:$res);
+
+ let builders = [OpBuilder<(ins "mlir::Value":$coarray, "mlir::Value":$team)>,
+ OpBuilder<(ins "mlir::Value":$team)>];
+
+ let hasVerifier = 1;
+ let assemblyFormat = [{
+ ( `coarray` $coarray^ )?
+ ( `team` $team^ )?
+ ( `dim` $dim^ )?
+ attr-dict `:` functional-type(operands, results)
+ }];
+}
+
+//===----------------------------------------------------------------------===//
+// Synchronization
+//===----------------------------------------------------------------------===//
+
+def mif_SyncAllOp : mif_Op<"sync_all", [AttrSizedOperandSegments,
+ MemoryEffects<[MemWrite]>]> {
----------------
bonachea wrote:
> `FullEffect` seems to imply all the memory resource is being read/written. It is not the case here, and if anything, I would imagine this is an indication that allows doing more optimizations (like deleting the first write in write after write).
The idea was that by including `MemRead<DefaultResource, 0, FullEffect>` then analysis must assume this operation starts by reading every single memory location, and then writes it `MemWrite<DefaultResource, 1, FullEffect>]>`. I suspect that first step automatically avoids the WAW hazard you mention by ensuring this operation starts by effectively reading all of memory. It's not literally how a `sync all` behaves, but we're looking for an annotation that inhibits all motion of memory access across this segment boundary. Conservatively saying that it consumes all of memory and then writes all of memory is more than necessary but should be sufficient.
All that being said, I yield to @jeanPerier 's expertise regarding the safest near-term approach being to remove the side effect interface from `sync {all,images,memory}`, which hopefully has the same fencing effect on problematic optimization across segment boundaries.
https://github.com/llvm/llvm-project/pull/161179
More information about the flang-commits
mailing list