Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8312425: [vectorapi] AArch64: Optimize vector math operations with SLEEF #16234

Closed
wants to merge 13 commits into from
Closed
16 changes: 16 additions & 0 deletions doc/building.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ <h1 class="title">Building the JDK</h1>
<li><a href="#x11" id="toc-x11">X11</a></li>
<li><a href="#alsa" id="toc-alsa">ALSA</a></li>
<li><a href="#libffi" id="toc-libffi">libffi</a></li>
<li><a href="#libsleef" id="toc-libsleef">libsleef</a></li>
</ul></li>
<li><a href="#build-tools-requirements"
id="toc-build-tools-requirements">Build Tools Requirements</a>
Expand Down Expand Up @@ -749,6 +750,21 @@ <h3 id="libffi">libffi</h3>
</ul>
<p>Use <code>--with-libffi=&lt;path&gt;</code> if <code>configure</code>
does not properly locate your libffi files.</p>
<h3 id="libsleef">libsleef</h3>
<p>libsleef, the <a href="https://sleef.org/">SIMD Library for
Evaluating Elementary Functions</a> is required when building
libvmath.so on Linux/aarch64 platforms.</p>
<ul>
<li>To install on an apt-based Linux, try running
<code>sudo apt-get install libsleef-dev</code>.</li>
<li>To install on an rpm-based Linux, try running
<code>sudo yum install sleef-devel</code>.</li>
</ul>
<p>Use <code>--with-libsleef=&lt;path&gt;</code> if
<code>configure</code> does not properly locate your libsleef files.
This is optional. If libsleef is neither installed nor specified
manually, the JDK build will succeed but without libvmath.so and the
performance enhancements provided by it.</p>
<h2 id="build-tools-requirements">Build Tools Requirements</h2>
<h3 id="autoconf">Autoconf</h3>
<p>The JDK requires <a
Expand Down
16 changes: 16 additions & 0 deletions doc/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,22 @@ Hotspot.
Use `--with-libffi=<path>` if `configure` does not properly locate your libffi
files.

### libsleef
XiaohongGong marked this conversation as resolved.
Show resolved Hide resolved

libsleef, the [SIMD Library for Evaluating Elementary Functions](
https://sleef.org/) is required when building libvmath.so on Linux/aarch64
XiaohongGong marked this conversation as resolved.
Show resolved Hide resolved
platforms.

* To install on an apt-based Linux, try running `sudo apt-get install
libsleef-dev`.
* To install on an rpm-based Linux, try running `sudo yum install
sleef-devel`.

Use `--with-libsleef=<path>` if `configure` does not properly locate your libsleef
files. This is optional. If libsleef is neither installed nor specified
manually, the JDK build will succeed but without libvmath.so and the
performance enhancements provided by it.

## Build Tools Requirements

### Autoconf
Expand Down
32 changes: 32 additions & 0 deletions make/autoconf/flags-cflags.m4
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,38 @@ AC_DEFUN([FLAGS_SETUP_CFLAGS_CPU_DEP],
IF_FALSE: [$2FDLIBM_CFLAGS=""])
fi
AC_SUBST($2FDLIBM_CFLAGS)

# Check whether the compiler supports the Arm C Language Extensions (ACLE)
# for SVE. Set SVE_CFLAGS to -march=armv8-a+sve if it does. Empty otherwise.
# ACLE and this flag are required to build the Arm SVE related functions in
# libvmath.
if test "x${OPENJDK_TARGET_CPU}" = "xaarch64"; then
if test "x$TOOLCHAIN_TYPE" = xgcc || test "x$TOOLCHAIN_TYPE" = xclang; then
AC_LANG_PUSH(C)
OLD_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -march=armv8-a+sve"

AC_MSG_CHECKING([if Arm SVE ACLE is supported])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([#include <arm_sve.h>],
[
svint32_t r = svdup_n_s32(1);
return 0;
])],
[
AC_MSG_RESULT([yes])
$2SVE_CFLAGS="-march=armv8-a+sve"
],
[
AC_MSG_RESULT([no])
$2SVE_CFLAGS=""
]
)

CFLAGS="$OLD_CFLAGS"
AC_LANG_POP(C)
fi
fi
AC_SUBST($2SVE_CFLAGS)
])

# FLAGS_SETUP_GCC6_COMPILER_FLAGS([PREFIX])
Expand Down
6 changes: 5 additions & 1 deletion make/autoconf/help.m4
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -119,6 +119,8 @@ apt_help() {
PKGHANDLER_COMMAND="sudo apt-get install systemtap-sdt-dev" ;;
capstone)
PKGHANDLER_COMMAND="sudo apt-get install libcapstone-dev" ;;
sleef)
PKGHANDLER_COMMAND="sudo apt-get install libsleef-dev" ;;
esac
}

