[clang] [flang] [Flang][AArch64][VecLib] Add libmvec support for Flang/AArch64 (PR #146453)
KAWASHIMA Takahiro via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 1 04:25:43 PDT 2025
https://github.com/kawashima-fj updated https://github.com/llvm/llvm-project/pull/146453
>From 158eb6d00a123e5d427b4aa52b37824ff3e20840 Mon Sep 17 00:00:00 2001
From: KAWASHIMA Takahiro <t-kawashima at fujitsu.com>
Date: Wed, 25 Jun 2025 16:34:21 +0900
Subject: [PATCH 1/2] [Flang][AArch64][VecLib] Add libmvec support for
Flang/AArch64
`-fveclib=libmvec` for AArch64 (NEON and SVE) in Clang was supported
by #143696. This patch does the same for Flang.
Vector functions defined in `libmvec` are used for the following
Fortran operator and functions currently.
- Power operator (`**`)
- Fortran intrinsic functions listed below for `real(kind=4)` and
`real(kind=8)`
(including their coresponding specific intrinsic functions)
- Fortran intrinsic functions which are expanded using functions
listed below
(for example, `sin` for `complex(kind=8)`)
```
sin
tan
cos
asin
acos
atan (both atan(x) and atan(y, x))
atan2
cosh
tanh
asinh
acosh
atanh
erf
erfc
exp
log
log10
```
As with Clang/AArch64, glibc 2.40 or higher is required to use all
these functions.
---
clang/lib/Driver/ToolChains/Flang.cpp | 9 ++++++++-
flang/docs/ReleaseNotes.md | 1 +
flang/test/Driver/fveclib-codegen.f90 | 4 ++++
flang/test/Driver/fveclib.f90 | 3 ++-
4 files changed, 15 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index e4e321ba1e195..75ba2af543c7a 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -484,11 +484,18 @@ void Flang::addTargetOptions(const ArgList &Args,
Triple.getArch() != llvm::Triple::x86_64)
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< Name << Triple.getArchName();
- } else if (Name == "libmvec" || Name == "AMDLIBM") {
+ } else if (Name == "AMDLIBM") {
if (Triple.getArch() != llvm::Triple::x86 &&
Triple.getArch() != llvm::Triple::x86_64)
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< Name << Triple.getArchName();
+ } else if (Name == "libmvec") {
+ if (Triple.getArch() != llvm::Triple::x86 &&
+ Triple.getArch() != llvm::Triple::x86_64 &&
+ Triple.getArch() != llvm::Triple::aarch64 &&
+ Triple.getArch() != llvm::Triple::aarch64_be)
+ D.Diag(diag::err_drv_unsupported_opt_for_target)
+ << Name << Triple.getArchName();
} else if (Name == "SLEEF" || Name == "ArmPL") {
if (Triple.getArch() != llvm::Triple::aarch64 &&
Triple.getArch() != llvm::Triple::aarch64_be)
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index 35da8323e0a10..bd3a215323de2 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -34,6 +34,7 @@ page](https://llvm.org/releases/).
* -floop-interchange is now recognized by flang.
* -floop-interchange is enabled by default at -O2 and above.
+* -fveclib=libmvec is supported for AArch64 (same as Flang/x86 and Clang/AArch64)
## Windows Support
diff --git a/flang/test/Driver/fveclib-codegen.f90 b/flang/test/Driver/fveclib-codegen.f90
index 4cbb1e284f18e..88b31da40167e 100644
--- a/flang/test/Driver/fveclib-codegen.f90
+++ b/flang/test/Driver/fveclib-codegen.f90
@@ -1,6 +1,8 @@
! test that -fveclib= is passed to the backend
! RUN: %if aarch64-registered-target %{ %flang -S -Ofast -target aarch64-unknown-linux-gnu -fveclib=SLEEF -o - %s | FileCheck %s --check-prefix=SLEEF %}
! RUN: %if x86-registered-target %{ %flang -S -Ofast -target x86_64-unknown-linux-gnu -fveclib=libmvec -o - %s | FileCheck %s %}
+! RUN: %if aarch64-registered-target %{ %flang -S -Ofast -target aarch64-unknown-linux-gnu -fveclib=libmvec -march=armv8.2-a+nosve -o - %s | FileCheck %s --check-prefix=LIBMVEC-AARCH64-NEON %}
+! RUN: %if aarch64-registered-target %{ %flang -S -Ofast -target aarch64-unknown-linux-gnu -fveclib=libmvec -march=armv8.2-a+sve -o - %s | FileCheck %s --check-prefix=LIBMVEC-AARCH64-SVE %}
! RUN: %if x86-registered-target %{ %flang -S -O3 -ffast-math -target x86_64-unknown-linux-gnu -fveclib=AMDLIBM -o - %s | FileCheck %s --check-prefix=AMDLIBM %}
! RUN: %flang -S -Ofast -fveclib=NoLibrary -o - %s | FileCheck %s --check-prefix=NOLIB
@@ -11,6 +13,8 @@ subroutine sb(a, b)
! check that we used a vectorized call to powf()
! CHECK: _ZGVbN4vv_powf
! SLEEF: _ZGVnN4vv_powf
+! LIBMVEC-AARCH64-NEON: _ZGVnN4vv_powf
+! LIBMVEC-AARCH64-SVE: _ZGVsMxvv_powf
! AMDLIBM: amd_vrs4_powf
! NOLIB: powf
a(i) = a(i) ** b(i)
diff --git a/flang/test/Driver/fveclib.f90 b/flang/test/Driver/fveclib.f90
index 431a4bfc02522..d21e85e486f8d 100644
--- a/flang/test/Driver/fveclib.f90
+++ b/flang/test/Driver/fveclib.f90
@@ -1,6 +1,7 @@
! RUN: %flang -### -c -fveclib=none %s 2>&1 | FileCheck -check-prefix CHECK-NOLIB %s
! RUN: %flang -### -c -fveclib=Accelerate %s 2>&1 | FileCheck -check-prefix CHECK-ACCELERATE %s
! RUN: %flang -### -c --target=x86_64-unknown-linux-gnu -fveclib=libmvec %s 2>&1 | FileCheck -check-prefix CHECK-libmvec %s
+! RUN: %flang -### -c --target=aarch64-unknown-linux-gnu -fveclib=libmvec %s 2>&1 | FileCheck -check-prefix CHECK-libmvec %s
! RUN: %flang -### -c -fveclib=MASSV %s 2>&1 | FileCheck -check-prefix CHECK-MASSV %s
! RUN: %flang -### -c -fveclib=Darwin_libsystem_m %s 2>&1 | FileCheck -check-prefix CHECK-DARWIN_LIBSYSTEM_M %s
! RUN: %flang -### -c --target=aarch64-none-none -fveclib=SLEEF %s 2>&1 | FileCheck -check-prefix CHECK-SLEEF %s
@@ -23,7 +24,7 @@
! RUN: not %flang --target=x86-none-none -c -fveclib=SLEEF %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
! RUN: not %flang --target=x86-none-none -c -fveclib=ArmPL %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
-! RUN: not %flang --target=aarch64-none-none -c -fveclib=libmvec %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
+! RUN: not %flang --target=riscv64-none-none -c -fveclib=libmvec %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
! RUN: not %flang --target=aarch64-none-none -c -fveclib=SVML %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
! RUN: not %flang --target=aarch64-none-none -c -fveclib=AMDLIBM %s 2>&1 | FileCheck -check-prefix CHECK-ERROR %s
! CHECK-ERROR: unsupported option {{.*}} for target
>From 943347688a6fd43be367143aa1b7a19587da2573 Mon Sep 17 00:00:00 2001
From: KAWASHIMA Takahiro <t-kawashima at fujitsu.com>
Date: Tue, 1 Jul 2025 20:13:35 +0900
Subject: [PATCH 2/2] Mention the required GLIBC version in help and release
note
---
clang/include/clang/Driver/Options.td | 3 ++-
flang/docs/ReleaseNotes.md | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 9911d752966e3..b4b2ab5f916df 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3487,7 +3487,8 @@ def fno_experimental_isel : Flag<["-"], "fno-experimental-isel">, Group<f_clang_
Alias<fno_global_isel>;
def fveclib : Joined<["-"], "fveclib=">, Group<f_Group>,
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
- HelpText<"Use the given vector functions library">,
+ HelpText<"Use the given vector functions library.\n"
+ " Note: -fveclib=libmvec on AArch64 requires GLIBC 2.40 or newer.">,
HelpTextForVariants<[ClangOption, CC1Option],
"Use the given vector functions library.\n"
" Note: -fveclib={ArmPL,SLEEF,libmvec} implies -fno-math-errno.\n"
diff --git a/flang/docs/ReleaseNotes.md b/flang/docs/ReleaseNotes.md
index bd3a215323de2..b2dbbcb5630f4 100644
--- a/flang/docs/ReleaseNotes.md
+++ b/flang/docs/ReleaseNotes.md
@@ -34,7 +34,8 @@ page](https://llvm.org/releases/).
* -floop-interchange is now recognized by flang.
* -floop-interchange is enabled by default at -O2 and above.
-* -fveclib=libmvec is supported for AArch64 (same as Flang/x86 and Clang/AArch64)
+* -fveclib=libmvec is supported for AArch64 (same as Flang/x86 and
+ Clang/AArch64) (requires GLIBC 2.40 or newer)
## Windows Support
More information about the cfe-commits
mailing list