[flang-commits] [flang] 90cf101 - [flang][hlfir] Lower NULL(MOLD) to a variable

Jean Perier via flang-commits flang-commits at lists.llvm.org
Tue Apr 25 00:03:06 PDT 2023


Author: Jean Perier
Date: 2023-04-25T09:02:33+02:00
New Revision: 90cf1014243b2edec249cab7a0bfde1e8c0fdf1c

URL: https://github.com/llvm/llvm-project/commit/90cf1014243b2edec249cab7a0bfde1e8c0fdf1c
DIFF: https://github.com/llvm/llvm-project/commit/90cf1014243b2edec249cab7a0bfde1e8c0fdf1c.diff

LOG: [flang][hlfir] Lower NULL(MOLD) to a variable

HLFIR lowering promotes intrinsic results lowered in memory to
hlfir.expr to underline their read-only aspect once they are created.
NULL(MOLD) should not be promoted to an hlfir.expr, it is the NULL
variable (we need to see it as an address).

Reviewed By: clementval

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

Added: 
    flang/test/Lower/HLFIR/null.f90

Modified: 
    flang/lib/Lower/ConvertCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index 39a8be38cb297..8f1980362c4f4 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -1247,8 +1247,10 @@ genIntrinsicRefCore(PreparedActualArguments &loweredActuals,
   hlfir::EntityWithAttributes resultEntity = extendedValueToHlfirEntity(
       loc, builder, resultExv, ".tmp.intrinsic_result");
   // Move result into memory into an hlfir.expr since they are immutable from
-  // that point, and the result storage is some temp.
-  if (resultEntity.isVariable()) {
+  // that point, and the result storage is some temp. "Null" is special: it
+  // returns a null pointer variable that should not be transformed into a value
+  // (what matters is the memory address).
+  if (resultEntity.isVariable() && intrinsicName != "null") {
     hlfir::AsExprOp asExpr;
     // Character/Derived MERGE lowering returns one of its argument address
     // (this is the only intrinsic implemented in that way so far). The

diff  --git a/flang/test/Lower/HLFIR/null.f90 b/flang/test/Lower/HLFIR/null.f90
new file mode 100644
index 0000000000000..6ae44082f3164
--- /dev/null
+++ b/flang/test/Lower/HLFIR/null.f90
@@ -0,0 +1,20 @@
+! Test lowering of NULL(MOLD) to HLFIR.
+! RUN: bbc -emit-fir -hlfir -o - %s | FileCheck %s
+subroutine test(mold)
+  integer, pointer :: mold(:)
+  interface
+    subroutine takes_ptr(p)
+      integer, pointer :: p(:)
+    end subroutine
+  end interface
+  call takes_ptr(null(mold))
+end subroutine
+! CHECK-LABEL:   func.func @_QPtest(
+! CHECK:  %[[VAL_1:.*]] = fir.alloca !fir.box<!fir.ptr<!fir.array<?xi32>>>
+! CHECK:  %[[VAL_3:.*]] = fir.zero_bits !fir.ptr<!fir.array<?xi32>>
+! CHECK:  %[[VAL_4:.*]] = arith.constant 0 : index
+! CHECK:  %[[VAL_5:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
+! CHECK:  %[[VAL_6:.*]] = fir.embox %[[VAL_3]](%[[VAL_5]]) : (!fir.ptr<!fir.array<?xi32>>, !fir.shape<1>) -> !fir.box<!fir.ptr<!fir.array<?xi32>>>
+! CHECK:  fir.store %[[VAL_6]] to %[[VAL_1]] : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>
+! CHECK:  %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>, !fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>)
+! CHECK:  fir.call @_QPtakes_ptr(%[[VAL_7]]#0) fastmath<contract> : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xi32>>>>) -> ()


        


More information about the flang-commits mailing list