Expand Down Expand Up @@ -161,6 +163,8 @@ yum_help() {
PKGHANDLER_COMMAND="sudo yum install libXtst-devel libXt-devel libXrender-devel libXrandr-devel libXi-devel" ;;
ccache)
PKGHANDLER_COMMAND="sudo yum install ccache" ;;
sleef)
PKGHANDLER_COMMAND="sudo yum install sleef-devel" ;;
esac
}

Expand Down
101 changes: 101 additions & 0 deletions make/autoconf/lib-sleef.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#
# Copyright (c) 2023, Arm Limited. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation. Oracle designates this
# particular file as subject to the "Classpath" exception as provided
# by Oracle in the LICENSE file that accompanied this code.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#

###############################################################################
#
# Setup sleef framework
#
AC_DEFUN_ONCE([LIB_SETUP_SLEEF],
[
AC_ARG_WITH(libsleef, [AS_HELP_STRING([--with-libsleef],
[specify prefix directory for the libsleef library
(expecting the libraries under PATH/lib and the headers under PATH/include)])])

ENABLE_LIBSLEEF=false
LIBSLEEF_CFLAGS=
LIBSLEEF_LIBS=

if test "x$NEEDS_LIB_SLEEF" = xfalse; then
if test "x${with_libsleef}" != "x" &&
test "x${with_libsleef}" != "xno"; then
AC_MSG_WARN([[libsleef is not used, so --with-libsleef is ignored]])
fi
else
LIBSLEEF_FOUND=no

if test "x${with_libsleef}" = "xno"; then
AC_MSG_NOTICE([libsleef is disabled])
else
if test "x${with_libsleef}" != "x" &&
test "x${with_libsleef}" != "xyes"; then
# Check the specified libsleef
AC_MSG_CHECKING([for the specified LIBSLEEF])
if test -e ${with_libsleef}/lib/libsleef.so &&
test -e ${with_libsleef}/include/sleef.h; then
XiaohongGong marked this conversation as resolved.
Show resolved Hide resolved
LIBSLEEF_FOUND=yes
LIBSLEEF_LIBS="-L${with_libsleef}/lib -lsleef"
LIBSLEEF_CFLAGS="-I${with_libsleef}/include"
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
AC_MSG_ERROR([Could not locate libsleef.so or sleef.h in ${with_libsleef}])
fi
else
# Check for the libsleef under system locations
# Do not try pkg-config if we have a sysroot set.
if test "x$SYSROOT" = "x" &&
test "x${LIBSLEEF_FOUND}" = "xno"; then
PKG_CHECK_MODULES([LIBSLEEF], [sleef], [LIBSLEEF_FOUND=yes], [LIBSLEEF_FOUND=no])
fi
if test "x${LIBSLEEF_FOUND}" = "xno"; then
AC_CHECK_HEADERS([sleef.h],
[
LIBSLEEF_FOUND=yes
LIBSLEEF_LIBS="-lsleef"
],
[]
)
fi

# Print error if user runs just with "--with-libsleef", but libsleef is not installed
if test "x${with_libsleef}" = "xyes" &&
test "x${LIBSLEEF_FOUND}" = "xno"; then
HELP_MSG_MISSING_DEPENDENCY([sleef])
AC_MSG_ERROR([Could not find libsleef! $HELP_MSG])
fi
fi

if test "x${LIBSLEEF_FOUND}" = "xyes"; then
ENABLE_LIBSLEEF=true
fi

fi

fi

AC_SUBST(ENABLE_LIBSLEEF)
AC_SUBST(LIBSLEEF_CFLAGS)
AC_SUBST(LIBSLEEF_LIBS)
])
10 changes: 10 additions & 0 deletions make/autoconf/libraries.m4
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ m4_include([lib-ffi.m4])
m4_include([lib-fontconfig.m4])
m4_include([lib-freetype.m4])
m4_include([lib-hsdis.m4])
m4_include([lib-sleef.m4])
m4_include([lib-std.m4])
m4_include([lib-x11.m4])

Expand Down Expand Up @@ -87,6 +88,14 @@ AC_DEFUN_ONCE([LIB_DETERMINE_DEPENDENCIES],
else
NEEDS_LIB_FFI=false
fi

# Check if sleef is needed
if test "x$OPENJDK_TARGET_OS" = xlinux && test "x$OPENJDK_TARGET_CPU" = xaarch64; then
NEEDS_LIB_SLEEF=true
else
# All other instances do not need sleef
NEEDS_LIB_SLEEF=false
fi
])

