[clang] [clang][CUDA][HIP] Use host alignment for device definitions (PR #246922)
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 29 08:33:05 PST 2026
https://github.com/yxsamliu updated https://github.com/llvm/llvm-project/pull/246922
>From 8abf8a7507f47ef002ff944b70e7c0449ceab899 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Sun, 29 Nov 2026 13:46:35 +0000
Subject: [PATCH] [clang][CUDA][HIP] Use host alignment for device definitions
After #196505, x86_64-windows-msvc aligns globals by size and no longer
uses the large-array rule. Device code copied the old rule but does not
see the new one. In a gfx1100 WMMA kernel, shared array alignment dropped
from 16 to 2 bytes. The compiler went from four ds_load_2addr_b32 loads
to two ds_load_b128 loads.
Use the host's minimum alignment for variables defined in device code.
Declarations without a definition and weak definitions are unchanged,
because another object may provide their storage. Only object alignment
changes. Struct layout and array stride stay the same.
https://github.com/ROCm/TheRock/issues/10272
AI assisted.
---
clang/lib/AST/ASTContext.cpp | 8 +++-
.../amdgpu-shared-array-alignment.hip | 39 +++++++++++++++++++
clang/test/SemaCUDA/global-alignment.cu | 14 +++++++
3 files changed, 60 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
create mode 100644 clang/test/SemaCUDA/global-alignment.cu
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index e30510684a6a2a..76e5070a14afb5 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2817,7 +2817,13 @@ unsigned ASTContext::getMinGlobalAlignOfVar(uint64_t Size,
// Make the default handling as that of a non-weak definition in the
// current translation unit.
bool HasNonWeakDef = !VD || (VD->hasDefinition() && !VD->isWeak());
- return getTargetInfo().getMinGlobalAlign(Size, HasNonWeakDef);
+ unsigned Align = getTargetInfo().getMinGlobalAlign(Size, HasNonWeakDef);
+ // In CUDA/HIP device compilation, also apply the host's minimum to a
+ // variable defined here. The storage of other declarations may come from an
+ // object built without the host's rule.
+ if (LangOpts.CUDAIsDevice && AuxTarget && VD && HasNonWeakDef)
+ Align = std::max(Align, AuxTarget->getMinGlobalAlign(Size, HasNonWeakDef));
+ return Align;
}
CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const {
diff --git a/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
new file mode 100644
index 00000000000000..4c267bb7b9f2d7
--- /dev/null
+++ b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
@@ -0,0 +1,39 @@
+// NOTE: Do not autogenerate. Check array alignment and the address query.
+// REQUIRES: amdgpu-registered-target
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \
+// RUN: -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,MSVC
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-unknown-linux-gnu \
+// RUN: -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,LINUX
+
+// MSVC-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 8{{$}}
+// LINUX-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 2{{$}}
+// MSVC-DAG: @{{.*}}medium = internal addrspace(3) global [8 x half] {{.*}}, align 8{{$}}
+// LINUX-DAG: @{{.*}}medium = internal addrspace(3) global [8 x half] {{.*}}, align 16{{$}}
+// CHECK-DAG: @{{.*}}large = internal addrspace(3) global [32 x half] {{.*}}, align 16{{$}}
+// CHECK-DAG: @{{.*}}explicit_align = internal addrspace(3) global [8 x half] {{.*}}, align 4{{$}}
+// MSVC-DAG: @external = external {{.*}}addrspace(1) global [16 x i8], align 1{{$}}
+
+__attribute__((device)) void use(_Float16 *);
+
+extern "C" {
+extern __attribute__((device)) char external[16];
+
+__attribute__((device)) bool is_external_aligned() {
+ // MSVC-LABEL: define {{.*}} @is_external_aligned(
+ // MSVC: %[[BITS:.*]] = and i64 {{.*}}@external{{.*}}, 7
+ // MSVC: %[[ALIGNED:.*]] = icmp eq i64 %[[BITS]], 0
+ // MSVC: ret i1 %[[ALIGNED]]
+ return __builtin_is_aligned(external, 8);
+}
+}
+
+__attribute__((global)) void kernel() {
+ __attribute__((shared)) _Float16 small[7];
+ __attribute__((shared)) _Float16 medium[8];
+ __attribute__((shared)) _Float16 large[32];
+ __attribute__((shared, aligned(4))) _Float16 explicit_align[8];
+ use(small);
+ use(medium);
+ use(large);
+ use(explicit_align);
+}
diff --git a/clang/test/SemaCUDA/global-alignment.cu b/clang/test/SemaCUDA/global-alignment.cu
new file mode 100644
index 00000000000000..49506a79774599
--- /dev/null
+++ b/clang/test/SemaCUDA/global-alignment.cu
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-pc-windows-msvc -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fcuda-is-device -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+extern __attribute__((device)) char array[16];
+__attribute__((device)) char array[16];
+static_assert(alignof(decltype(array)) == 1);
+static_assert(__builtin_is_aligned(array, 8));
+
+__attribute__((device)) short scalar;
+static_assert(alignof(decltype(scalar)) == 2);
+static_assert(__builtin_is_aligned(&scalar, 4));
More information about the cfe-commits
mailing list