[flang-commits] [flang] 7eecfc0 - [Flang] Add flag dependent code to execute the loop-body atleast once

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Tue Jun 7 03:12:41 PDT 2022


Author: Kiran Chandramohan
Date: 2022-06-07T10:12:09Z
New Revision: 7eecfc077f36fe249d5457e2d9a0e294cb25d615

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

LOG: [Flang] Add flag dependent code to execute the loop-body atleast once

Given the flag `--always-execute-loop-body` the compiler emits code
to execute the body of the loop atleast once.

Note: This is part of upstreaming from the fir-dev branch of
https://github.com/flang-compiler/f18-llvm-project.

Reviewed By: awarzynski, schweitz

Differential Revision: https://reviews.llvm.org/D127128

Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Co-authored-by: V Donaldson <vdonaldson at nvidia.com>
Co-authored-by: Valentin Clement <clementval at gmail.com>
Co-authored-by: Sameeran Joshi <sameeranjayant.joshi at amd.com>

Added: 
    flang/test/Lower/always-execute-loop-body.f90

Modified: 
    flang/lib/Lower/Bridge.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index f81456bc601a7..3ea23857624d5 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1106,6 +1106,14 @@ class FirConverter : public Fortran::lower::AbstractConverter {
         tripCount =
             builder->create<mlir::arith::DivSIOp>(loc, 
diff 2, info.stepValue);
       }
+      if (forceLoopToExecuteOnce) { // minimum tripCount is 1
+        mlir::Value one =
+            builder->createIntegerConstant(loc, tripCount.getType(), 1);
+        auto cond = builder->create<mlir::arith::CmpIOp>(
+            loc, mlir::arith::CmpIPredicate::slt, tripCount, one);
+        tripCount =
+            builder->create<mlir::arith::SelectOp>(loc, cond, one, tripCount);
+      }
       info.tripVariable = builder->createTemporary(loc, tripCount.getType());
       builder->create<fir::StoreOp>(loc, tripCount, info.tripVariable);
       builder->create<fir::StoreOp>(loc, lowerValue, info.loopVariable);

diff  --git a/flang/test/Lower/always-execute-loop-body.f90 b/flang/test/Lower/always-execute-loop-body.f90
new file mode 100644
index 0000000000000..7c4ef1af33929
--- /dev/null
+++ b/flang/test/Lower/always-execute-loop-body.f90
@@ -0,0 +1,20 @@
+! RUN: bbc --always-execute-loop-body --emit-fir %s -o - | FileCheck %s
+! RUN: %flang_fc1 -mmlir --always-execute-loop-body -emit-fir %s -o - | FileCheck %s
+
+! Given the flag `--always-execute-loop-body` the compiler emits an extra
+! code to change to tripcount, test tries to verify the extra emitted FIR.
+
+! CHECK-LABEL: func @_QPsome
+subroutine some()
+  integer :: i
+
+  ! CHECK: [[tripcount:%[0-9]+]] = arith.divsi
+  ! CHECK: [[one:%c1_i32[_0-9]*]] = arith.constant 1 : i32
+  ! CHECK: [[cmp:%[0-9]+]] = arith.cmpi slt, [[tripcount]], [[one]] : i32
+  ! CHECK: [[newtripcount:%[0-9]+]] = arith.select [[cmp]], [[one]], [[tripcount]] : i32
+  ! CHECK: fir.store [[newtripcount]] to %{{[0-9]+}} : !fir.ref<i32>
+  do i=4,1,1
+    stop 2
+  end do
+  return
+end


        


More information about the flang-commits mailing list