Fails to build with llvm-project repository override
This happens because vars.bzl is created by the implementation function of llvm_configure() which is not called in the case of a repository override. I think the instructions should be modified to override llvm-raw instead which only requires creating a WORKSPACE and BUILD.bazel in the LLVM repo and does not need to create an overlay. I'm happy to provide a patch for that. Hi, @RoboTux 

Apologize for the delay and It seems like there is some issue with the `local_repository` rule in `bazel` is for external bazel repositories only. To use a non-bazel external repository, we need to use `new_local_repository `which takes `build_file` as an argument.

Bazel has the `new_local_repository` check this link `https://bazel.build/reference/be/workspace#new_local_repository` which allows you to use a local directory as a repo.

You will need to change the `tf_http_archive(name = "llvm-project",...)` section in `tensorflow/workspace.bzl `into `new_local_repository`, and add necessary BUILD files for llvm, mlir, and mlir/tests directory in your `local llvm directory.` you can check this [discussion](https://groups.google.com/a/tensorflow.org/g/mlir/c/EVWnlwPKLT4) and stack overflow [answer](https://stackoverflow.com/questions/42918662/bazel-error-building-tensorflow-with-local-llvm-repository), I hope it will help you to resolve your issue.

If something is missing in the instructions and you've workaround for it then PR will be welcomed from your end so please submit PR for it ? Thank you for noticing the issue in our instructions. Thank you! Any reason why we don't override llvm-raw instead of llvm-project? When I tried it seemed to work fine and avoid the issues mentionned here. The patch I have is to change the instructions to override llvm-raw instead of llvm-project. Hi, dude! Did u solve this problem? I ran into the same problem as you. 

I followed the steps given in the [readme](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/compiler/mlir), and roll back the version of llvm-project to [corresponding version](https://github.com/tensorflow/tensorflow/tree/master/third_party/llvm).

#### When I start build
```shell
 bazel clean --expunge

bazel build --override_repository=llvm-project=$LLVM_BAZEL_OVERLAY \
  -c opt tensorflow/compiler/mlir:tf-opt

bazel build --override_repository=llvm-project=$LLVM_BAZEL_OVERLAY \
  -c opt tensorflow/compiler/mlir:tf-mlir-translate
```

#### I got Error output below
```shell
ERROR: /private/var/tmp/_bazel_isolateya/af219aaee912b96bcf0876417404d886/external/llvm-project/mlir/BUILD.bazel:5904:11: error loading package '@llvm-project//llvm': at /private/var/tmp/_bazel_isolateya/af219aaee912b96bcf0876417404d886/external/llvm-project/llvm/config.bzl:8:5: cannot load '@llvm-project//:vars.bzl': no such file and referenced by '@llvm-project//mlir:Transforms'
ERROR: Analysis of target '//tensorflow/compiler/mlir:tf-opt' failed; build aborted: 
INFO: Elapsed time: 38.684s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (122 packages loaded, 463 targets \
configured)
    currently loading: @com_google_protobuf// ... (4 packages)
    Fetching https://storage.googleapis.com/.../nsync/archive/1.25.0.tar.gz
    Fetching https://storage.googleapis.com/.../archive/v2.0.6.tar.gz
    Fetching https://storage.googleapis.com/...65082ee350509af1d113344d.tar.gz
```
```shell
ERROR: /Volumes/back/mlir_project/tensorflow/tensorflow/compiler/mlir/BUILD:219:13: error loading package '@llvm-project//llvm': at /private/var/tmp/_bazel_isolateya/af219aaee912b96bcf0876417404d886/external/llvm-project/llvm/config.bzl:8:5: cannot load '@llvm-project//:vars.bzl': no such file and referenced by '//tensorflow/compiler/mlir:tf-mlir-translate'
ERROR: Analysis of target '//tensorflow/compiler/mlir:tf-mlir-translate' failed; build aborted: Analysis failed
INFO: Elapsed time: 27.395s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (54 packages loaded, 247 targets configured)
    currently loading: @llvm-project//llvm
    Fetching @local_config_cc; Building xcode-locator
    Fetching https://storage.googleapis.com/.../abseil-cpp/archive/273292d1cfc0a94a65082ee350509af1d113344d.tar.gz
```

#### Standalone code to reproduce the issue
```shell
LLVM_SRC=/Volumes/back/mlir_project/llvm-project
LLVM_BAZEL_OVERLAY=${LLVM_SRC}/bazel # Note: this can be anywhere
mkdir -p ${LLVM_BAZEL_OVERLAY}
# This will symlink your LLVM sources with the BUILD files to be usable by Bazel.
python ${LLVM_SRC}/utils/bazel/overlay_directories.py \
    --src ${LLVM_SRC} \
    --overlay ${LLVM_SRC}/utils/bazel/llvm-project-overlay/ \
    --target ${LLVM_BAZEL_OVERLAY}
touch ${LLVM_BAZEL_OVERLAY}/BUILD.bazel ${LLVM_BAZEL_OVERLAY}/WORKSPACE
echo 'llvm_targets = ["X86","NVPTX","AMDGPU","AArch64","ARM"]' > ${LLVM_BAZEL_OVERLAY}/llvm/targets.bzl
```

#### Environment
```shell
bazel 5.3.0
openjdk 11.0.16.1
python 3.9.16
llvm 15.0.5
OS mac(M2)
```

 Hi, @isolateFeng 

Apologize for the delayed response and May I know have you tried this [workaround](https://github.com/tensorflow/tensorflow/issues/59739#issuecomment-1454792939), If yes is it resolving your issue or not ? I see @RoboTux has submitted one PR [59942](https://github.com/tensorflow/tensorflow/pull/59942) to solve this issue so we'll wait to merge that PR, meanwhile if you did not follow above workaround could you please give it try ? Thank you! Hi, @gaikwadrahul8 

Sorry for replying so late. I did try this, but I'm not sure if I tried it correctly.

I modified the `tensorflow/workspace.bzl`(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/workspace2.bzl), added the following content to it, but it did't work

```shell
tf_http_archive(
      name = "llvm-project",
      urls = [
          /Volumes/back/mlir_project/llvm-project
      ],
      build_file = str(Label("//third_party/llvm:llvm.BUILD")),
  )
```

I just don't know how to modify the  `tensorflow/workspace.bzl`  correctly, for I don't know anything about bazel I saw RoboTux's  PR https://github.com/tensorflow/tensorflow/pull/59942 had  failed merge test, maybe I'll try it (qwq

or could someone give me an alternative method that I can try > I saw RoboTux's PR #59942 had failed merge test, maybe I'll try it (qwq
> 
> or could someone give me an alternative method that I can try

I don't think the failure is related to the patch since it fails to find patchelf and the patch only changes the documentation which does not require patchelf to build obviously. I'd be interested to know if the new instructions suggested in the patch fail to work for you.

Best regards,
Thomas Hi, Thomas
I'm sorry to inform you that it(PR https://github.com/tensorflow/tensorflow/pull/59942) didn't work  for me ：(
it returns me the ERROR below
```shell
INFO: Found applicable config definition build:short_logs in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:macos in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --apple_platform_type=macos --copt=-DGRPC_BAZEL_BUILD --copt=-w --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
ERROR: no such package '@llvm-raw//utils/bazel': The repository's path is "llvm-raw" (absolute: "/Volumes/back/mlir_project/llvm-project/bazel") but it does not exist or is not a directory.
INFO: Elapsed time: 2.574s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
```
I will try to try other ways I can find, thanks a lot

Best regards,
Tingfeng Why does it mention (absolute: "/Volumes/back/mlir_project/llvm-project/bazel")  insteal of /Volumes/back/mlir_project/llvm-project? Is your llvm tree checkout out in the bazel directory? I used the command `git checkout 31c39439a894` in the llvm-project main folder ( download at 22:30 3.7.2023) > I used the command `git checkout 31c39439a894` in the llvm-project main folder ( download at 22:30 3.7.2023)

What was the bazel command that you ran? Note: there is a mistake in my patch, the bazel build line should be LLVM_SRC not LLVM_BAZEL_OVERLAY There have been too many things in the course recently, I sincerely apologize for my late answer

### at the first I used 
```shell
LLVM_SRC=/Volumes/back/mlir_project/llvm-project
touch ${LLVM_SRC}/BUILD.bazel ${LLVM_SRC}/WORKSPACE

# build
bazel build --override_repository=llvm-raw=$LLVM_BAZEL_OVERLAY \
-c opt tensorflow/compiler/mlir:tf-opt
```
I was so impetuous that I didn't notice the problem with the bazel build line


### just now I tried 
```shell
LLVM_SRC=/Volumes/back/mlir_project/llvm-project
touch ${LLVM_SRC}/BUILD.bazel ${LLVM_SRC}/WORKSPACE

# build
bazel build --override_repository=llvm-raw=$LLVM_SRC \
-c opt tensorflow/compiler/mlir:tf-opt
```

I get Error below
```shell
ERROR: /private/var/tmp/_bazel_isolateya/af219aaee912b96bcf0876417404d886/external/com_google_protobuf/BUILD.bazel:459:10: Linking external/com_google_protobuf/protoc failed: (Exit 1): cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh @bazel-out/darwin_arm64-opt-exec-50AE0418/bin/external/com_google_protobuf/protoc-2.params
clang: error: invalid linker name in argument '-fuse-ld=-debugger-tuning=lldb'
```

I did install llvm, `lldb --version` will output `lldb version 15.0.5`

### the full build info
```
INFO: Found applicable config definition build:short_logs in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:macos in file /Volumes/back/mlir_project/tensorflow/.bazelrc: --apple_platform_type=macos --copt=-DGRPC_BAZEL_BUILD --copt=-w --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
INFO: Analyzed target //tensorflow/compiler/mlir:tf-opt (369 packages loaded, 25834 targets configured).
INFO: Found 1 target...
ERROR: /private/var/tmp/_bazel_isolateya/af219aaee912b96bcf0876417404d886/external/com_google_protobuf/BUILD.bazel:459:10: Linking external/com_google_protobuf/protoc failed: (Exit 1): cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh @bazel-out/darwin_arm64-opt-exec-50AE0418/bin/external/com_google_protobuf/protoc-2.params
clang: error: invalid linker name in argument '-fuse-ld=-debugger-tuning=lldb'
Target //tensorflow/compiler/mlir:tf-opt failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 447.115s, Critical Path: 66.04s
INFO: 6768 processes: 4338 internal, 2430 local.
FAILED: Build did NOT complete successfully
```


Thank you for all your help

Best regards,
Tingfeng
 Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59739">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/59739">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Build/Install

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

2.9

### Custom Code

No

### OS Platform and Distribution

Linux Ubuntu 22.04

### Mobile device

_No response_

### Python version

3.10

### Bazel version

5.3.0

### GCC/Compiler version

11.3

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

```shell
Following instructions from tensorflow/compiler/mlir/README.md with ["X86", "AArch64", "ARM"] in targets.bzl results in a build failure with:

cannot load '@llvm-project//:vars.bzl': no such file and referenced by '//tensorflow/compiler/mlir/tensorflow:tensorflow_test_passes'
```


### Standalone code to reproduce the issue

```shell
LLVM_SRC=/home/ubuntu/src/llvm-project
LLVM_BAZEL_OVERLAY=/home/ubuntu/src/llvm-overlay
mkdir -p ${LLVM_BAZEL_OVERLAY}
python3 ${LLVM_SRC}/utils/bazel/overlay_directories.py \
    --src ${LLVM_SRC} \
    --overlay ${LLVM_SRC}/utils/bazel/llvm-project-overlay/ \
    --target ${LLVM_BAZEL_OVERLAY}
touch ${LLVM_BAZEL_OVERLAY}/BUILD.bazel ${LLVM_BAZEL_OVERLAY}/WORKSPACE
echo 'llvm_targets = ["X86", "AArch64", "ARM"]' > ${LLVM_BAZEL_OVERLAY}/llvm/targets.bzl
```


### Relevant log output

```shell
INFO: Repository stablehlo instantiated at:
  /home/ubuntu/src/tensorflow/WORKSPACE:15:14: in <toplevel>
  /home/ubuntu/src/tensorflow/tensorflow/workspace2.bzl:960:28: in workspace
  /home/ubuntu/src/tensorflow/tensorflow/workspace2.bzl:81:14: in _initialize_third_party
  /home/ubuntu/src/tensorflow/third_party/stablehlo/workspace.bzl:11:20: in repo
  /home/ubuntu/src/tensorflow/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /home/ubuntu/src/tensorflow/third_party/repo.bzl:89:35: in <toplevel>
INFO: Repository nsync instantiated at:
  /home/ubuntu/src/tensorflow/WORKSPACE:15:14: in <toplevel>
  /home/ubuntu/src/tensorflow/tensorflow/workspace2.bzl:967:21: in workspace
  /home/ubuntu/src/tensorflow/tensorflow/workspace2.bzl:470:20: in _tf_repositories
  /home/ubuntu/src/tensorflow/third_party/repo.bzl:136:21: in tf_http_archive
Repository rule _tf_http_archive defined at:
  /home/ubuntu/src/tensorflow/third_party/repo.bzl:89:35: in <toplevel>
ERROR: /home/ubuntu/src/tensorflow/tensorflow/compiler/mlir/tensorflow/BUILD:1455:11: error loading package '@llvm-project//llvm': at /home/ubuntu/.cache/bazel/_bazel_ubuntu/b813f517b143a3c1665dd902035fe00f/external/llvm-project/llvm/config.bzl:8:5: cannot load '@llvm-project//:vars.bzl': no such file and referenced by '//tensorflow/compiler/mlir/tensorflow:tensorflow_test_passes'
ERROR: Analysis of target '//tensorflow/compiler/mlir:tf-opt' failed; build aborted: 
INFO: Elapsed time: 59.508s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (84 packages loaded, 255 targets configured)
    currently loading: @llvm-project//mlir ... (3 packages)
    Fetching https://storage.googleapis.com/.../github.com/openxla/stablehlo/archive/51f005f0a8ff6e28f535adfec4de936cb4097aa4.zip
    Fetching https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/nsync/archive/1.25.0.tar.gz
```
</details>