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

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Tue Jun 23 08:54:31 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);
----------------
vzakhari wrote:

`ub` is `nullptr` for non-triplet index, so it is incorrect to access it in the cmp operations below. If you do not expect non-triple indices to be met here, then please add an assert, otherwise, please add a LIT test executing this path.

As for the fix, whenever it is not a triplet you may compare lbs for inequality to produce the disjoint check.

P.S. it may be worth changing `getOrderedBounds` to return {lb, lb} whenever the descriptor is not a triplet, but I am not sure how well this change works with the existing code.

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


More information about the flang-commits mailing list