[flang-commits] [flang] [flang][HLFIR] Relax InlineElementals to support more than two users (PR #186916)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue Apr 21 04:31:07 PDT 2026


================
@@ -31,29 +33,217 @@ namespace hlfir {
 #include "flang/Optimizer/HLFIR/Passes.h.inc"
 } // namespace hlfir
 
+/// Collects all memory values (buffers/references) that the elemental body
+/// reads from.
+static void getReadDependencies(hlfir::ElementalOp elemental,
+                                llvm::SmallVectorImpl<mlir::Value> &deps) {
+  elemental.getRegion().walk([&](mlir::Operation *op) {
+    if (auto designate = mlir::dyn_cast<hlfir::DesignateOp>(op))
+      deps.push_back(designate.getMemref());
+    else if (auto load = mlir::dyn_cast<fir::LoadOp>(op))
+      deps.push_back(load.getMemref());
+    // Capture any value defined outside the elemental but used inside it.
+    for (mlir::Value operand : op->getOperands()) {
+      if (operand.getParentRegion() != &elemental.getRegion())
+        if (mlir::isa<fir::ReferenceType, fir::PointerType, fir::HeapType,
+                      fir::BoxType>(operand.getType()))
+          deps.push_back(operand);
+    }
+  });
+}
+
+/// Checks if an operation 'op' potentially modifies any memory location that
+/// the elemental reads from (captured in 'deps').
+static bool isConflictingWrite(mlir::Operation *op,
+                               const llvm::SmallVectorImpl<mlir::Value> &deps,
+                               mlir::AliasAnalysis &aa) {
+  // Operations explicitly marked as having no memory effects are safe.
+  if (mlir::isMemoryEffectFree(op))
+    return false;
+
+  // Explicitly allow safe HLFIR/FIR metadata/lifetime operations.
+  // While these may have internal effects (e.g. allocating a descriptor),
+  // they do not modify the user data being read by the elemental.
+  if (mlir::isa<hlfir::DeclareOp, hlfir::AssociateOp, hlfir::EndAssociateOp,
+                fir::AllocaOp, hlfir::NoReassocOp>(op))
+    return false;
+
+  // Check for explicit memory effects via the MemoryEffectOpInterface.
+  if (auto memInterface = mlir::dyn_cast<mlir::MemoryEffectOpInterface>(op)) {
+    llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects;
+    memInterface.getEffects(effects);
+
+    for (const auto &effect : effects) {
+      // Analyze effects that modify memory or release resources.
+      if (mlir::isa<mlir::MemoryEffects::Write>(effect.getEffect()) ||
+          mlir::isa<mlir::MemoryEffects::Free>(effect.getEffect())) {
+
+        mlir::Value accessedValue = effect.getValue();
+        // If the effect is on an unknown resource (e.g. external call),
+        // assume a conflict.
+        if (!accessedValue)
+          return true;
+
+        // Perform alias analysis against all read dependencies.
+        for (mlir::Value dep : deps) {
+          if (!aa.alias(accessedValue, dep).isNo())
+            return true;
+        }
+      }
+    }
+  } else if (op->getNumRegions() == 0) {
+    // Conservative Fallback: If an operation lacks the interface and has no
+    // regions (e.g. a fir.call to an external function), assume it can
+    // potentially modifies any memory.
+    return true;
+  }
+
+  // Recursive Analysis into structured control flow regions.
+  // (e.g. fir.if, fir.do_loop) to find nested conflicting writes.
+  for (mlir::Region &region : op->getRegions()) {
+    for (mlir::Block &block : region) {
+      for (mlir::Operation &nestedOp : block) {
----------------
tblah wrote:

nit: this could just use op->walk() as above

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


More information about the flang-commits mailing list