diff --git a/.bazelrc b/.bazelrc index 4f706b9..b1c3718 100644 --- a/.bazelrc +++ b/.bazelrc @@ -28,17 +28,15 @@ 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 @@ -46,3 +44,7 @@ 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 diff --git a/.github/workflows/bazelBuildAndTestTcp.yml b/.github/workflows/bazelBuildAndTestTcp.yml index 0a8fd65..a60d9fd 100644 --- a/.github/workflows/bazelBuildAndTestTcp.yml +++ b/.github/workflows/bazelBuildAndTestTcp.yml @@ -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 diff --git a/.gitignore b/.gitignore index 1c5de2d..8bf3e09 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8124920 --- /dev/null +++ b/.vscode/settings.json @@ -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", diff --git a/BUILD b/BUILD index 1b28363..7627fba 100644 --- a/BUILD +++ b/BUILD @@ -322,9 +322,3 @@ cc_binary( "@stablehlo//:register", ], ) - -load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier") - -buildifier( - name = "buildifier", -) diff --git a/README.md b/README.md index 736868d..0ea4b76 100644 --- a/README.md +++ b/README.md @@ -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/... diff --git a/WORKSPACE b/WORKSPACE index 86226c2..6d39194 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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", @@ -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( @@ -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() diff --git a/docker/Dockerfile b/docker/Dockerfile index 6126bcb..d2860be 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -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 diff --git a/tools/buildifier/BUILD b/tools/buildifier/BUILD new file mode 100644 index 0000000..7fb4dd6 --- /dev/null +++ b/tools/buildifier/BUILD @@ -0,0 +1,5 @@ +load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier") + +buildifier( + name = "buildifier", +) diff --git a/tools/clangd/BUILD b/tools/clangd/BUILD new file mode 100644 index 0000000..808b55d --- /dev/null +++ b/tools/clangd/BUILD @@ -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", + ], +)