[flang-commits] [flang] 6e3292f - [flang] Lower parentheses with hlfir.as_expr and hlfir.no_reassoc

Jean Perier via flang-commits flang-commits at lists.llvm.org
Wed Dec 7 05:46:38 PST 2022


Author: Jean Perier
Date: 2022-12-07T14:45:55+01:00
New Revision: 6e3292fba53002538579fb915cc090c12df04ad6

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

LOG: [flang] Lower parentheses with hlfir.as_expr and hlfir.no_reassoc

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index c29c6034e7695..454f224f2e5c1 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -581,9 +581,14 @@ struct UnaryOp<Fortran::evaluate::ComplexComponent<KIND>> {
 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)};
   }
 };
 

diff  --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp
index 17a413093792d..76fc053b50378 100644
--- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp
+++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp
@@ -291,12 +291,19 @@ void hlfir::genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
   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");
   }

diff  --git a/flang/test/Lower/HLFIR/unary-ops.f90 b/flang/test/Lower/HLFIR/unary-ops.f90
index 148491c6ebd93..11503354930b1 100644
--- a/flang/test/Lower/HLFIR/unary-ops.f90
+++ b/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


        


More information about the flang-commits mailing list