This is an archive of the discontinued LLVM Phabricator instance.

[flang] Support GNU extensions IARGC and GETARG in runtime
ClosedPublic

Authored by peixin on Sep 9 2022, 1:21 AM.

Details

Summary

The GNU extension intrinsic IARGC is equivalent to
COMMAND_ARGUMENT_COUNT, and the GETARG is similar to
GET_COMMAND_ARGUMENT, but with less arguments. Reuse the runtime of
COMMAND_ARGUMENT_COUNT and GET_COMMAND_ARGUMENT for IARGC and GETARG.

Diff Detail

Event Timeline

peixin created this revision.Sep 9 2022, 1:21 AM
Herald added a project: Restricted Project. · View Herald Transcript
peixin requested review of this revision.Sep 9 2022, 1:21 AM

I think we need any entry for these two non-standard intrinsics in flang/docs/Extensions.md.

flang/lib/Evaluate/intrinsics.cpp
479 ↗(On Diff #458978)

This should be after the second iand?

I think we need any entry for these two non-standard intrinsics in flang/docs/Extensions.md.

What is the current stand about extensions? Should we have RFC before introducing them?

peixin updated this revision to Diff 458990.Sep 9 2022, 2:16 AM
peixin added a comment.EditedSep 9 2022, 2:20 AM

I think we need any entry for these two non-standard intrinsics in flang/docs/Extensions.md.

What is the current stand about extensions? Should we have RFC before introducing them?

Some old workloads/applications use these two non-standard intrinsics. I hit this when building PNetCDF 1.11.2. Should I submit one RFC for this?

flang/lib/Evaluate/intrinsics.cpp
479 ↗(On Diff #458978)

Thanks. Fixed.

I think we need any entry for these two non-standard intrinsics in flang/docs/Extensions.md.

What is the current stand about extensions? Should we have RFC before introducing them?

I think in general this is a good idea.

PeteSteinfeld added a subscriber: PeteSteinfeld.

@klausler, has more experience in this area than anyone I know. It would be good to get his approval before submitting.

@klausler, has more experience in this area than anyone I know. It would be good to get his approval before submitting.

OK. Thanks. I will wait for @klausler 's review.

Why do these need to be intrinsic procedures, and not just entry points in a standard library?

Why do these need to be intrinsic procedures, and not just entry points in a standard library?

  1. What do you mean by "entry points in a standard library"?Does it mean the implementation of c_sizoof and sizeof? There is only one module "__Fortran_builtins" implicitly used.
  1. GETARG and GET_COMMAND_ARGUMENT have different arguments. It seems they cannot be implemented using the same entry point.
peixin updated this revision to Diff 462652.Sep 24 2022, 2:30 AM
peixin edited the summary of this revision. (Show Details)

Why do these need to be intrinsic procedures, and not just entry points in a standard library?

I get what you mean. Add IARGC as the alias of COMMAND_ARGUMENT_COUNT since they have the same interface. GETARG and GET_COMMAND_ARGUMENT have different interfaces. If they have the same entry point, the following test case will pass. However, it is non-conforming in gfortran.

PROGRAM test_get_command_argument
  INTEGER :: i
  CHARACTER(len=32) :: arg

  i = 0
  DO
    CALL GETARG(i, arg, length)
    IF (LEN_TRIM(arg) == 0) EXIT
    print *, i, " ", arg, " ", length
    i = i+1
  END DO
END PROGRAM

I am not suggesting that you simply establish some aliases.

IARGC and GETARG can be written in Fortran or C with code that calls the standard intrinsic procedures. Why not do that instead of creating new extension intrinsics?

I am not suggesting that you simply establish some aliases.

IARGC and GETARG can be written in Fortran or C with code that calls the standard intrinsic procedures. Why not do that instead of creating new extension intrinsics?

I am sorry that I am still confused about what you mean. Do you mean that Fortran users should handle this instead of supporting them in the compiler?

I am not suggesting that you simply establish some aliases.

IARGC and GETARG can be written in Fortran or C with code that calls the standard intrinsic procedures. Why not do that instead of creating new extension intrinsics?

I am sorry that I am still confused about what you mean. Do you mean that Fortran users should handle this instead of supporting them in the compiler?

No.

I mean that IARGC and GETARG can be defined in the runtime library without being intrinsics.

peixin updated this revision to Diff 463461.Sep 28 2022, 2:01 AM
peixin retitled this revision from [flang] Support GNU extension intrinsics IARGC and GETARG to [flang] Support GNU extensions IARGC and GETARG in runtime.
peixin edited the summary of this revision. (Show Details)

Implemented in runtime.

peixin updated this revision to Diff 463570.Sep 28 2022, 8:25 AM

Add <cstdint> to fix CI fail.

peixin updated this revision to Diff 463754.Sep 28 2022, 8:28 PM

Fix clang-format issue.

klausler added inline comments.Oct 10 2022, 8:43 AM
flang/include/flang/Runtime/extensions.h
16

Would functions and subroutines ever have distinct name mangling conventions? I think that you could have just one macro here.

peixin updated this revision to Diff 466528.Oct 10 2022, 9:13 AM
peixin added inline comments.
flang/include/flang/Runtime/extensions.h
16

Thanks. Fixed.

klausler added inline comments.Oct 10 2022, 9:22 AM
flang/runtime/extensions.cpp
19

Now that you are adding more definitions to the namespace Fortran::runtime, this comment should be moved down so that it applies to the definition of FLUSH only, not to the entire namespace.

peixin updated this revision to Diff 466534.Oct 10 2022, 9:35 AM

Move the comments as suggested.

klausler accepted this revision.Oct 10 2022, 10:35 AM
klausler added inline comments.
flang/runtime/extensions.cpp
35

n could be a reference.

arg could be const.

This revision is now accepted and ready to land.Oct 10 2022, 10:35 AM
peixin updated this revision to Diff 466684.Oct 10 2022, 7:02 PM

Thanks @klausler. Addressed the comments.

flang/runtime/extensions.cpp
35

Fix the *n to &n.

arg should not be const since Descriptor::Create requires it to be non-const. I hit the following error when I change it.

error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
   36 |   Descriptor value{*Descriptor::Create(1, length, arg, 0)};
      |                                                   ^~~
      |                                                   |
      |                                                   const void*
This revision was automatically updated to reflect the committed changes.