################################################################################
Expand Down Expand Up @@ -125,6 +134,7 @@ AC_DEFUN_ONCE([LIB_SETUP_LIBRARIES],
LIB_SETUP_HSDIS
LIB_SETUP_LIBFFI
LIB_SETUP_MISC_LIBS
LIB_SETUP_SLEEF
LIB_SETUP_X11

LIB_TESTS_SETUP_GTEST
Expand Down
6 changes: 6 additions & 0 deletions make/autoconf/spec.gmk.in
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ USE_EXTERNAL_LIBPNG:=@USE_EXTERNAL_LIBPNG@
PNG_LIBS:=@PNG_LIBS@
PNG_CFLAGS:=@PNG_CFLAGS@

ENABLE_LIBSLEEF:=@ENABLE_LIBSLEEF@
XiaohongGong marked this conversation as resolved.
Show resolved Hide resolved
LIBSLEEF_CFLAGS:=@LIBSLEEF_CFLAGS@
LIBSLEEF_LIBS:=@LIBSLEEF_LIBS@
####################################################
#
# Misc
Expand All @@ -902,6 +905,9 @@ OS_VERSION_MAJOR:=@OS_VERSION_MAJOR@
OS_VERSION_MINOR:=@OS_VERSION_MINOR@
OS_VERSION_MICRO:=@OS_VERSION_MICRO@

# Arm SVE
SVE_CFLAGS:=@SVE_CFLAGS@

# Images directory definitions
JDK_IMAGE_SUBDIR:=jdk
JRE_IMAGE_SUBDIR:=jre
Expand Down
14 changes: 13 additions & 1 deletion make/modules/jdk.incubator.vector/Lib.gmk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -39,4 +39,16 @@ ifeq ($(call isTargetOs, linux windows)+$(call isTargetCpu, x86_64)+$(INCLUDE_CO
TARGETS += $(BUILD_LIBJSVML)
endif

ifeq ($(call isTargetOs, linux)+$(call isTargetCpu, aarch64)+$(INCLUDE_COMPILER2)+$(ENABLE_LIBSLEEF), true+true+true+true)
$(eval $(call SetupJdkLibrary, BUILD_LIBVMATH, \
NAME := vmath, \
CFLAGS := $(CFLAGS_JDKLIB) $(LIBSLEEF_CFLAGS) $(SVE_CFLAGS) -fvisibility=default, \
LDFLAGS := $(LDFLAGS_JDKLIB) \
$(call SET_SHARED_LIBRARY_ORIGIN), \
LIBS := $(JDKLIB_LIBS) $(LIBSLEEF_LIBS) \
))

TARGETS += $(BUILD_LIBVMATH)
endif

################################################################################
28 changes: 24 additions & 4 deletions src/hotspot/cpu/aarch64/aarch64.ad
Original file line number Diff line number Diff line change
Expand Up @@ -2326,14 +2326,18 @@ const TypeVectMask* Matcher::predicate_reg_type(const Type* elemTy, int length)
return new TypeVectMask(elemTy, length);
}

// Vector calling convention not yet implemented.
bool Matcher::supports_vector_calling_convention(void) {
return false;
return EnableVectorSupport && UseVectorStubs;
}

OptoRegPair Matcher::vector_return_value(uint ideal_reg) {
Unimplemented();
return OptoRegPair(0, 0);
assert(EnableVectorSupport && UseVectorStubs, "sanity");
int lo = V0_num;
int hi = V0_H_num;
if (ideal_reg == Op_VecX || ideal_reg == Op_VecA) {
hi = V0_K_num;
}
return OptoRegPair(hi, lo);
}

// Is this branch offset short enough that a short branch can be used?
Expand Down Expand Up @@ -16548,6 +16552,22 @@ instruct CallLeafDirect(method meth)
ins_pipe(pipe_class_call);
%}

// Call Runtime Instruction without safepoint and with vector arguments
instruct CallLeafDirectVector(method meth)
%{
match(CallLeafVector);

effect(USE meth);

ins_cost(CALL_COST);

format %{ "CALL, runtime leaf vector $meth" %}

ins_encode(aarch64_enc_java_to_runtime(meth));

ins_pipe(pipe_class_call);
%}

// Call Runtime Instruction

instruct CallLeafNoFPDirect(method meth)
Expand Down
15 changes: 14 additions & 1 deletion src/hotspot/cpu/aarch64/sharedRuntime_aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,20 @@ static int c_calling_convention_priv(const BasicType *sig_bt,
int SharedRuntime::vector_calling_convention(VMRegPair *regs,
uint num_bits,
uint total_args_passed) {
Unimplemented();
// More than 8 argument inputs are not supported now.
assert(total_args_passed <= Argument::n_float_register_parameters_c, "unsupported");
assert(num_bits >= 64 && num_bits <= 2048 && is_power_of_2(num_bits), "unsupported");

static const FloatRegister VEC_ArgReg[Argument::n_float_register_parameters_c] = {
v0, v1, v2, v3, v4, v5, v6, v7
};

// On SVE, we use the same vector registers with 128-bit vector registers on NEON.
int next_reg_val = num_bits == 64 ? 1 : 3;
for (uint i = 0; i < total_args_passed; i++) {
VMReg vmreg = VEC_ArgReg[i]->as_VMReg();
regs[i].set_pair(vmreg->next(next_reg_val), vmreg);
}
return 0;
}

Expand Down
Loading