diff --git a/build.sh b/build.sh index 2e6d2167b3efe2796a58548eb8fcedb0e28d11c9..69736df350849a2452984b0a8e3bad1359e596fa 100755 --- a/build.sh +++ b/build.sh @@ -5,7 +5,6 @@ C_COMPILER_PATH=gcc CXX_COMPILER_PATH=g++ # Initialize our own variables: -enable_acpo="1" enable_autotuner="1" buildtype=RelWithDebInfo backends="all" @@ -26,6 +25,9 @@ build_dir_name="build" install_dir_name="install" build_prefix="$dir/$build_dir_name" install_prefix="$dir/$install_dir_name" +# ACPO specific flags +enable_acpo="1" # Support ACPO. 0: disable 1: enable +acpo_aot=0 # Support ACPO AOT recompile. 0: disable 1: enable # Use 8 threads for builds and tests by default. Use more threads if possible, # but avoid overloading the system by using up to 50% of available cores. @@ -72,13 +74,15 @@ EOF # Process command-line options. Remember the options for passing to the # containerized build script. -while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do +while getopts :aAb:d:ceEhiI:j:oOrstvfX: optchr; do case "$optchr" in a) enable_autotuner="0" ;; A) enable_acpo="0" + acpo_aot=0 + echo "$0: ACPO project is disabled!" ;; b) buildtype="$OPTARG" @@ -126,6 +130,9 @@ while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do o) install_toolchain_only=1 ;; + O) + acpo_aot=1 + ;; r) clean=1 ;; @@ -164,6 +171,20 @@ if [ -z "$gold" -o ! -x "$gold" ]; then exit 1 fi +if [ $acpo_aot -eq 1 ] && [ $enable_acpo -eq 0 ]; then + echo "$0: when use '-A' to disable acpo cant' use '-O' to enable acpo_aot." + exit 1 +fi + +if [ $acpo_aot -eq "1" ]; then + echo "enable ACPO aot" + # TensorFlow environment variables must be set if AOT is requested. + CMAKE_OPTIONS="$CMAKE_OPTIONS \ + -DACPO_AOT=ON \ + -DACPO_AOT_COMPILE=ON \ + -DTENSORFLOW_AOT_PATH=${TENSORFLOW_AOT_PATH}" +fi + # If the invocation does not force a particular binutils installation, check # that we are using an acceptable version. if [ -n "$BINUTILS_INCDIR" ]; then diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index b0afb47a7243067918aaf154612583e1d52051bd..f76e07292a89e28cb747198913324de62357beff 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -1004,6 +1004,13 @@ set(TENSORFLOW_AOT_PATH "" CACHE PATH "Path to TensorFlow pip install dir") set(LLVM_INLINER_MODEL_PATH "" CACHE PATH "Path to the inliner model") set(ACPO_AOT OFF CACHE BOOL "Whether or not ACPO AOT is enabled") +if (ACPO_AOT_COMPILE STREQUAL "ON") + find_file(TENSORFLOW_XLA_PATH xla_aot_runtime_src ${TENSORFLOW_AOT_PATH}) + if(TENSORFLOW_XLA_PATH STREQUAL "") + message(FATAL_ERROR "TENSORFLOW_AOT_PATH:${TENSORFLOW_AOT_PATH} don't exist") + endif() +endif() + if (NOT TENSORFLOW_AOT_PATH STREQUAL "") set(LLVM_HAVE_TF_AOT "ON" CACHE BOOL "Tensorflow AOT available") set(TENSORFLOW_AOT_COMPILER diff --git a/llvm/cmake/modules/AcpoAot.cmake b/llvm/cmake/modules/AcpoAot.cmake new file mode 100644 index 0000000000000000000000000000000000000000..010da1bacf1171798abd4ba7e9968a687337e996 --- /dev/null +++ b/llvm/cmake/modules/AcpoAot.cmake @@ -0,0 +1,41 @@ +# ACPO AOT compile cmake file +set(ACPO_ABS_PATH "" CACHE PATH "Absolute Path to ACPO") +set(LLVM_ACPO_MODEL_NAMES "") +set(ACPO_MODEL_PATH "" CACHE PATH "Model Path in ACPO_ABS_PATH") +set(LLVM_ACPO_MODEL_SIGNATURES "") +set(CMAKE_INSTALL_RPATH_USE_LINK_PATH True) +# Test demo + +foreach (model_path IN LISTS ACPO_MODEL_PATH) + set(LLVM_ACPO_MODEL_PATHS ${LLVM_ACPO_MODEL_PATHS};${ACPO_ABS_PATH}/${model_path}) +endforeach() + +function(acpo_tf_find_and_compile model tag_set signature_def_key fname cpp_class) + set(prefix ${CMAKE_CURRENT_BINARY_DIR}/${fname}) + set(obj_file ${prefix}.o) + set(hdr_file ${prefix}.h) + string(REPLACE "lib" "include/llvm" new_hdr_file "${hdr_file}") + message("Using model at " ${model}) + add_custom_command(OUTPUT ${obj_file} ${hdr_file} + COMMAND ${TENSORFLOW_AOT_COMPILER} aot_compile_cpu + --multithreading false + --dir ${model} + --tag_set ${tag_set} + --signature_def_key ${signature_def_key} + --output_prefix ${prefix} + --cpp_class ${cpp_class} + --target_triple ${LLVM_HOST_TRIPLE} + COMMAND ${CMAKE_COMMAND} -E copy ${hdr_file} ${new_hdr_file} + ) + + # Aggregate the objects so that results of different tf_compile calls may be + # grouped into one target. + set(GENERATED_OBJS ${GENERATED_OBJS} ${obj_file} PARENT_SCOPE) + set_source_files_properties(${obj_file} PROPERTIES + GENERATED 1 EXTERNAL_OBJECT 1) + + set(GENERATED_HEADERS ${GENERATED_HEADERS} ${hdr_file} PARENT_SCOPE) + set_source_files_properties(${hdr_file} PROPERTIES + GENERATED 1) + +endfunction() diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt index 961b5037dd4899a361e3e9ea8df27eced3a7163f..d05f5f3a3ac7fe8fdd3a9a7212010d0072a566ee 100644 --- a/llvm/lib/Analysis/CMakeLists.txt +++ b/llvm/lib/Analysis/CMakeLists.txt @@ -5,6 +5,9 @@ if (DEFINED LLVM_HAVE_TF_AOT OR LLVM_HAVE_TFLITE) set(LLVM_INLINER_MODEL_CURRENT_URL "" CACHE STRING "URL to download the LLVM inliner model") if (ACPO_AOT) + if (ACPO_AOT_COMPILE STREQUAL "ON") + include(AcpoAot) + endif() foreach (model_name model_path model_signature IN ZIP_LISTS LLVM_ACPO_MODEL_NAMES LLVM_ACPO_MODEL_PATHS LLVM_ACPO_MODEL_SIGNATURES) set(fname ${model_name}CompiledModel) string(TOUPPER ${fname} fname_allcaps) @@ -15,17 +18,26 @@ if (DEFINED LLVM_HAVE_TF_AOT OR LLVM_HAVE_TFLITE) set(LLVM_OVERRIDE_MODEL_OBJECT_${fname_allcaps} ${LLVM_ACPO_OVERRIDE_PATH}/${fname}-${arch_allcaps}.o) endif() - - tf_find_and_compile( - ${model_path} - ${LLVM_INLINER_MODEL_CURRENT_URL} - ${LLVM_INLINER_MODEL_PATH_DEFAULT} - "" - serve - "${model_signature}" - "${fname}" - "llvm::${fname}" - ) + if (ACPO_AOT_COMPILE STREQUAL "ON") + acpo_tf_find_and_compile( + ${model_path} + serve + "${model_signature}" + "${fname}" + "llvm::${fname}" + ) + else() + tf_find_and_compile( + ${model_path} + ${LLVM_INLINER_MODEL_CURRENT_URL} + ${LLVM_INLINER_MODEL_PATH_DEFAULT} + "" + serve + "${model_signature}" + "${fname}" + "llvm::${fname}" + ) + endif() endforeach() endif() if (DEFINED LLVM_HAVE_TF_AOT)