llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Flang error parsing fixed form fortran source with `-save-temps`

Open macurtis-amd opened this issue 2 years ago • 2 comments

Here is the problematic fixed form fortran source reduced.f:

      subroutine foo(a, b)
      if ( (a  .eq. 0) .and.
     >     (b . eq. 1) ) then
         print *, "foo"
      end if
      end subroutine

Prior to 9fb2db1 flang -save-temps -c produced reduced.i:

      subroutine foo(a, b)
      if ( (a  .eq. 0) .and.(b.eq.1))then

         print *, "foo"
      end if
      end subroutine

With 9fb2db1, flang -save-temps -c produces reduced.i:

      subroutine foo(a, b)
      if ( (a  .eq. 0) .and.(b. eq. 1)) then

         print *, "foo"
      end if
      end subroutine

Which produces:

error: Could not parse reduced.i
./reduced.f:2:31: error: expected ')'
        if ( (a  .eq. 0) .and.(b. eq. 1)) then
                                ^
...

In either case the commands produced by the driver look like:

flang-new -fc1 -E -o reduced.i -x f95-cpp-input reduced.f
flang-new -fc1 -emit-llvm-bc ... -o reduced.bc -x f95 reduced.i
...

macurtis-amd avatar Nov 26 '24 13:11 macurtis-amd

The -x f95-cpp-input option doesn't seem appropriate for a .f source file.

klausler avatar Nov 26 '24 15:11 klausler

Conceptually we don't need the existing -x f95... distinction anyway, I believe it's just there for gfortran compatibility. As far as we're concerned it really means -x fortran because we decide on fixed vs free form elsewhere, and we don't care about preprocessed vs non-preprocessed input as the flang preprocessor always runs as part of parsing regardless.

I posted the following on the PR, cross-posting here for visibility: It feels wrong that -save-temps runs the preprocessor separately anyway for flang. There's no "temp" generated by the flang preprocessor as preprocessing is not a separate step to parsing. We don't appear to need to do this for compatibility with gfortran either because gfortran doesn't appear to save a preprocessed file with -save-temps (at least on my system). Even if we do chose to have -save-temps save a preprocessed file for some reason, I think .i is the wrong extension as that's usually used for C/C++ preprocessed files. Does your use case specifically require -save-temps to save a preprocessed file, and if so does it have to have the .i extension? If not, I'd propose we modify -save-temps to not preprocess separately.

DavidTruby avatar Nov 27 '24 01:11 DavidTruby