[flang-commits] [flang] [Flang]Add support for inlining hlfir.assign operation where both LHS and RHS are slices of the same array (PR #204532)

Kaviya Rajendiran via flang-commits flang-commits at lists.llvm.org
Tue Jun 23 23:46:34 PDT 2026


================
@@ -193,6 +194,56 @@ ArraySectionAnalyzer::analyze(mlir::Value ref1, mlir::Value ref2,
   return SlicesOverlapKind::Unknown;
 }
 
+mlir::Value ArraySectionAnalyzer::genRuntimeDisjointnessCheck(
+    mlir::Location loc, mlir::OpBuilder &builder, mlir::Value lhsRef,
+    mlir::Value rhsRef) {
+  auto des1 = lhsRef.getDefiningOp<hlfir::DesignateOp>();
+  auto des2 = rhsRef.getDefiningOp<hlfir::DesignateOp>();
+  if (!des1 || !des2)
+    return {};
+
+  if (des1.getMemref() != des2.getMemref())
+    return {};
+
+  if (des1.getComponent() != des2.getComponent() ||
+      des1.getComponentShape() != des2.getComponentShape() ||
+      des1.getSubstring() != des2.getSubstring() ||
+      des1.getComplexPart() != des2.getComplexPart() ||
+      des1.getTypeparams() != des2.getTypeparams())
+    return {};
+
+  if (des1.getIsTriplet().empty() ||
+      !llvm::equal(des1.getIsTriplet(), des2.getIsTriplet()))
+    return {};
+
+  mlir::Type idxTy = mlir::IndexType::get(des1.getContext());
+  auto toIdx = [&](mlir::Value v) -> mlir::Value {
+    return fir::ConvertOp::create(builder, loc, idxTy, v);
+  };
+
+  mlir::Value disjoint;
+  auto des1It = des1.getIndices().begin();
+  auto des2It = des2.getIndices().begin();
+  for (bool isTriplet : des1.getIsTriplet()) {
+    SectionDesc desc1 = readSectionDesc(des1It, isTriplet);
+    SectionDesc desc2 = readSectionDesc(des2It, isTriplet);
+    auto [lb1, ub1] = getOrderedBounds(desc1);
----------------
kaviya2510 wrote:

The above scenario is already handled by the existing compiler flow.

- `readSectionDesc(..)` returns {lb, nullptr, nullptr} for scalar (non-triplet) subscripts.
- The `SectionDesc` constructor calls `normalize()`, which checks if ub == nullptr. If so, it sets ub = lb. So after normalization, scalar subscripts are represented with lb == ub.
- As a result, by the time `getOrderedBounds` is called, ub is guaranteed to be equal to lb and is no longer nullptr.

I have added a testcase for this case as well.

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


More information about the flang-commits mailing list