How to compile tf-api?

Compiling using the docker image latest-devel and devel.

When I try to compile:

bazel build --override_repository="llvm-raw=${LLVM_SRC}" -c opt tensorflow/compiler/mlir:tf-opt

I get this error:

ERROR: /root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/external/llvm-project/llvm/BUILD.bazel:219:11: no such package '@llvm_zlib//': The repository '@llvm_zlib' could not be resolved: Repository '@llvm_zlib' is not defined and referenced by '@llvm-project//llvm:Support'
ERROR: Analysis of target '//tensorflow/compiler/mlir:tf-opt' failed; build aborted:

How do I compile it?

Standalone code to reproduce the issue

# run docker (latest-devel)
docker run -it -w /tensorflow_src -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" tensorflow/tensorflow:latest-devel bash
git pull
cd $HOME

# install dependencies
sudo apt-get update
sudo apt-get install -y build-essential gdb lcov pkg-config \
      libbz2-dev libffi-dev libgdbm-dev libgdbm-compat-dev liblzma-dev \
      libncurses-dev libreadline-dev libsqlite3-dev libssl-dev \
      lzma lzma-dev tk-dev uuid-dev zlib1g-dev patchelf
sudo apt-get install -y python3-dev python3-pip
pip install -U --user pip

# install llvm
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 17
sudo ln -sf /usr/bin/clang-17 /usr/bin/clang

# install local python 3.11
mkdir .python3.11
mkdir src
cd src
wget https://www.python.org/ftp/python/3.11.9/Python-3.11.9.tgz
tar -zxvf Python-3.11.9.tgz
cd Python-3.11.9
make clean
./configure --prefix=$HOME/.python3.11
make
make install
export PATH=$PATH:$HOME/.python3.11/bin
cd $HOME

# install virtualenv and activate
pip3.11 install virtualenv
$HOME/.python3.11/bin/virtualenv --python=$HOME/.python3.11/bin/python3.11 $HOME/.tfdev
source $HOME/.tfdev/bin/activate

# get llvm src
cd $HOME
git clone https://github.com/llvm/llvm-project.git
cd llvm-project
git checkout llvmorg-17.0.6
export LLVM_SRC=$HOME/llvm-project
touch ${LLVM_SRC}/BUILD.bazel ${LLVM_SRC}/WORKSPACE

# compile tensorflow
cd /tensorflow_src
export TF_PYTHON_VERSION=3.11
export PYTHON_BIN_PATH=$(which python)
export PYTHON_LIB_PATH=$HOME/.tfdev/lib/python3.11/site-packages
export TF_NEED_ROCM=0
export TF_NEED_CUDA=0
export TF_NEED_CLANG=1
export CLANG_COMPILER_PATH=$(which clang)
export CC_OPT_FLAGS=-Wno-sign-compare
export TF_SET_ANDROID_WORKSPACE=0
./configure

# build tensorflow mlir tools
bazel build --override_repository="llvm-raw=${LLVM_SRC}" -c opt tensorflow/compiler/mlir:tf-opt

Relevant log output

INFO: Reading 'startup' options from /tensorflow_src/.bazelrc: --windows_enable_symlinks
INFO: Options provided by the client:
  Inherited 'common' options: --isatty=1 --terminal_columns=170
INFO: Reading rc options for 'build' from /tensorflow_src/.bazelrc:
  Inherited 'common' options: --experimental_repo_remote_exec
INFO: Reading rc options for 'build' from /tensorflow_src/.bazelrc:
  'build' options: --define framework_shared_object=true --define tsl_protobuf_header_only=true --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=standalone -c opt --announce_rc --define=grpc_no_ares=true --noincompatible_remove_legacy_whole_archive --features=-force_no_whole_archive --enable_platform_specific_config --define=with_xla_support=true --config=short_logs --config=v2 --define=no_aws_support=true --define=no_hdfs_support=true --experimental_cc_shared_library --experimental_link_static_libraries_once=false --incompatible_enforce_config_setting_visibility
