OffTarget

Observed issues › catalog/c-allocate-const-mem-firstprivate-wrong

Appears to behave incorrectlyWrong answer · C · OpenMP · Open

A firstprivate scalar allocated with omp_const_mem_alloc has the wrong value on the device

Legality: independently confirmed. 2 independent readings find the program legal and the result required. How this is checked

Affected variants

CompilerMI300A gfx942
unified memory (XNACK 1)
MI300A gfx942
copy (XNACK 0)
MI250X gfx90a
copy (XNACK 0)
MI300X gfx942
copy (XNACK 0)
MI350X gfx950
copy (XNACK 0)
A100 sm_80
separate memory
A100 sm_80
managed memory
AFAR 24.3.0behaves incorrectlybehaves incorrectlybehaves incorrectlybehaves incorrectlybehaves incorrectlyn/an/a
CCE 19.0.0behaves incorrectlybehaves incorrectlybehaves incorrectlyunknown
not run
unknown
not run
n/an/a
CCE 20.0.2behaves incorrectlybehaves incorrectlybehaves incorrectlyunknown
not run
unknown
not run
n/an/a
CCE 21.0.3behaves incorrectlybehaves incorrectlybehaves incorrectlyunknown
not run
unknown
not run
n/an/a
CCE 21.0.4behaves incorrectlybehaves incorrectlybehaves incorrectlyunknown
not run
unknown
not run
n/an/a
NVHPC 25.11n/an/an/an/an/afails another wayfails another way
NVHPC 26.9n/an/an/an/an/afails another wayfails another way
GCC 16.2.0correctcorrectcorrectcorrectcorrectcorrectn/a

"behaves incorrectly" / "not supported": this issue, confirmed in up to four environments (another build directory, another node, a repeat). "fails another way": a different failure, not counted as this issue. "unknown": not run there, or the runs disagree.

Messages

ResultCompiler and GPUMessage
behaves incorrectlyAFAR 24.3.0, MI300A unified
AFAR 24.3.0, MI300A copy
AFAR 24.3.0, MI250X
AFAR 24.3.0, MI300X
AFAR 24.3.0, MI350X
CCE 19.0.0, MI300A unified
CCE 19.0.0, MI300A copy
CCE 19.0.0, MI250X
CCE 20.0.2, MI300A unified
CCE 20.0.2, MI300A copy
CCE 20.0.2, MI250X
CCE 21.0.3, MI300A unified
CCE 21.0.3, MI300A copy
CCE 21.0.3, MI250X
CCE 21.0.4, MI300A unified
CCE 21.0.4, MI300A copy
CCE 21.0.4, MI250X
show
RESULT ran_on_host=0 check=fail
fails another wayNVHPC 25.11, A100 separate
NVHPC 25.11, A100 managed
NVHPC 26.9, A100 separate
NVHPC 26.9, A100 managed
show
"c-allocate-const-mem-firstprivate-wrong.c", line 17: error: invalid text in pragma
        host_flag) firstprivate(x) allocate(omp_const_mem_alloc: x) \
                                   ^
"c-allocate-const-mem-firstprivate-wrong.c", line 17: error: extra text after expected end of preprocessing directive
        host_flag) firstprivate(x) allocate(omp_const_mem_alloc: x) \
                                           ^
2 errors detected in the compilation of "c-allocate-const-mem-firstprivate-wrong.c".

Reproduce

AFAR 24.3.0, MI300A unified

$AFAR/bin/amdclang -fopenmp --offload-arch=gfx942 -fopenmp-version=52 -fopenmp-version=52 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro

CCE 19.0.0, MI300A unified

module load cce/19.0.0 craype-accel-amd-gfx942 rocm/6.4.3
module unload cray-mpich
craycc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx942 -fopenmp-version=52 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro

CCE 20.0.2, MI300A unified

module load cce/20.0.2 craype-accel-amd-gfx942 rocm/6.4.3
module unload cray-mpich
craycc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx942 -fopenmp-version=52 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro

CCE 21.0.3, MI300A unified

module load cce/21.0.3 craype-accel-amd-gfx942 rocm/7.2.0
module unload cray-mpich
craycc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx942 -fopenmp-version=52 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro

CCE 21.0.4, MI300A unified

module load cce/21.0.4 craype-accel-amd-gfx942 rocm/7.2.0
module unload cray-mpich
craycc -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -Xopenmp-target=amdgcn-amd-amdhsa -march=gfx942 -fopenmp-version=52 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro

GCC 16.2.0, MI300A unified (runs correctly)

