[flang-commits] [PATCH] D139179: [flang] Lower logical operations to HLFIR

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


jeanPerier created this revision.
jeanPerier added a reviewer: clementval.
jeanPerier added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

Depends on D139176 <https://reviews.llvm.org/D139176>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139179

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


Index: flang/test/Lower/HLFIR/binary-ops.f90
===================================================================
--- flang/test/Lower/HLFIR/binary-ops.f90
+++ flang/test/Lower/HLFIR/binary-ops.f90
@@ -289,3 +289,37 @@
 ! 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
Index: flang/lib/Lower/ConvertExprToHLFIR.cpp
===================================================================
--- flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -457,6 +457,36 @@
   }
 };
 
+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:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139179.479559.patch
Type: text/x-patch
Size: 3662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221202/13d79251/attachment.bin>


More information about the flang-commits mailing list