[flang-commits] [flang] 248fcb1 - [flang] Lower logical operations to HLFIR

Jean Perier via flang-commits flang-commits at lists.llvm.org
Fri Dec 2 05:19:41 PST 2022


Author: Jean Perier
Date: 2022-12-02T14:19:12+01:00
New Revision: 248fcb13be80a323491ea67360d91989586a7728

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

LOG: [flang] Lower logical operations to HLFIR

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

Added: 
    

Modified: 
    flang/lib/Lower/ConvertExprToHLFIR.cpp
    flang/test/Lower/HLFIR/binary-ops.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index c278072ac75d..eade15ce08d1 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -457,6 +457,36 @@ struct BinaryOp<Fortran::evaluate::Relational<
   }
 };
 
+template <int KIND>
+struct BinaryOp<Fortran::evaluate::LogicalOperation<KIND>> {
+  using Op = Fortran::evaluate::LogicalOperation<KIND>;
+  static hlfir::EntityWithAttributes gen(mlir::Location loc,
+                                         fir::FirOpBuilder &builder,
+                                         const Op &op, hlfir::Entity lhs,
+                                         hlfir::Entity rhs) {
+    mlir::Type i1Type = builder.getI1Type();
+    mlir::Value i1Lhs = builder.createConvert(loc, i1Type, lhs);
+    mlir::Value i1Rhs = builder.createConvert(loc, i1Type, rhs);
+    switch (op.logicalOperator) {
+    case Fortran::evaluate::LogicalOperator::And:
+      return hlfir::EntityWithAttributes{
+          builder.create<mlir::arith::AndIOp>(loc, i1Lhs, i1Rhs)};
+    case Fortran::evaluate::LogicalOperator::Or:
+      return hlfir::EntityWithAttributes{
+          builder.create<mlir::arith::OrIOp>(loc, i1Lhs, i1Rhs)};
+    case Fortran::evaluate::LogicalOperator::Eqv:
+      return hlfir::EntityWithAttributes{builder.create<mlir::arith::CmpIOp>(
+          loc, mlir::arith::CmpIPredicate::eq, i1Lhs, i1Rhs)};
+    case Fortran::evaluate::LogicalOperator::Neqv:
+      return hlfir::EntityWithAttributes{builder.create<mlir::arith::CmpIOp>(
+          loc, mlir::arith::CmpIPredicate::ne, i1Lhs, i1Rhs)};
+    case Fortran::evaluate::LogicalOperator::Not:
+      // lib/evaluate expression for .NOT. is Fortran::evaluate::Not<KIND>.
+      llvm_unreachable(".NOT. is not a binary operator");
+    }
+  }
+};
+
 /// Lower Expr to HLFIR.
 class HlfirBuilder {
 public:

diff  --git a/flang/test/Lower/HLFIR/binary-ops.f90 b/flang/test/Lower/HLFIR/binary-ops.f90
index 8f504c49a1cb..79dbad0448ea 100644
--- a/flang/test/Lower/HLFIR/binary-ops.f90
+++ b/flang/test/Lower/HLFIR/binary-ops.f90
@@ -289,3 +289,37 @@ subroutine cmp_char(l, x, y)
 ! CHECK:  %[[VAL_12:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_8]], %[[VAL_9]], %[[VAL_10]], %[[VAL_11]]) fastmath<contract> : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
 ! CHECK:  %[[VAL_13:.*]] = arith.constant 0 : i32
 ! CHECK:  %[[VAL_14:.*]] = arith.cmpi eq, %[[VAL_12]], %[[VAL_13]] : i32
+
+subroutine logical_and(x, y, z)
+  logical :: x, y, z
+  x = y.and.z
+end subroutine
+! CHECK-LABEL: func.func @_QPlogical_and(
+! CHECK:  %[[VAL_4:.*]]:2 = hlfir.declare %{{.*}}y"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
+! CHECK:  %[[VAL_5:.*]]:2 = hlfir.declare %{{.*}}z"} : (!fir.ref<!fir.logical<4>>) -> (!fir.ref<!fir.logical<4>>, !fir.ref<!fir.logical<4>>)
+! CHECK:  %[[VAL_6:.*]] = fir.load %[[VAL_4]]#0 : !fir.ref<!fir.logical<4>>
+! CHECK:  %[[VAL_7:.*]] = fir.load %[[VAL_5]]#0 : !fir.ref<!fir.logical<4>>
+! CHECK:  %[[VAL_8:.*]] = fir.convert %[[VAL_6]] : (!fir.logical<4>) -> i1
+! CHECK:  %[[VAL_9:.*]] = fir.convert %[[VAL_7]] : (!fir.logical<4>) -> i1
+! CHECK:  %[[VAL_10:.*]] = arith.andi %[[VAL_8]], %[[VAL_9]] : i1
+
+subroutine logical_or(x, y, z)
+  logical :: x, y, z
+  x = y.or.z
+end subroutine
+! CHECK-LABEL: func.func @_QPlogical_or(
+! CHECK:  %[[VAL_10:.*]] = arith.ori
+
+subroutine logical_eqv(x, y, z)
+  logical :: x, y, z
+  x = y.eqv.z
+end subroutine
+! CHECK-LABEL: func.func @_QPlogical_eqv(
+! CHECK:  %[[VAL_10:.*]] = arith.cmpi eq
+
+subroutine logical_neqv(x, y, z)
+  logical :: x, y, z
+  x = y.neqv.z
+end subroutine
+! CHECK-LABEL: func.func @_QPlogical_neqv(
+! CHECK:  %[[VAL_10:.*]] = arith.cmpi ne


        


More information about the flang-commits mailing list