INFO: Reading rc options for 'build' from /tensorflow_src/.tf_configure.bazelrc:
  'build' options: --action_env PYTHON_BIN_PATH=/root/.tfdev/bin/python --action_env PYTHON_LIB_PATH=/root/.tfdev/lib/python3.11/site-packages --python_path=/root/.tfdev/bin/python --action_env CLANG_COMPILER_PATH=/usr/lib/llvm-17/bin/clang --repo_env=CC=/usr/lib/llvm-17/bin/clang --repo_env=BAZEL_COMPILER=/usr/lib/llvm-17/bin/clang --copt=-Wno-gnu-offsetof-extensions
INFO: Found applicable config definition build:short_logs in file /tensorflow_src/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file /tensorflow_src/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:linux in file /tensorflow_src/.bazelrc: --host_copt=-w --copt=-Wno-all --copt=-Wno-extra --copt=-Wno-deprecated --copt=-Wno-deprecated-declarations --copt=-Wno-ignored-attributes --copt=-Wno-array-bounds --copt=-Wunused-result --copt=-Werror=unused-result --copt=-Wswitch --copt=-Werror=switch --copt=-Wno-error=unused-but-set-variable --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib --define=INCLUDEDIR=$(PREFIX)/include --define=PROTOBUF_INCLUDE_PATH=$(PREFIX)/include --cxxopt=-std=c++17 --host_cxxopt=-std=c++17 --config=dynamic_kernels --experimental_guard_against_concurrent_changes
INFO: Found applicable config definition build:dynamic_kernels in file /tensorflow_src/.bazelrc: --define=dynamic_loaded_kernels=true --copt=-DAUTOLOAD_DYNAMIC_KERNELS
INFO: Repository stablehlo instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:947:28: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:88:14: in _initialize_third_party
  /tensorflow_src/third_party/stablehlo/workspace.bzl:11:20: in repo
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository double_conversion instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:954:21: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:639:20: in _tf_repositories
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository zlib instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:954:21: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:487:20: in _tf_repositories
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository flatbuffers instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:947:28: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:70:16: in _initialize_third_party
  /tensorflow_src/third_party/flatbuffers/workspace.bzl:14:20: in repo
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository farmhash_archive instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:947:28: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:69:13: in _initialize_third_party
  /tensorflow_src/third_party/farmhash/workspace.bzl:14:20: in repo
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository com_googlesource_code_re2 instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:954:21: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:275:20: in _tf_repositories
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository snappy instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:954:21: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:506:20: in _tf_repositories
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository gif instantiated at:
  /tensorflow_src/WORKSPACE:111:14: in <toplevel>
  /tensorflow_src/tensorflow/workspace2.bzl:954:21: in workspace
  /tensorflow_src/tensorflow/workspace2.bzl:340:20: in _tf_repositories
  /tensorflow_src/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /tensorflow_src/third_party/repo.bzl:89:35: in <toplevel>
ERROR: /root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/external/llvm-project/llvm/BUILD.bazel:219:11: no such package '@llvm_zlib//': The repository '@llvm_zlib' could not be resolved: Repository '@llvm_zlib' is not defined and referenced by '@llvm-project//llvm:Support'
ERROR: Analysis of target '//tensorflow/compiler/mlir:tf-opt' failed; build aborted:
INFO: Elapsed time: 41.334s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (253 packages loaded, 3347 targets configured)
    currently loading: tensorflow/compiler/mlir/lite/schema
    Fetching repository @eigen_archive; starting
    Fetching https://storage.googleapis.com/.../github.com/google/flatbuffers/archive/e6463926479bd6b330cbcf673f7e917803fd5831.tar.gz; 1.7 MiB (76.0%)
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/github.com/openxla/stablehlo/archive/14e2323f0ee3d308c1384fdb806dc6d0c98b16ca.zip; 1.4 MiB (8.2%)
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/double-conversion/archive/v3.2.0.tar.gz
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/zlib.net/fossils/zlib-1.2.13.tar.gz
    Fetching /root/.cache/bazel/_bazel_root/43801f1e35f242fb634ebbc6079cf6c5/external/eigen_archive; Extracting eigen-c1d637433e3b3f9012b226c2c9125c494b470ae6.tar.gz
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/farmhash/archive/0d859a811870d10f53a594927d0d0b97573ad06d.tar.gz
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/re2/archive/03da4fc0857c285e3a26782f6bc8931c4c950df4.tar.gz ... (10 fetches)