[clang] [clang][CUDA][HIP] Use host alignment for device definitions (PR #246922)
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 1 12:52:10 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 1/5] [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));
>From 3d87eb40773d1637004febba3530798710fe3d14 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Mon, 30 Nov 2026 12:50:23 +0000
Subject: [PATCH 2/5] [Clang] Forward device variable alignment queries in
TargetInfo
Have TargetInfo combine the device and auxiliary host minimums. Keep the declaration and compilation mode checks in ASTContext and preserve the existing per-target hook.
AI assisted.
---
clang/include/clang/Basic/TargetInfo.h | 5 +++++
clang/lib/AST/ASTContext.cpp | 11 ++++-------
clang/lib/Basic/TargetInfo.cpp | 9 +++++++++
3 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 7c5c35a5cfb7c5..5e498912c91bb0 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -756,6 +756,11 @@ class TargetInfo : public TransferrableTargetInfo,
return MinGlobalAlign;
}
+ /// Return the minimum alignment of a variable. In CUDA/HIP device code,
+ /// HostTarget may supply an additional minimum for a non-weak definition.
+ unsigned getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
+ const TargetInfo *HostTarget) const;
+
/// Return the largest alignment for which a suitably-sized allocation with
/// '::operator new(size_t)' is guaranteed to produce a correctly-aligned
/// pointer.
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 76e5070a14afb5..0d5fc94849965c 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2817,13 +2817,10 @@ 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());
- 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;
+ // Apply the host's minimum only when querying an actual variable in a
+ // device translation unit.
+ return getTargetInfo().getMinGlobalAlignForVar(
+ Size, HasNonWeakDef, LangOpts.CUDAIsDevice && VD ? AuxTarget : nullptr);
}
CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const {
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index 6e390226d2e4b8..c4896f73ae0f96 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -435,6 +435,15 @@ bool TargetInfo::isTypeSigned(IntType T) {
};
}
+unsigned
+TargetInfo::getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
+ const TargetInfo *HostTarget) const {
+ unsigned Align = getMinGlobalAlign(Size, HasNonWeakDef);
+ if (HostTarget && HasNonWeakDef)
+ return std::max(Align, HostTarget->getMinGlobalAlign(Size, HasNonWeakDef));
+ return Align;
+}
+
/// adjust - Set forced language options.
/// Apply changes to the target information with respect to certain
/// language options which change the target configuration and adjust
>From c8961366f18c937ecda32b5499a430d84095de4c Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Mon, 30 Nov 2026 21:06:46 +0000
Subject: [PATCH 3/5] [Clang] Scope offload alignment to device storage
definitions
Use the configured auxiliary host's size and large-array placement
rules for device definitions that own their storage. Preserve the
existing target layout copy so that external declarations do not
inherit a new alignment assumption.
Apply the host ABI compatibility options to the auxiliary target
before the frontend queries the definition's alignment. This keeps
managed allocation registration and device accesses in agreement.
AI assisted.
---
clang/include/clang/Basic/TargetInfo.h | 9 +++--
clang/lib/AST/ASTContext.cpp | 37 +++++++++++++------
clang/lib/Basic/TargetInfo.cpp | 19 ++++++++--
clang/lib/Basic/Targets/NVPTX.cpp | 4 +-
clang/lib/Frontend/CompilerInstance.cpp | 10 ++++-
.../amdgpu-shared-array-alignment.hip | 26 ++++++++-----
.../test/CodeGenHIP/managed-abi-alignment.hip | 27 ++++++++++++++
clang/test/SemaCUDA/global-alignment.cu | 22 +++++++++--
8 files changed, 120 insertions(+), 34 deletions(-)
create mode 100644 clang/test/CodeGenHIP/managed-abi-alignment.hip
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 5e498912c91bb0..95a55c0ae9e5a7 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -756,10 +756,13 @@ class TargetInfo : public TransferrableTargetInfo,
return MinGlobalAlign;
}
- /// Return the minimum alignment of a variable. In CUDA/HIP device code,
- /// HostTarget may supply an additional minimum for a non-weak definition.
+ /// Return the minimum alignment of a variable. For a CUDA/HIP device
+ /// definition, AuxTarget supplies the host's minimum.
unsigned getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
- const TargetInfo *HostTarget) const;
+ const TargetInfo *AuxTarget) const;
+
+ /// Return the minimum alignment for a constant-size array of Width bits.
+ unsigned getMinArrayAlign(uint64_t Width, const TargetInfo *AuxTarget) const;
/// Return the largest alignment for which a suitably-sized allocation with
/// '::operator new(size_t)' is guaranteed to produce a correctly-aligned
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 0d5fc94849965c..df9cb0736fe26b 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1845,6 +1845,22 @@ const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
}
}
+static const TargetInfo *
+getHostTargetForDeviceAlignment(const ASTContext &Context, const VarDecl *VD) {
+ if (!Context.getLangOpts().CUDAIsDevice || !VD || !VD->hasGlobalStorage())
+ return nullptr;
+ const TargetInfo *HostTarget = Context.getAuxTargetInfo();
+ if (!HostTarget)
+ return nullptr;
+
+ // Storage supplied by another object can have a different alignment.
+ const VarDecl *Def = VD->getDefinition();
+ if (!Def || Def->isWeak() || Def->hasAttr<AliasAttr>() ||
+ Def->getMaxAlignment())
+ return nullptr;
+ return HostTarget;
+}
+
CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
unsigned Align = Target->getCharWidth();
@@ -1882,14 +1898,15 @@ CharUnits ASTContext::getDeclAlign(const Decl *D, bool ForAlignof) const {
else if (!BaseT->isIncompleteType()) {
// Adjust alignments of declarations with array type by the
// large-array alignment on the target.
- if (const ArrayType *arrayType = getAsArrayType(T)) {
- unsigned MinWidth = Target->getLargeArrayMinWidth();
- if (!ForAlignof && MinWidth) {
- if (isa<VariableArrayType>(arrayType))
- Align = std::max(Align, Target->getLargeArrayAlign());
- else if (isa<ConstantArrayType>(arrayType) &&
- MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
- Align = std::max(Align, Target->getLargeArrayAlign());
+ if (!ForAlignof) {
+ if (const auto *CAT = getAsConstantArrayType(T)) {
+ const TargetInfo *Aux =
+ getHostTargetForDeviceAlignment(*this, dyn_cast<VarDecl>(D));
+ Align =
+ std::max(Align, Target->getMinArrayAlign(getTypeSize(CAT), Aux));
+ } else if (Target->getLargeArrayMinWidth() &&
+ isa_and_nonnull<VariableArrayType>(getAsArrayType(T))) {
+ Align = std::max(Align, Target->getLargeArrayAlign());
}
}
Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
@@ -2817,10 +2834,8 @@ 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());
- // Apply the host's minimum only when querying an actual variable in a
- // device translation unit.
return getTargetInfo().getMinGlobalAlignForVar(
- Size, HasNonWeakDef, LangOpts.CUDAIsDevice && VD ? AuxTarget : nullptr);
+ Size, HasNonWeakDef, getHostTargetForDeviceAlignment(*this, VD));
}
CharUnits ASTContext::getOffsetOfBaseWithVBPtr(const CXXRecordDecl *RD) const {
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index c4896f73ae0f96..da57d64be47ca8 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -437,10 +437,23 @@ bool TargetInfo::isTypeSigned(IntType T) {
unsigned
TargetInfo::getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
- const TargetInfo *HostTarget) const {
+ const TargetInfo *AuxTarget) const {
unsigned Align = getMinGlobalAlign(Size, HasNonWeakDef);
- if (HostTarget && HasNonWeakDef)
- return std::max(Align, HostTarget->getMinGlobalAlign(Size, HasNonWeakDef));
+ if (AuxTarget)
+ return std::max(Align, AuxTarget->getMinGlobalAlign(Size, HasNonWeakDef));
+ return Align;
+}
+
+unsigned TargetInfo::getMinArrayAlign(uint64_t Width,
+ const TargetInfo *AuxTarget) const {
+ auto GetAlign = [Width](const TargetInfo &Target) {
+ return Target.LargeArrayMinWidth && Width >= Target.LargeArrayMinWidth
+ ? Target.LargeArrayAlign
+ : 0U;
+ };
+ unsigned Align = GetAlign(*this);
+ if (AuxTarget)
+ return std::max(Align, GetAlign(*AuxTarget));
return Align;
}
diff --git a/clang/lib/Basic/Targets/NVPTX.cpp b/clang/lib/Basic/Targets/NVPTX.cpp
index 61efae2ccae879..10fc8bbc6bf33b 100644
--- a/clang/lib/Basic/Targets/NVPTX.cpp
+++ b/clang/lib/Basic/Targets/NVPTX.cpp
@@ -153,8 +153,8 @@ NVPTXTargetInfo::NVPTXTargetInfo(const llvm::Triple &Triple,
MaxAtomicInlineWidth = HostTarget->getMaxAtomicInlineWidth();
// Properties intentionally not copied from host:
- // - LargeArrayMinWidth, LargeArrayAlign: Not visible across the
- // host/device boundary.
+ // - LargeArrayMinWidth, LargeArrayAlign: Keep NVPTX's own defaults.
+ // ASTContext queries the host rule for CUDA device definitions.
// - SuitableAlign: Not visible across the host/device boundary, and may
// correctly be different on host/device, e.g. if host has wider vector
// types than device.
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 0b42e03d1320ef..a31ad6d2599634 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -167,8 +167,16 @@ bool CompilerInstance::createTarget() {
// created. This complexity should be lifted elsewhere.
getTarget().adjust(getDiagnostics(), getLangOpts(), getAuxTarget());
- if (auto *Aux = getAuxTarget())
+ if (auto *Aux = getAuxTarget()) {
getTarget().setAuxTarget(Aux);
+ if (getLangOpts().CUDAIsDevice) {
+ // Device definitions query the host's configured placement policy
+ // separately from the layout properties copied above.
+ LangOptions HostLangOpts = getLangOpts();
+ HostLangOpts.CUDAIsDevice = false;
+ Aux->adjust(getDiagnostics(), HostLangOpts, &getTarget());
+ }
+ }
return true;
}
diff --git a/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
index 4c267bb7b9f2d7..77aeff056b081f 100644
--- a/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
+++ b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
@@ -1,30 +1,36 @@
-// NOTE: Do not autogenerate. Check array alignment and the address query.
+// NOTE: Do not autogenerate. Share allocation checks across host ABI modes.
// 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: -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,WIN,MSVC
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \
+// RUN: -fclang-abi-compat=22 -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,WIN,LARGE-ARRAY
// 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
+// RUN: -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,LARGE-ARRAY
// MSVC-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 8{{$}}
-// LINUX-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 2{{$}}
+// LARGE-ARRAY-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{{$}}
+// LARGE-ARRAY-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{{$}}
+// WIN-DAG: @external = external {{.*}}addrspace(1) global [16 x i8], align 1{{$}}
+// WIN-DAG: @weak_array = weak addrspace(1) externally_initialized global [16 x i8] zeroinitializer, align 1{{$}}
__attribute__((device)) void use(_Float16 *);
extern "C" {
extern __attribute__((device)) char external[16];
+__attribute__((device, weak)) char weak_array[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]]
+ // WIN-LABEL: define {{.*}} @is_external_aligned(
+ // WIN: %[[MASKED:.*]] = and i64 {{.*}}@external{{.*}}, 7
+ // WIN: %[[ALIGNED:.*]] = icmp eq i64 %[[MASKED]], 0
+ // WIN: ret i1 %[[ALIGNED]]
return __builtin_is_aligned(external, 8);
}
+
+__attribute__((device)) char *external_address() { return external; }
}
__attribute__((global)) void kernel() {
diff --git a/clang/test/CodeGenHIP/managed-abi-alignment.hip b/clang/test/CodeGenHIP/managed-abi-alignment.hip
new file mode 100644
index 00000000000000..0f6a07e1036b47
--- /dev/null
+++ b/clang/test/CodeGenHIP/managed-abi-alignment.hip
@@ -0,0 +1,27 @@
+// NOTE: Do not autogenerate. Use the same alignment parameters for host
+// registration and device accesses.
+// REQUIRES: amdgpu-registered-target
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc \
+// RUN: -foffload-via-llvm -emit-llvm -o - %s | FileCheck %s --check-prefix=HOST -D#SMALL_ALIGN=8 -D#LARGE_ALIGN=8
+// 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-prefix=DEVICE -D#SMALL_ALIGN=8 -D#LARGE_ALIGN=8
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc \
+// RUN: -fclang-abi-compat=22 -foffload-via-llvm -emit-llvm -o - %s | FileCheck %s --check-prefix=HOST -D#SMALL_ALIGN=1 -D#LARGE_ALIGN=16
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \
+// RUN: -fclang-abi-compat=22 -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefix=DEVICE -D#SMALL_ALIGN=1 -D#LARGE_ALIGN=16
+
+// HOST-DAG: @.offloading.entry.managed_small = {{.*}} i64 8, i64 [[#SMALL_ALIGN]], ptr @managed_small }
+// HOST-DAG: @.offloading.entry.managed_large = {{.*}} i64 16, i64 [[#LARGE_ALIGN]], ptr @managed_large }
+
+extern "C" {
+__attribute__((managed)) char managed_small[8];
+__attribute__((managed)) char managed_large[16];
+
+// DEVICE-LABEL: define {{.*}} @read_small(
+// DEVICE: load i8, ptr {{.*}}, align [[#SMALL_ALIGN]]{{$}}
+__attribute__((device)) char read_small() { return managed_small[0]; }
+
+// DEVICE-LABEL: define {{.*}} @read_large(
+// DEVICE: load i8, ptr {{.*}}, align [[#LARGE_ALIGN]]{{$}}
+__attribute__((device)) char read_large() { return managed_large[0]; }
+}
diff --git a/clang/test/SemaCUDA/global-alignment.cu b/clang/test/SemaCUDA/global-alignment.cu
index 49506a79774599..5ace0e1c471bb5 100644
--- a/clang/test/SemaCUDA/global-alignment.cu
+++ b/clang/test/SemaCUDA/global-alignment.cu
@@ -1,14 +1,28 @@
-// 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
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -DARRAY_ALIGN=16 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-linux-gnu -DARRAY_ALIGN=16 -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));
+static_assert(__builtin_is_aligned(array, ARRAY_ALIGN));
+__attribute__((constant)) char constant_array[16];
+static_assert(__builtin_is_aligned(constant_array, ARRAY_ALIGN));
+
+__attribute__((global)) void kernel() {
+ __attribute__((shared)) char shared_array[16];
+ static_assert(__builtin_is_aligned(shared_array, ARRAY_ALIGN));
+}
+
+#if ARRAY_ALIGN == 8
__attribute__((device)) short scalar;
static_assert(alignof(decltype(scalar)) == 2);
static_assert(__builtin_is_aligned(&scalar, 4));
+#endif
>From 73e23fa4642501dceb4e0ddd011e633c78734fa1 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 1 Dec 2026 03:34:05 +0000
Subject: [PATCH 4/5] [Clang][CUDA] Test alignment of globals and local arrays
Cover the host and device alignment of plain globals, constexpr globals and members, and static locals. Check the existing stack alignment on the same target pairs.
AI assisted.
---
clang/test/SemaCUDA/global-alignment.cu | 28 +++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)
diff --git a/clang/test/SemaCUDA/global-alignment.cu b/clang/test/SemaCUDA/global-alignment.cu
index 5ace0e1c471bb5..8769641bcacff2 100644
--- a/clang/test/SemaCUDA/global-alignment.cu
+++ b/clang/test/SemaCUDA/global-alignment.cu
@@ -2,9 +2,9 @@
// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fsyntax-only -verify %s
-// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify=stack %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -DARRAY_ALIGN=16 -fsyntax-only -verify %s
-// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-linux-gnu -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-unknown-linux-gnu -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify=stack %s
// expected-no-diagnostics
@@ -26,3 +26,27 @@ __attribute__((device)) short scalar;
static_assert(alignof(decltype(scalar)) == 2);
static_assert(__builtin_is_aligned(&scalar, 4));
#endif
+
+constexpr char constexpr_array[16] = {};
+static_assert(__builtin_is_aligned(constexpr_array, ARRAY_ALIGN));
+
+struct WithConstexprArray {
+ static constexpr char member_array[16] = {};
+};
+static_assert(__builtin_is_aligned(WithConstexprArray::member_array, ARRAY_ALIGN));
+
+char host_array[16];
+static_assert(__builtin_is_aligned(host_array, ARRAY_ALIGN));
+
+__attribute__((host, device)) void hd_fn() {
+ static constexpr char local_static_array[16] = {};
+ static_assert(__builtin_is_aligned(local_static_array, ARRAY_ALIGN));
+
+#if ARRAY_ALIGN == 16
+ // Automatic arrays follow the current target's alignment rules.
+ char local_array[16];
+ // stack-error at +2 {{static assertion expression is not an integral constant expression}}
+ // stack-note at +1 {{cannot constant evaluate whether run-time alignment is at least 16}}
+ static_assert(__builtin_is_aligned(local_array, ARRAY_ALIGN));
+#endif
+}
>From 3848f6e5a4d4d4131befd484b9e49636f151f825 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 1 Dec 2026 20:43:51 +0000
Subject: [PATCH 5/5] [Clang][CUDA][HIP] Select the host alignment policy for
device definitions
Use the auxiliary host's global and large-array alignment policy when
the caller supplies it for a known device definition. This gives
constant evaluation and device emission the host's alignment.
Check the exact alignment in both jobs for MSVC x64 defaults, MSVC ABI
compatibility mode, and Linux x64.
AI assisted.
---
clang/include/clang/Basic/TargetInfo.h | 5 +-
clang/lib/Basic/TargetInfo.cpp | 20 +++-----
.../amdgpu-shared-array-alignment.hip | 33 +++++-------
clang/test/SemaCUDA/global-alignment.cu | 50 ++++++++++++++-----
4 files changed, 61 insertions(+), 47 deletions(-)
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 95a55c0ae9e5a7..0eb9f869a4512d 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -756,12 +756,13 @@ class TargetInfo : public TransferrableTargetInfo,
return MinGlobalAlign;
}
- /// Return the minimum alignment of a variable. For a CUDA/HIP device
- /// definition, AuxTarget supplies the host's minimum.
+ /// Return the minimum alignment of a variable using AuxTarget's policy
+ /// when supplied.
unsigned getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
const TargetInfo *AuxTarget) const;
/// Return the minimum alignment for a constant-size array of Width bits.
+ /// Use AuxTarget's alignment policy when supplied.
unsigned getMinArrayAlign(uint64_t Width, const TargetInfo *AuxTarget) const;
/// Return the largest alignment for which a suitably-sized allocation with
diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp
index da57d64be47ca8..001561080ad344 100644
--- a/clang/lib/Basic/TargetInfo.cpp
+++ b/clang/lib/Basic/TargetInfo.cpp
@@ -438,23 +438,17 @@ bool TargetInfo::isTypeSigned(IntType T) {
unsigned
TargetInfo::getMinGlobalAlignForVar(uint64_t Size, bool HasNonWeakDef,
const TargetInfo *AuxTarget) const {
- unsigned Align = getMinGlobalAlign(Size, HasNonWeakDef);
- if (AuxTarget)
- return std::max(Align, AuxTarget->getMinGlobalAlign(Size, HasNonWeakDef));
- return Align;
+ const TargetInfo &AlignmentTarget = AuxTarget ? *AuxTarget : *this;
+ return AlignmentTarget.getMinGlobalAlign(Size, HasNonWeakDef);
}
unsigned TargetInfo::getMinArrayAlign(uint64_t Width,
const TargetInfo *AuxTarget) const {
- auto GetAlign = [Width](const TargetInfo &Target) {
- return Target.LargeArrayMinWidth && Width >= Target.LargeArrayMinWidth
- ? Target.LargeArrayAlign
- : 0U;
- };
- unsigned Align = GetAlign(*this);
- if (AuxTarget)
- return std::max(Align, GetAlign(*AuxTarget));
- return Align;
+ const TargetInfo &AlignmentTarget = AuxTarget ? *AuxTarget : *this;
+ return AlignmentTarget.LargeArrayMinWidth &&
+ Width >= AlignmentTarget.LargeArrayMinWidth
+ ? AlignmentTarget.LargeArrayAlign
+ : 0;
}
/// adjust - Set forced language options.
diff --git a/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
index 77aeff056b081f..7abca9ba3301a4 100644
--- a/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
+++ b/clang/test/CodeGenHIP/amdgpu-shared-array-alignment.hip
@@ -1,22 +1,17 @@
-// NOTE: Do not autogenerate. Share allocation checks across host ABI modes.
+// NOTE: Do not autogenerate. Check the placement of representative device
+// definitions and the alignment query for external storage.
// 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,WIN,MSVC
-// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc \
-// RUN: -fclang-abi-compat=22 -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,WIN,LARGE-ARRAY
-// 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,LARGE-ARRAY
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,WIN -D#SMALL_ALIGN=4 -D#SHARED_ALIGN=8
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -fcuda-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,WIN -D#SMALL_ALIGN=1 -D#SHARED_ALIGN=16
+// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-unknown-linux-gnu -fcuda-is-device -emit-llvm -o - %s | FileCheck %s -D#SMALL_ALIGN=1 -D#SHARED_ALIGN=16
-// MSVC-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 8{{$}}
-// LARGE-ARRAY-DAG: @{{.*}}small = internal addrspace(3) global [7 x half] {{.*}}, align 2{{$}}
-// MSVC-DAG: @{{.*}}medium = internal addrspace(3) global [8 x half] {{.*}}, align 8{{$}}
-// LARGE-ARRAY-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{{$}}
+// CHECK-DAG: @{{.*}}small = internal addrspace(3) global [7 x i8] {{.*}}, align [[#SMALL_ALIGN]]{{$}}
+// CHECK-DAG: @{{.*}}rows = internal addrspace(3) global [16 x i8] {{.*}}, align [[#SHARED_ALIGN]]{{$}}
+// CHECK-DAG: @{{.*}}explicit_align = internal addrspace(3) global [16 x i8] {{.*}}, align 4{{$}}
// WIN-DAG: @external = external {{.*}}addrspace(1) global [16 x i8], align 1{{$}}
// WIN-DAG: @weak_array = weak addrspace(1) externally_initialized global [16 x i8] zeroinitializer, align 1{{$}}
-__attribute__((device)) void use(_Float16 *);
+__attribute__((device)) void use(char *);
extern "C" {
extern __attribute__((device)) char external[16];
@@ -34,12 +29,10 @@ __attribute__((device)) char *external_address() { return external; }
}
__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];
+ __attribute__((shared)) char small[7];
+ __attribute__((shared)) char rows[16];
+ __attribute__((shared, aligned(4))) char explicit_align[16];
use(small);
- use(medium);
- use(large);
+ use(rows);
use(explicit_align);
}
diff --git a/clang/test/SemaCUDA/global-alignment.cu b/clang/test/SemaCUDA/global-alignment.cu
index 8769641bcacff2..8faad461c4b624 100644
--- a/clang/test/SemaCUDA/global-alignment.cu
+++ b/clang/test/SemaCUDA/global-alignment.cu
@@ -1,5 +1,7 @@
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -triple nvptx64-nvidia-cuda -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -DARRAY_ALIGN=8 -fcuda-is-device -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple amdgpu11.00-amd-amdhsa -aux-triple x86_64-pc-windows-msvc -fclang-abi-compat=22 -DARRAY_ALIGN=16 -fcuda-is-device -fsyntax-only -verify=stack %s
@@ -8,25 +10,49 @@
// 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, ARRAY_ALIGN));
+#define DEVICE __attribute__((device))
+template <auto Ptr, unsigned Align, bool Aligned = __builtin_is_aligned(Ptr, Align)>
+constexpr bool known_aligned(int) {
+ return Aligned;
+}
+template <auto Ptr, unsigned Align>
+constexpr bool known_aligned(...) {
+ return false;
+}
+
+extern DEVICE char forward[16];
+DEVICE char defined[16];
__attribute__((constant)) char constant_array[16];
-static_assert(__builtin_is_aligned(constant_array, ARRAY_ALIGN));
+alignas(16) DEVICE char explicitly_aligned[16];
+
+// The host alignment of a known definition can affect a kernel argument
+// layout. Check the layout in both compilation jobs.
+template <auto Ptr> struct KernelArg {
+ int tag;
+ char payload[known_aligned<Ptr, ARRAY_ALIGN>(0) ? 32 : 8];
+ int tail;
+};
+static_assert(sizeof(KernelArg<defined>) == 40);
+static_assert(__builtin_offsetof(KernelArg<defined>, tail) == 36);
+static_assert(sizeof(KernelArg<constant_array>) == 40);
+static_assert(sizeof(KernelArg<explicitly_aligned>) == 40);
+static_assert(__builtin_offsetof(KernelArg<explicitly_aligned>, tail) == 36);
+DEVICE char forward[16];
+static_assert(__builtin_is_aligned(forward, ARRAY_ALIGN));
+
+DEVICE short scalar;
+static_assert(__builtin_is_aligned(&scalar, alignof(short)));
-__attribute__((global)) void kernel() {
+__attribute__((global)) void kernel(KernelArg<defined>) {
__attribute__((shared)) char shared_array[16];
static_assert(__builtin_is_aligned(shared_array, ARRAY_ALIGN));
+ static_assert(!__builtin_constant_p(
+ __builtin_is_aligned(shared_array, ARRAY_ALIGN * 2)));
+ alignas(4) __attribute__((shared)) char explicit_shared[16];
+ static_assert(__builtin_is_aligned(explicit_shared, 4));
}
-#if ARRAY_ALIGN == 8
-__attribute__((device)) short scalar;
-static_assert(alignof(decltype(scalar)) == 2);
-static_assert(__builtin_is_aligned(&scalar, 4));
-#endif
-
constexpr char constexpr_array[16] = {};
static_assert(__builtin_is_aligned(constexpr_array, ARRAY_ALIGN));
More information about the cfe-commits
mailing list