[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
Fri May 8 09:22:36 PDT 2026
================
@@ -31,29 +33,265 @@ namespace hlfir {
#include "flang/Optimizer/HLFIR/Passes.h.inc"
} // namespace hlfir
+/// Collects all memory values (buffers/references) that the elemental body
+/// reads from. Use MemoryEffectOpInterface for a fail-safe implementation.
+static void getReadDependencies(hlfir::ElementalOp elemental,
+ llvm::SmallVectorImpl<mlir::Value> &deps) {
+ elemental.getRegion().walk([&](mlir::Operation *op) {
+ // Check if the operation explicitly implements memory effects.
+ if (auto memInterface = mlir::dyn_cast<mlir::MemoryEffectOpInterface>(op)) {
+ llvm::SmallVector<mlir::MemoryEffects::EffectInstance, 4> effects;
+ memInterface.getEffects(effects);
+ bool hasUnspecifiedRead = false;
+ for (const auto &effect : effects) {
+ if (mlir::isa<mlir::MemoryEffects::Read>(effect.getEffect())) {
+ if (mlir::Value val = effect.getValue()) {
+ deps.push_back(val);
+ } else {
+ // Read effect on an unspecified resource (e.g., global state).
+ hasUnspecifiedRead = true;
+ }
+ }
+ }
+ // If the op has a read effect but the specific value is unknown,
+ // conservatively capture all potential reference operands.
+ if (!hasUnspecifiedRead)
+ return;
+ }
+
+ // Fail-safe Fallback: For operations without the interface or with
+ // unspecified effects, capture any external reference used inside.
+ 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);
+ }
+ }
+ }
+ });
----------------
tblah wrote:
If the operation does not define the memory effects interface we should assume this cannot be transformed as we cannot reason about the operation. For example I think function calls don't have a memory effect interface: meaning "I don't know - could be doing anything".
https://github.com/llvm/llvm-project/pull/186916
More information about the flang-commits
mailing list