Skip to content

Commit c1e2f73

Browse files
committed
[llvm][NFC] expose LLVM_HAVE_TF_API through llvm-config.h
Summary: This allows users of the llvm library discover whether llvm was built with the tensorflow c API dependency, which helps if using the TFUtils wrapper, for example. We don't do the same for the LLVM_HAVE_TF_AOT flag, because that does not expose any API. Reviewers: mehdi_amini, davidxl Subscribers: mgorny, aaron.ballman, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D83746
1 parent c6e8bf7 commit c1e2f73

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

llvm/CMakeLists.txt

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,21 @@ configure_file(
832832
${LLVM_INCLUDE_DIR}/llvm/Config/Targets.def
833833
)
834834

835+
# For up-to-date instructions for installing the Tensorflow dependency, refer to
836+
# the bot setup script: https://github.com/google/ml-compiler-opt/blob/master/buildbot/buildbot_init.sh
837+
# In this case, the latest C API library is available for download from
838+
# https://www.tensorflow.org/install/lang_c.
839+
# We will expose the conditional compilation variable,
840+
# LLVM_HAVE_TF_API, through llvm-config.h, so that a user of the LLVM library may
841+
# also leverage the dependency.
842+
set(TENSORFLOW_C_LIB_PATH "" CACHE PATH "Path to TensorFlow C library install")
843+
find_library(tensorflow_c_api tensorflow PATHS ${TENSORFLOW_C_LIB_PATH}/lib)
844+
845+
if (tensorflow_c_api)
846+
set(LLVM_HAVE_TF_API "ON" CACHE BOOL "Full Tensorflow API available")
847+
include_directories(${TENSORFLOW_C_LIB_PATH}/include)
848+
endif()
849+
835850
# Configure the three LLVM configuration header files.
836851
configure_file(
837852
${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/config.h.cmake
@@ -972,27 +987,18 @@ set(TENSORFLOW_AOT_PATH "" CACHE PATH "Path to TensorFlow pip install dir")
972987

973988
if (NOT TENSORFLOW_AOT_PATH STREQUAL "")
974989
set(LLVM_HAVE_TF_AOT "ON" CACHE BOOL "Tensorflow AOT available")
975-
set(TENSORFLOW_AOT_COMPILER
976-
"${TENSORFLOW_AOT_PATH}/../../../../bin/saved_model_cli"
977-
CACHE PATH "Path to the Tensorflow AOT compiler")
990+
set(TENSORFLOW_AOT_COMPILER
991+
"${TENSORFLOW_AOT_PATH}/../../../../bin/saved_model_cli"
992+
CACHE PATH "Path to the Tensorflow AOT compiler")
993+
# Unlike the LLVM_HAVE_TF_API case, we don't need to expose this through
994+
# llvm-config.h, because it's an internal implementation detail. A user of the llvm library that wants to also
995+
# use the TF AOT compiler may do so through their custom build step.
978996
add_definitions("-DLLVM_HAVE_TF_AOT")
979997
include_directories(${TENSORFLOW_AOT_PATH}/include)
980998
add_subdirectory(${TENSORFLOW_AOT_PATH}/xla_aot_runtime_src
981999
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/tf_runtime)
9821000
endif()
9831001

984-
set(TENSORFLOW_C_LIB_PATH "" CACHE PATH "Path to TensorFlow C library install")
985-
find_library(tensorflow_c_api tensorflow PATHS ${TENSORFLOW_C_LIB_PATH}/lib)
986-
987-
# Similar to the above Tensorflow dependency, please refer to the same script.
988-
# In this case, the latest C API library is available for download from
989-
# https://www.tensorflow.org/install/lang_c
990-
if (tensorflow_c_api)
991-
set(LLVM_HAVE_TF_API "ON" CACHE BOOL "Full Tensorflow API available")
992-
add_definitions("-DLLVM_HAVE_TF_API")
993-
include_directories(${TENSORFLOW_C_LIB_PATH}/include)
994-
endif()
995-
9961002
# Put this before tblgen. Else we have a circular dependence.
9971003
add_subdirectory(lib/Demangle)
9981004
add_subdirectory(lib/Support)

llvm/include/llvm/Analysis/Utils/TFUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#ifndef LLVM_ANALYSIS_UTILS_TFUTILS_H
1010
#define LLVM_ANALYSIS_UTILS_TFUTILS_H
1111

12+
#include "llvm/Config/config.h"
13+
1214
#ifdef LLVM_HAVE_TF_API
1315
#include "tensorflow/c/c_api.h"
1416
#include "llvm/IR/LLVMContext.h"

llvm/include/llvm/Config/llvm-config.h.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@
7979
*/
8080
#cmakedefine01 LLVM_FORCE_ENABLE_STATS
8181

82+
/* Define if LLVM was built with a dependency to the libtensorflow dynamic library */
83+
#cmakedefine LLVM_HAVE_TF_API
84+
8285
#endif

llvm/unittests/Analysis/InlineSizeEstimatorAnalysisTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using namespace llvm;
2626
extern const char *TestMainArgv0;
2727
extern cl::opt<std::string> TFIR2NativeModelPath;
2828

29-
#if LLVM_HAVE_TF_API
29+
#ifdef LLVM_HAVE_TF_API
3030
static std::string getModelPath() {
3131
SmallString<128> InputsDir = unittest::getInputFileDirectory(TestMainArgv0);
3232
llvm::sys::path::append(InputsDir, "ir2native_x86_64_model");
@@ -87,13 +87,13 @@ define internal i32 @top() {
8787
)IR");
8888

8989
FunctionAnalysisManager FAM = buildFAM();
90-
#if LLVM_HAVE_TF_API
90+
#ifdef LLVM_HAVE_TF_API
9191
TFIR2NativeModelPath = getModelPath();
9292
#endif
9393

9494
InlineSizeEstimatorAnalysis FA;
9595
auto SizeEstimate = FA.run(*M->getFunction("branches"), FAM);
96-
#if LLVM_HAVE_TF_API
96+
#ifdef LLVM_HAVE_TF_API
9797
EXPECT_GT(*SizeEstimate, 0);
9898
#else
9999
EXPECT_FALSE(SizeEstimate.hasValue());

0 commit comments

Comments
 (0)