llvm-project/clang/test/CodeGenCXX/sret_cast_with_nonzero_alloca_as.cpp
Yingwei Zheng 5569bf26f0
[Clang][CodeGen] Preserve alignment information for pointer arithmetics (#152575)
Previously, the alignment of pointer arithmetics was inferred from the
pointee type, losing the alignment information from its operands:

503c0908c3/clang/lib/CodeGen/CGExpr.cpp (L1446-L1449)

This patch preserves alignment information for pointer arithmetics `P
+/- C`, to match the behavior of identical array subscript `&P[C]`:
https://godbolt.org/z/xx1hfTrx4.

Closes https://github.com/llvm/llvm-project/issues/152330. Although the
motivating case can be fixed by
https://github.com/llvm/llvm-project/pull/145733, the alignment cannot
be recovered without a dominating memory access with larger alignment.
2025-08-25 20:08:32 +08:00

32 lines
1.6 KiB
C++

// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa-gnu -target-cpu gfx900 -emit-llvm -o - %s | FileCheck %s
struct X { int z[17]; };
// CHECK-LABEL: define dso_local void @_Z3foocc(
// CHECK-SAME: ptr addrspace(5) dead_on_unwind noalias writable sret([[STRUCT_X:%.*]]) align 4 [[AGG_RESULT:%.*]], i8 noundef signext [[X:%.*]], i8 noundef signext [[Y:%.*]]) #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[X_ADDR:%.*]] = alloca i8, align 1, addrspace(5)
// CHECK-NEXT: [[Y_ADDR:%.*]] = alloca i8, align 1, addrspace(5)
// CHECK-NEXT: [[X_ADDR_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[X_ADDR]] to ptr
// CHECK-NEXT: [[Y_ADDR_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[Y_ADDR]] to ptr
// CHECK-NEXT: [[AGG_RESULT_ASCAST:%.*]] = addrspacecast ptr addrspace(5) [[AGG_RESULT]] to ptr
// CHECK-NEXT: store i8 [[X]], ptr [[X_ADDR_ASCAST]], align 1
// CHECK-NEXT: store i8 [[Y]], ptr [[Y_ADDR_ASCAST]], align 1
// CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[X_ADDR_ASCAST]], align 1
// CHECK-NEXT: [[ADD_PTR:%.*]] = getelementptr inbounds i8, ptr [[AGG_RESULT_ASCAST]], i64 1
// CHECK-NEXT: store i8 [[TMP0]], ptr [[ADD_PTR]], align 1
// CHECK-NEXT: [[TMP1:%.*]] = load i8, ptr [[Y_ADDR_ASCAST]], align 1
// CHECK-NEXT: [[ADD_PTR1:%.*]] = getelementptr inbounds i8, ptr [[AGG_RESULT_ASCAST]], i64 2
// CHECK-NEXT: store i8 [[TMP1]], ptr [[ADD_PTR1]], align 2
// CHECK-NEXT: ret void
//
X foo(char x, char y) {
X r;
*(((char*)&r) + 1) = x;
*(reinterpret_cast<char*>(&r) + 2) = y;
return r;
}