Skip to content

Commit 2277a93

Browse files
xiangzhaiLeslie Zhai
authored and
Leslie Zhai
committed
Initial loongarch port
Co-authored-by: yangwenqing <[email protected]> Signed-off-by: Leslie Zhai <[email protected]> Signed-off-by: yangwenqing <[email protected]>
1 parent aa8af26 commit 2277a93

31 files changed

+1918
-7
lines changed

CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required (VERSION 3.18.4)
1+
cmake_minimum_required (VERSION 3.13.4)
22

33
project (vectorscan C CXX)
44

@@ -127,6 +127,9 @@ elseif (ARCH_ARM32 OR ARCH_AARCH64)
127127
elseif (ARCH_PPC64EL)
128128
include (${CMAKE_MODULE_PATH}/cflags-ppc64le.cmake)
129129
set(ARCH_FLAG mcpu)
130+
elseif (ARCH_LOONGARCH64)
131+
include (${CMAKE_MODULE_PATH}/cflags-loongarch64.cmake)
132+
set(ARCH_FLAG march)
130133
endif ()
131134

132135
# Detect Native arch flags if requested
@@ -152,6 +155,11 @@ foreach (CONFIG ${CMAKE_BUILD_TYPE} ${CMAKE_CONFIGURATION_TYPES})
152155
string(REGEX REPLACE "-O[^ ]*" "" CMAKE_CXX_FLAGS_${CONFIG} "${CMAKE_CXX_FLAGS_${CONFIG}}")
153156
endforeach ()
154157

158+
if (ARCH_LOONGARCH64)
159+
set(ARCH_C_FLAGS "-mlsx")
160+
set(ARCH_CXX_FLAGS "-mlsx")
161+
endif(ARCH_LOONGARCH64)
162+
155163
message(STATUS "ARCH_C_FLAGS : ${ARCH_C_FLAGS}")
156164
message(STATUS "ARCH_CXX_FLAGS : ${ARCH_CXX_FLAGS}")
157165

@@ -178,7 +186,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ARCH_CXX_FLAGS}")
178186
# PCRE check, we have a fixed requirement for PCRE to use Chimera
179187
# and hscollider
180188
set(PCRE_REQUIRED_MAJOR_VERSION 8)
181-
set(PCRE_REQUIRED_MINOR_VERSION 41)
189+
set(PCRE_REQUIRED_MINOR_VERSION 39)
182190
set(PCRE_REQUIRED_VERSION ${PCRE_REQUIRED_MAJOR_VERSION}.${PCRE_REQUIRED_MINOR_VERSION})
183191
include (${CMAKE_MODULE_PATH}/pcre.cmake)
184192
if (NOT CORRECT_PCRE_VERSION)
@@ -252,6 +260,11 @@ elseif (ARCH_PPC64EL)
252260
set (hs_exec_common_SRCS
253261
${hs_exec_common_SRCS}
254262
src/util/arch/ppc64el/cpuid_flags.c)
263+
elseif (ARCH_LOONGARCH64)
264+
set (hs_exec_common_SRCS
265+
${hs_exec_common_SRCS}
266+
src/util/arch/loongarch64/cpuid_flags.c
267+
)
255268
endif ()
256269

257270
set (hs_exec_SRCS
@@ -410,6 +423,11 @@ set (hs_exec_SRCS
410423
${hs_exec_SRCS}
411424
src/nfa/vermicelli_simd.cpp
412425
src/util/supervector/arch/ppc64el/impl.cpp)
426+
elseif (ARCH_LOONGARCH64)
427+
set (hs_exec_SRCS
428+
${hs_exec_SRCS}
429+
src/nfa/vermicelli_simd.cpp
430+
src/util/supervector/arch/loongarch64/impl.cpp)
413431
endif()
414432

415433
if (ARCH_IA32 OR ARCH_X86_64)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Vectorscan?
22

33
A fork of Intel's Hyperscan, modified to run on more platforms. Currently ARM NEON/ASIMD
4-
is 100% functional, and Power VSX are in development. ARM SVE2 will be implemented when
5-
harwdare becomes accessible to the developers. More platforms will follow in the future,
6-
on demand/request.
4+
is 100% functional, LoongArch LSX is 100% functional, and Power VSX are in development.
5+
ARM SVE2 will be implemented when harwdare becomes accessible to the developers.
6+
More platforms will follow in the future, on demand/request.
77

88
Vectorscan will follow Intel's API and internal algorithms where possible, but will not
99
hesitate to make code changes where it is thought of giving better performance or better

cmake/archdetect.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ else()
8484
elseif(ARCH_ARM32)
8585
set(GNUCC_ARCH armv7a)
8686
set(TUNE_FLAG generic)
87+
elseif(ARCH_LOONGARCH64)
88+
set(GNUCC_ARCH la464)
89+
set(TUNE_FLAG generic)
8790
else()
8891
set(GNUCC_ARCH power9)
8992
set(TUNE_FLAG power9)

cmake/cflags-loongarch64.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
CHECK_INCLUDE_FILE_CXX(lsxintrin.h HAVE_C_LOONGARCH64_LSXINTRIN_H)
3+
4+
if (HAVE_C_LOONGARCH64_LSXINTRIN_H)
5+
set (INTRIN_INC_H "lsxintrin.h")
6+
else()
7+
message (FATAL_ERROR "No intrinsics header found for LSX")
8+
endif ()
9+
10+
set(CMAKE_REQUIRED_FLAGS "-mlsx")
11+
CHECK_C_SOURCE_COMPILES("#include <${INTRIN_INC_H}>
12+
int main() {
13+
__m128i a = __lsx_vreplgr2vr_w(1);
14+
(void)a;
15+
}" HAVE_LSX)
16+
17+
if (NOT HAVE_LSX)
18+
message(FATAL_ERROR "LSX support required for LoongArch support")
19+
endif ()

cmake/config.h.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
/* "Define if building for PPC64EL" */
2525
#cmakedefine ARCH_PPC64EL
2626

27+
/* "Define if building for LOONGARCH64" */
28+
#cmakedefine ARCH_LOONGARCH64
29+
2730
/* "Define if cross compiling for AARCH64" */
2831
#cmakedefine CROSS_COMPILE_AARCH64
2932

@@ -81,6 +84,9 @@
8184
/* C compiler has arm_neon.h */
8285
#cmakedefine HAVE_C_PPC64EL_ALTIVEC_H
8386

87+
/* C compiler has lsxintrin.h */
88+
#cmakedefine HAVE_C_LOONGARCH64_LSXINTRIN_H
89+
8490
/* Define to 1 if you have the declaration of `pthread_setaffinity_np', and to
8591
0 if you don't. */
8692
#cmakedefine HAVE_DECL_PTHREAD_SETAFFINITY_NP

cmake/platform.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ CHECK_C_SOURCE_COMPILES("#if !(defined(__i386__) || defined(_M_IX86))\n#error no
55
CHECK_C_SOURCE_COMPILES("#if !defined(__ARM_ARCH_ISA_A64)\n#error not 64bit\n#endif\nint main(void) { return 0; }" ARCH_AARCH64)
66
CHECK_C_SOURCE_COMPILES("#if !defined(__ARM_ARCH_ISA_ARM)\n#error not 32bit\n#endif\nint main(void) { return 0; }" ARCH_ARM32)
77
CHECK_C_SOURCE_COMPILES("#if !defined(__PPC64__) && !(defined(__LITTLE_ENDIAN__) && defined(__VSX__))\n#error not ppc64el\n#endif\nint main(void) { return 0; }" ARCH_PPC64EL)
8-
if (ARCH_X86_64 OR ARCH_AARCH64 OR ARCH_PPC64EL)
8+
CHECK_C_SOURCE_COMPILES("#if !(defined(__loongarch_lp64) || defined( __loongarch64))\n#error not 64bit\n#endif\nint main(void) { return 0; }" ARCH_LOONGARCH64)
9+
if (ARCH_X86_64 OR ARCH_AARCH64 OR ARCH_PPC64EL OR ARCH_LOONGARCH64)
910
set(ARCH_64_BIT TRUE)
1011
else()
1112
set(ARCH_32_BIT TRUE)

src/hs.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#if defined(ARCH_IA32) || defined(ARCH_X86_64)
4949
#include "util/arch/x86/cpuid_inline.h"
5050
#elif defined(ARCH_ARM32) || defined(ARCH_AARCH64)
51+
#elif defined(ARCH_LOONGARCH64)
5152
#endif
5253
#include "util/depth.h"
5354
#include "util/popcount.h"

src/hs_valid_platform.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ hs_error_t HS_CDECL hs_valid_platform(void) {
5252
}
5353
#elif defined(ARCH_PPC64EL)
5454
return HS_SUCCESS;
55+
#elif defined(ARCH_LOONGARCH64)
56+
return HS_SUCCESS;
5557
#endif
5658
}

src/nfa/loongarch64/shufti.hpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2015-2017, Intel Corporation
3+
* Copyright (c) 2020-2021, VectorCamp PC
4+
* Copyright (c) 2023, Loongson Technology
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of Intel Corporation nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/** \file
32+
* \brief Shufti: character class acceleration.
33+
*/
34+
35+
template <uint16_t S>
36+
static really_inline
37+
const SuperVector<S> blockSingleMask(SuperVector<S> mask_lo, SuperVector<S> mask_hi, SuperVector<S> chars) {
38+
const SuperVector<S> low4bits = SuperVector<S>::dup_u8(0xf);
39+
40+
SuperVector<S> c_lo = chars & low4bits;
41+
SuperVector<S> c_hi = chars.template vshr_8_imm<4>();
42+
c_lo = mask_lo.template pshufb<false>(c_lo);
43+
c_hi = mask_hi.template pshufb<false>(c_hi);
44+
45+
return (c_lo & c_hi) > (SuperVector<S>::Zeroes());
46+
}
47+
48+
template <uint16_t S>
49+
static really_inline
50+
SuperVector<S> blockDoubleMask(SuperVector<S> mask1_lo, SuperVector<S> mask1_hi, SuperVector<S> mask2_lo, SuperVector<S> mask2_hi, SuperVector<S> chars) {
51+
52+
const SuperVector<S> low4bits = SuperVector<S>::dup_u8(0xf);
53+
SuperVector<S> chars_lo = chars & low4bits;
54+
chars_lo.print8("chars_lo");
55+
SuperVector<S> chars_hi = chars.template vshr_64_imm<4>() & low4bits;
56+
chars_hi.print8("chars_hi");
57+
SuperVector<S> c1_lo = mask1_lo.template pshufb<true>(chars_lo);
58+
c1_lo.print8("c1_lo");
59+
SuperVector<S> c1_hi = mask1_hi.template pshufb<true>(chars_hi);
60+
c1_hi.print8("c1_hi");
61+
SuperVector<S> t1 = c1_lo | c1_hi;
62+
t1.print8("t1");
63+
64+
SuperVector<S> c2_lo = mask2_lo.template pshufb<true>(chars_lo);
65+
c2_lo.print8("c2_lo");
66+
SuperVector<S> c2_hi = mask2_hi.template pshufb<true>(chars_hi);
67+
c2_hi.print8("c2_hi");
68+
SuperVector<S> t2 = c2_lo | c2_hi;
69+
t2.print8("t2");
70+
t2.template vshr_128_imm<1>().print8("t2.vshr_128(1)");
71+
SuperVector<S> t = t1 | (t2.template vshr_128_imm<1>());
72+
t.print8("t");
73+
74+
return !t.eq(SuperVector<S>::Ones());
75+
}

src/nfa/loongarch64/truffle.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2015-2017, Intel Corporation
3+
* Copyright (c) 2020-2021, VectorCamp PC
4+
* Copyright (c) 2023, Loongson Technology
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
* * Neither the name of Intel Corporation nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/** \file
32+
* \brief Truffle: character class acceleration.
33+
*
34+
*/
35+
36+
template <uint16_t S>
37+
static really_inline
38+
const SuperVector<S> blockSingleMask(SuperVector<S> shuf_mask_lo_highclear, SuperVector<S> shuf_mask_lo_highset, SuperVector<S> chars) {
39+
40+
chars.print8("chars");
41+
shuf_mask_lo_highclear.print8("shuf_mask_lo_highclear");
42+
shuf_mask_lo_highset.print8("shuf_mask_lo_highset");
43+
44+
SuperVector<S> highconst = SuperVector<S>::dup_u8(0x80);
45+
highconst.print8("highconst");
46+
SuperVector<S> shuf_mask_hi = SuperVector<S>::dup_u64(0x8040201008040201);
47+
shuf_mask_hi.print8("shuf_mask_hi");
48+
49+
SuperVector<S> shuf1 = shuf_mask_lo_highclear.pshufb(chars);
50+
shuf1.print8("shuf1");
51+
SuperVector<S> t1 = chars ^ highconst;
52+
t1.print8("t1");
53+
SuperVector<S> shuf2 = shuf_mask_lo_highset.pshufb(t1);
54+
shuf2.print8("shuf2");
55+
SuperVector<S> t2 = highconst.opandnot(chars.template vshr_64_imm<4>());
56+
t2.print8("t2");
57+
SuperVector<S> shuf3 = shuf_mask_hi.pshufb(t2);
58+
shuf3.print8("shuf3");
59+
SuperVector<S> res = (shuf1 | shuf2) & shuf3;
60+
res.print8("(shuf1 | shuf2) & shuf3");
61+
62+
return !res.eq(SuperVector<S>::Zeroes());
63+
}

0 commit comments

Comments
 (0)