1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-20 03:23:01 +02:00
llvm-mirror/examples/ThinLtoJIT/bench
Stefan Gränitz a7a66b3792 Add ThinLtoJIT example
Summary:
Prototype of a JIT compiler that utilizes ThinLTO summaries to compile modules ahead of time. This is an implementation of the concept I presented in my "ThinLTO Summaries in JIT Compilation" talk at the 2018 Developers' Meeting: http://llvm.org/devmtg/2018-10/talk-abstracts.html#lt8

Upfront the JIT first populates the *combined ThinLTO module index*, which provides fast access to the global call-graph and module paths by function. Next, it loads the main function's module and compiles it. All functions in the module will be emitted with prolog instructions that *fire a discovery flag* once execution reaches them. In parallel, the *discovery thread* is busy-watching the existing flags. Once it detects one has fired, it uses the module index to find all functions that are reachable from it within a given number of calls and submits their defining modules to the compilation pipeline.

While execution continues, more flags are fired and further modules added. Ideally the JIT can be tuned in a way, so that in the majority of cases the code on the execution path can be compiled ahead of time. In cases where it doesn't work, the JIT has a *definition generator* in place that loads modules if missing functions are reached.

Reviewers: lhames, dblaikie, jfb, tejohnson, pree-jackie, AlexDenisov, kavon

Subscribers: mgorny, mehdi_amini, inglorion, hiraditya, steven_wu, dexonsmith, arphaman, jfb, merge_guards_bot, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D72486
2020-02-01 20:25:09 +01:00

101 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
#set -x
if [ $# -gt 2 ]; then
TOOLS_DIR="$1"
SOURCE_DIR="$2"
MAIN_SOURCE_FILE="$3"
else
echo "Usage: bench <path to llvm binaries> <path to c-sources> <main source file> [<override sysroot>]"
exit 1
fi
if [ $# -gt 3 ]; then
SYS_ROOT="$4"
else
SYS_ROOT="/"
fi
function check_tool ()
{
if [ -e "${TOOLS_DIR}/$1" ]; then
echo "Found: $1"
else
echo "!!! Cannot find required tool, please provide it in the LLVM binaries folder: $1"
fi
}
check_tool lli
check_tool SpeculativeJIT
check_tool ThinLtoJIT
SKIP_BITCODE_GEN=0
if [[ -e bc-default || -e bc-thinlto || -e ll-default || -e ll-thinlto ]]; then
echo "Skipping bitcode generation: output directories existing"
echo "Please clean up manually: rm -R bc-default bc-thinlto ll-default ll-thinlto"
SKIP_BITCODE_GEN=1
else
check_tool clang
check_tool llvm-dis
check_tool llvm-lto
mkdir bc-default
mkdir bc-thinlto
mkdir ll-default
mkdir ll-thinlto
fi
ROOT_DIR=$(pwd)
ALL_BITCODE_FILES=""
MAIN_FILE_BASENAME=$(basename "${MAIN_SOURCE_FILE%.c*}")
LLI_EXTRA_MODULES=""
for f in ${SOURCE_DIR}/*.c* ; do
BASE_NAME=$(basename "${f%.c*}")
if [ ${SKIP_BITCODE_GEN} -eq 0 ]; then
echo "Compile: $f -> ${BASE_NAME}.bc"
${TOOLS_DIR}/clang -c -I ${SOURCE_DIR} ${CFLAGS} -isysroot ${SYS_ROOT} -emit-llvm \
-o "bc-default/${BASE_NAME}.bc" "$f"
${TOOLS_DIR}/clang -c -I ${SOURCE_DIR} ${CFLAGS} -isysroot ${SYS_ROOT} -flto=thin \
-o "bc-thinlto/${BASE_NAME}.bc" "$f"
echo "Disassemble ${BASE_NAME}.bc -> ${BASE_NAME}.ll"
${TOOLS_DIR}/llvm-dis bc-default/${BASE_NAME}.bc -o ll-default/${BASE_NAME}.ll
${TOOLS_DIR}/llvm-dis bc-thinlto/${BASE_NAME}.bc -o ll-thinlto/${BASE_NAME}.ll
fi
ALL_BITCODE_FILES="${ALL_BITCODE_FILES} ${BASE_NAME}.bc"
if [ "${BASE_NAME}" != "${MAIN_FILE_BASENAME}" ]; then
LLI_EXTRA_MODULES="${LLI_EXTRA_MODULES} -extra-module=${BASE_NAME}.bc"
fi
done
if [ ${SKIP_BITCODE_GEN} -eq 0 ]; then
echo "Link global index file: index.thinlto.bc"
cd ${ROOT_DIR}/bc-thinlto
${TOOLS_DIR}/llvm-lto --thinlto -o ${ROOT_DIR}/bc-thinlto/index ${ALL_BITCODE_FILES}
echo "Disassemble global index file: index.thinlto.ll"
cd ${ROOT_DIR}/ll-thinlto
${TOOLS_DIR}/llvm-dis -o index.thinlto.ll ${ROOT_DIR}/bc-thinlto/index.thinlto.bc
fi
set -x
cd ${ROOT_DIR}/bc-default
time (${TOOLS_DIR}/clang -o ${MAIN_FILE_BASENAME} -O0 ${LDFLAGS} ${ALL_BITCODE_FILES} && ./${MAIN_FILE_BASENAME} ${EXEC_ARGS} 1>/dev/null)
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=mcjit "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-mcjit "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy -compile-threads=8 "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy -per-module-lazy "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy -per-module-lazy -compile-threads=8 "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy -per-module-lazy -compile-threads=8 -O1 "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/lli ${LLI_EXTRA_MODULES} -jit-kind=orc-lazy -per-module-lazy -compile-threads=8 -O0 "${MAIN_FILE_BASENAME}.bc" ${EXEC_ARGS} 1>/dev/null
time ${TOOLS_DIR}/SpeculativeJIT -num-threads=8 ${ALL_BITCODE_FILES} --args ${EXEC_ARGS} 1>/dev/null
cd ${ROOT_DIR}/bc-thinlto
#time (${TOOLS_DIR}/clang -flto=thin -o test ${ALL_BITCODE_FILES} && ./test ${EXEC_ARGS} 1>/dev/null)
time ${TOOLS_DIR}/ThinLtoJIT index.thinlto.bc --args ${EXEC_ARGS} 1>/dev/null