[clang] [Clang][LoongArch] Fix va_arg alignment for direct _BitInt arguments (PR #248361)

Jiakai Xu via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 00:03:16 PST 2026


https://github.com/6eanut created https://github.com/llvm/llvm-project/pull/248361

The caller uses the natural alignment of the lowered integer when
passing a _BitInt variadic argument directly. va_arg used the alignment
of the C type, so an aligned typedef could make it read the argument
and subsequent arguments from the wrong locations.

Use the natural type alignment for directly passed _BitInt arguments.
Cover over-aligned and under-aligned typedefs on LoongArch32 and
LoongArch64 and check the following argument in both cases.

Assisted-by: Codex

### Testing

- The new CodeGen regression test fails before the fix and passes after it
  for `ilp32d` and `lp64d`.
- The local LoongArch Clang CodeGen suite passed 47 tests, with 2 unsupported.
- A LoongArch64 QEMU reproducer passed at `-O0` and `-O2` with natural
  alignment, `aligned(1)`, and `aligned(32)`.


>From 1aab83343610e4a9cdd5cfadc1d09206a6b04828 Mon Sep 17 00:00:00 2001
From: 6eanut <jiakaiPeanut at gmail.com>
Date: Thu, 3 Dec 2026 08:02:37 +0000
Subject: [PATCH] [Clang][LoongArch] Fix va_arg alignment for direct _BitInt
 arguments

The caller uses the natural alignment of the lowered integer when
passing a _BitInt variadic argument directly. va_arg used the alignment
of the C type, so an aligned typedef could make it read the argument
and subsequent arguments from the wrong locations.

Use the natural type alignment for directly passed _BitInt arguments.
Cover over-aligned and under-aligned typedefs on LoongArch32 and
LoongArch64 and check the following argument in both cases.

Assisted-by: Codex
---
 clang/lib/CodeGen/Targets/LoongArch.cpp       |  11 +-
 clang/test/CodeGen/LoongArch/bitint-varargs.c | 140 ++++++++++++++++++
 2 files changed, 147 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/CodeGen/LoongArch/bitint-varargs.c

diff --git a/clang/lib/CodeGen/Targets/LoongArch.cpp b/clang/lib/CodeGen/Targets/LoongArch.cpp
index ca080b22fcabd8..38a1b16ecab8e2 100644
--- a/clang/lib/CodeGen/Targets/LoongArch.cpp
+++ b/clang/lib/CodeGen/Targets/LoongArch.cpp
@@ -436,10 +436,13 @@ RValue LoongArchABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
 
   auto TInfo = getContext().getTypeInfoInChars(Ty);
 
-  // Arguments bigger than 2*GRLen bytes are passed indirectly.
-  return emitVoidPtrVAArg(CGF, VAListAddr, Ty,
-                          /*IsIndirect=*/TInfo.Width > 2 * SlotSize, TInfo,
-                          SlotSize,
+  // Arguments bigger than 2 * GRLen bits are passed indirectly.
+  bool IsIndirect = TInfo.Width > 2 * SlotSize;
+  // The caller passes direct _BitInt arguments using their natural alignment.
+  if (!IsIndirect && Ty->isBitIntType())
+    TInfo.Align = getContext().getTypeUnadjustedAlignInChars(Ty);
+
+  return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect, TInfo, SlotSize,
                           /*AllowHigherAlign=*/true, Slot);
 }
 
