This is an archive of the discontinued LLVM Phabricator instance.

[flang][hlfir] Fixed lowering for intrinsic calls with null() box argument.
ClosedPublic

Authored by vzakhari on May 12 2023, 6:17 PM.

Diff Detail

Event Timeline

vzakhari created this revision.May 12 2023, 6:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 12 2023, 6:17 PM
vzakhari added inline comments.May 12 2023, 6:20 PM
flang/lib/Lower/ConvertCall.cpp
1245

I wonder if we need to do the same for the NullPointer lowering in ConvertExprToHLFIR.cpp. Then this code here won't be needed.

jeanPerier accepted this revision.May 15 2023, 12:37 AM

LGTM, thanks!

flang/lib/Lower/ConvertCall.cpp
1245

It is not possible to move this for NullPointer in ConvertExprToHLFIR.cpp because this has memory operations (store/alloca), and the NullPointer code in ConvertExprToHLFIR is required in to generate code inside global initializer to deal with C_PTR NullPointer initializer, which appears in compiler generated derived type descriptor symbols.

See for instance when generating code for:

subroutine foo(x)
type t
  integer :: i
end type
  type(t) :: x
end subroutine

There are NULLs() in:

.c.t, SAVE, TARGET (CompilerCreated, ReadOnly): ObjectEntity type: TYPE(component) shape: 0_8:0_8 init:[component::component(name=.n.i,genre=1_1,category=0_1,kind=4_1,rank=0_1,offset=0_8,characterlen=value(genre=1_1,value=0_8),derived=NULL(),lenvalue=NULL(),bounds=NULL(),initialization=NULL())]

So I think this is the right place to deal with this. Beside, as stated in table 16.5, the NULL() appearance here technically takes the "Type, type parameters, and rank" of "the corresponding dummy argument". Technically, we should query here the type and rank from the characteristic and create a fir.box with that.

But in the case of ASSOCIATED that you are fixing here, there is not really any type and rank info for "POINTER"... The arguments are Assumed Type/Assumed Rank since they can be of any type and rank, so there is an infinite loop (Dummy is assumed and takes type and rank from actual, actual is NULL and takes type and ranks from actual)^^. Anyway, my understanding is that ASSOCIATED(NULL(), xx) and ASSOCIATED(xx, NULL()) are false and that the runtime just needs the pointer/target base address to be null to correctly deduce that.

So creating a fir.box<!fir.ptr<None>> is the best we can do I think. I looked at the 11 other cases where "asInquired" is used, and I do not think NULL() would be allowed in any of them. So your code is fine. However, I would rather keep it here in case we need context specific information at some point from the dummy argument to create the null box for some new intrinsic.

This revision is now accepted and ready to land.May 15 2023, 12:37 AM
vzakhari added inline comments.May 15 2023, 9:03 AM
flang/lib/Lower/ConvertCall.cpp
1245

Oh, I see. I missed the point that NullPointer in some contexts is just a nullptr constant. Thanks for the example!