Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ jobs:
run: |
make build/libllhttp.a

- name: Run dispatch test
shell: bash
run: |
make test-c

- name: Build WebAssembly
run: npm run build-wasm
if: runner.os == 'Linux'
Expand Down
19 changes: 19 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ set(LLHTTP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/api.c
)

# On x86-64, compile a second instantiation of the parser with SSE4.2 enabled so
# llhttp_execute() can select it at runtime (see src/api.c). The default build
# passes no -march, so the SSE4.2 span scanners in the generated code are
# otherwise never compiled in. Skipped on MSVC (no __builtin_cpu_supports).
if(NOT MSVC AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64)$")
list(APPEND LLHTTP_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/llhttp_sse42.c)
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/llhttp_sse42.c
PROPERTIES COMPILE_OPTIONS "-msse4.2"
)
endif()

set(LLHTTP_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/llhttp.h
)
Expand All @@ -48,6 +60,13 @@ configure_file(
function(config_library target)
target_sources(${target} PRIVATE ${LLHTTP_SOURCES} ${LLHTTP_HEADERS})

# Enable the runtime SSE4.2 dispatch in api.c only where the second object
# (llhttp_sse42.c) is also compiled, so a build without it keeps the baseline
# path and does not reference llhttp__internal_execute_sse42.
if(NOT MSVC AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64)$")
target_compile_definitions(${target} PRIVATE LLHTTP_SSE42_DISPATCH)
endif()

target_include_directories(${target} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
Expand Down
40 changes: 36 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@ endif

INCLUDES += -Ibuild/

# On x86-64 (non-Windows), also build a second instantiation of the generated
# parser with -msse4.2 so llhttp_execute() can select the SSE4.2 span scanners
# at runtime (see src/native/api.c). The default build passes no -march, so the
# SSE4.2 code generated under `#ifdef __SSE4_2__` is otherwise never compiled
# in. LLHTTP_SSE42_DISPATCH enables the dispatch in api.c and is defined only
# here, where the second object is also built. Detect the *target* from the
# compiler, not the host (uname -m), so a cross-compile (e.g. CLANG with
# --target=aarch64-...) does not add an x86-only object. Windows/MSVC is
# skipped (no __builtin_cpu_supports), matching the CMake and gyp builds.
TARGET_MACHINE ?= $(shell $(CLANG) $(CFLAGS) -dumpmachine 2>/dev/null)
TARGET_CPU := $(firstword $(subst -, ,$(TARGET_MACHINE)))
IS_WINDOWS := $(or $(findstring windows,$(TARGET_MACHINE)),$(findstring mingw,$(TARGET_MACHINE)))
ifeq (,$(IS_WINDOWS))
ifneq (,$(filter $(TARGET_CPU),x86_64 amd64))
SSE42_OBJ = build/c/llhttp_sse42.o
CFLAGS += -DLLHTTP_SSE42_DISPATCH
endif
endif

INSTALL ?= install
PREFIX ?= /usr/local
LIBDIR = $(PREFIX)/lib
Expand All @@ -23,17 +42,20 @@ clean:
rm -rf build/
rm -rf test/tmp

build/libllhttp.so: build/c/llhttp.o build/native/api.o \
build/libllhttp.so: build/c/llhttp.o $(SSE42_OBJ) build/native/api.o \
build/native/http.o
$(CLANG) -shared $^ -o $@

build/libllhttp.a: build/c/llhttp.o build/native/api.o \
build/libllhttp.a: build/c/llhttp.o $(SSE42_OBJ) build/native/api.o \
build/native/http.o
$(AR) rcs $@ build/c/llhttp.o build/native/api.o build/native/http.o
$(AR) rcs $@ build/c/llhttp.o $(SSE42_OBJ) build/native/api.o build/native/http.o

build/c/llhttp.o: build/c/llhttp.c
$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@

build/c/llhttp_sse42.o: build/c/llhttp_sse42.c build/c/llhttp.c
$(CLANG) $(CFLAGS) -msse4.2 $(INCLUDES) -c $< -o $@

build/native/%.o: src/native/%.c build/llhttp.h src/native/api.h \
build/native
$(CLANG) $(CFLAGS) $(INCLUDES) -c $< -o $@
Expand All @@ -52,6 +74,8 @@ release: clean all generate
mkdir -p release/include
cp -rf build/llhttp.h release/include/
cp -rf build/c/llhttp.c release/src/
cp -rf build/c/llhttp_sse42.c release/src/
cp -rf build/c/llhttp_sse42_rename.h release/src/
cp -rf src/native/*.c release/src/
cp -rf src/llhttp.gyp release/
cp -rf src/common.gypi release/
Expand Down Expand Up @@ -87,11 +111,19 @@ postversion: release
generate:
node --import tsx bin/generate.ts

# Links a small C program against the built library (which contains api.c and,
# on x86-64, both parser instantiations) and drives the runtime dispatch added
# in src/native/api.c. The markdown test suite never compiles api.c.
test-c: build/libllhttp.a
$(CLANG) $(CFLAGS) $(INCLUDES) test/dispatch/dispatch_test.c \
build/libllhttp.a -o build/dispatch_test
./build/dispatch_test

install: build/libllhttp.a build/libllhttp.so
$(INSTALL) -d $(DESTDIR)$(INCLUDEDIR)
$(INSTALL) -d $(DESTDIR)$(LIBDIR)
$(INSTALL) -C build/llhttp.h $(DESTDIR)$(INCLUDEDIR)/llhttp.h
$(INSTALL) -C build/libllhttp.a $(DESTDIR)$(LIBDIR)/libllhttp.a
$(INSTALL) build/libllhttp.so $(DESTDIR)$(LIBDIR)/libllhttp.so

.PHONY: all generate clean release postversion github-release
.PHONY: all generate clean release postversion github-release test-c
31 changes: 31 additions & 0 deletions bin/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const semverRE = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\

const C_FILE = resolve(__dirname, '../build/c/llhttp.c');
const HEADER_FILE = resolve(__dirname, '../build/llhttp.h');
const SSE42_RENAME_FILE = resolve(__dirname, '../build/c/llhttp_sse42_rename.h');
const SSE42_C_FILE = resolve(__dirname, '../build/c/llhttp_sse42.c');

const pkg = JSON.parse(
readFileSync(resolve(__dirname, '..', 'package.json')).toString(),
Expand Down Expand Up @@ -49,3 +51,32 @@ ${nativeHeaders}
mkdirSync(dirname(C_FILE), { recursive: true });
writeFileSync(HEADER_FILE, headers);
writeFileSync(C_FILE, generated.c);

// A second instantiation of the generated parser, compiled with -msse4.2, lets
// llhttp_execute() use the SSE4.2 span scanners at runtime on default x86-64
// builds (which pass no -march, so the `#ifdef __SSE4_2__` code is otherwise
// dead). The parser defines symbols prefixed `llhttp__internal`; the callbacks
// it imports are prefixed `llhttp__on`/`llhttp__before`/`llhttp__after` and stay
// shared. Rename only the defined `llhttp__internal*` symbols so the second copy
// links beside the first. Derived from the generated source, so it stays correct
// across grammar changes.
const internalSymbols = Array.from(
new Set(generated.c.match(/\bllhttp__internal[0-9A-Za-z_]*/g) ?? []),
).sort();
const renameHeader = `/* Generated by bin/generate.ts. Renames the defined symbols of llhttp.c so a
* second, -msse4.2 instantiation (llhttp_sse42.c) can link beside the first. */
#ifndef INCLUDE_LLHTTP_SSE42_RENAME_H_
#define INCLUDE_LLHTTP_SSE42_RENAME_H_
${internalSymbols.map((s) => `#define ${s} ${s}_sse42`).join('\n')}
#endif /* INCLUDE_LLHTTP_SSE42_RENAME_H_ */
`;
const sse42Wrapper = `/* Generated by bin/generate.ts. See src/native/api.c: a second instantiation of
* the generated parser with the SSE4.2 span scanners enabled, selected once at
* runtime by llhttp_execute(). Built with -msse4.2 by the build system. */
#if defined(__x86_64__) || defined(_M_X64)
#include "llhttp_sse42_rename.h"
#include "llhttp.c"
#endif
`;
writeFileSync(SSE42_RENAME_FILE, renameHeader);
writeFileSync(SSE42_C_FILE, sse42Wrapper);
33 changes: 32 additions & 1 deletion src/llhttp.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,37 @@
'sources': [
'<@(llhttp_sources)',
],
'conditions': [
# On x86-64, api.c (llhttp_execute) calls llhttp__internal_execute_sse42,
# a second instantiation of the parser compiled with -msse4.2 and chosen
# at runtime via __builtin_cpu_supports (see src/native/api.c). It is a
# separate target so -msse4.2 applies only to that one translation unit.
# Skipped on MSVC, which has no __builtin_cpu_supports (the dispatch in
# api.c is then compiled out and the symbol is not referenced).
['OS!="win" and (target_arch=="x64" or target_arch=="x86_64")', {
'dependencies': [ 'llhttp_sse42' ],
# Enables the dispatch in api.c; defined only here, where the second
# object is also built, so other consumers keep the baseline path.
'defines': [ 'LLHTTP_SSE42_DISPATCH' ],
}],
],
},
]
],
'conditions': [
['OS!="win" and (target_arch=="x64" or target_arch=="x86_64")', {
'targets': [
{
'target_name': 'llhttp_sse42',
'type': 'static_library',
'include_dirs': [ '.', 'include' ],
'sources': [ 'src/llhttp_sse42.c' ],
'cflags': [ '-msse4.2' ],
'cflags_cc': [ '-msse4.2' ],
'xcode_settings': {
'OTHER_CFLAGS': [ '-msse4.2' ],
},
},
],
}],
],
}
31 changes: 31 additions & 0 deletions src/native/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,40 @@ void llhttp_reset(llhttp_t* parser) {
}


/* Opt-in runtime SSE4.2 dispatch. The build system defines
* LLHTTP_SSE42_DISPATCH only when it also compiles the second parser
* instantiation (llhttp_sse42.c, built with -msse4.2); see the Makefile,
* CMakeLists.txt and src/llhttp.gyp. A consumer that compiles llhttp.c, api.c
* and http.c directly with its own build system (aiohttp, distro packagers,
* other bindings) does not define it and keeps the baseline path, so it needs
* no second object and does not get an unresolved
* llhttp__internal_execute_sse42. _MSC_VER is excluded because MSVC/clang-cl
* lack __builtin_cpu_supports and those builds skip the second object anyway. */
#if defined(LLHTTP_SSE42_DISPATCH) && \
(defined(__x86_64__) || defined(_M_X64)) && !defined(__SSE4_2__) && \
!defined(_MSC_VER) && (defined(__GNUC__) || defined(__clang__))
/* The second instantiation turns on the SSE4.2 span scanners that the
* generated code guards with `#ifdef __SSE4_2__`; the default x86-64 build (no
* -march) never contains them. Both instantiations come from the same
* generated source, so the parser state layout is identical and a parser may
* be driven by either. */
llhttp_errno_t llhttp__internal_execute_sse42(llhttp_t* state, const char* p,
const char* endp);

llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) {
/* __builtin_cpu_supports only loads and tests __cpu_model, which libgcc /
* compiler-rt initialize in a constructor, so there is no benefit to caching
* the result and no data race from doing without the cache. */
if (__builtin_cpu_supports("sse4.2")) {
return llhttp__internal_execute_sse42(parser, data, data + len);
}
return llhttp__internal_execute(parser, data, data + len);
}
#else
llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len) {
return llhttp__internal_execute(parser, data, data + len);
}
#endif


void llhttp_settings_init(llhttp_settings_t* settings) {
Expand Down
Loading
Loading