Skip to content

Commit b0b04fd

Browse files
Krish GuptaKrish Gupta
authored andcommitted
[OpenMP][Flang] Fix atomic size for complex types
Fixes #165184 Use element type instead of pointer type when computing size for atomic operations on struct types.
1 parent 5cef6f3 commit b0b04fd

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
! Test lowering of atomic read to LLVM IR for complex types.
2+
! This is a regression test for issue #165184.
3+
4+
! REQUIRES: x86-registered-target || aarch64-registered-target
5+
6+
! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fopenmp %s -o - | FileCheck %s
7+
! RUN: %flang_fc1 -triple aarch64-unknown-linux-gnu -emit-llvm -fopenmp %s -o - | FileCheck %s
8+
9+
! Test that atomic read operations with complex types emit the correct
10+
! size parameter to __atomic_load:
11+
! - complex(4) (8 bytes total): should call __atomic_load(i64 8, ...)
12+
! - complex(8) (16 bytes total): should call __atomic_load(i64 16, ...)
13+
14+
program atomic_read_complex
15+
implicit none
16+
17+
! Test complex(4) - single precision (8 bytes)
18+
complex(4) :: c41, c42
19+
! Test complex(8) - double precision (16 bytes)
20+
complex(8) :: c81, c82
21+
22+
c42 = (1.0_4, 1.0_4)
23+
c82 = (1.0_8, 1.0_8)
24+
25+
! CHECK-LABEL: define {{.*}} @_QQmain
26+
27+
! Single precision complex: 8 bytes
28+
! CHECK: call void @__atomic_load(i64 8, ptr {{.*}}, ptr {{.*}}, i32 {{.*}})
29+
!$omp atomic read
30+
c41 = c42
31+
32+
! Double precision complex: 16 bytes (this was broken before the fix)
33+
! CHECK: call void @__atomic_load(i64 16, ptr {{.*}}, ptr {{.*}}, i32 {{.*}})
34+
!$omp atomic read
35+
c81 = c82
36+
37+
end program atomic_read_complex
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
! Test lowering of atomic write to LLVM IR for complex types.
2+
! This is a regression test for issue #165184.
3+
4+
! REQUIRES: x86-registered-target || aarch64-registered-target
5+
6+
! RUN: %flang_fc1 -triple x86_64-unknown-linux-gnu -emit-llvm -fopenmp %s -o - | FileCheck %s
7+
! RUN: %flang_fc1 -triple aarch64-unknown-linux-gnu -emit-llvm -fopenmp %s -o - | FileCheck %s
8+
9+
! Test that atomic write operations with complex types emit the correct
10+
! size parameter to __atomic_store:
11+
! - complex(4) (8 bytes total): should call __atomic_store(i64 8, ...)
12+
! - complex(8) (16 bytes total): should call __atomic_store(i64 16, ...)
13+
14+
program atomic_write_complex
15+
implicit none
16+
17+
! Test complex(4) - single precision (8 bytes)
18+
complex(4) :: c41, c42
19+
! Test complex(8) - double precision (16 bytes)
20+
complex(8) :: c81, c82
21+
22+
c42 = (1.0_4, 1.0_4)
23+
c82 = (1.0_8, 1.0_8)
24+
25+
! CHECK-LABEL: define {{.*}} @_QQmain
26+
27+
! Single precision complex: 8 bytes
28+
! CHECK: call void @__atomic_store(i64 8, ptr {{.*}}, ptr {{.*}}, i32 {{.*}})
29+
!$omp atomic write
30+
c41 = c42
31+
32+
! Double precision complex: 16 bytes (this was broken before the fix)
33+
! CHECK: call void @__atomic_store(i64 16, ptr {{.*}}, ptr {{.*}}, i32 {{.*}})
34+
!$omp atomic write
35+
c81 = c82
36+
37+
end program atomic_write_complex

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9338,9 +9338,8 @@ OpenMPIRBuilder::createAtomicRead(const LocationDescription &Loc,
93389338
// target does not support `atomicrmw` of the size of the struct
93399339
LoadInst *OldVal = Builder.CreateLoad(XElemTy, X.Var, "omp.atomic.read");
93409340
OldVal->setAtomic(AO);
9341-
const DataLayout &LoadDL = OldVal->getModule()->getDataLayout();
9342-
unsigned LoadSize =
9343-
LoadDL.getTypeStoreSize(OldVal->getPointerOperand()->getType());
9341+
const DataLayout &DL = OldVal->getModule()->getDataLayout();
9342+
unsigned LoadSize = DL.getTypeStoreSize(XElemTy);
93449343
OpenMPIRBuilder::AtomicInfo atomicInfo(
93459344
&Builder, XElemTy, LoadSize * 8, LoadSize * 8, OldVal->getAlign(),
93469345
OldVal->getAlign(), true /* UseLibcall */, AllocaIP, X.Var);
@@ -9384,9 +9383,8 @@ OpenMPIRBuilder::createAtomicWrite(const LocationDescription &Loc,
93849383
XSt->setAtomic(AO);
93859384
} else if (XElemTy->isStructTy()) {
93869385
LoadInst *OldVal = Builder.CreateLoad(XElemTy, X.Var, "omp.atomic.read");
9387-
const DataLayout &LoadDL = OldVal->getModule()->getDataLayout();
9388-
unsigned LoadSize =
9389-
LoadDL.getTypeStoreSize(OldVal->getPointerOperand()->getType());
9386+
const DataLayout &DL = OldVal->getModule()->getDataLayout();
9387+
unsigned LoadSize = DL.getTypeStoreSize(XElemTy);
93909388
OpenMPIRBuilder::AtomicInfo atomicInfo(
93919389
&Builder, XElemTy, LoadSize * 8, LoadSize * 8, OldVal->getAlign(),
93929390
OldVal->getAlign(), true /* UseLibcall */, AllocaIP, X.Var);

0 commit comments

Comments
 (0)