[clang] [clang] Accept floating-point SVE vectors in __arithmetic_fence (PR #239248)
Ente Wang via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 03:50:30 PST 2026
https://github.com/Wet-nat updated https://github.com/llvm/llvm-project/pull/239248
>From 9e2e02f2cd3ee022b1704f42043e55143447c029 Mon Sep 17 00:00:00 2001
From: Ente Wang <ente.wang at h-partners.com>
Date: Wed, 4 Nov 2026 22:49:48 +0800
Subject: [PATCH] [clang] Accept floating-point SVE vectors in
__arithmetic_fence
Use the existing SVE type helpers to recognize single floating-point
vectors while retaining diagnostics for predicates, tuples, and other
non-floating operands. Reuse the existing arithmetic-fence lowering.
Add C/C++ semantic and code-generation coverage, including active and
inactive fences and prevention of multiply-add contraction.
Fixes llvm/llvm-project#223145
Assisted-by: OpenAI Codex
---
clang/docs/ReleaseNotes.md | 2 +
clang/lib/Sema/SemaChecking.cpp | 4 +-
.../CodeGen/AArch64/sve-arithmetic-fence.c | 45 +++++++++++++++++++
.../test/Sema/aarch64-sve-arithmetic-fence.c | 40 +++++++++++++++++
4 files changed, 90 insertions(+), 1 deletion(-)
create mode 100644 clang/test/CodeGen/AArch64/sve-arithmetic-fence.c
create mode 100644 clang/test/Sema/aarch64-sve-arithmetic-fence.c
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 83248f68f539ee..cf8b9e5ebe4a18 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -852,6 +852,8 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
#### Bug Fixes to Compiler Builtins
+- Fixed `__arithmetic_fence` incorrectly rejecting sizeless SVE vectors of
+ floating-point elements. (#GH223145)
- Dropped invalid ``constexpr`` specifier from ``__builtin_assume_separate_storage``, which is not supported in constexpr evaluation. (#GH225335)
- Fixed a crash when classifying a call to a builtin with dependent arguments,
such as when the call is used as an `auto` non-type template argument.
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4b9c1d187dfeb9..068e5196207ab7 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -6877,7 +6877,9 @@ bool Sema::BuiltinArithmeticFence(CallExpr *TheCall) {
return false;
QualType ArgTy = Arg->getType();
- if (!ArgTy->hasFloatingRepresentation())
+ if (!ArgTy->hasFloatingRepresentation() &&
+ !(ArgTy->isSveVLSBuiltinType() &&
+ ArgTy->getSveEltType(Context)->isFloatingType()))
return Diag(TheCall->getEndLoc(), diag::err_typecheck_expect_flt_or_vector)
<< ArgTy;
if (Arg->isLValue()) {
diff --git a/clang/test/CodeGen/AArch64/sve-arithmetic-fence.c b/clang/test/CodeGen/AArch64/sve-arithmetic-fence.c
new file mode 100644
index 00000000000000..c3c5ce663c8a0f
--- /dev/null
+++ b/clang/test/CodeGen/AArch64/sve-arithmetic-fence.c
@@ -0,0 +1,45 @@
+// REQUIRES: aarch64-registered-target
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -emit-llvm -disable-O0-optnone -mreassociate -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,FENCE
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -emit-llvm -disable-O0-optnone -o - %s | opt -S -passes=mem2reg | FileCheck %s --check-prefixes=CHECK,IDENTITY --implicit-check-not=llvm.arithmetic.fence
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -O2 -mreassociate -ffp-contract=fast -S -o - %s | FileCheck %s --check-prefix=ASM
+
+#include <arm_sve.h>
+
+// CHECK-LABEL: @test_f16(
+// FENCE: [[F16:%.*]] = call reassoc <vscale x 8 x half> @llvm.arithmetic.fence.nxv8f16(<vscale x 8 x half> %x)
+// FENCE-NEXT: ret <vscale x 8 x half> [[F16]]
+// IDENTITY: ret <vscale x 8 x half> %x
+svfloat16_t test_f16(svfloat16_t x) { return __arithmetic_fence(x); }
+
+// CHECK-LABEL: @test_f32(
+// FENCE: [[F32:%.*]] = call reassoc <vscale x 4 x float> @llvm.arithmetic.fence.nxv4f32(<vscale x 4 x float> %x)
+// FENCE-NEXT: ret <vscale x 4 x float> [[F32]]
+// IDENTITY: ret <vscale x 4 x float> %x
+svfloat32_t test_f32(svfloat32_t x) { return __arithmetic_fence(x); }
+
+// CHECK-LABEL: @test_f64(
+// FENCE: [[F64:%.*]] = call reassoc <vscale x 2 x double> @llvm.arithmetic.fence.nxv2f64(<vscale x 2 x double> %x)
+// FENCE-NEXT: ret <vscale x 2 x double> [[F64]]
+// IDENTITY: ret <vscale x 2 x double> %x
+svfloat64_t test_f64(svfloat64_t x) { return __arithmetic_fence(x); }
+
+// CHECK-LABEL: @test_bf16(
+// FENCE: [[BF16:%.*]] = call reassoc <vscale x 8 x bfloat> @llvm.arithmetic.fence.nxv8bf16(<vscale x 8 x bfloat> %x)
+// FENCE-NEXT: ret <vscale x 8 x bfloat> [[BF16]]
+// IDENTITY: ret <vscale x 8 x bfloat> %x
+svbfloat16_t test_bf16(svbfloat16_t x) { return __arithmetic_fence(x); }
+
+// ASM-LABEL: test_fenced_muladd:
+// ASM: fmul z
+// ASM: fadd z
+// ASM: ret
+svfloat32_t test_fenced_muladd(svfloat32_t a, svfloat32_t b, svfloat32_t c) {
+ return __arithmetic_fence(a * b) + c;
+}
+
+// ASM-LABEL: test_unfenced_muladd:
+// ASM: {{fmad|fmla}} z
+// ASM: ret
+svfloat32_t test_unfenced_muladd(svfloat32_t a, svfloat32_t b, svfloat32_t c) {
+ return a * b + c;
+}
diff --git a/clang/test/Sema/aarch64-sve-arithmetic-fence.c b/clang/test/Sema/aarch64-sve-arithmetic-fence.c
new file mode 100644
index 00000000000000..700686d9605abc
--- /dev/null
+++ b/clang/test/Sema/aarch64-sve-arithmetic-fence.c
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple aarch64 -target-feature +sve -fsyntax-only -verify -x c++ %s
+
+#include <arm_sve.h>
+
+svfloat16_t test_f16(svfloat16_t x) { return __arithmetic_fence(x); }
+svfloat32_t test_f32(svfloat32_t x) { return __arithmetic_fence(x); }
+svfloat64_t test_f64(svfloat64_t x) { return __arithmetic_fence(x); }
+svbfloat16_t test_bf16(svbfloat16_t x) { return __arithmetic_fence(x); }
+
+svfloat32_t test_const(const svfloat32_t x) {
+ return __arithmetic_fence(x);
+}
+
+void test_invalid(svint32_t i, svuint64_t u, svbool_t p,
+ svfloat32x2_t t2, svfloat64x3_t t3, svfloat16x4_t t4,
+ svcount_t count, svmfloat8_t m) {
+ __arithmetic_fence(i); // expected-error {{invalid operand of type 'svint32_t'}}
+ __arithmetic_fence(u); // expected-error {{invalid operand of type 'svuint64_t'}}
+ __arithmetic_fence(p); // expected-error {{invalid operand of type 'svbool_t'}}
+ __arithmetic_fence(t2); // expected-error {{invalid operand of type 'svfloat32x2_t'}}
+ __arithmetic_fence(t3); // expected-error {{invalid operand of type 'svfloat64x3_t'}}
+ __arithmetic_fence(t4); // expected-error {{invalid operand of type 'svfloat16x4_t'}}
+ __arithmetic_fence(count); // expected-error {{invalid operand of type 'svcount_t'}}
+ __arithmetic_fence(m); // expected-error {{invalid operand of type 'svmfloat8_t'}}
+}
+
+#ifdef __cplusplus
+template <typename T> T test_template(T x) {
+ return __arithmetic_fence(x); // expected-error {{invalid operand of type '__SVInt32_t'}}
+}
+
+svfloat64_t test_dependent(svfloat64_t x) {
+ return test_template(x);
+}
+
+void test_dependent_invalid(svint32_t x) {
+ test_template(x); // expected-note {{in instantiation of function template specialization 'test_template<__SVInt32_t>' requested here}}
+}
+#endif
More information about the cfe-commits
mailing list