export LD_LIBRARY_PATH=$GCC_16_2/lib64:/opt/rocm-7.2.1/lib
$GCC_16_2/bin/gcc -fopenmp -foffload=amdgcn-amdhsa -foffload-options=amdgcn-amdhsa=-march=gfx942 -O2 repro.c -o repro
HSA_XNACK=1 OMP_TARGET_OFFLOAD=mandatory ./repro
Program (72 lines)
// # Draft finding: omp52-c-w-allocate-const-firstprivate on afar
//
// - compiler: afar
// - toolchain: afar; arch: gfx942; machine: tuolumne; memory mode: unified; opt: -O2
// - program: catalog omp52-c-w-allocate-const-firstprivate (c, version 52, sha256 2f1a38cbd60a463c)
// - expected: check=pass (trusted; elision + model)
// - actual: wrong_answer check=fail (check_fail: ran_on_host=0 check=fail)
// - oracle audit: run on AFAR, CCE 19, 20, 21 (copy mode) and NVHPC: no toolchain passes the original (NVHPC rejects the clauses), so the mutant cannot be credited (unaudited, not weak); in sequential elision the original prints check=pass and the mutant (x's contribution removed) check=fail, so the check depends on the firstprivate value
// - verdict: compiler_bug by rule R20-runtime-bug: a legal program fails at run time, deterministically, against an expected value from two or more independent sources with an executed witness, and another toolchain, mode, arch or the elision gives the expected value
// - reason: expected trusted check=pass (elision + model): stored main verdict: elision + model agree; witnesses: elision
// - citations: RUL-DERIVED-NEEDS-EXECUTION (rulings.md); RUL-WITNESS-PER-MODE (rulings.md); RUL-SAME-COMPILER-RUNS (rulings.md); RUL-PRESENCE-WITNESS (rulings.md); RUL-METAMORPHIC-OTHER-FAMILY (rulings.md)
// - legality: legal: offload.engine.prerun.check accepts it (rerun: offload.engine.rules with the
//   race rule and the restriction checks, and the AFAR clang front end, not under test here); no
//   compiler under test, gfortran or gcc is a legality witness
// - witnesses: elision
// - withdrawn: none
//
// Generated by compliance.widen (OpenMP 5.2, C11; catalog widening). Do not edit.
// id: omp52-c-w-allocate-const-firstprivate
// primary atom: pair.omp.clause.allocate+omp.clause.firstprivate
// atoms: pair.omp.clause.allocate+omp.clause.firstprivate, omp.clause.allocate, omp.clause.firstprivate
// legal: yes
// observable: yes
// ran_on_host: inside the target region of interest
// evidence: construct
// needs: none
// std: c11
// feature: a firstprivate scalar allocated with omp_const_mem_alloc (uses_allocators) on a target loop
// spec: the parts' own programs
// pair of: omp.clause.allocate + omp.clause.firstprivate (each has a program of its own)
#include <stdio.h>
#include <stdbool.h>
#include <omp.h>

enum { n = 64 };

int main(void) {
  int a[n], b[n], ref[n];
  int host_flag = 1;
  int x = 5;
  for (int i = 0; i < n; i++) {
    a[i] = i + 1;
    b[i] = 0;
    ref[i] = i + 1 + 5;
  }
  #pragma omp target teams distribute parallel for map(to: a) map(from: b) map(tofrom: \
      host_flag) firstprivate(x) allocate(omp_const_mem_alloc: x) \
      uses_allocators(omp_const_mem_alloc)
  for (int i = 0; i < n; i++) {
    b[i] = a[i] + x;
    if (i == 0) host_flag = omp_is_initial_device();
  }
  bool ok = true;
  for (int i = 0; i < n; i++)
    ok = ok && (b[i] == ref[i]);
  printf("RESULT ran_on_host=%d check=%s\n", host_flag, ok ? "pass" : "fail");
  return 0;
}
// spec (requirement): Variables in the constant memory space may be initialized through the firstprivate clause.
// "Variables allocated in the omp_const_mem_space memory space may be initialized through the firstprivate clause"
//   (5.2 §6.1 p.171 l.9-10)
// spec (requirement): A firstprivate list item that also appears in allocate gets its storage from that allocator and
//   is initialized from the original list item.
// "If a list item in the clause also appears in a data-sharing attribute clause on the same directive that privatizes
//   the list item, allocations that arise from that list item in the clause will be provided by the memory
//   allocator." (5.2 §6.6 p.178 l.12-14)
// spec (requirement): The new list item of a firstprivate clause is initialized from the original list item.
// "In addition, the new list item is initialized from the original list item that exists before the construct." (5.2
//   §5.4.4 p.112 l.11-12)
// spec (this program): firstprivate(x) allocate(omp_const_mem_alloc: x) with uses_allocators(omp_const_mem_alloc) is
//   the form 5.2 names for constant memory, so every device copy of x must hold 5; craycc yields the wrong value on
//   the device.

Source in the repository

Something to add? Dispute its legality · Report it fixed in a newer release · Report a mistake on this page

Expected and observed

Expected

check=pass

Observed where it behaves incorrectly

RESULT ran_on_host=0 check=fail

What the specification requires

The specification text this program relies on and the compiler is expected to support.

OpenMP 5.2 §6.1 p.171 l.9-10

Variables allocated in the omp_const_mem_space memory space may be initialized through the firstprivate clause

OpenMP 5.2 §6.6 p.178 l.12-14

If a list item in the clause also appears in a data-sharing attribute clause on the same directive that privatizes the list item, allocations that arise from that list item in the clause will be provided by the memory allocator.

OpenMP 5.2 §5.4.4 p.112 l.11-12

In addition, the new list item is initialized from the original list item that exists before the construct.

Evidence

Same compiler, one change

  • the allocate and uses_allocators clauses removed (x firstprivate only): AFAR 24.3.0: correct; CCE 19.0.0: correct; CCE 20.0.2: correct; CCE 21.0.3: correct; CCE 21.0.4: correct
    Control program
    #include <stdio.h>
    #include <stdbool.h>
    #include <omp.h>
    
    enum { n = 64 };
    
    int main(void) {
      int a[n], b[n], ref[n];
      int host_flag = 1;
      int x = 5;
      for (int i = 0; i < n; i++) {
        a[i] = i + 1;
        b[i] = 0;
        ref[i] = i + 1 + 5;
      }
      #pragma omp target teams distribute parallel for map(to: a) map(from: b) map(tofrom: \
          host_flag) firstprivate(x)
      for (int i = 0; i < n; i++) {
        b[i] = a[i] + x;
        if (i == 0) host_flag = omp_is_initial_device();
      }
      bool ok = true;
      for (int i = 0; i < n; i++)
        ok = ok && (b[i] == ref[i]);
      printf("RESULT ran_on_host=%d check=%s\n", host_flag, ok ? "pass" : "fail");
      return 0;
    }
    

Other compilers run it correctly

GCC 16.2.0