diff --git a/clang/test/CodeGen/LoongArch/bitint-varargs.c b/clang/test/CodeGen/LoongArch/bitint-varargs.c
new file mode 100644
index 00000000000000..d3a063e7c8879a
--- /dev/null
+++ b/clang/test/CodeGen/LoongArch/bitint-varargs.c
@@ -0,0 +1,140 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
+// RUN: %clang_cc1 -triple loongarch32 -target-abi ilp32d -O1 -emit-llvm -o - %s \
+// RUN:   | FileCheck %s --check-prefix=LA32
+// RUN: %clang_cc1 -triple loongarch64 -target-abi lp64d -O1 -emit-llvm -o - %s \
+// RUN:   | FileCheck %s --check-prefix=LA64
+
+typedef _BitInt(__loongarch_grlen + 1) pair_t;
+typedef pair_t overaligned_pair_t __attribute__((aligned(32)));
+typedef pair_t underaligned_pair_t __attribute__((aligned(1)));
+
+// The argument uses the natural alignment of the register pair. Read a
+// trailing int to check where va_arg advances the list.
+// LA32-LABEL: define dso_local range(i32 0, 2) i32 @read_overaligned_pair(
+// LA32-SAME: i32 noundef [[N:%.*]], ...) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// LA32-NEXT:  [[ENTRY:.*:]]
+// LA32-NEXT:    [[AP:%.*]] = alloca ptr, align 4
+// LA32-NEXT:    call void @llvm.lifetime.start.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    call void @llvm.va_start.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    [[ARGP_CUR:%.*]] = load ptr, ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR]], i32 7
+// LA32-NEXT:    [[ARGP_CUR_ALIGNED:%.*]] = call align 8 ptr @llvm.ptrmask.p0.i32(ptr nonnull [[TMP0]], i32 -8)
+// LA32-NEXT:    [[ARGP_NEXT:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i32 8
+// LA32-NEXT:    store ptr [[ARGP_NEXT]], ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP1:%.*]] = load i64, ptr [[ARGP_CUR_ALIGNED]], align 8, !tbaa [[TBAA7:![0-9]+]]
+// LA32-NEXT:    [[LOADEDV:%.*]] = trunc i64 [[TMP1]] to i33
+// LA32-NEXT:    [[ARGP_NEXT2:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i32 12
+// LA32-NEXT:    store ptr [[ARGP_NEXT2]], ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP2:%.*]] = load i32, ptr [[ARGP_NEXT]], align 8, !tbaa [[TBAA9:![0-9]+]]
+// LA32-NEXT:    call void @llvm.va_end.p0(ptr [[AP]])
+// LA32-NEXT:    [[CMP:%.*]] = icmp eq i33 [[LOADEDV]], 1
+// LA32-NEXT:    [[CMP4:%.*]] = icmp eq i32 [[TMP2]], 2
+// LA32-NEXT:    [[TMP3:%.*]] = select i1 [[CMP]], i1 [[CMP4]], i1 false
+// LA32-NEXT:    [[LAND_EXT:%.*]] = zext i1 [[TMP3]] to i32
+// LA32-NEXT:    call void @llvm.lifetime.end.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    ret i32 [[LAND_EXT]]
+//
+// LA64-LABEL: define dso_local signext range(i32 0, 2) i32 @read_overaligned_pair(
+// LA64-SAME: i32 noundef signext [[N:%.*]], ...) local_unnamed_addr #[[ATTR0:[0-9]+]] {
+// LA64-NEXT:  [[ENTRY:.*:]]
+// LA64-NEXT:    [[AP:%.*]] = alloca ptr, align 8
+// LA64-NEXT:    call void @llvm.lifetime.start.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    call void @llvm.va_start.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    [[ARGP_CUR:%.*]] = load ptr, ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR]], i64 15
+// LA64-NEXT:    [[ARGP_CUR_ALIGNED:%.*]] = call align 16 ptr @llvm.ptrmask.p0.i64(ptr nonnull [[TMP0]], i64 -16)
+// LA64-NEXT:    [[ARGP_NEXT:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i64 16
+// LA64-NEXT:    store ptr [[ARGP_NEXT]], ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP1:%.*]] = load i128, ptr [[ARGP_CUR_ALIGNED]], align 16, !tbaa [[TBAA7:![0-9]+]]
+// LA64-NEXT:    [[LOADEDV:%.*]] = trunc i128 [[TMP1]] to i65
+// LA64-NEXT:    [[ARGP_NEXT2:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i64 24
+// LA64-NEXT:    store ptr [[ARGP_NEXT2]], ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP2:%.*]] = load i32, ptr [[ARGP_NEXT]], align 16, !tbaa [[TBAA9:![0-9]+]]
+// LA64-NEXT:    call void @llvm.va_end.p0(ptr [[AP]])
+// LA64-NEXT:    [[CMP:%.*]] = icmp eq i65 [[LOADEDV]], 1
+// LA64-NEXT:    [[CMP4:%.*]] = icmp eq i32 [[TMP2]], 2
+// LA64-NEXT:    [[TMP3:%.*]] = select i1 [[CMP]], i1 [[CMP4]], i1 false
+// LA64-NEXT:    [[LAND_EXT:%.*]] = zext i1 [[TMP3]] to i32
+// LA64-NEXT:    call void @llvm.lifetime.end.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    ret i32 [[LAND_EXT]]
+//
+int read_overaligned_pair(int n, ...) {
+  __builtin_va_list ap;
+  __builtin_va_start(ap, n);
+  overaligned_pair_t value = __builtin_va_arg(ap, overaligned_pair_t);
+  int tail = __builtin_va_arg(ap, int);
+  __builtin_va_end(ap);
+  return value == 1 && tail == 2;
+}
+
+// LA32-LABEL: define dso_local range(i32 0, 2) i32 @read_underaligned_pair(
+// LA32-SAME: i32 noundef [[N:%.*]], ...) local_unnamed_addr #[[ATTR0]] {
+// LA32-NEXT:  [[ENTRY:.*:]]
+// LA32-NEXT:    [[AP:%.*]] = alloca ptr, align 4
+// LA32-NEXT:    call void @llvm.lifetime.start.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    call void @llvm.va_start.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    [[ARGP_CUR:%.*]] = load ptr, ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR]], i32 7
+// LA32-NEXT:    [[ARGP_CUR_ALIGNED:%.*]] = call align 8 ptr @llvm.ptrmask.p0.i32(ptr nonnull [[TMP0]], i32 -8)
+// LA32-NEXT:    [[ARGP_NEXT:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i32 8
+// LA32-NEXT:    store ptr [[ARGP_NEXT]], ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP1:%.*]] = load i64, ptr [[ARGP_CUR_ALIGNED]], align 8, !tbaa [[TBAA7]]
+// LA32-NEXT:    [[LOADEDV:%.*]] = trunc i64 [[TMP1]] to i33
+// LA32-NEXT:    [[ARGP_NEXT2:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i32 12
+// LA32-NEXT:    store ptr [[ARGP_NEXT2]], ptr [[AP]], align 4
+// LA32-NEXT:    [[TMP2:%.*]] = load i32, ptr [[ARGP_NEXT]], align 8, !tbaa [[TBAA9]]
+// LA32-NEXT:    call void @llvm.va_end.p0(ptr [[AP]])
+// LA32-NEXT:    [[CMP:%.*]] = icmp eq i33 [[LOADEDV]], 1
+// LA32-NEXT:    [[CMP4:%.*]] = icmp eq i32 [[TMP2]], 2
+// LA32-NEXT:    [[TMP3:%.*]] = select i1 [[CMP]], i1 [[CMP4]], i1 false
+// LA32-NEXT:    [[LAND_EXT:%.*]] = zext i1 [[TMP3]] to i32
+// LA32-NEXT:    call void @llvm.lifetime.end.p0(ptr nonnull [[AP]])
+// LA32-NEXT:    ret i32 [[LAND_EXT]]
+//
+// LA64-LABEL: define dso_local signext range(i32 0, 2) i32 @read_underaligned_pair(
+// LA64-SAME: i32 noundef signext [[N:%.*]], ...) local_unnamed_addr #[[ATTR0]] {
+// LA64-NEXT:  [[ENTRY:.*:]]
+// LA64-NEXT:    [[AP:%.*]] = alloca ptr, align 8
+// LA64-NEXT:    call void @llvm.lifetime.start.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    call void @llvm.va_start.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    [[ARGP_CUR:%.*]] = load ptr, ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP0:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR]], i64 15
+// LA64-NEXT:    [[ARGP_CUR_ALIGNED:%.*]] = call align 16 ptr @llvm.ptrmask.p0.i64(ptr nonnull [[TMP0]], i64 -16)
+// LA64-NEXT:    [[ARGP_NEXT:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i64 16
+// LA64-NEXT:    store ptr [[ARGP_NEXT]], ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP1:%.*]] = load i128, ptr [[ARGP_CUR_ALIGNED]], align 16, !tbaa [[TBAA7]]
+// LA64-NEXT:    [[LOADEDV:%.*]] = trunc i128 [[TMP1]] to i65
+// LA64-NEXT:    [[ARGP_NEXT2:%.*]] = getelementptr inbounds nuw i8, ptr [[ARGP_CUR_ALIGNED]], i64 24
+// LA64-NEXT:    store ptr [[ARGP_NEXT2]], ptr [[AP]], align 8
+// LA64-NEXT:    [[TMP2:%.*]] = load i32, ptr [[ARGP_NEXT]], align 16, !tbaa [[TBAA9]]
+// LA64-NEXT:    call void @llvm.va_end.p0(ptr [[AP]])
+// LA64-NEXT:    [[CMP:%.*]] = icmp eq i65 [[LOADEDV]], 1
+// LA64-NEXT:    [[CMP4:%.*]] = icmp eq i32 [[TMP2]], 2
+// LA64-NEXT:    [[TMP3:%.*]] = select i1 [[CMP]], i1 [[CMP4]], i1 false
+// LA64-NEXT:    [[LAND_EXT:%.*]] = zext i1 [[TMP3]] to i32
+// LA64-NEXT:    call void @llvm.lifetime.end.p0(ptr nonnull [[AP]])
+// LA64-NEXT:    ret i32 [[LAND_EXT]]
+//
+int read_underaligned_pair(int n, ...) {
+  __builtin_va_list ap;
+  __builtin_va_start(ap, n);
+  underaligned_pair_t value = __builtin_va_arg(ap, underaligned_pair_t);
+  int tail = __builtin_va_arg(ap, int);
+  __builtin_va_end(ap);
+  return value == 1 && tail == 2;
+}
+//.
+// LA32: [[META4:![0-9]+]] = !{!"int", [[META5:![0-9]+]], i64 0}
+// LA32: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
+// LA32: [[META6]] = !{!"Simple C/C++ TBAA"}
+// LA32: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
+// LA32: [[META8]] = !{!"_BitInt(33)", [[META5]], i64 0}
+// LA32: [[TBAA9]] = !{[[META4]], [[META4]], i64 0}
+//.
+// LA64: [[META4:![0-9]+]] = !{!"int", [[META5:![0-9]+]], i64 0}
+// LA64: [[META5]] = !{!"omnipotent char", [[META6:![0-9]+]], i64 0}
+// LA64: [[META6]] = !{!"Simple C/C++ TBAA"}
+// LA64: [[TBAA7]] = !{[[META8:![0-9]+]], [[META8]], i64 0}
+// LA64: [[META8]] = !{!"_BitInt(65)", [[META5]], i64 0}
+// LA64: [[TBAA9]] = !{[[META4]], [[META4]], i64 0}
+//.



More information about the cfe-commits mailing list