Import SIMD feature and add some workload samples (#438)

This commit is contained in:
Wenyong Huang
2020-11-05 18:15:15 +08:00
committed by GitHub
parent 667282eea9
commit a3074df21b
84 changed files with 7780 additions and 318 deletions

View File

@ -0,0 +1,34 @@
All workloads have similar a requirment of software dependencies. It includes
**wasi-sdk**, **clang-11**, **emsdk**, **wabt** and **binaryen**
> It might slightly different when using MacOS, and other linux distro than Ubuntu. This document only target
Ubuntu 18.04 as an example.
## Installation instructions
- **wasi-sdk**. Install
[latest release](https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-linux.tar.gz)
in */opt/wasi-sdk* or */opt/wasi-sdk-11*
- **wabt**. Install
[latest release](https://github.com/WebAssembly/wabt/releases/download/${WABT_VER}/wabt-1.0.19-ubuntu.tar.gz)
in */opt/wabt* or */opt/wabt-1.0.19*
- **clang-11**. Refer to [the guide](https://apt.llvm.org/).
- **emsdk**. Refer to [the guide](https://emscripten.org/docs/getting_started/downloads.html). Don't forget to activate
emsdk and set up environment variables. Verify it with `echo ${EMSDK}`.
- **libclang_rt.builtins-wasm32.a**. *wasi* has its private rt library. Put it under clang search path
``` shell
# copy it
$ cp -r /opt/wasi-sdk-11.0/lib/clang/10.0.0/lib/wasi /usr/lib/llvm-11/lib/clang/11.0.0/lib/
# or just link it
$ ln -sf /opt/wasi-sdk-11.0/lib/clang/10.0.0/lib/wasi/ /usr/lib/llvm-11/lib/clang/11.0.0/lib/
```
- **binaryen**. Install
[latest release](https://github.com/WebAssembly/binaryen/releases/download/version_97/binaryen-version_97-x86_64-linux.tar.gz)
in */opt/binaryen* or */opt/binaryen-version_97*

4
samples/workload/bwa/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
build
libz
bwa
include

View File

@ -0,0 +1,134 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 3.0)
project(bwa_wasm C)
################ LIBZ ################
set(LIBZ_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../libz)
add_library(z_wasm STATIC
${LIBZ_SRC_DIR}/adler32.c
${LIBZ_SRC_DIR}/compress.c
${LIBZ_SRC_DIR}/crc32.c
${LIBZ_SRC_DIR}/deflate.c
${LIBZ_SRC_DIR}/gzclose.c
${LIBZ_SRC_DIR}/gzlib.c
${LIBZ_SRC_DIR}/gzread.c
${LIBZ_SRC_DIR}/gzwrite.c
${LIBZ_SRC_DIR}/infback.c
${LIBZ_SRC_DIR}/inffast.c
${LIBZ_SRC_DIR}/inflate.c
${LIBZ_SRC_DIR}/inftrees.c
${LIBZ_SRC_DIR}/trees.c
${LIBZ_SRC_DIR}/uncompr.c
${LIBZ_SRC_DIR}/zutil.c
)
set_target_properties(z_wasm PROPERTIES LINKER_LANGUAGE C)
target_compile_definitions(z_wasm PRIVATE Z_HAVE_UNISTD_H _LARGEFILE64_SOURCE=1)
target_compile_options(z_wasm
PRIVATE
-Wno-unused-function
-Wno-unused-variable
)
target_include_directories(z_wasm
PUBLIC
${LIBZ_SRC_DIR}
)
################ BWA_WASM ################
set(BWA_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(BWA_SOURCE
${BWA_SRC_DIR}/utils.c
${BWA_SRC_DIR}/kthread.c
${BWA_SRC_DIR}/kstring.c
${BWA_SRC_DIR}/ksw.c
${BWA_SRC_DIR}/bwt.c
${BWA_SRC_DIR}/bntseq.c
${BWA_SRC_DIR}/bwa.c
${BWA_SRC_DIR}/bwamem.c
${BWA_SRC_DIR}/bwamem_pair.c
${BWA_SRC_DIR}/bwamem_extra.c
${BWA_SRC_DIR}/malloc_wrap.c
${BWA_SRC_DIR}/QSufSort.c
${BWA_SRC_DIR}/bwt_gen.c
${BWA_SRC_DIR}/rope.c
${BWA_SRC_DIR}/rle.c
${BWA_SRC_DIR}/is.c
${BWA_SRC_DIR}/bwtindex.c
${BWA_SRC_DIR}/bwashm.c
${BWA_SRC_DIR}/bwase.c
${BWA_SRC_DIR}/bwaseqio.c
${BWA_SRC_DIR}/bwtgap.c
${BWA_SRC_DIR}/bwtaln.c
${BWA_SRC_DIR}/bamlite.c
${BWA_SRC_DIR}/bwape.c
${BWA_SRC_DIR}/kopen.c
${BWA_SRC_DIR}/pemerge.c
${BWA_SRC_DIR}/maxk.c
${BWA_SRC_DIR}/bwtsw2_core.c
${BWA_SRC_DIR}/bwtsw2_main.c
${BWA_SRC_DIR}/bwtsw2_aux.c
${BWA_SRC_DIR}/bwt_lite.c
${BWA_SRC_DIR}/bwtsw2_chain.c
${BWA_SRC_DIR}/fastmap.c
${BWA_SRC_DIR}/bwtsw2_pair.c
${BWA_SRC_DIR}/main.c
)
add_executable(${PROJECT_NAME} ${BWA_SOURCE})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME bwa.wasm)
target_include_directories(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../include/SSE
${CMAKE_CURRENT_SOURCE_DIR}/../include/pthread
)
target_compile_definitions(${PROJECT_NAME}
PRIVATE
USE_MALLOC_WRAPPERS
__SSE__ __SSE2__ __SSE4_1__
_WASI_EMULATED_MMAN _WASI_EMULATED_SIGNAL
)
target_compile_options(${PROJECT_NAME}
PRIVATE
-Wno-unused-function
-Wno-unused-variable
)
target_link_options(${PROJECT_NAME}
PRIVATE
-Wno-unused-command-line-argument
LINKER:--allow-undefined,--export=__heap_base,--export=__data_end
LINKER:-z,stack-size=1048576
)
target_link_libraries(${PROJECT_NAME} z_wasm)
find_program(WASM_OPT
NAMES wasm-opt
PATHS /opt/binaryen-version_97/bin /opt/binaryen/bin
)
if (NOT WASM_OPT)
message(WARNING "can not find wasm-opt and will not optimize any wasm module")
endif()
add_custom_target(bwa_wasm_opt ALL
COMMAND
${WASM_OPT} -Oz --enable-simd -o bwa.opt.wasm bwa.wasm
BYPRODUCTS
${CMAKE_CURRENT_BINARY_DIR}/bwa.opt.wasm
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
)
add_dependencies(bwa_wasm_opt ${PROJECT_NAME})

View File

@ -0,0 +1,91 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 3.0)
project(bwa_wasm)
################ EMCC ################
if(NOT DEFINED ENV{EMSDK})
message(FATAL_ERROR
"can not find emsdk. "
"please refer to https://emscripten.org/docs/getting_started/downloads.html "
"and install it, "
"or active emsdk by 'source ./emsdk_env.sh'"
)
endif()
################ BINARYEN ################
find_program(WASM_OPT
NAMES wasm-opt
PATHS /opt/binaryen-version_97/bin /opt/binaryen/bin
)
if (NOT WASM_OPT)
message(FATAL_ERROR
"can not find wasm-opt. "
"please download it from "
"https://github.com/WebAssembly/binaryen/releases/download/version_97/binaryen-version_97-x86_64-linux.tar.gz "
"and install it under /opt"
)
endif()
#######################################
include(ExternalProject)
################ HEADERS ################
ExternalProject_Add(headers_from_emcc
PREFIX headers
SOURCE_DIR "$ENV{EMSDK}/upstream/emscripten/system/include/SSE"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND mkdir -p ${CMAKE_CURRENT_SOURCE_DIR}/include/SSE
&& ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/sys
&& ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_SOURCE_DIR}/include/emscripten
# copy emscripten SSE header files
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/SSE/immintrin.h ${CMAKE_CURRENT_SOURCE_DIR}/include/SSE/
# SSE
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/SSE/xmmintrin.h ${CMAKE_CURRENT_SOURCE_DIR}/include/SSE/
# SSE2
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/SSE/emmintrin.h ${CMAKE_CURRENT_SOURCE_DIR}/include/SSE/
# SSE4.1
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/SSE/smmintrin.h ${CMAKE_CURRENT_SOURCE_DIR}/include/SSE/
# a fake empty header to aovid further depenency
&& ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_SOURCE_DIR}/include/emscripten/emscripten.h
# copy emscripten pthread related header files
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/libc/pthread.h ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/libc/signal.h ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/libc/netdb.h ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/libc/sys/wait.h ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/sys/
&& ${CMAKE_COMMAND} -E copy $ENV{EMSDK}/upstream/emscripten/system/include/libc/sys/socket.h ${CMAKE_CURRENT_SOURCE_DIR}/include/pthread/sys/
)
################ libz ################
ExternalProject_Add(libz_src
PREFIX libz
GIT_REPOSITORY https://github.com/madler/zlib.git
GIT_TAG master
GIT_PROGRESS ON
GIT_SHALLOW ON
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libz
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
################ bwa ################
ExternalProject_Add(bwa
PREFIX bwa
GIT_REPOSITORY https://github.com/lh3/bwa.git
GIT_TAG master
GIT_PROGRESS ON
GIT_SHALLOW ON
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bwa
DEPENDS libz_src headers_from_emcc
UPDATE_COMMAND git clean -fd && git checkout -- *
&& ${CMAKE_COMMAND} -E echo "Copying pre-installed CMakeLists.txt"
&& ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.bwa_wasm.txt CMakeLists.txt
CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/../cmake/toolchain.cmake ${CMAKE_CURRENT_SOURCE_DIR}/bwa
BUILD_COMMAND make bwa_wasm_opt
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./bwa.opt.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/bwa.wasm
)

View File

@ -0,0 +1,47 @@
"bwa" sample introduction
==============
This sample demonstrates how to build [bwa](https://github.com/lh3/bwa) into
WebAssembly with simd support and run it with iwasm.
## Preparation
please refer to [installation instructions](../README.md).
## Build
``` shell
$ mkdir build && cd build
$ cmake ..
$ make
# to verify
$ ls bwa.wasm
```
## Download sample data
Download the bwa-0.7.15 binary package from
[such an address](https://sourceforge.net/projects/bio-bwa/files/bwakit/bwakit-0.7.15_x64-linux.tar.bz2/download),
a sample data file named **hs38DH.fa** will be used later.
If want more data, please refer to http://hgdownload.cse.ucsc.edu/goldenpath/hg19/bigZips/
## Run workload
Firstly please build iwasm with simd support:
``` shell
$ cd <wamr dir>/product-mini/platforms/linux/
$ mkdir build && cd build
$ cmake .. -DWAMR_BUILD_SIMD=1
$ make
```
Then compile wasm file to aot file and run:
``` shell
$ cd <wamr dir>/wamr-compiler/build
$ ./wamrc --enable-simd -o bwa.aot ./bwa.wasm
$ cd <wamr dir>/product-mini/platforms/linux/
$ ./iwasm --dir=. ./bwa.aot index hs38DH.fa
```

View File

@ -0,0 +1,100 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 3.0)
if(DEFINED _WAMR_TOOLCHAIN_CMAKE_)
return()
else()
set(_WAMR_TOOLCHAIN_CMAKE_ 1)
endif()
SET(CMAKE_SYSTEM_NAME Linux)
################ COMPILER ################
find_program(CLANG_11 NAMES clang clang-11 REQUIRED)
find_program(CLANG++_11 NAMES clang++ clang++-11 REQUIRED)
if(NOT CLANG_11)
message(FATAL_ERROR "clang not found")
else()
message(STATUS "use ${CLANG_11} as the c compiler")
endif()
if(NOT CLANG++_11)
message(FATAL_ERROR "clang++ not found")
else()
message(STATUS "use ${CLANG++_11} as the c++ compiler")
endif()
set(CMAKE_C_COMPILER "${CLANG_11}" CACHE STRING "C compiler" FORCE)
set(CMAKE_C_COMPILER_ID Clang CACHE STRING "C compiler ID" FORCE)
set(CMAKE_CXX_COMPILER "${CLANG++_11}" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_COMPILER_ID Clang CACHE STRING "C++ compiler ID" FORCE)
################ WASI AS SYSROOT ################
find_path(WASI_SYSROOT
wasi-sysroot
PATHS /opt/wasi-sdk-11.0/share /opt/wasi-sdk/share
REQUIRED
)
if(NOT WASI_SYSROOT)
message(FATAL_ERROR
"can not find wasi sysroot. "
"please download it from "
"https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-11.0-linux.tar.gz "
"and install it under /opt"
)
endif()
set(CMAKE_SYSROOT ${WASI_SYSROOT}/wasi-sysroot CACHE STRING "--sysroot to compiler" FORCE)
add_compile_options(
--target=wasm32-wasi
-msimd128
$<IF:$<CONFIG:Debug>,-O0,-O3>
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-v>
)
################ AR ################
find_program(LLVM_AR NAMES llvm-ar llvm-ar-11 REQUIRED)
if(NOT LLVM_AR)
message(FATAL_ERROR "llvm-ar not found")
else()
message(STATUS "use ${LLVM_AR} as the AR")
endif()
set(CMAKE_AR "${LLVM_AR}" CACHE STRING "AR" FORCE)
################ RANLIB ################
find_program(LLVM_RANLIB NAMES llvm-ranlib llvm-ranlib-11 REQUIRED)
if(NOT LLVM_RANLIB)
message(FATAL_ERROR "llvm-ranlib not found")
else()
message(STATUS "use ${LLVM_RANLIB} as the ranlib")
endif()
set(CMAKE_RANLIB "${LLVM_RANLIB}" CACHE STRING "RANLIB" FORCE)
################ LD ################
find_program(WASM_LD NAMES wasm-ld wasm-ld-11 REQUIRED)
if(NOT WASM_LD)
message(FATAL_ERROR "wasm-ld not found")
else()
message(STATUS "use ${WASM_LD} as the linker")
endif()
add_link_options(
--target=wasm32-wasi
-fuse-ld=${WASM_LD}
LINKER:--allow-undefined
$<IF:$<CONFIG:Debug>,-O0,-O3>
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-v>
)

1
samples/workload/docker/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build_scripts

View File

@ -0,0 +1,77 @@
FROM ubuntu:18.04 as builder
#
# install clang and llvm
COPY llvm.sh /tmp
RUN apt update \
&& apt install -y lsb-release wget software-properties-common build-essential \
&& cd /tmp \
&& chmod a+x llvm.sh \
&& ./llvm.sh 11
ARG WASI_SDK_VER=11.0
ARG WABT_VER=1.0.19
ARG CMAKE_VER=3.16.2
ARG BINARYEN_VER=version_97
#
# install wasi-sdk
ARG WASI_SDK_FILE="wasi-sdk-${WASI_SDK_VER}-linux.tar.gz"
COPY ${WASI_SDK_FILE} /opt
RUN cd /opt \
&& tar zxf ${WASI_SDK_FILE} \
&& rm ${WASI_SDK_FILE} \
&& ln -sf /opt/wasi-sdk-${WASI_SDK_VER} /opt/wasi-sdk \
&& ln -sf /opt/wasi-sdk/lib/clang/10.0.0/lib/wasi/ /usr/lib/llvm-11/lib/clang/11.0.0/lib/
#
# install wabt
ARG WABT_FILE="wabt-${WABT_VER}-ubuntu.tar.gz"
COPY ${WABT_FILE} /opt
RUN cd /opt \
&& tar zxf ${WABT_FILE} \
&& rm ${WABT_FILE} \
&& ln -sf /opt/wabt-${WABT_VER} /opt/wabt
#
# install cmake
ARG CMAKE_FILE="cmake-${CMAKE_VER}-Linux-x86_64.sh"
COPY ${CMAKE_FILE} /tmp
RUN cd /tmp \
&& chmod a+x ${CMAKE_FILE} \
&& mkdir /opt/cmake \
&& ./${CMAKE_FILE} --prefix=/opt/cmake --skip-license \
&& ln -sf /opt/cmake/bin/cmake /usr/local/bin/cmake
#
# install tools
RUN apt install -y git tree
#
# install emsdk
RUN cd /opt \
&& git clone https://github.com/emscripten-core/emsdk.git \
&& cd emsdk \
&& git pull \
&& ./emsdk install latest \
&& ./emsdk activate latest \
&& echo "source /opt/emsdk/emsdk_env.sh" >> /root/.bashrc
#
# install binaryen
ARG BINARYEN_FILE="binaryen-${BINARYEN_VER}-x86_64-linux.tar.gz"
COPY ${BINARYEN_FILE} /opt
RUN cd /opt \
&& tar zxf ${BINARYEN_FILE} \
&& rm ${BINARYEN_FILE} \
&& ln -sf /opt/binaryen-${BINARYEN_VER} /opt/binaryen
#
# Clean up
RUN apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/*
VOLUME /data
WORKDIR /data

View File

@ -0,0 +1,48 @@
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#!/bin/bash
if [[ ! -d build_scripts ]]; then
mkdir build_scripts
fi
WASI_SDK_VER=11.0
WABT_VER=1.0.19
CMAKE_VER=3.16.2
BINARYEN_VER=version_97
cd build_scripts
if [[ ! -f wasi-sdk-${WASI_SDK_VER}-linux.tar.gz ]]; then
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk-${WASI_SDK_VER}-linux.tar.gz
fi
if [[ ! -f wabt-${WABT_VER}-ubuntu.tar.gz ]]; then
wget https://github.com/WebAssembly/wabt/releases/download/${WABT_VER}/wabt-${WABT_VER}-ubuntu.tar.gz
fi
if [[ ! -f llvm.sh ]]; then
wget https://apt.llvm.org/llvm.sh
fi
if [[ ! -f cmake-${CMAKE_VER}-Linux-x86_64.sh ]]; then
wget https://github.com/Kitware/CMake/releases/download/v${CMAKE_VER}/cmake-${CMAKE_VER}-Linux-x86_64.sh
fi
if [[ ! -f binaryen-${BINARYEN_VER}-x86_64-linux.tar.gz ]]; then
wget https://github.com/WebAssembly/binaryen/releases/download/${BINARYEN_VER}/binaryen-${BINARYEN_VER}-x86_64-linux.tar.gz
fi
cd -
docker build \
--build-arg http_proxy=${http_proxy} \
--build-arg https_proxy=${https_proxy} \
--build-arg HTTP_PROXY=${http_proxy} \
--build-arg HTTPS_PROXY=${https_proxy} \
--build-arg WASI_SDK_VER=11.0 \
--build-arg WABT_VER=${WABT_VER} \
--build-arg CMAKE_VER=${CMAKE_VER} \
--build-arg BINARYEN_VER=${BINARYEN_VER} \
-t clang_env:0.1 -f Dockerfile build_scripts

10
samples/workload/docker/run.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
docker run --rm -it \
-e http_proxy=${http_proxy} \
-e https_proxy=${https_proxy} \
-e HTTP_PROXY=${http_proxy} \
-e HTTPS_PROXY=${htpps_proxy} \
--name workload_w_clang \
--mount type=bind,source=$(pwd)/..,target=/data \
clang_env:0.1

View File

@ -0,0 +1,2 @@
build
meshoptimizer

View File

@ -0,0 +1,39 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required (VERSION 3.0)
project(bench-meshoptimizer)
################ BINARYEN ################
find_program(WASM_OPT
NAMES wasm-opt
PATHS /opt/binaryen-version_97/bin /opt/binaryen/bin
)
if (NOT WASM_OPT)
message(FATAL_ERROR
"can not find wasm-opt. "
"please download it from "
"https://github.com/WebAssembly/binaryen/releases/download/version_97/binaryen-version_97-x86_64-linux.tar.gz "
"and install it under /opt"
)
endif()
################ MESHOPTIMIZER ################
include(ExternalProject)
ExternalProject_Add(codecbench
PREFIX codecbench
GIT_REPOSITORY https://github.com/zeux/meshoptimizer.git
GIT_TAG master
GIT_SHALLOW ON
GIT_PROGRESS ON
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer
UPDATE_COMMAND git clean -fd && git checkout -- *
&& ${CMAKE_COMMAND} -E echo "Applying patch"
&& git apply ${CMAKE_CURRENT_SOURCE_DIR}/codecbench.patch
CONFIGURE_COMMAND ${CMAKE_COMMAND} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_CURRENT_SOURCE_DIR}/../cmake/toolchain.cmake ${CMAKE_CURRENT_SOURCE_DIR}/meshoptimizer
BUILD_COMMAND make codecbench.opt
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy ./codecbench.opt.wasm ${CMAKE_CURRENT_SOURCE_DIR}/build/codecbench.wasm
)

View File

@ -0,0 +1,59 @@
"codecbench of meshoptimizer" sample introduction
==============
This sample demonstrates how to build [codecbench of messoptimizer](https://github.com/zeux/meshoptimizer) into
WebAssembly with simd support and run it with iwasm.
## Preparation
please refer to [installation instructions](../README.md).
## Build with clang-11 and wasi-sdk
``` shell
$ mkdir build && cd build
$ cmake ..
$ make
# to verify
$ ls codecbench.wasm
```
## Or build with EMCC
EMCC is another toolchain to compile C code to WASM. In this case, will have
a higher performance with EMCC.
``` shell
$ git clone https://github.com/zeux/meshoptimizer.git
$ cd messoptimizer
$ emcc tools/codecbench.cpp src/vertexcodec.cpp src/vertexfilter.cpp \
src/overdrawanalyzer.cpp src/indexgenerator.cpp src/vcacheoptimizer.cpp \
src/clusterizer.cpp src/indexcodec.cpp src/vfetchanalyzer.cpp \
src/spatialorder.cpp src/allocator.cpp src/vcacheanalyzer.cpp \
src/vfetchoptimizer.cpp src/overdrawoptimizer.cpp src/simplifier.cpp \
src/stripifier.cpp -O3 -msimd128 \
-s TOTAL_MEMORY=268435456 -s "EXPORTED_FUNCTIONS=['_main']" \
-o codecbench.wasm
$ ls -l codecbench.wasm
```
## Run workload
Firstly please build iwasm with simd support:
``` shell
$ cd <wamr dir>/product-mini/platforms/linux/
$ mkdir build && cd build
$ cmake .. -DWAMR_BUILD_SIMD=1
$ make
```
Then compile wasm file to aot file and run:
``` shell
$ cd <wamr dir>/wamr-compiler/build
$ ./wamrc --enable-simd -o codecbench.aot codecbench.wasm
$ cd <wamr dir>/product-mini/platforms/linux/
$ ./iwasm codecbench.aot
```

View File

@ -0,0 +1,47 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index eccc49e..dac126c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -127,3 +127,42 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/meshoptimizerConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/meshoptimizer)
+
+##################################################
+# codecbench
+##################################################
+add_executable(codecbench tools/codecbench.cpp ${SOURCES})
+
+set_target_properties(codecbench PROPERTIES OUTPUT_NAME codecbench.wasm)
+
+target_compile_options(codecbench
+ PUBLIC
+ -std=c++11
+ -Wno-unused-function
+ -Wno-unused-variable
+)
+
+target_link_options(codecbench
+ PUBLIC
+ LINKER:-allow-undefined,--demangle
+)
+
+find_program(WASM_OPT
+ NAMES wasm-opt
+ PATHS /opt/binaryen-version_97/bin /opt/binaryen/bin
+)
+
+if (NOT WASM_OPT)
+ message(WARNING "can not find wasm-opt and will not optimize any wasm module")
+endif()
+
+add_custom_target(codecbench.opt ALL
+ COMMAND
+ ${WASM_OPT} -Oz --enable-simd -o codecbench.opt.wasm codecbench.wasm
+ BYPRODUCTS
+ ${CMAKE_CURRENT_BINARY_DIR}/codecbench.opt.wasm
+ WORKING_DIRECTORY
+ ${CMAKE_CURRENT_BINARY_DIR}
+)
+
+add_dependencies(codecbench.opt codecbench)

View File

@ -1,8 +1,20 @@
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#!/bin/bash
####################################
# build tensorflow-lite sample #
####################################
if [ ! -d "${EMSDK}" ]; then
echo "can not find emsdk. "
echo "please refer to https://emscripten.org/docs/getting_started/downloads.html "
echo "to install it, or active it by 'source <emsdk_dir>emsdk_env.sh'"
exit
fi
set -xe
EMSDK_WASM_DIR="$EM_CACHE/wasm"
@ -64,7 +76,15 @@ fi
if [ -d "${TF_LITE_BUILD_DIR}/gen" ]; then
rm -fr ${TF_LITE_BUILD_DIR}/gen
fi
make -j 4 -C "${TENSORFLOW_DIR}" -f ${TF_LITE_BUILD_DIR}/Makefile
if [[ $1 == '--sgx' ]]; then
make -j 4 -C "${TENSORFLOW_DIR}" -f ${TF_LITE_BUILD_DIR}/Makefile
else
export BUILD_WITH_SIMD=true
make -j 4 -C "${TENSORFLOW_DIR}" -f ${TF_LITE_BUILD_DIR}/Makefile
fi
# remove patch file and recover emcc libc.a after building
Clear_Before_Exit
# 2.5 copy /make/gen target files to out/
rm -rf ${OUT_DIR}
@ -84,7 +104,7 @@ cd ${OUT_DIR}
if [[ $1 == '--sgx' ]]; then
${WAMRC_CMD} -sgx -o benchmark_model.aot benchmark_model.wasm
else
${WAMRC_CMD} -o benchmark_model.aot benchmark_model.wasm
${WAMRC_CMD} --enable-simd -o benchmark_model.aot benchmark_model.wasm
fi
# 4. build iwasm with pthread and libc_emcc enable
@ -101,7 +121,7 @@ if [[ $1 == '--sgx' ]]; then
else
cd ${WAMR_PLATFORM_DIR}/linux
rm -fr build && mkdir build
cd build && cmake .. -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
cd build && cmake .. -DWAMR_BUILD_SIMD=1 -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
make
fi
@ -122,8 +142,6 @@ else
fi
${IWASM_CMD} --heap-size=10475860 \
${OUT_DIR}/benchmark_model.aot \
--graph=mobilenet_quant_v1_224.tflite --max_secs=300
Clear_Before_Exit
${OUT_DIR}/benchmark_model.aot \
--graph=mobilenet_quant_v1_224.tflite --max_secs=300

View File

@ -1,5 +1,5 @@
diff --git a/tensorflow/lite/tools/make/Makefile b/tensorflow/lite/tools/make/Makefile
index c7ddff5844..1082644043 100644
index c7ddff5844..17146868f7 100644
--- a/tensorflow/lite/tools/make/Makefile
+++ b/tensorflow/lite/tools/make/Makefile
@@ -48,11 +48,7 @@ INCLUDES += -I/usr/local/include
@ -15,10 +15,16 @@ index c7ddff5844..1082644043 100644
-ldl
# There are no rules for compiling objects for the host system (since we don't
@@ -84,14 +80,18 @@ endif # ifeq ($(HOST_ARCH),$(TARGET_ARCH))
@@ -84,14 +80,24 @@ endif # ifeq ($(HOST_ARCH),$(TARGET_ARCH))
endif # ifeq ($(HOST_OS),$(TARGET))
endif
+BUILD_WITH_SIMD ?= false
+ifeq ($(BUILD_WITH_SIMD), true)
+CFLAGS+=-msimd128
+CXXFLAGS+=-msimd128
+endif
+
+LIBFLAGS += -s TOTAL_STACK=1048576 \
+ -Wl,--export=__data_end -Wl,--export=__heap_base \
+ -s ERROR_ON_UNDEFINED_SYMBOLS=0
@ -36,7 +42,7 @@ index c7ddff5844..1082644043 100644
# A small example program that shows how to link against the library.
MINIMAL_SRCS := \
@@ -277,12 +277,16 @@ LIB_PATH := $(LIBDIR)$(LIB_NAME)
@@ -277,12 +283,16 @@ LIB_PATH := $(LIBDIR)$(LIB_NAME)
BENCHMARK_LIB := $(LIBDIR)$(BENCHMARK_LIB_NAME)
BENCHMARK_BINARY := $(BINDIR)$(BENCHMARK_BINARY_NAME)
BENCHMARK_PERF_OPTIONS_BINARY := $(BINDIR)$(BENCHMARK_PERF_OPTIONS_BINARY_NAME)

View File

@ -0,0 +1,22 @@
"wasm-av1" sample introduction
==============
This sample demonstrates how to build [wasm-av1](https://github.com/GoogleChromeLabs/wasm-av1) into WebAssembly with emcc toolchain and run it with iwasm. Please first install [emsdk](https://github.com/emscripten-core/emsdk):
```bash
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
```
And set up ensdk environment:
```bash
source emsdk_env.sh
```
Then run
```bash
./build.sh
```
to build wasm-av1 and run it with iwasm, which basically contains the following steps:
- hack emcc to delete some objects in libc.a
- patch wasm-av1 and build it with emcc compiler
- build iwasm with simd and libc-emcc support
- run testav1.aot with iwasm

View File

@ -0,0 +1,100 @@
#
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
#!/bin/bash
####################################
# build wasm-av1 sample #
####################################
if [ ! -d "${EMSDK}" ]; then
echo "can not find emsdk. "
echo "please refer to https://emscripten.org/docs/getting_started/downloads.html "
echo "to install it, or active it by 'source <emsdk_dir>emsdk_env.sh'"
exit
fi
set -xe
EMSDK_WASM_DIR="$EM_CACHE/wasm"
BUILD_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT_DIR="${BUILD_SCRIPT_DIR}/out"
WASM_AV1_DIR="${BUILD_SCRIPT_DIR}/wasm-av1"
WAMR_PLATFORM_DIR="${BUILD_SCRIPT_DIR}/../../../product-mini/platforms"
IWASM_CMD="${WAMR_PLATFORM_DIR}/linux/build/iwasm"
WAMRC_DIR="${BUILD_SCRIPT_DIR}/../../../wamr-compiler"
WAMRC_CMD="${WAMRC_DIR}/build/wamrc"
function Clear_Before_Exit
{
[[ -f ${WASM_AV1_DIR}/wasm-av1.patch ]] &&
rm -f ${WASM_AV1_DIR}/wasm-av1.patch
# resume the libc.a under EMSDK_WASM_DIR
cd ${EMSDK_WASM_DIR}
mv libc.a.bak libc.a
}
# 1.hack emcc
cd ${EMSDK_WASM_DIR}
# back up libc.a
cp libc.a libc.a.bak
# delete some objects in libc.a
emar d libc.a fopen.o
emar d libc.a fread.o
emar d libc.a feof.o
emar d libc.a fclose.o
# 2. build wasm-av1
cd ${BUILD_SCRIPT_DIR}
# 2.1 clone wasm-av1 repo from Github
if [ ! -d "wasm-av1" ]; then
git clone https://github.com/GoogleChromeLabs/wasm-av1.git
fi
# 2.2 copy the wasm-av1.patch to wasm-av1 and apply the patch
cd ${WASM_AV1_DIR}
cp -a ${BUILD_SCRIPT_DIR}/wasm-av1.patch .
git checkout Makefile
git checkout test.c
git checkout third_party/aom
if [[ $(git apply wasm-av1.patch 2>&1) =~ "error" ]]; then
echo "git apply patch failed, please check wasm-av1 related changes..."
Clear_Before_Exit
exit 0
fi
make testavx -j 4
# remove patch file and recover emcc libc.a after building
Clear_Before_Exit
# 2.3 copy /make/gen target files to out/
rm -rf ${OUT_DIR} && mkdir ${OUT_DIR}
cp -a ${WASM_AV1_DIR}/testavx.wasm ${OUT_DIR}/
# 3. compile wasm-av1.wasm to wasm-av1.aot with wamrc
# 3.1 build wamr-compiler
cd ${WAMRC_DIR}
./build_llvm.sh
rm -fr build && mkdir build
cd build && cmake ..
make
# 3.2 compile wasm-av1.wasm to wasm-av1.aot
cd ${OUT_DIR}
${WAMRC_CMD} --enable-simd -o testavx.aot testavx.wasm
# 4. build iwasm with pthread and libc_emcc enable
cd ${WAMR_PLATFORM_DIR}/linux
rm -fr build && mkdir build
cd build && cmake .. -DWAMR_BUILD_SIMD=1 -DWAMR_BUILD_LIB_PTHREAD=1 -DWAMR_BUILD_LIBC_EMCC=1
make
# 5. run wasm-av1 with iwasm
echo "---> run testav1.aot with iwasm"
cd ${OUT_DIR}
${IWASM_CMD} testavx.aot ../wasm-av1/third_party/samples/elephants_dream_480p24.ivf

View File

@ -0,0 +1,696 @@
diff --git a/Makefile b/Makefile
index c39fff6..4682d43 100644
--- a/Makefile
+++ b/Makefile
@@ -59,11 +59,13 @@ $(TARGET): $(DEPS) blob-api.c yuv-to-rgb.c $(EMLIBAV1)
]" \
blob-api.c yuv-to-rgb.c $(SRCS) $(INC) -L $(LIBDIR) -l$(LIB)
-$(TESTTARGET): test.c $(DEPS) $(X86LIBAV1)
- cc -o $@ -O3 test.c $(SRCS) $(INC) -L $(X86LIBDIR) -l$(LIB)
+$(TESTTARGET): test.c $(DEPS) $(EMLIBAV1)
+ emcc -o $@.wasm -O3 test.c $(SRCS) $(INC) -L $(LIBDIR) -l$(LIB) \
+ -s TOTAL_MEMORY=104857600 -s ERROR_ON_UNDEFINED_SYMBOLS=0
-$(TESTTARGET)g: test.c $(DEPS) $(X86LIBAV1)
- cc -o $@ -g test.c $(SRCS) $(INC) -L $(X86LIBDIR) -l$(LIB)
+$(TESTTARGET)g: test.c $(DEPS) $(EMLIBAV1)
+ emcc -o $@.wasm -g test.c $(SRCS) $(INC) -L $(LIBDIR) -l$(LIB) \
+ -s TOTAL_MEMORY=104857600 -s ERROR_ON_UNDEFINED_SYMBOLS=0
clean:
-rm $(TARGET) $(TESTTARGET) $(TESTTARGET)g
@@ -80,7 +82,7 @@ $(EMLIBAV1): $(LIBDIR)
-DCONFIG_RUNTIME_CPU_DETECT=0 \
-DCONFIG_UNIT_TESTS=0 \
-DCONFIG_WEBM_IO=0 \
- -DCMAKE_TOOLCHAIN_FILE=`../../get-emcmake.sh`; \
+ -DCMAKE_TOOLCHAIN_FILE=${EMSDK}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake; \
make \
)
diff --git a/test.c b/test.c
index df2d44b..8e81cdc 100644
--- a/test.c
+++ b/test.c
@@ -18,6 +18,9 @@
#include "decode-av1-priv.h"
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
static void
dump_raw_frame(AVX_Video_Frame *avf, int id) {
FILE *f;
@@ -26,12 +29,13 @@ dump_raw_frame(AVX_Video_Frame *avf, int id) {
void *buf;
sprintf(name, "frame%04d.yuv", id);
+ printf("writing %s ..\n", name);
if ((f = fopen(name, "wb")) == NULL) {
return;
}
buf = AVX_Video_Frame_get_buffer(avf);
size = AVX_Video_Frame_get_size(avf);
- fwrite(buf, size, 1, f);
+ emcc_fwrite(buf, size, 1, f);
fclose(f);
}
@@ -63,6 +67,7 @@ main(int argc, char *argv[]) {
static int i = 0;
++i;
+ printf("##decode raw frame %d\n", i);
if (30 <= i && i < 40) {
dump_raw_frame(af, i);
}
diff --git a/third_party/aom/CMakeLists.txt b/third_party/aom/CMakeLists.txt
index 9dbe301..20c7be4 100644
--- a/third_party/aom/CMakeLists.txt
+++ b/third_party/aom/CMakeLists.txt
@@ -56,6 +56,10 @@ option(BUILD_SHARED_LIBS "CMake should generate a shared library build." OFF)
project(AOM C CXX)
+set(CMAKE_C_FLAGS "-msimd128 -msse2 -msse3 -msse4.1 -msse4.2 ${CMAKE_C_FLAGS}")
+set(CMAKE_CXX_FLAGS "-msimd128 -msse2 -msse3 -msse4.1 -msse4.2 ${CMAKE_CXX_FLAGS}")
+set(CMAKE_VERBOSE_MAKEFILE on)
+
set(AOM_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
set(AOM_CONFIG_DIR "${CMAKE_CURRENT_BINARY_DIR}")
set(INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include"
@@ -347,7 +351,7 @@ if(CONFIG_AV1_DECODER AND ENABLE_EXAMPLES)
em_link_post_js(inspect "${AOM_ROOT}/tools/inspect-post.js")
# Force generation of Wasm instead of asm.js
append_link_flag_to_target("inspect" "-s WASM=1")
- append_compiler_flag("-s WASM=1")
+ append_compiler_flag("-O3 -s WASM=1 -s ERROR_ON_UNDEFINED_SYMBOLS=0")
endif()
endif()
diff --git a/third_party/aom/aom/src/aom_codec.c b/third_party/aom/aom/src/aom_codec.c
index dbd6fa5..a8d2a49 100644
--- a/third_party/aom/aom/src/aom_codec.c
+++ b/third_party/aom/aom/src/aom_codec.c
@@ -132,6 +132,7 @@ void aom_internal_error(struct aom_internal_error_info *info,
info->detail[sz - 1] = '\0';
}
+ printf("##aom internal error: %s\n", info->detail);
if (info->setjmp) longjmp(info->jmp, info->error_code);
}
diff --git a/third_party/aom/aom_dsp/grain_table.c b/third_party/aom/aom_dsp/grain_table.c
index 0d6a73f..4b05833 100644
--- a/third_party/aom/aom_dsp/grain_table.c
+++ b/third_party/aom/aom_dsp/grain_table.c
@@ -293,6 +293,9 @@ aom_codec_err_t aom_film_grain_table_read(
return error_info->error_code;
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
aom_codec_err_t aom_film_grain_table_write(
const aom_film_grain_table_t *t, const char *filename,
struct aom_internal_error_info *error_info) {
@@ -305,7 +308,7 @@ aom_codec_err_t aom_film_grain_table_write(
return error_info->error_code;
}
- if (!fwrite(kFileMagic, 8, 1, file)) {
+ if (!emcc_fwrite(kFileMagic, 8, 1, file)) {
aom_internal_error(error_info, AOM_CODEC_ERROR,
"Unable to write file magic");
fclose(file);
diff --git a/third_party/aom/aomdec.c b/third_party/aom/aomdec.c
index 4addee8..f850147 100644
--- a/third_party/aom/aomdec.c
+++ b/third_party/aom/aomdec.c
@@ -274,6 +274,9 @@ static void update_image_md5(const aom_image_t *img, const int planes[3],
}
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
static void write_image_file(const aom_image_t *img, const int *planes,
const int num_planes, FILE *file) {
int i, y;
@@ -287,7 +290,7 @@ static void write_image_file(const aom_image_t *img, const int *planes,
const int h = aom_img_plane_height(img, plane);
for (y = 0; y < h; ++y) {
- fwrite(buf, bytes_per_sample, w, file);
+ emcc_fwrite(buf, bytes_per_sample, w, file);
buf += stride;
}
}
diff --git a/third_party/aom/aomenc.c b/third_party/aom/aomenc.c
index 64155b0..3ed5080 100644
--- a/third_party/aom/aomenc.c
+++ b/third_party/aom/aomenc.c
@@ -59,9 +59,12 @@ static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
}
#define fread wrap_fread
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream) {
- return fwrite(ptr, size, nmemb, stream);
+ return emcc_fwrite(ptr, size, nmemb, stream);
}
#define fwrite wrap_fwrite
diff --git a/third_party/aom/aomstats.c b/third_party/aom/aomstats.c
index 0cfeea2..6833776 100644
--- a/third_party/aom/aomstats.c
+++ b/third_party/aom/aomstats.c
@@ -80,9 +80,12 @@ void stats_close(stats_io_t *stats, int last_pass) {
}
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
void stats_write(stats_io_t *stats, const void *pkt, size_t len) {
if (stats->file) {
- (void)fwrite(pkt, 1, len, stats->file);
+ (void)emcc_fwrite(pkt, 1, len, stats->file);
} else {
if (stats->buf.sz + len > stats->buf_alloc_sz) {
size_t new_sz = stats->buf_alloc_sz + 64 * 1024;
diff --git a/third_party/aom/av1/common/debugmodes.c b/third_party/aom/av1/common/debugmodes.c
index 868f341..c44258c 100644
--- a/third_party/aom/av1/common/debugmodes.c
+++ b/third_party/aom/av1/common/debugmodes.c
@@ -89,10 +89,13 @@ void av1_print_modes_and_motion_vectors(AV1_COMMON *cm, const char *file) {
fclose(mvs);
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
void av1_print_uncompressed_frame_header(const uint8_t *data, int size,
const char *filename) {
FILE *hdrFile = fopen(filename, "w");
- fwrite(data, size, sizeof(uint8_t), hdrFile);
+ emcc_fwrite(data, size, sizeof(uint8_t), hdrFile);
fclose(hdrFile);
}
diff --git a/third_party/aom/av1/encoder/encoder.c b/third_party/aom/av1/encoder/encoder.c
index a557380..d709d26 100644
--- a/third_party/aom/av1/encoder/encoder.c
+++ b/third_party/aom/av1/encoder/encoder.c
@@ -2799,6 +2799,9 @@ AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
#endif // CONFIG_INTERNAL_STATS
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
void av1_remove_compressor(AV1_COMP *cpi) {
AV1_COMMON *cm;
unsigned int i;
@@ -2814,7 +2817,7 @@ void av1_remove_compressor(AV1_COMP *cpi) {
if (cpi->oxcf.pass != 1) {
fprintf(stderr, "Writing counts.stt\n");
FILE *f = fopen("counts.stt", "wb");
- fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
+ emcc_fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
fclose(f);
}
#endif // CONFIG_ENTROPY_STATS
@@ -3013,7 +3016,7 @@ void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
int h = s->y_height;
do {
- fwrite(src, s->y_width, 1, f);
+ emcc_fwrite(src, s->y_width, 1, f);
src += s->y_stride;
} while (--h);
@@ -3021,7 +3024,7 @@ void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
h = s->uv_height;
do {
- fwrite(src, s->uv_width, 1, f);
+ emcc_fwrite(src, s->uv_width, 1, f);
src += s->uv_stride;
} while (--h);
@@ -3029,7 +3032,7 @@ void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
h = s->uv_height;
do {
- fwrite(src, s->uv_width, 1, f);
+ emcc_fwrite(src, s->uv_width, 1, f);
src += s->uv_stride;
} while (--h);
}
@@ -3121,7 +3124,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
do {
- fwrite(src16, s->y_width, 2, yuv_rec_file);
+ emcc_fwrite(src16, s->y_width, 2, yuv_rec_file);
src16 += s->y_stride;
} while (--h);
@@ -3129,7 +3132,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
h = s->uv_height;
do {
- fwrite(src16, s->uv_width, 2, yuv_rec_file);
+ emcc_fwrite(src16, s->uv_width, 2, yuv_rec_file);
src16 += s->uv_stride;
} while (--h);
@@ -3137,7 +3140,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
h = s->uv_height;
do {
- fwrite(src16, s->uv_width, 2, yuv_rec_file);
+ emcc_fwrite(src16, s->uv_width, 2, yuv_rec_file);
src16 += s->uv_stride;
} while (--h);
@@ -3146,7 +3149,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
}
do {
- fwrite(src, s->y_width, 1, yuv_rec_file);
+ emcc_fwrite(src, s->y_width, 1, yuv_rec_file);
src += s->y_stride;
} while (--h);
@@ -3154,7 +3157,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
h = s->uv_height;
do {
- fwrite(src, s->uv_width, 1, yuv_rec_file);
+ emcc_fwrite(src, s->uv_width, 1, yuv_rec_file);
src += s->uv_stride;
} while (--h);
@@ -3162,7 +3165,7 @@ void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
h = s->uv_height;
do {
- fwrite(src, s->uv_width, 1, yuv_rec_file);
+ emcc_fwrite(src, s->uv_width, 1, yuv_rec_file);
src += s->uv_stride;
} while (--h);
@@ -3241,16 +3244,16 @@ static int dump_one_image(AV1_COMMON *cm,
// --- Y ---
for (h = 0; h < cm->height; ++h) {
- fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
+ emcc_fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
}
// --- U ---
for (h = 0; h < (cm->height >> 1); ++h) {
- fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
+ emcc_fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
f_ref);
}
// --- V ---
for (h = 0; h < (cm->height >> 1); ++h) {
- fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
+ emcc_fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
f_ref);
}
@@ -4692,17 +4695,17 @@ static void dump_filtered_recon_frames(AV1_COMP *cpi) {
// --- Y ---
for (h = 0; h < cm->height; ++h) {
- fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
+ emcc_fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
f_recon);
}
// --- U ---
for (h = 0; h < (cm->height >> 1); ++h) {
- fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
+ emcc_fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
f_recon);
}
// --- V ---
for (h = 0; h < (cm->height >> 1); ++h) {
- fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
+ emcc_fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
f_recon);
}
diff --git a/third_party/aom/av1/encoder/firstpass.c b/third_party/aom/av1/encoder/firstpass.c
index bb73fde..b963043 100644
--- a/third_party/aom/av1/encoder/firstpass.c
+++ b/third_party/aom/av1/encoder/firstpass.c
@@ -476,6 +476,9 @@ static double raw_motion_error_stdev(int *raw_motion_err_list,
return raw_err_stdev;
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
#define UL_INTRA_THRESH 50
#define INVALID_ROW -1
void av1_first_pass(AV1_COMP *cpi, const struct lookahead_entry *source) {
@@ -1077,7 +1080,7 @@ void av1_first_pass(AV1_COMP *cpi, const struct lookahead_entry *source) {
else
recon_file = fopen(filename, "ab");
- (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
+ (void)emcc_fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
fclose(recon_file);
}
diff --git a/third_party/aom/build/cmake/aom_configure.cmake b/third_party/aom/build/cmake/aom_configure.cmake
index 9220a32..fb8bf9f 100644
--- a/third_party/aom/build/cmake/aom_configure.cmake
+++ b/third_party/aom/build/cmake/aom_configure.cmake
@@ -260,7 +260,7 @@ if(MSVC)
add_compiler_flag_if_supported("/WX")
endif()
else()
- require_c_flag("-std=c99" YES)
+ #require_c_flag("-std=c99" YES)
add_compiler_flag_if_supported("-Wall")
add_compiler_flag_if_supported("-Wdisabled-optimization")
add_compiler_flag_if_supported("-Wextra")
diff --git a/third_party/aom/examples/resize_util.c b/third_party/aom/examples/resize_util.c
index 5485691..e60ed86 100644
--- a/third_party/aom/examples/resize_util.c
+++ b/third_party/aom/examples/resize_util.c
@@ -45,6 +45,9 @@ static int parse_dim(char *v, int *width, int *height) {
return 1;
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
int main(int argc, char *argv[]) {
char *fin, *fout;
FILE *fpin, *fpout;
@@ -111,7 +114,7 @@ int main(int argc, char *argv[]) {
av1_resize_frame420(inbuf, width, inbuf_u, inbuf_v, width / 2, height,
width, outbuf, target_width, outbuf_u, outbuf_v,
target_width / 2, target_height, target_width);
- fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
+ emcc_fwrite(outbuf, target_width * target_height * 3 / 2, 1, fpout);
f++;
}
printf("%d frames processed\n", f);
diff --git a/third_party/aom/examples/scalable_encoder.c b/third_party/aom/examples/scalable_encoder.c
index 10d647e..fcf31e1 100644
--- a/third_party/aom/examples/scalable_encoder.c
+++ b/third_party/aom/examples/scalable_encoder.c
@@ -91,6 +91,9 @@ void usage_exit(void) {
exit(EXIT_FAILURE);
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
int frame_index, int flags, FILE *outfile) {
int got_pkts = 0;
@@ -105,7 +108,7 @@ static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
- if (fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile) !=
+ if (emcc_fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile) !=
pkt->data.frame.sz) {
die_codec(codec, "Failed to write compressed frame");
}
diff --git a/third_party/aom/ivfenc.c b/third_party/aom/ivfenc.c
index 80f4d14..d0e4e34 100644
--- a/third_party/aom/ivfenc.c
+++ b/third_party/aom/ivfenc.c
@@ -14,6 +14,9 @@
#include "aom/aom_encoder.h"
#include "aom_ports/mem_ops.h"
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
void ivf_write_file_header(FILE *outfile, const struct aom_codec_enc_cfg *cfg,
unsigned int fourcc, int frame_cnt) {
char header[32];
@@ -32,7 +35,7 @@ void ivf_write_file_header(FILE *outfile, const struct aom_codec_enc_cfg *cfg,
mem_put_le32(header + 24, frame_cnt); // length
mem_put_le32(header + 28, 0); // unused
- fwrite(header, 1, 32, outfile);
+ emcc_fwrite(header, 1, 32, outfile);
}
void ivf_write_frame_header(FILE *outfile, int64_t pts, size_t frame_size) {
@@ -41,12 +44,12 @@ void ivf_write_frame_header(FILE *outfile, int64_t pts, size_t frame_size) {
mem_put_le32(header, (int)frame_size);
mem_put_le32(header + 4, (int)(pts & 0xFFFFFFFF));
mem_put_le32(header + 8, (int)(pts >> 32));
- fwrite(header, 1, 12, outfile);
+ emcc_fwrite(header, 1, 12, outfile);
}
void ivf_write_frame_size(FILE *outfile, size_t frame_size) {
char header[4];
mem_put_le32(header, (int)frame_size);
- fwrite(header, 1, 4, outfile);
+ emcc_fwrite(header, 1, 4, outfile);
}
diff --git a/third_party/aom/test/decode_perf_test.cc b/third_party/aom/test/decode_perf_test.cc
index 3c93e7d..2d364ae 100644
--- a/third_party/aom/test/decode_perf_test.cc
+++ b/third_party/aom/test/decode_perf_test.cc
@@ -24,6 +24,11 @@
using ::testing::make_tuple;
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
namespace {
#define VIDEO_NAME 0
@@ -153,7 +158,7 @@ class AV1NewEncodeDecodePerfTest
// Write frame header and data.
ivf_write_frame_header(outfile_, out_frames_, pkt->data.frame.sz);
- ASSERT_EQ(fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_),
+ ASSERT_EQ(emcc_fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_),
pkt->data.frame.sz);
}
diff --git a/third_party/aom/test/film_grain_table_test.cc b/third_party/aom/test/film_grain_table_test.cc
index 0688146..dbb8e6b 100644
--- a/third_party/aom/test/film_grain_table_test.cc
+++ b/third_party/aom/test/film_grain_table_test.cc
@@ -5,6 +5,11 @@
#include "av1/encoder/grain_test_vectors.h"
#include "test/video_source.h"
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
void grain_equal(const aom_film_grain_t *expected,
const aom_film_grain_t *actual) {
EXPECT_EQ(expected->apply_grain, actual->apply_grain);
@@ -168,7 +173,7 @@ TEST_F(FilmGrainTableIOTest, ReadTruncatedFile) {
std::string grain_file;
FILE *file = libaom_test::GetTempOutFile(&grain_file);
- fwrite("deadbeef", 8, 1, file);
+ emcc_fwrite("deadbeef", 8, 1, file);
fclose(file);
ASSERT_EQ(AOM_CODEC_ERROR,
aom_film_grain_table_read(&table, grain_file.c_str(), &error_));
diff --git a/third_party/aom/test/resize_test.cc b/third_party/aom/test/resize_test.cc
index e1c4e9f..9c2bce8 100644
--- a/third_party/aom/test/resize_test.cc
+++ b/third_party/aom/test/resize_test.cc
@@ -22,6 +22,11 @@
// Enable(1) or Disable(0) writing of the compressed bitstream.
#define WRITE_COMPRESSED_STREAM 0
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
namespace {
#if WRITE_COMPRESSED_STREAM
@@ -55,13 +60,13 @@ static void write_ivf_file_header(const aom_codec_enc_cfg_t *const cfg,
mem_put_le32(header + 24, frame_cnt); /* length */
mem_put_le32(header + 28, 0); /* unused */
- (void)fwrite(header, 1, 32, outfile);
+ (void)emcc_fwrite(header, 1, 32, outfile);
}
static void write_ivf_frame_size(FILE *const outfile, const size_t size) {
char header[4];
mem_put_le32(header, static_cast<unsigned int>(size));
- (void)fwrite(header, 1, 4, outfile);
+ (void)emcc_fwrite(header, 1, 4, outfile);
}
static void write_ivf_frame_header(const aom_codec_cx_pkt_t *const pkt,
@@ -76,7 +81,7 @@ static void write_ivf_frame_header(const aom_codec_cx_pkt_t *const pkt,
mem_put_le32(header + 4, pts & 0xFFFFFFFF);
mem_put_le32(header + 8, pts >> 32);
- (void)fwrite(header, 1, 12, outfile);
+ (void)emcc_fwrite(header, 1, 12, outfile);
}
#endif // WRITE_COMPRESSED_STREAM
@@ -309,7 +314,7 @@ class ResizeInternalTestLarge : public ResizeTest {
// Write frame header and data.
write_ivf_frame_header(pkt, outfile_);
- (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
+ (void)emcc_fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
}
#endif
@@ -608,7 +613,7 @@ class ResizeCspTest : public ResizeTest {
// Write frame header and data.
write_ivf_frame_header(pkt, outfile_);
- (void)fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
+ (void)emcc_fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile_);
}
#endif
diff --git a/third_party/aom/test/y4m_test.cc b/third_party/aom/test/y4m_test.cc
index ad901d9..f24093f 100644
--- a/third_party/aom/test/y4m_test.cc
+++ b/third_party/aom/test/y4m_test.cc
@@ -19,6 +19,11 @@
#include "test/util.h"
#include "test/y4m_video_source.h"
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
namespace {
using std::string;
@@ -68,7 +73,7 @@ static void write_image_file(const aom_image_t *img, FILE *file) {
(plane ? (img->d_w + img->x_chroma_shift) >> img->x_chroma_shift
: img->d_w);
for (y = 0; y < h; ++y) {
- fwrite(buf, bytes_per_sample, w, file);
+ emcc_fwrite(buf, bytes_per_sample, w, file);
buf += stride;
}
}
diff --git a/third_party/aom/third_party/googletest/src/googletest/src/gtest.cc b/third_party/aom/third_party/googletest/src/googletest/src/gtest.cc
index 5a8932c..ac2c435 100644
--- a/third_party/aom/third_party/googletest/src/googletest/src/gtest.cc
+++ b/third_party/aom/third_party/googletest/src/googletest/src/gtest.cc
@@ -146,6 +146,11 @@
# define vsnprintf _vsnprintf
#endif // GTEST_OS_WINDOWS
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
namespace testing {
using internal::CountIf;
@@ -3867,7 +3872,7 @@ class ScopedPrematureExitFile {
// errors are ignored as there's nothing better we can do and we
// don't want to fail the test because of this.
FILE* pfile = posix::FOpen(premature_exit_filepath, "w");
- fwrite("0", 1, 1, pfile);
+ emcc_fwrite("0", 1, 1, pfile);
fclose(pfile);
}
}
diff --git a/third_party/aom/third_party/libwebm/mkvmuxer/mkvwriter.cc b/third_party/aom/third_party/libwebm/mkvmuxer/mkvwriter.cc
index 84655d8..0004093 100644
--- a/third_party/aom/third_party/libwebm/mkvmuxer/mkvwriter.cc
+++ b/third_party/aom/third_party/libwebm/mkvmuxer/mkvwriter.cc
@@ -14,6 +14,11 @@
#include <share.h> // for _SH_DENYWR
#endif
+extern "C" {
+ size_t
+ emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+}
+
namespace mkvmuxer {
MkvWriter::MkvWriter() : file_(NULL), writer_owns_file_(true) {}
@@ -32,7 +37,7 @@ int32 MkvWriter::Write(const void* buffer, uint32 length) {
if (buffer == NULL)
return -1;
- const size_t bytes_written = fwrite(buffer, 1, length, file_);
+ const size_t bytes_written = emcc_fwrite(buffer, 1, length, file_);
return (bytes_written == length) ? 0 : -1;
}
diff --git a/third_party/aom/tools_common.c b/third_party/aom/tools_common.c
index 7abc20c..fbc30bc 100644
--- a/third_party/aom/tools_common.c
+++ b/third_party/aom/tools_common.c
@@ -185,6 +185,9 @@ const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc) {
}
#endif // CONFIG_AV1_DECODER
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
void aom_img_write(const aom_image_t *img, FILE *file) {
int plane;
@@ -197,7 +200,7 @@ void aom_img_write(const aom_image_t *img, FILE *file) {
int y;
for (y = 0; y < h; ++y) {
- fwrite(buf, 1, w, file);
+ emcc_fwrite(buf, 1, w, file);
buf += stride;
}
}
diff --git a/third_party/aom/video_writer.c b/third_party/aom/video_writer.c
index 4e072c7..6b1ca54 100644
--- a/third_party/aom/video_writer.c
+++ b/third_party/aom/video_writer.c
@@ -66,10 +66,13 @@ void aom_video_writer_close(AvxVideoWriter *writer) {
}
}
+size_t
+emcc_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
int aom_video_writer_write_frame(AvxVideoWriter *writer, const uint8_t *buffer,
size_t size, int64_t pts) {
ivf_write_frame_header(writer->file, pts, size);
- if (fwrite(buffer, 1, size, writer->file) != size) return 0;
+ if (emcc_fwrite(buffer, 1, size, writer->file) != size) return 0;
++writer->frame_count;