[clang] [RISCV] Add riscv_packed.h for P extension intrinsics (PR #181115)
via cfe-commits
cfe-commits at lists.llvm.org
Wed May 20 02:59:04 PDT 2026
================
@@ -0,0 +1,249 @@
+/*===---- riscv_packed.h - RISC-V P intrinsics -----------------------------===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#ifndef __RISCV_PACKED_H
+#define __RISCV_PACKED_H
+
+#include <stdint.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* Packed SIMD Types */
+
+typedef int8_t int8x4_t __attribute__((__vector_size__(4), __aligned__(4)));
----------------
sihuan wrote:
Looks like `__aligned__` here is redundant.
Clang computes the default alignment for vector types in `ASTContext::getTypeInfo` as `max(8 bit, total_width)`, then caps it at `TargetInfo::MaxVectorAlign` if that's set.
https://github.com/llvm/llvm-project/blob/24b74dd3e568184e4dc59f73b7447b7d43a4e302/clang/lib/AST/ASTContext.cpp#L2092-L2129
`TargetInfo` defaults are `MaxVectorAlign = 0` (no cap) and `VectorsAreElementAligned = false`. RISC-V doesn't override either of these. X86's `__m128` / `__m128i` / `__m128d` look redundant the same way — clang already computes 16-byte alignment for them. (X86 *does* set `MaxVectorAlign`, but it doesn't affect 128-bit vectors here.)
Verified:
```c
#include <stdint.h>
/* RISC-V P-ext packed types */
typedef int8_t int8x4_t __attribute__((__vector_size__(4)));
typedef uint8_t uint8x4_t __attribute__((__vector_size__(4)));
typedef int16_t int16x2_t __attribute__((__vector_size__(4)));
typedef uint16_t uint16x2_t __attribute__((__vector_size__(4)));
typedef int8_t int8x8_t __attribute__((__vector_size__(8)));
typedef uint8_t uint8x8_t __attribute__((__vector_size__(8)));
typedef int16_t int16x4_t __attribute__((__vector_size__(8)));
typedef uint16_t uint16x4_t __attribute__((__vector_size__(8)));
typedef int32_t int32x2_t __attribute__((__vector_size__(8)));
typedef uint32_t uint32x2_t __attribute__((__vector_size__(8)));
/* X86 __m128* shapes, without __aligned__ */
typedef float __m128 __attribute__((__vector_size__(16)));
typedef long long __m128i __attribute__((__vector_size__(16)));
typedef double __m128d __attribute__((__vector_size__(16)));
_Static_assert(__alignof__(int8x4_t) == 4, "");
_Static_assert(__alignof__(uint8x4_t) == 4, "");
_Static_assert(__alignof__(int16x2_t) == 4, "");
_Static_assert(__alignof__(uint16x2_t) == 4, "");
_Static_assert(__alignof__(int8x8_t) == 8, "");
_Static_assert(__alignof__(uint8x8_t) == 8, "");
_Static_assert(__alignof__(int16x4_t) == 8, "");
_Static_assert(__alignof__(uint16x4_t) == 8, "");
_Static_assert(__alignof__(int32x2_t) == 8, "");
_Static_assert(__alignof__(uint32x2_t) == 8, "");
_Static_assert(__alignof__(__m128) == 16, "");
_Static_assert(__alignof__(__m128i) == 16, "");
_Static_assert(__alignof__(__m128d) == 16, "");
```
All 13 assertions hold under `--target={riscv32,riscv64,x86_64}`.
https://github.com/llvm/llvm-project/pull/181115
More information about the cfe-commits
mailing list