Skip to content

Commit

Permalink
Support clangd for code completion, navigation and insights (#30)
Browse files Browse the repository at this point in the history
Uses
[bazel-compile-commands-extractor](https://github.com/hedronvision/bazel-compile-commands-extractor)
to generate the compilation database from our bazel build.

Other minor improvements include:
- simplify .bazelrc
- move buildifier and compile-commands-extractor targets to separate
`tools/` to make downstream integrations easier (else they fail on
loading these deps)
- bump bazel to 6.4.0 (for `--noenable_bzlmod` option)
  • Loading branch information
sjain-stanford authored Jan 19, 2024
1 parent 5f6a0b9 commit 10a2ac0
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
18 changes: 10 additions & 8 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,23 @@ build:generic_clang --cxxopt=-Wno-range-loop-analysis --host_cxxopt=-Wno-range-l
# not the point of the Bazel build to catch usage of deprecated APIs.
build:generic_clang --copt=-Wno-deprecated --host_copt=-Wno-deprecated

# Automatically detect host platform to pick config
common --enable_platform_specific_config

# lld links faster than other linkers. Assume that anybody using clang on linux
# also has lld available.
build:clang_linux --linkopt=-fuse-ld=lld --host_linkopt=-fuse-ld=lld
build:clang_linux --config=generic_clang
build:linux --linkopt=-fuse-ld=lld --host_linkopt=-fuse-ld=lld
build:linux --config=generic_clang

build:clang_macos --config=generic_clang

# Automatically detect host platform to pick config
common --enable_platform_specific_config
build:linux --config=clang_linux
build:macos --config=clang_macos
build:macos --config=generic_clang

# Other compilation modes
build:opt --compilation_mode=opt
build:dbg --compilation_mode=dbg

# GDB builds in dbg mode
build:gdb --config=dbg

# TODO: migrate all dependencies from WORKSPACE to MODULE.bazel
# https://bazel.build/external/migration
common --noenable_bzlmod
4 changes: 2 additions & 2 deletions .github/workflows/bazelBuildAndTestTcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ jobs:
-v "$(pwd)":"/opt/src/mlir-tcp" \
-v "${HOME}/.cache/bazel":"${HOME}/.cache/bazel" \
mlir-tcp:ci \
bazel run //:buildifier
bazel run //tools/buildifier:buildifier
if [ -n "$(git status --porcelain)" ]; then
echo "Please 'bazel run //:buildifier' and commit changes."
echo "Please 'bazel run //tools/buildifier:buildifier' and commit changes."
exit 1
fi
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# Also available under a BSD-style license. See LICENSE.

# build related
bazel-bin
bazel-out
bazel-mlir-tcp
bazel-testlogs
third_party/

# clangd related
.cache
compile_commands.json
external
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Configure clangd
"clangd.detectExtensionConflicts": true,
"clangd.restartAfterCrash": true,
"clangd.onConfigChanged": "restart",
"clangd.arguments": [
"--malloc-trim",
"--pretty",
"--pch-storage=disk",
"--background-index",
"--header-insertion=never",
"--compile-commands-dir=${workspaceFolder}/",
"--query-driver=**",
"-j=4"
],
// Disable conflicting settings of the ms-vscode.cpptools extension
"C_Cpp.intelliSenseEngine": "Disabled",
"C_Cpp.autocomplete": "Disabled",
"C_Cpp.errorSquiggles": "Disabled",
6 changes: 0 additions & 6 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,3 @@ cc_binary(
"@stablehlo//:register",
],
)

load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

buildifier(
name = "buildifier",
)
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ We welcome contributions to `mlir-tcp`. If you do contribute, please finalize yo
find . -type f -name "*.cpp" -o -name "*.h" | xargs clang-format -i

# buildifer
bazel run //:buildifier
bazel run //tools/buildifier:buildifier
```

To enable clangd (for code completion, navigation and insights), generate the compilation database using [bazel-compile-commands-extractor](https://github.com/hedronvision/bazel-compile-commands-extractor):
```shell
bazel build //...

bazel run //tools/clangd:refresh_compile_commands
```
When run successfully, a `compile_commands.json` is generated at the workspace root (and refreshed upon re-runs). If you're using VSCode, just hit CMD+SHIFT+P and select `clangd: Restart language server` to start clangd. Note that this only works for non-docker builds at the moment.

When bumping upstream dependencies (LLVM, Torch-MLIR, StableHLO), you may validate the set of "green commits" by running the corresponding third-party tests:
```shell
bazel test @llvm-project//mlir/...
Expand Down
26 changes: 21 additions & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ load("@torch-mlir-raw//utils/bazel:configure.bzl", "torch_mlir_configure")

torch_mlir_configure(name = "torch-mlir")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# --------------------------- #
# Buildifier dependencies #
# --------------------------- #

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
# https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md

# buildifier is written in Go and hence needs rules_go to be built.
# See https://github.com/bazelbuild/rules_go for the up to date setup instructions.
http_archive(
name = "io_bazel_rules_go",
sha256 = "6dc2da7ab4cf5d7bfc7c949776b1b7c733f05e56edc4bcd9022bb249d2e2a996",
Expand Down Expand Up @@ -60,8 +60,6 @@ http_archive(

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

# If you use WORKSPACE.bazel, use the following line instead of the bare gazelle_dependencies():
# gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE.bazel")
gazelle_dependencies()

http_archive(
Expand All @@ -85,3 +83,21 @@ http_archive(
"https://github.com/bazelbuild/buildtools/archive/refs/tags/4.2.2.tar.gz",
],
)

# ----------------------------- #
# Compile Commands Extractor #
# for Bazel (clangd) #
# ----------------------------- #

# https://github.com/hedronvision/bazel-compile-commands-extractor/blob/main/README.md

http_archive(
name = "hedron_compile_commands",
sha256 = "2188c3cd3a16404a6b20136151b37e7afb5a320e150453750c15080de5ba3058",
strip_prefix = "bazel-compile-commands-extractor-6d58fa6bf39f612304e55566fa628fd160b38177",
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/6d58fa6bf39f612304e55566fa628fd160b38177.tar.gz",
)

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ RUN apt-get update && \

# Install bazel
ARG ARCH="x86_64"
ARG BAZEL_VERSION=5.4.0
ARG BAZEL_VERSION=6.4.0
RUN wget -q https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-linux-${ARCH} -O /usr/bin/bazel \
&& chmod a+x /usr/bin/bazel

Expand Down
5 changes: 5 additions & 0 deletions tools/buildifier/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")

buildifier(
name = "buildifier",
)
26 changes: 26 additions & 0 deletions tools/clangd/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")

refresh_compile_commands(
name = "refresh_compile_commands",

# Keep this list updated by running the following query:
# bazel query 'kind("(cc.*) rule", //...)'
targets = [
"//:Pipeline",
"//:StablehloToTcp",
"//:TcpConversionPasses",
"//:TcpConversionPassesIncGen",
"//:TcpDialect",
"//:TcpDialectPasses",
"//:TcpDialectPassesIncGen",
"//:TcpInitAll",
"//:TcpOpsIncGen",
"//:TcpToArith",
"//:TcpToLinalg",
"//:TcpTypesIncGen",
"//:TorchToTcp",
"//:tcp-opt",
"//test:AotCompile/test_aot_compiled_basic_tcp_ops",
"//test:aot_compiled_basic_tcp_ops",
],
)

0 comments on commit 10a2ac0

Please sign in to comment.