[flang-commits] [flang] fe0f366 - [flang] Fixed hoisting order in fir.do_concurrent simplification. (#174044)

via flang-commits flang-commits at lists.llvm.org
Wed Dec 31 10:08:37 PST 2025


Author: Slava Zakharin
Date: 2025-12-31T10:08:32-08:00
New Revision: fe0f366f6ea0bd2a9bac7c88677dd862bbe52966

URL: https://github.com/llvm/llvm-project/commit/fe0f366f6ea0bd2a9bac7c88677dd862bbe52966
DIFF: https://github.com/llvm/llvm-project/commit/fe0f366f6ea0bd2a9bac7c88677dd862bbe52966.diff

LOG: [flang] Fixed hoisting order in fir.do_concurrent simplification. (#174044)

The order has to be fixed after #173502. This results in
reversing the order of `fir.alloca`, but that should be
insignificant.

Added: 
    

Modified: 
    flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp
    flang/test/Transforms/do_concurrent-to-do_loop-unodered.fir

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp b/flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp
index 6c754f226e26f..3c4da62eda93c 100644
--- a/flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp
+++ b/flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp
@@ -268,7 +268,7 @@ class DoConcurrentConversion
 
     // Move alloca operations into the alloca-block, and all other
     // operations - right before fir.do_concurrent.
-    for (mlir::Operation *op : llvm::reverse(opsToMove))
+    for (mlir::Operation *op : opsToMove)
       if (mlir::isa<fir::AllocaOp>(op))
         rewriter.moveOpBefore(op, allocIt, allocIt->begin());
       else

diff  --git a/flang/test/Transforms/do_concurrent-to-do_loop-unodered.fir b/flang/test/Transforms/do_concurrent-to-do_loop-unodered.fir
index eb04e7e58bec9..e5cd9c1192dbc 100644
--- a/flang/test/Transforms/do_concurrent-to-do_loop-unodered.fir
+++ b/flang/test/Transforms/do_concurrent-to-do_loop-unodered.fir
@@ -58,8 +58,8 @@ func.func @dc_2d(%i_lb: index, %i_ub: index, %i_st: index,
 // CHECK-SAME:                     %[[J_UB:[^[:space:]]+]]: index,
 // CHECK-SAME:                     %[[J_ST:[^[:space:]]+]]: index) {
 
-// CHECK:           %[[I:.*]] = fir.alloca i32
 // CHECK:           %[[J:.*]] = fir.alloca i32
+// CHECK:           %[[I:.*]] = fir.alloca i32
 // CHECK:           llvm.br ^bb1
 
 // CHECK:         ^bb1:
@@ -105,8 +105,8 @@ func.func @dc_2d_reduction(%i_lb: index, %i_ub: index, %i_st: index,
 // CHECK-SAME:                     %[[J_UB:[^[:space:]]+]]: index,
 // CHECK-SAME:                     %[[J_ST:[^[:space:]]+]]: index) {
 
-// CHECK:           %[[I:.*]] = fir.alloca i32
 // CHECK:           %[[J:.*]] = fir.alloca i32
+// CHECK:           %[[I:.*]] = fir.alloca i32
 // CHECK:           %[[SUM:.*]] = fir.alloca i32
 
 // CHECK:           fir.do_loop %[[I_IV:.*]] = %[[I_LB]] to %[[I_UB]] step %[[I_ST]] unordered reduce({{.*}}<add>] -> %[[SUM]] : !fir.ref<i32>) {
@@ -225,3 +225,44 @@ func.func @after_licm(%i_lb: index, %i_ub: index, %i_st: index, %val : i32) {
 // CHECK:           }
 // CHECK:           return
 // CHECK:         }
+
+// -----
+
+// Check that a chain of dependent operations (e.g. subi/index_cast below)
+// is moved in proper order.
+func.func @after_licm2(%i_lb: index, %i_ub: index, %i_st: index, %val : i32) {
+  %c1_i32 = arith.constant 1 : i32
+  %c7_i32 = arith.constant 7 : i32
+  cf.br ^bb1
+^bb1:
+  %newval1 = arith.muli %val, %c7_i32 : i32
+  fir.do_concurrent {
+    %i = fir.alloca index
+    %newval2 = arith.subi %newval1, %c1_i32 : i32
+    %newval3 = arith.index_cast %newval2 : i32 to index
+    fir.do_concurrent.loop (%i_iv) = (%i_lb) to (%i_ub) step (%i_st) {
+      %1 = arith.addi %newval3, %i_iv : index
+      fir.store %1 to %i : !fir.ref<index>
+    }
+  }
+  return
+}
+// CHECK-LABEL:   func.func @after_licm2(
+// CHECK-SAME:      %[[ARG0:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: index,
+// CHECK-SAME:      %[[ARG1:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: index,
+// CHECK-SAME:      %[[ARG2:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: index,
+// CHECK-SAME:      %[[ARG3:[0-9]+|[a-zA-Z$._-][a-zA-Z0-9$._-]*]]: i32) {
+// CHECK:           %[[CONSTANT_0:.*]] = arith.constant 7 : i32
+// CHECK:           %[[CONSTANT_1:.*]] = arith.constant 1 : i32
+// CHECK:           %[[ALLOCA_0:.*]] = fir.alloca index
+// CHECK:           cf.br ^bb1
+// CHECK:         ^bb1:
+// CHECK:           %[[MULI_0:.*]] = arith.muli %[[ARG3]], %[[CONSTANT_0]] : i32
+// CHECK:           %[[SUBI_0:.*]] = arith.subi %[[MULI_0]], %[[CONSTANT_1]] : i32
+// CHECK:           %[[INDEX_CAST_0:.*]] = arith.index_cast %[[SUBI_0]] : i32 to index
+// CHECK:           fir.do_loop %[[VAL_0:.*]] = %[[ARG0]] to %[[ARG1]] step %[[ARG2]] unordered {
+// CHECK:             %[[ADDI_0:.*]] = arith.addi %[[INDEX_CAST_0]], %[[VAL_0]] : index
+// CHECK:             fir.store %[[ADDI_0]] to %[[ALLOCA_0]] : !fir.ref<index>
+// CHECK:           }
+// CHECK:           return
+// CHECK:         }


        


More information about the flang-commits mailing list