Skip to content

clang/OpenCL: Add baseline test showing broken codegen #138862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2025

Conversation

arsenm
Copy link
Contributor

@arsenm arsenm commented May 7, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-backend-amdgpu

Author: Matt Arsenault (arsenm)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/138862.diff

2 Files Affected:

  • (modified) clang/test/CodeGenCXX/amdgcn-automatic-variable.cpp (+24)
  • (modified) clang/test/CodeGenOpenCL/amdgcn-automatic-variable.cl (+45)
diff --git a/clang/test/CodeGenCXX/amdgcn-automatic-variable.cpp b/clang/test/CodeGenCXX/amdgcn-automatic-variable.cpp
index c1f9310141000..0f425d78c3332 100644
--- a/clang/test/CodeGenCXX/amdgcn-automatic-variable.cpp
+++ b/clang/test/CodeGenCXX/amdgcn-automatic-variable.cpp
@@ -133,3 +133,27 @@ void func7() {
   use(&x);
 }
 
+#define __private __attribute__((opencl_private))
+
+// CHECK-LABEL: @_Z34explicit_private_address_space_ptrv(
+// CHECK-NEXT:  entry:
+// CHECK-NEXT:    [[PLONG:%.*]] = alloca i64, align 8, addrspace(5)
+// CHECK-NEXT:    [[PLONGP:%.*]] = alloca ptr, align 8, addrspace(5)
+// CHECK-NEXT:    [[PLONGP_B:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// CHECK-NEXT:    [[PLONG_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[PLONG]] to ptr
+// CHECK-NEXT:    [[PLONGP_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[PLONGP]] to ptr
+// CHECK-NEXT:    [[PLONGP_B_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[PLONGP_B]] to ptr
+// CHECK-NEXT:    store ptr [[PLONG_ASCAST]], ptr [[PLONGP_ASCAST]], align 8
+// CHECK-NEXT:    [[PLONG_ASCAST_ASCAST:%.*]] = addrspacecast ptr [[PLONG_ASCAST]] to ptr addrspace(5)
+// CHECK-NEXT:    store ptr addrspace(5) [[PLONG_ASCAST_ASCAST]], ptr [[PLONGP_B_ASCAST]], align 4
+// CHECK-NEXT:    [[TMP0:%.*]] = load ptr, ptr [[PLONGP_ASCAST]], align 8
+// CHECK-NEXT:    store i64 8, ptr [[TMP0]], align 8
+// CHECK-NEXT:    ret void
+//
+void explicit_private_address_space_ptr() {
+  long plong;
+  long *plongp = &plong;
+
+  __private long *plongp_b = (__private long *)&plong;
+  *plongp = 8;
+}
diff --git a/clang/test/CodeGenOpenCL/amdgcn-automatic-variable.cl b/clang/test/CodeGenOpenCL/amdgcn-automatic-variable.cl
index dba6519966eb5..b252f1041d68c 100644
--- a/clang/test/CodeGenOpenCL/amdgcn-automatic-variable.cl
+++ b/clang/test/CodeGenOpenCL/amdgcn-automatic-variable.cl
@@ -109,3 +109,48 @@ void func2(void) {
 void func3(void) {
   float a[16][1] = {{0.}};
 }
+
+// CL12-LABEL: define dso_local void @wrong_store_type_private_pointer_alloca(
+// CL12-SAME: ) #[[ATTR0]] {
+// CL12-NEXT:  [[ENTRY:.*:]]
+// CL12-NEXT:    [[PLONG:%.*]] = alloca i64, align 8, addrspace(5)
+// CL12-NEXT:    [[PLONGP:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// CL12-NEXT:    [[GLONGP:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// CL12-NEXT:    store i64 5, ptr addrspace(5) [[PLONG]], align 8
+// CL12-NEXT:    store ptr addrspace(5) [[PLONG]], ptr addrspace(5) [[PLONGP]], align 4
+// CL12-NEXT:    [[TMP0:%.*]] = load ptr addrspace(5), ptr addrspace(5) [[PLONGP]], align 4
+// CL12-NEXT:    store i64 8, ptr addrspace(5) [[TMP0]], align 8
+// CL12-NEXT:    store ptr addrspace(5) [[PLONG]], ptr addrspace(5) [[GLONGP]], align 4
+// CL12-NEXT:    [[TMP1:%.*]] = load ptr addrspace(5), ptr addrspace(5) [[GLONGP]], align 4
+// CL12-NEXT:    store i64 9, ptr addrspace(5) [[TMP1]], align 8
+// CL12-NEXT:    ret void
+//
+// CL20-LABEL: define dso_local void @wrong_store_type_private_pointer_alloca(
+// CL20-SAME: ) #[[ATTR0]] {
+// CL20-NEXT:  [[ENTRY:.*:]]
+// CL20-NEXT:    [[PLONG:%.*]] = alloca i64, align 8, addrspace(5)
+// CL20-NEXT:    [[PLONGP:%.*]] = alloca ptr addrspace(5), align 4, addrspace(5)
+// CL20-NEXT:    [[GLONGP:%.*]] = alloca ptr, align 8, addrspace(5)
+// CL20-NEXT:    [[PLONG_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[PLONG]] to ptr
+// CL20-NEXT:    [[PLONGP_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[PLONGP]] to ptr
+// CL20-NEXT:    [[GLONGP_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[GLONGP]] to ptr
+// CL20-NEXT:    store i64 5, ptr [[PLONG_ASCAST]], align 8
+// CL20-NEXT:    store ptr [[PLONG_ASCAST]], ptr [[PLONGP_ASCAST]], align 4
+// CL20-NEXT:    [[TMP0:%.*]] = load ptr addrspace(5), ptr [[PLONGP_ASCAST]], align 4
+// CL20-NEXT:    store i64 8, ptr addrspace(5) [[TMP0]], align 8
+// CL20-NEXT:    store ptr [[PLONG_ASCAST]], ptr [[GLONGP_ASCAST]], align 8
+// CL20-NEXT:    [[TMP1:%.*]] = load ptr, ptr [[GLONGP_ASCAST]], align 8
+// CL20-NEXT:    store i64 9, ptr [[TMP1]], align 8
+// CL20-NEXT:    ret void
+//
+void wrong_store_type_private_pointer_alloca() {
+  long plong = 5;
+
+  // This needs to write an addrspace(5) pointer to the temporary alloca
+  __private long *plongp = &plong;
+  *plongp = 8;
+
+  // This needs to write an addrspace(0) pointer to the temporary alloca in CL2.0
+  long *glongp = &plong;
+  *glongp = 9;
+}

@arsenm arsenm marked this pull request as ready for review May 7, 2025 13:14
@llvmbot llvmbot added the clang Clang issues not falling into any other category label May 7, 2025
Comment on lines 149 to 153
// This needs to write an addrspace(5) pointer to the temporary alloca
__private long *plongp = &plong;
*plongp = 8;

// This needs to write an addrspace(0) pointer to the temporary alloca in CL2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to mention what we're currently doing wrong, as a FIXME/TODO.
One could dig through the IR checks, but it's much easier when one knows what to look for, specifically.

Comment on lines 154 to 155
long *glongp = &plong;
*glongp = 9;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd split it into two separate tests -- one for generic pointer, and one for private. That is, unless the issue is when both pointers are needed to reproduce the issue. Right now the checks have way too many [PG]LONGP? that require an effort to keep track of.

It may be the case where semantically sensible naming in the source code that uses prefix/suffix makes things harder to read when it comes to the test checks. It's a nit. If the test in the current shape works for you I'm OK with it. I just found it to be not quite "painfully obvious".

How about something like this:

  long data = 5;

  // This needs to write an addrspace(5) pointer to the temporary alloca
  __private long *pvtp = &data;
  *pvtp = 8;
  long *genp = &data;
  *genp = 9;

Maybe, also make genp and pvtp static, so their own allocas do not clutter the test checks. In the checks themselves, I'd also rename generic _ASCAST so it reflects the AS we cast to. E.g. _GEN or _PVT, so the reader does not have to back-track to where the value is captured.

Copy link
Contributor Author

arsenm commented May 8, 2025

Merge activity

  • May 8, 1:36 AM EDT: A user started a stack merge that includes this pull request via Graphite.
  • May 8, 1:38 AM EDT: Graphite rebased this pull request as part of a merge.
  • May 8, 1:41 AM EDT: Graphite rebased this pull request as part of a merge.
  • May 8, 1:43 AM EDT: Graphite rebased this pull request as part of a merge.
  • May 8, 1:46 AM EDT: @arsenm merged this pull request with Graphite.

@arsenm arsenm force-pushed the users/arsenm/clang/opencl-add-baseline-test-ub-store branch 2 times, most recently from e82e94d to a19c3d3 Compare May 8, 2025 05:40
@arsenm arsenm force-pushed the users/arsenm/clang/opencl-add-baseline-test-ub-store branch from a19c3d3 to 0f4d8cd Compare May 8, 2025 05:43
@arsenm arsenm merged commit 26572ba into main May 8, 2025
6 of 10 checks passed
@arsenm arsenm deleted the users/arsenm/clang/opencl-add-baseline-test-ub-store branch May 8, 2025 05:46
petrhosek pushed a commit to petrhosek/llvm-project that referenced this pull request May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AMDGPU clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category OpenCL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants