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

feat: support AArch64 for native funcs #526

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-linux-arm64.yml
@@ -1,6 +1,6 @@
name: Benchmark Linux-ARM

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/benchmark-linux-x64.yml
@@ -1,6 +1,6 @@
name: Benchmark Linux-X64

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compatibility_test-windows.yml
@@ -1,6 +1,6 @@
name: Compatibility Test Windows-X64

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compatibility_test.yml
@@ -1,6 +1,6 @@
name: Compatibility Test Linux-X64|ARM

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fuzzing-linux-x64.yml
@@ -1,6 +1,6 @@
name: Fuzz Test Linux-X64

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/generic_test.yml
@@ -1,6 +1,6 @@
name: Generic Test Go1.18-Linux-X64

on: pull_request
on: push

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/license-check.yml
@@ -1,6 +1,6 @@
name: License Check

on: pull_request
on: push

jobs:
build:
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
@@ -1,3 +1,7 @@
[submodule "tools/asm2asm"]
path = tools/asm2asm
url = https://github.com/chenzhuoyu/asm2asm

[submodule "tools/simde"]
path = tools/simde
url = https://github.com/simd-everywhere/simde
55 changes: 31 additions & 24 deletions Makefile
Expand Up @@ -22,28 +22,35 @@ SRC_FILE := native/native.c
CPU_avx := amd64
CPU_avx2 := amd64
CPU_sse := amd64
CPU_neon := arm64

TMPL_avx := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
TMPL_avx2 := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
TMPL_sse := fastint_amd64_test fastfloat_amd64_test native_amd64_test recover_amd64_test
TMPL_amd64 := fastint_test fastfloat_test native_test recover_amd64_test
TMPL_arm64 := fastint_test fastfloat_test native_test recover_arm64_test native_export_arm64

CFLAGS_avx := -msse -mno-sse4 -mavx -mpclmul -mno-avx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=0
CFLAGS_avx2 := -msse -mno-sse4 -mavx -mpclmul -mavx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=1
CFLAGS_sse := -msse -mno-sse4 -mno-avx -mno-avx2 -mpclmul

CC_amd64 := clang
ASM2ASM_amd64 := tools/asm2asm/asm2asm.py
ASM2ASM_arm64 := tools/asm2arm/arm.py

ASM_FLAGS_amd64 := -r
ASM_FLAGS_arm64 :=

ASM_OUT_amd64 := native_text_amd64.go
ASM_OUT_arm64 := native_arm64.s

CC := clang

CFLAGS := -mno-red-zone
CFLAGS += -target x86_64-apple-macos11
CFLAGS += -fno-asynchronous-unwind-tables
CFLAGS += -fno-builtin
CFLAGS += -fno-exceptions
CFLAGS += -fno-rtti
CFLAGS += -fno-stack-protector
CFLAGS += -nostdlib
CFLAGS += -O3
CFLAGS += -Wall -Werror
# CFLAGS += -Wall -Werror
CFLAGS_avx := -target x86_64-apple-macos11 -msse -mno-sse4 -mavx -mpclmul -mno-avx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=0
CFLAGS_avx2 := -target x86_64-apple-macos11 -msse -mno-sse4 -mavx -mpclmul -mavx2 -mstack-alignment=0 -DUSE_AVX=1 -DUSE_AVX2=1
CFLAGS_sse := -target x86_64-apple-macos11 -msse -mno-sse4 -mno-avx -mno-avx2 -mpclmul
CFLAGS_neon := -target aarch64-apple-macos11 -march=armv8-a+simd -Itools/simde/simde

NATIVE_SRC := $(wildcard native/*.h)
NATIVE_SRC += $(wildcard native/*.c)
Expand All @@ -63,19 +70,12 @@ ${@dest}: ${@tmpl}
endef

define build_arch
$(eval @cpu := $(value CPU_$(1)))
$(eval @deps := $(foreach tmpl,$(value TMPL_$(1)),${OUT_DIR}/$(1)/${tmpl}.go))
$(eval @asmin := ${TMP_DIR}/$(1)/native.s)
$(eval @asmout := ${OUT_DIR}/$(1)/native_text_${@cpu}.go)
$(eval @stubin := ${OUT_DIR}/native_${@cpu}.tmpl)
$(eval @stubout := ${OUT_DIR}/$(1)/native_${@cpu}.go)

$(1): ${@asmout} ${@deps}

${@asmout}: ${@stubout} ${NATIVE_SRC}
mkdir -p ${TMP_DIR}/$(1)
$${CC_${@cpu}} $${CFLAGS} $${CFLAGS_$(1)} -S -o ${TMP_DIR}/$(1)/native.s ${SRC_FILE}
python3 $${ASM2ASM_${@cpu}} -r ${@stubout} ${TMP_DIR}/$(1)/native.s
$(eval @cpu := $(value CPU_$(1)))
$(eval @deps := $(foreach tmpl,$(value TMPL_${@cpu}),${OUT_DIR}/$(1)/${tmpl}.go))
$(eval @asmin := ${TMP_DIR}/$(1)/native.s)
$(eval @asmout := ${OUT_DIR}/$(1)/$(value ASM_OUT_${@cpu}))
$(eval @stubin := ${OUT_DIR}/native_${@cpu}.tmpl)
$(eval @stubout := ${OUT_DIR}/$(1)/native_${@cpu}.go)

$(eval $(call \
build_tmpl, \
Expand All @@ -84,9 +84,16 @@ $(eval $(call \
${@stubout} \
))

$(1): ${@asmout} ${@deps}

${@asmout}: ${@stubout} ${NATIVE_SRC}
mkdir -p ${TMP_DIR}/$(1)
$${CC} $${CFLAGS} $${CFLAGS_$(1)} -S -o ${TMP_DIR}/$(1)/native.s ${SRC_FILE}
python3 $${ASM2ASM_${@cpu}} $${ASM_FLAGS_${@cpu}} ${@stubout} ${TMP_DIR}/$(1)/native.s

$(foreach \
tmpl, \
$(value TMPL_$(1)), \
$(value TMPL_${@cpu}), \
$(eval $(call \
build_tmpl, \
$(1), \
Expand Down
11 changes: 1 addition & 10 deletions ast/api_amd64.go → ast/api.go
@@ -1,4 +1,4 @@
// +build amd64,go1.16,!go1.23
// +build amd64,go1.16,!go1.23 arm64,go1.20,!go1.23

/*
* Copyright 2022 ByteDance Inc.
Expand Down Expand Up @@ -27,7 +27,6 @@ import (
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
uq `github.com/bytedance/sonic/unquote`
`github.com/chenzhuoyu/base64x`
)

var typeByte = rt.UnpackEface(byte(0)).Type
Expand Down Expand Up @@ -77,14 +76,6 @@ func unquote(src string) (string, types.ParsingError) {
return uq.String(src)
}

func decodeBase64(src string) ([]byte, error) {
return base64x.StdEncoding.DecodeString(src)
}

func encodeBase64(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}

func (self *Parser) decodeValue() (val types.JsonState) {
sv := (*rt.GoString)(unsafe.Pointer(&self.s))
flag := types.F_USE_NUMBER
Expand Down
154 changes: 76 additions & 78 deletions ast/api_compat.go
@@ -1,111 +1,109 @@
// +build !amd64 !go1.16 go1.23
// +build !amd64,!arm64 go1.23 !go1.16 arm64,!go1.20

/*
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2022 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ast

import (
`encoding/base64`
`encoding/json`
`encoding/json`

`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
`github.com/bytedance/sonic/internal/native/types`
`github.com/bytedance/sonic/internal/rt`
)

func init() {
println("WARNING: sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
println("WARNING:(ast) sonic only supports Go1.16~1.21, but your environment is not suitable")
}

func quote(buf *[]byte, val string) {
quoteString(buf, val)
quoteString(buf, val)
}

func unquote(src string) (string, types.ParsingError) {
sp := rt.IndexChar(src, -1)
out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
if !ok {
return "", types.ERR_INVALID_ESCAPE
}
return rt.Mem2Str(out), 0
sp := rt.IndexChar(src, -1)
out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2))
if !ok {
return "", types.ERR_INVALID_ESCAPE
}
return rt.Mem2Str(out), 0
}

func decodeBase64(src string) ([]byte, error) {
return base64.StdEncoding.DecodeString(src)
}

func encodeBase64(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}

func (self *Parser) decodeValue() (val types.JsonState) {
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
if e < 0 {
return v
}
self.p = e
return v
e, v := decodeValue(self.s, self.p, self.dbuf == nil)
if e < 0 {
return v
}
self.p = e
return v
}

func (self *Parser) skip() (int, types.ParsingError) {
e, s := skipValue(self.s, self.p)
if e < 0 {
return self.p, types.ParsingError(-e)
}
self.p = e
return s, 0
e, s := skipValue(self.s, self.p)
if e < 0 {
return self.p, types.ParsingError(-e)
}
self.p = e
return s, 0
}

func (self *Parser) skipFast() (int, types.ParsingError) {
e, s := skipValueFast(self.s, self.p)
if e < 0 {
return self.p, types.ParsingError(-e)
}
self.p = e
return s, 0
e, s := skipValueFast(self.s, self.p)
if e < 0 {
return self.p, types.ParsingError(-e)
}
self.p = e
return s, 0
}

func (self *Node) encodeInterface(buf *[]byte) error {
out, err := json.Marshal(self.packAny())
if err != nil {
return err
}
*buf = append(*buf, out...)
return nil
out, err := json.Marshal(self.packAny())
if err != nil {
return err
}
*buf = append(*buf, out...)
return nil
}

func (self *Parser) getByPath(path ...interface{}) (int, types.ParsingError) {
var err types.ParsingError
for _, p := range path {
if idx, ok := p.(int); ok && idx >= 0 {
if err = self.searchIndex(idx); err != 0 {
return -1, err
}
} else if key, ok := p.(string); ok {
if err = self.searchKey(key); err != 0 {
return -1, err
}
} else {
panic("path must be either int(>=0) or string")
}
}
var err types.ParsingError

var start int
if start, err = self.skip(); err != 0 {
return -1, err
}
return start, 0
}
for _, p := range path {
if idx, ok := p.(int); ok && idx >= 0 {
if err = self.searchIndex(idx); err != 0 {
return -1, err
}
} else if key, ok := p.(string); ok {
if err = self.searchKey(key); err != 0 {
return -1, err
}
} else {
panic("path must be either int(>=0) or string")
}
}

var start = self.p
if start, err = self.skip(); err != 0 {
return -1, err
}
ns := len(self.s)
if self.p > ns || start >= ns || start>=self.p {
return -1, types.ERR_EOF
}

return start, 0
}