Skip to content

Commit ba81b40

Browse files
TerrorJacksunfishcode
authored andcommittedMay 20, 2022
Implement BULK_MEMORY_THRESHOLD
1 parent 5d0a755 commit ba81b40

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed
 

‎Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ BUILD_LIBC_TOP_HALF ?= yes
2222
# The directory where we're store intermediate artifacts.
2323
OBJDIR ?= $(CURDIR)/build
2424

25+
# When the length is no larger than this threshold, we consider the
26+
# overhead of bulk memory opcodes to outweigh the performance benefit,
27+
# and fall back to the original musl implementation. See
28+
# https://github.com/WebAssembly/wasi-libc/pull/263 for relevant
29+
# discussion
30+
BULK_MEMORY_THRESHOLD ?= 32
31+
2532
# Variables from this point on are not meant to be overridable via the
2633
# make command-line.
2734

@@ -394,6 +401,9 @@ $(MUSL_PRINTSCAN_NO_FLOATING_POINT_OBJS): CFLAGS += \
394401
$(BULK_MEMORY_OBJS): CFLAGS += \
395402
-mbulk-memory
396403

404+
$(BULK_MEMORY_OBJS): CFLAGS += \
405+
-DBULK_MEMORY_THRESHOLD=$(BULK_MEMORY_THRESHOLD)
406+
397407
$(LIBWASI_EMULATED_SIGNAL_MUSL_OBJS): CFLAGS += \
398408
-D_WASI_EMULATED_SIGNAL
399409

‎libc-top-half/musl/src/string/memcpy.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
66
{
77
#if defined(__wasm_bulk_memory__)
8-
return __builtin_memcpy(dest, src, n);
9-
#else
8+
if (n > BULK_MEMORY_THRESHOLD)
9+
return __builtin_memcpy(dest, src, n);
10+
#endif
1011
unsigned char *d = dest;
1112
const unsigned char *s = src;
1213

@@ -124,5 +125,4 @@ void *memcpy(void *restrict dest, const void *restrict src, size_t n)
124125

125126
for (; n; n--) *d++ = *s++;
126127
return dest;
127-
#endif // __wasm_bulk_memory__
128128
}

‎libc-top-half/musl/src/string/memmove.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ typedef __attribute__((__may_alias__)) size_t WT;
99
void *memmove(void *dest, const void *src, size_t n)
1010
{
1111
#if defined(__wasm_bulk_memory__)
12-
return __builtin_memmove(dest, src, n);
13-
#else
12+
if (n > BULK_MEMORY_THRESHOLD)
13+
return __builtin_memmove(dest, src, n);
14+
#endif
1415
char *d = dest;
1516
const char *s = src;
1617

@@ -42,5 +43,4 @@ void *memmove(void *dest, const void *src, size_t n)
4243
}
4344

4445
return dest;
45-
#endif // __wasm_bulk_memory__
4646
}

‎libc-top-half/musl/src/string/memset.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
void *memset(void *dest, int c, size_t n)
55
{
66
#if defined(__wasm_bulk_memory__)
7-
return __builtin_memset(dest, c, n);
8-
#else
7+
if (n > BULK_MEMORY_THRESHOLD)
8+
return __builtin_memset(dest, c, n);
9+
#endif
910
unsigned char *s = dest;
1011
size_t k;
1112

@@ -90,5 +91,4 @@ void *memset(void *dest, int c, size_t n)
9091
#endif
9192

9293
return dest;
93-
#endif // __wasm_bulk_memory__
9494
}

0 commit comments

Comments
 (0)
Please sign in to comment.