[Flang][OpenMP] Execution error when array assignment statement for an array containing ":" is specified in forall construct within workshare construct
Version of flang : 21.0.0(b68565b8c70fc590f8e59799996f450b1072b532)/AArch64
In the attached program (snggw638_220.f90), when array assignment statement for an array containing ":" is specified in forall construct within workshare construct, the execution terminates abnormally.
Execution ends normally in the following cases:
- No compilation option (
-fopenmp) - Change the value specified for
OMP_NUM_THREADfrom2to1 - Change
":"to array subscripts (1and2) seesnggw638_221.f90below. - Change
":"to loop variable (n0) and add loop (forall(n0=1:2) / end forall) seesnggw638_222.f90below.
The combination of OpenMP workshare construct and array assignment statement for an array containing ":" in forall construct does not appear to work correctly.
The following are the test program, Flang, Gfortran and ifx compilation/execution result.
snggw638_220.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n1=1:1)
forall (n2=1:3)
a1(:,n1,n2) = a1(:,n1,n2)+1
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_220.f90; ./a.out
Segmentation fault (core dumped)
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp snggw638_220.f90; ./a.out
pass
$
$ export OMP_NUM_THREADS=2; ifx -qopenmp snggw638_220.f90; ./a.out
pass
$
snggw638_221.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n1=1:1)
forall (n2=1:3)
! a1(:,n1,n2) = a1(:,n1,n2)+1
a1(1,n1,n2) = a1(1,n1,n2)+1
a1(2,n1,n2) = a1(2,n1,n2)+1
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_221.f90; ./a.out
pass
$
snggw638_222.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n0=1:2)
forall (n1=1:1)
forall (n2=1:3)
! a1(:,n1,n2) = a1(:,n1,n2)+1
a1(n0,n1,n2) = a1(n0,n1,n2)+1
end forall
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_222.f90; ./a.out
pass
$
Seems to be fixed? My version is flang version 21.0.0 git 0c495ce9c4237f0f090b672efd94839e52cb5f18
Thank you for your comment. I will check again with the new version of flang.
I checked with flang built on June 16, and the execution result is the same (Segmentation fault).
$ git branch -r --contains 0c495ce9c4237f0f090b672efd94839e52cb5f18
origin/HEAD -> origin/main
origin/main
I'm in the main branch of LLVM.
$ git show --format=fuller -s 0c495ce9c4237f0f090b672efd94839e52cb5f18
commit 0c495ce9c4237f0f090b672efd94839e52cb5f18
Author: Owen Pan <[email protected]>
AuthorDate: 2025-06-07 12:43:35 +0900
Commit: GitHub <[email protected]>
CommitDate: 2025-06-07 12:43:35 +0900
[clang-format] Handle function decls with MS calling conventions (#143083)
$ flang --version
flang version 21.0.0git (https://github.com/llvm/llvm-project.git 22ad0359f9006f47a1707170896f359abbd6e10d)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20250616-22ad0359f900/bin
Build config: +assertions
$
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_220.f90; ./a.out
flang-21: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
Segmentation fault (core dumped)
$
I think the source of this confusion is that this issue is intermittent:
$ for i in {1..20}; do echo "=== Attempt $i ==="; ./build/bin/flang -L./build/runtimes/runtimes-bins/openmp/runtime/src/ -fopenmp sandbox/test.f90 -o sandbox/test 2>&1 && LD_LIBRARY_PATH=./build/runtimes/runtimes-bins/openmp/runtime/src ./sandbox/test 2>&1; done
=== Attempt 1 ===
pass
=== Attempt 2 ===
pass
=== Attempt 3 ===
pass
=== Attempt 4 ===
pass
=== Attempt 5 ===
pass
=== Attempt 6 ===
pass
=== Attempt 7 ===
pass
=== Attempt 8 ===
pass
=== Attempt 9 ===
pass
=== Attempt 10 ===
Segmentation fault (core dumped)
=== Attempt 11 ===
Segmentation fault (core dumped)
=== Attempt 12 ===
pass
=== Attempt 13 ===
pass
=== Attempt 14 ===
Segmentation fault (core dumped)
=== Attempt 15 ===
pass
=== Attempt 16 ===
pass
=== Attempt 17 ===
pass
=== Attempt 18 ===
pass
=== Attempt 19 ===
pass
=== Attempt 20 ===
pass
I'm investigating it.
I may need some insight about this. Let me share the debugging first.
Inspecting the core file in gdb:
Core was generated by `./test'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000ac2f60db304c in _QQmain..omp_par () at test.f90:8
8 a1(:,n1,n2) = a1(:,n1,n2)+1
Disassembly:
(gdb) x/10i $pc-20
0xac2f60db3038 <_QQmain..omp_par+2440>: ldur x8, [x8, #-256]
0xac2f60db303c <_QQmain..omp_par+2444>: subs x11, x10, #0x1
0xac2f60db3040 <_QQmain..omp_par+2448>: lsr x11, x11, #0
0xac2f60db3044 <_QQmain..omp_par+2452>: lsr x11, x11, #0
0xac2f60db3048 <_QQmain..omp_par+2456>: add x11, x11, #0x0
=> 0xac2f60db304c <_QQmain..omp_par+2460>: ldr w8, [x8, x11, lsl #2]
0xac2f60db3050 <_QQmain..omp_par+2464>: add w8, w8, #0x1
0xac2f60db3054 <_QQmain..omp_par+2468>: subs x10, x10, #0x1
0xac2f60db3058 <_QQmain..omp_par+2472>: lsr x10, x10, #0
0xac2f60db305c <_QQmain..omp_par+2476>: lsr x10, x10, #0
(gdb) x $x8
0xfffc5395da7c: Cannot access memory at address 0xfffc5395da7c
So, the base address in x8 is invalid.
Looking at the LLVM IR from that test:
%51 = insertvalue { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } { ptr undef, i64 4, i32 20240719, i8 1, i8 9, i8 0, i8 0, [1 x [3 x i64]] [[3 x i64] [i64 1, i64 2, i64 4]] }, ptr %50, 0
store { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] } %51, ptr %2, align 8
call void @llvm.memcpy.p0.p0.i32(ptr %15, ptr %2, i32 48, i1 false)
ptr undef is uninitialized. This descriptor is being created with ptr undef, then the pointer is inserted, stored to memory and memcpy'd to %15. However, if we look at %2:
omp.par.region1: ; preds = %omp.par.region
%2 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, align 8
It is an alloca, and we're storing the struct with one field properly set but reading it from memory can give us uninitialized values due to alignment. This is why it's intermittent .
I think the problem lies in flang/lib/Optimizer/CodeGen/CodeGen.cpp. More specifically in
template <typename OP>
struct EmboxCommonConversion : public fir::FIROpConversion<OP>
My initial guess was that this descriptor should be zero-initialized:
mlir::Value descriptor =
mlir::LLVM::UndefOp::create(rewriter, loc, llvmBoxTy);
However, this breaks a bunch of tests that expect ptr Undef.
Any suggestions on how this should be dealt with?
btw, the other two tests work because they don't have array slice operations inside OpenMP workshare constructs.
Hm, I think there is a more fundamental problem before the above. There is a racing condition in the shared stack. I'll submit a PR later today.
@llvm/issue-subscribers-flang-ir
Author: ohno-fj (ohno-fj)
In the attached program (snggw638_220.f90), when array assignment statement for an array containing ":" is specified in forall construct within workshare construct, the execution terminates abnormally.
Execution ends normally in the following cases:
- No compilation option (
-fopenmp) - Change the value specified for
OMP_NUM_THREADfrom2to1 - Change
":"to array subscripts (1and2) seesnggw638_221.f90below. - Change
":"to loop variable (n0) and add loop (forall(n0=1:2) / end forall) seesnggw638_222.f90below.
The combination of OpenMP workshare construct and array assignment statement for an array containing ":" in forall construct does not appear to work correctly.
The following are the test program, Flang, Gfortran and ifx compilation/execution result.
snggw638_220.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n1=1:1)
forall (n2=1:3)
a1(:,n1,n2) = a1(:,n1,n2)+1
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_220.f90; ./a.out
Segmentation fault (core dumped)
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp snggw638_220.f90; ./a.out
pass
$
$ export OMP_NUM_THREADS=2; ifx -qopenmp snggw638_220.f90; ./a.out
pass
$
snggw638_221.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n1=1:1)
forall (n2=1:3)
! a1(:,n1,n2) = a1(:,n1,n2)+1
a1(1,n1,n2) = a1(1,n1,n2)+1
a1(2,n1,n2) = a1(2,n1,n2)+1
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_221.f90; ./a.out
pass
$
snggw638_222.f90:
program main
integer a1(2,1,3)
a1 = 0
!$omp parallel
!$omp workshare
forall (n0=1:2)
forall (n1=1:1)
forall (n2=1:3)
! a1(:,n1,n2) = a1(:,n1,n2)+1
a1(n0,n1,n2) = a1(n0,n1,n2)+1
end forall
end forall
end forall
!$omp end workshare
!$omp end parallel
print *,'pass'
end program main
$ export OMP_NUM_THREADS=2; flang -fopenmp snggw638_222.f90; ./a.out
pass
$