[RFC][OpenMP] Remove support for Privatisation of Statement Function Variables

There was some work here to support privatization of variables referenced in statement functions. This was motivated by issues from the Fujitsu test suite: [Flang][OpenMP] Incorrect execution result of using statement function in task construct · Issue #74273 · llvm/llvm-project · GitHub. Gfortran does support privatising statement function variables.

However, as raised here, the OpenMP standard prohibits privatisation of statement function variables. See 5.2 section 5.3:

Variables that appear in namelist statements, in variable format expressions, and in expressions for statement function definitions, must not be privatized.

I have a PR preventing statement function variable privatization.

Should Flang follow the standard in preventing this privatization, or should we continue to support it?

@mjklemm @Meinersbur @bob.belcher @luporl

I’m generally fine with following the standards closely, but I’d like to check with some of the AMD customers to see if they’d have trouble when this is actually treated as an error.

When a feature is already supported by other compilers, is used by applications, and doesn’t change the semantics of a conforming program, supporting that feature improves portability and tends to be the right thing to do.

1 Like

As a general principle, this compiler will accept by default and without complaint many legacy features, extensions to the standard language, and features that have been deleted from the standard, so long as the recognition of those features would not cause a standard-conforming program to be rejected or misinterpreted.

From Fortran Extensions supported by Flang

1 Like

Besides GFortran, IFORT and IFX also support privatization of variables referenced in statement functions.

But when privatization is performed through the default clause, there is a change in behavior.

Consider this program:

program test
  sf(iexp)=func(iimp)+iexp

  iimp = 11
  iexp = 22
  !$omp parallel default(firstprivate)
  !$omp single
      iexp = sf(iexp)
  !$omp end single
  !$omp end parallel
  print *, iimp

contains
  integer function func(i)
    i = i * 3
    func = i
  end function
end program

AFAIK this is a standard-conforming program, that should print 33, as iimp shouldn’t be privatized. When privatization is allowed, however, it prints 11 instead.

Also from Fortran Extensions supported by Flang

Other non-standard features, which do conflict with the current standard specification of the Fortran programming language, are accepted if enabled by command-line options.

Thanks for the clever example. It seems current flang-new and gfortran print 11 here, but armflang (classic flang) correctly prints 33. I don’t have IFORT and IFX available to test.

From this example I think we should follow classic flang and not privatise these variables. Would that be okay @klausler and @sscalpone?

You can use Compiler Explorer for ifx/ifort. They seem to print 11 here.

nvfortran (22.1) prints 33 here like classic flang.

2 Likes

@klausler @sscalpone @mjklemm ping

I think we should remove support for privatisation of statement function variables because of @luporl’s example which produces different results for the base language. Are you okay with this?

I don’t understand your point, perhaps because “base language” means just Fortran to me.

Sorry that was unclear. I meant Fortran+OpenMP as described by the standards, without extensions such as this one

Looks like GNU, Intel, and Fujitsu all privatize variables referenced in statement functions, so whether or not you support the extension, you should at least emit a portability warning message that might help debug codes that depend on it (or not)…

1 Like

I agree that OpenMP should not change the behavior of the base language and therefore we should heed the OpenMP specification here.

I see very little use of statement functions in applications and the few cases I recall are confusing enough without implementation-defined behavior.

I don’t think that any Fortran-only behavior is in question. It’s an OpenMP issue only.

@mjklemm messaged me privately to say he is happy to move forward with this proposal.

The PR is ready for review [flang][semantics][OpenMP] no privatisation of stmt functions by tblah · Pull Request #106550 · llvm/llvm-project · GitHub