llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.86k stars 11.48k forks source link

[Flang] Preprocessor too aggressively removes continuation lines #100345

Closed mjklemm closed 1 week ago

mjklemm commented 1 month ago

With this reproducer:

program omp_blank_continuation
    use omp_lib
    implicit none

    integer :: tid

    !$omp parallel &
#ifdef XYZ
    !$omp default(none) &
#endif
    !$omp    private(tid)
        tid = omp_get_thread_num()
        print '(A," ",I0)', 'Hello from', tid
    !$omp end parallel
end program omp_blank_continuation

The preprocessors generates invalid OpenMP directives:

> flang-new -fopenmp -cpp -c omp_blank_continuation.f90                                                     [amd-trunk-dev:dev]
error: Could not parse omp_blank_continuation.f90
./omp_blank_continuation.f90:7:12: error: expected OpenMP construct
      !$omp parallel &
             ^
./omp_blank_continuation.f90:7:5: in the context: specification construct
      !$omp parallel &
      ^
./omp_blank_continuation.f90:7:5: in the context: declaration construct
      !$omp parallel &
      ^
./omp_blank_continuation.f90:2:5: in the context: specification part
      use omp_lib
      ^
./omp_blank_continuation.f90:1:1: in the context: main program
  program omp_blank_continuation
  ^
./omp_blank_continuation.f90:14:5: error: expected 'END'
      !$omp end parallel
      ^
./omp_blank_continuation.f90:14:5: in the context: END PROGRAM statement
      !$omp end parallel
      ^
./omp_blank_continuation.f90:1:1: in the context: main program
  program omp_blank_continuation
  ^
./omp_blank_continuation.f90:14:5: error: expected 'END PROGRAM'
      !$omp end parallel
      ^
./omp_blank_continuation.f90:14:5: in the context: END PROGRAM statement
      !$omp end parallel
      ^
./omp_blank_continuation.f90:1:1: in the context: main program
  program omp_blank_continuation
  ^

This is due to seemingly too agressive removal of continuation lines:

#line "./omp_blank_continuation.f90" 1
      program omp_blank_continuation
      use omp_lib
      implicit none

      integer :: tid

    !$omp parallel !$omp private(tid)

        tid = omp_get_thread_num()
        print '(A," ",I0)', 'Hello from', tid
    !$omp end parallel
      end program omp_blank_continuation
mjklemm commented 1 month ago

@klausler Can you please have a look at this one?

klausler commented 1 month ago

what would you like to see in the -E output? !$omp parallel private(tid)?

mjklemm commented 1 month ago

I think the best idea would be something like this:

#line "./omp_blank_continuation.f90" 1
      program omp_blank_continuation
      use omp_lib
      implicit none

      integer :: tid

    !$omp parallel &

    !$omp private(tid)
        tid = omp_get_thread_num()
        print '(A," ",I0)', 'Hello from', tid
    !$omp end parallel
      end program omp_blank_continuation

So, maybe keep the structure of the lines incl. their continuation. because then we do not have to mess with the rewriting of OpenMP (or OpenACC maybe, too) directives to merge them.

Leporacanthicus commented 3 weeks ago

We are also hitting this issue in some of the test-applications we're using internally.

llvmbot commented 3 weeks ago

@llvm/issue-subscribers-flang-frontend

Author: Michael Klemm (mjklemm)

With this reproducer: ```Fortran program omp_blank_continuation use omp_lib implicit none integer :: tid !$omp parallel & #ifdef XYZ !$omp default(none) & #endif !$omp private(tid) tid = omp_get_thread_num() print '(A," ",I0)', 'Hello from', tid !$omp end parallel end program omp_blank_continuation ``` The preprocessors generates invalid OpenMP directives: ``` > flang-new -fopenmp -cpp -c omp_blank_continuation.f90 [amd-trunk-dev:dev] error: Could not parse omp_blank_continuation.f90 ./omp_blank_continuation.f90:7:12: error: expected OpenMP construct !$omp parallel & ^ ./omp_blank_continuation.f90:7:5: in the context: specification construct !$omp parallel & ^ ./omp_blank_continuation.f90:7:5: in the context: declaration construct !$omp parallel & ^ ./omp_blank_continuation.f90:2:5: in the context: specification part use omp_lib ^ ./omp_blank_continuation.f90:1:1: in the context: main program program omp_blank_continuation ^ ./omp_blank_continuation.f90:14:5: error: expected 'END' !$omp end parallel ^ ./omp_blank_continuation.f90:14:5: in the context: END PROGRAM statement !$omp end parallel ^ ./omp_blank_continuation.f90:1:1: in the context: main program program omp_blank_continuation ^ ./omp_blank_continuation.f90:14:5: error: expected 'END PROGRAM' !$omp end parallel ^ ./omp_blank_continuation.f90:14:5: in the context: END PROGRAM statement !$omp end parallel ^ ./omp_blank_continuation.f90:1:1: in the context: main program program omp_blank_continuation ^ ``` This is due to seemingly too agressive removal of continuation lines: ```Fortran #line "./omp_blank_continuation.f90" 1 program omp_blank_continuation use omp_lib implicit none integer :: tid !$omp parallel !$omp private(tid) tid = omp_get_thread_num() print '(A," ",I0)', 'Hello from', tid !$omp end parallel end program omp_blank_continuation ```
mjklemm commented 2 weeks ago

@klausler Would you mind spending some time on this? It has popped up in the linked report #100730, too.

klausler commented 2 weeks ago

https://github.com/llvm/llvm-project/pull/105572