llvm / llvm/llvm-project

[Flang][OpenMP] Incorrect result of an addition of a firstprivate array in a workshare construct

Open
#190,581 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

flang:openmp
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Summary

When an array is firstprivate in an OpenMP workshare construct, an addition using it in the construct returns an incorrect result.

Reproducer

$ cat test.f90

program test
  integer(4),parameter::n=7*7
  integer(4),dimension(n)::z,a
  z=10
  a=0
  call sub1(a,z,n)
  print *,'pass'
end

subroutine sub1(a,z,n)
  integer(4)::n
  integer(4),dimension(n)::z,a
!$omp parallel workshare firstprivate(z)
  a=z+1
!$omp end parallel workshare
  if (any(a/=z+1) ) then
    print *,"fail",a
    error stop
  end if
end subroutine
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git 58208a0cc165c23d2fad4ed46950b342156875ac)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang -fopenmp -O0 test.f90 && env OMP_NUM_THREADS=4 ./a.out
 fail 11 11 11 11 11 11 11 11 11 11 11 11 11 65536 -1476406295 65536 6 1
 -1468463655 65536 -1468511103 65536 0 1 -1468464959 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1
Fortran ERROR STOP: code 1

IEEE arithmetic exceptions signaled: INEXACT

Elements of array a from index 14 onwards show incorrect values.

Non-problematic Conditions

The issue does not occur under the following conditions:

  • Disabling OpenMP (e.g., -fno-openmp, or OMP_NUM_THREADS=1).
  • Enabling optimization (i.e., specifying -O1 or higher).
  • Changing the firstprivate variable to a shared variable.
  • Removing the addition in the construct:
      !$omp parallel workshare firstprivate(z)
    -   a=z+1
    +   a=z
      !$omp end parallel workshare
    

Other Compilers

  • GFortran

    $ gfortran --version
    GNU Fortran (GCC) 15.2.0
    Copyright (C) 2025 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ gfortran -fopenmp -O0 test.f90 && env OMP_NUM_THREADS=4 ./a.out
     pass
    
  • Intel Fortran

    $ ifx --version
    ifx (IFX) 2024.2.0 20240602
    Copyright (C) 1985-2024 Intel Corporation. All rights reserved.
    
    $ ifx -fopenmp -O0 test.f90 && env OMP_NUM_THREADS=4 ./a.out
     pass
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the test.f90 reproducer and compile it using flang -fopenmp -O0, then run it with OMP_NUM_THREADS=4 to reproduce the incorrect array values. The work is done when this firstprivate array addition produces the expected result and the program prints "pass" under the reported conditions.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.