[flang-commits] [PATCH] D139523: [flang] Lower parentheses with hlfir.as_expr and hlfir.no_reassoc

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Dec 7 03:14:16 PST 2022


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

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139523

Files:
  flang/lib/Lower/ConvertExprToHLFIR.cpp
  flang/lib/Optimizer/Builder/HLFIRTools.cpp
  flang/test/Lower/HLFIR/unary-ops.f90


Index: flang/test/Lower/HLFIR/unary-ops.f90
===================================================================
--- flang/test/Lower/HLFIR/unary-ops.f90
+++ flang/test/Lower/HLFIR/unary-ops.f90
@@ -1,6 +1,39 @@
 ! Test lowering of unary intrinsic operations to HLFIR
 ! RUN: bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
 
+subroutine parentheses_numeric_var(x)
+  integer :: x
+  call bar((x))
+end subroutine
+! CHECK-LABEL: func.func @_QPparentheses_numeric_var(
+! CHECK:  %[[VAL_2:.*]] = fir.load %{{.*}} : !fir.ref<i32>
+! CHECK:  %[[VAL_3:.*]] = hlfir.no_reassoc %[[VAL_2]] : i32
+
+subroutine parentheses_numeric_expr(x)
+  real :: x
+  call bar((x+1000.)*2.)
+end subroutine
+! CHECK-LABEL: func.func @_QPparentheses_numeric_expr(
+! CHECK:  %[[VAL_4:.*]] = arith.addf
+! CHECK:  %[[VAL_5:.*]] = hlfir.no_reassoc %[[VAL_4]] : f32
+! CHECK:  %[[VAL_7:.*]] = arith.mulf %[[VAL_5]], %{{.*}}
+
+subroutine parentheses_char_var(x)
+  character(*) :: x
+  call bar2((x))
+end subroutine
+! CHECK-LABEL: func.func @_QPparentheses_char_var(
+! CHECK:  %[[VAL_2:.*]]:2 = hlfir.declare
+! CHECK:  %[[VAL_3:.*]] = hlfir.as_expr %[[VAL_2]]#0 : (!fir.boxchar<1>) -> !hlfir.expr<!fir.char<1,?>>
+
+subroutine parentheses_char_expr(x)
+  character(*) :: x
+  call bar2((x//x)//x)
+end subroutine
+! CHECK-LABEL: func.func @_QPparentheses_char_expr(
+! CHECK:  %[[VAL_4:.*]] = hlfir.concat
+! CHECK:  %[[VAL_5:.*]] = hlfir.no_reassoc %[[VAL_4]] : !hlfir.expr<!fir.char<1,?>>
+! CHECK:  %[[VAL_7:.*]] = hlfir.concat %[[VAL_5]], %{{.*}} len %{{.*}}
 subroutine test_not(l, x)
   logical :: l, x
   l = .not.x
Index: flang/lib/Optimizer/Builder/HLFIRTools.cpp
===================================================================
--- flang/lib/Optimizer/Builder/HLFIRTools.cpp
+++ flang/lib/Optimizer/Builder/HLFIRTools.cpp
@@ -291,12 +291,19 @@
   if (!entity.hasLengthParameters())
     return;
   if (entity.getType().isa<hlfir::ExprType>()) {
+    mlir::Value expr = entity;
+    if (auto reassoc = expr.getDefiningOp<hlfir::NoReassocOp>())
+      expr = reassoc.getVal();
     // Going through fir::ExtendedValue would create a temp,
     // which is not desired for an inquiry.
     // TODO: make this an interface when adding further character producing ops.
-    if (auto concat = entity.getDefiningOp<hlfir::ConcatOp>()) {
+    if (auto concat = expr.getDefiningOp<hlfir::ConcatOp>()) {
       result.push_back(concat.getLength());
       return;
+    } else if (auto asExpr = expr.getDefiningOp<hlfir::AsExprOp>()) {
+      hlfir::genLengthParameters(loc, builder, hlfir::Entity{asExpr.getVar()},
+                                 result);
+      return;
     }
     TODO(loc, "inquire type parameters of hlfir.expr");
   }
Index: flang/lib/Lower/ConvertExprToHLFIR.cpp
===================================================================
--- flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -581,9 +581,14 @@
 template <typename T>
 struct UnaryOp<Fortran::evaluate::Parentheses<T>> {
   using Op = Fortran::evaluate::Parentheses<T>;
-  static hlfir::EntityWithAttributes
-  gen(mlir::Location loc, fir::FirOpBuilder &, const Op &, hlfir::Entity) {
-    TODO(loc, "Parentheses lowering to HLFIR");
+  static hlfir::EntityWithAttributes gen(mlir::Location loc,
+                                         fir::FirOpBuilder &builder,
+                                         const Op &op, hlfir::Entity lhs) {
+    if (lhs.isVariable())
+      return hlfir::EntityWithAttributes{
+          builder.create<hlfir::AsExprOp>(loc, lhs)};
+    return hlfir::EntityWithAttributes{
+        builder.create<hlfir::NoReassocOp>(loc, lhs.getType(), lhs)};
   }
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139523.480835.patch
Type: text/x-patch
Size: 3692 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221207/8b96c6dd/attachment-0001.bin>


More information about the flang-commits mailing list