diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp --- a/flang/lib/Lower/ConvertCall.cpp +++ b/flang/lib/Lower/ConvertCall.cpp @@ -1247,8 +1247,10 @@ 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 --- /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>> +! CHECK: %[[VAL_3:.*]] = fir.zero_bits !fir.ptr> +! 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.shape<1>) -> !fir.box>> +! CHECK: fir.store %[[VAL_6]] to %[[VAL_1]] : !fir.ref>>> +! CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_1]] {uniq_name = ".tmp.intrinsic_result"} : (!fir.ref>>>) -> (!fir.ref>>>, !fir.ref>>>) +! CHECK: fir.call @_QPtakes_ptr(%[[VAL_7]]#0) fastmath : (!fir.ref>>>) -> ()