From 6ec16306f328472df0749c74a94ac587f2391fb0 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 14 Feb 2024 19:22:05 +0000 Subject: [PATCH 1/3] Add support for Bazel 6 & 7 Previous PR #2006 added support for Bazel 7 but some of the changes in Bazel 7 are not backwards compatible with 6. This is a problem for some of our consumers that embed our project in other Bazel workspaces. Specifically, the name of the runfiles directory was changed from the workspace name to be '_main'. The solution adopted here is to use @python_rules and programmatically determine the runfiles directory relative to the lit.cfg.py file. --- build_tools/utils.bzl | 12 ++++++++++++ stablehlo/conversions/linalg/tests/BUILD.bazel | 13 ++++++++++--- stablehlo/conversions/tosa/tests/BUILD.bazel | 13 ++++++++++--- stablehlo/testdata/BUILD.bazel | 13 ++++++++++--- stablehlo/tests/BUILD.bazel | 13 ++++++++++--- 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 build_tools/utils.bzl diff --git a/build_tools/utils.bzl b/build_tools/utils.bzl new file mode 100644 index 00000000000..1b435234a51 --- /dev/null +++ b/build_tools/utils.bzl @@ -0,0 +1,12 @@ +"""A collection of starlark functions for use in BUILD files that are useful for StableHLO.""" + +def is_bzlmod_enabled(): + """Determine whether bzlmod mode is enabled.""" + + # If bzlmod is enabled, then `str(Label(...))` returns a canonical label, + # these start with `@@`. + return str(Label("//:invalid")).startswith("@@") + +def workspace_name(): + """Return the name of the workspace.""" + return "_main" if is_bzlmod_enabled() else "stablehlo" diff --git a/stablehlo/conversions/linalg/tests/BUILD.bazel b/stablehlo/conversions/linalg/tests/BUILD.bazel index 707b809decb..a9fe899669a 100644 --- a/stablehlo/conversions/linalg/tests/BUILD.bazel +++ b/stablehlo/conversions/linalg/tests/BUILD.bazel @@ -25,9 +25,15 @@ expand_template( testonly = True, out = "lit.site.cfg.py", substitutions = { - "\"@STABLEHLO_SOURCE_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "\"@STABLEHLO_TOOLS_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.", + "\"@STABLEHLO_SOURCE_DIR@\"": "RUNFILES_DIR", + "\"@STABLEHLO_TOOLS_DIR@\"": "RUNFILES_DIR", + "@LIT_SITE_CFG_IN_HEADER@": '''# Autogenerated, do not edit. +from python.runfiles import Runfiles +from pathlib import Path + +r = Runfiles.Create() +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/linalg/tests/lite.cfg.py")) +RUNFILES_DIR = LITE_CFG_PY.parents[4].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, template = "lit.site.cfg.py.in", @@ -46,6 +52,7 @@ expand_template( "@llvm-project//llvm:FileCheck", ], tags = ["stablehlo_linalg_tests"], + deps = ["@rules_python//python/runfiles"], ) for src in glob(["**/*.mlir"]) ] diff --git a/stablehlo/conversions/tosa/tests/BUILD.bazel b/stablehlo/conversions/tosa/tests/BUILD.bazel index bf50dbf8713..e95b02a1b90 100644 --- a/stablehlo/conversions/tosa/tests/BUILD.bazel +++ b/stablehlo/conversions/tosa/tests/BUILD.bazel @@ -25,9 +25,15 @@ expand_template( testonly = True, out = "lit.site.cfg.py", substitutions = { - "\"@STABLEHLO_SOURCE_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "\"@STABLEHLO_TOOLS_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.", + "\"@STABLEHLO_SOURCE_DIR@\"": "RUNFILES_DIR", + "\"@STABLEHLO_TOOLS_DIR@\"": "RUNFILES_DIR", + "@LIT_SITE_CFG_IN_HEADER@": '''# Autogenerated, do not edit. +from python.runfiles import Runfiles +from pathlib import Path + +r = Runfiles.Create() +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/tosa/tests/lite.cfg.py")) +RUNFILES_DIR = LITE_CFG_PY.parents[4].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, template = "lit.site.cfg.py.in", @@ -46,6 +52,7 @@ expand_template( "@llvm-project//llvm:FileCheck", ], tags = ["stablehlo_tosa_tests"], + deps = ["@rules_python//python/runfiles"], ) for src in glob(["**/*.mlir"]) ] diff --git a/stablehlo/testdata/BUILD.bazel b/stablehlo/testdata/BUILD.bazel index dde4d7c1078..f9fb0d0c2fe 100644 --- a/stablehlo/testdata/BUILD.bazel +++ b/stablehlo/testdata/BUILD.bazel @@ -25,9 +25,15 @@ expand_template( testonly = True, out = "lit.site.cfg.py", substitutions = { - "\"@STABLEHLO_SOURCE_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "\"@STABLEHLO_TOOLS_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.", + "\"@STABLEHLO_SOURCE_DIR@\"": "RUNFILES_DIR", + "\"@STABLEHLO_TOOLS_DIR@\"": "RUNFILES_DIR", + "@LIT_SITE_CFG_IN_HEADER@": '''# Autogenerated, do not edit. +from python.runfiles import Runfiles +from pathlib import Path + +r = Runfiles.Create() +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/testdata/lite.cfg.py")) +RUNFILES_DIR = LITE_CFG_PY.parents[2].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, template = "lit.site.cfg.py.in", @@ -47,6 +53,7 @@ expand_template( "@llvm-project//llvm:FileCheck", ], tags = ["stablehlo_testdata_tests"], + deps = ["@rules_python//python/runfiles"], ) for src in glob(["**/*.mlir"]) ] diff --git a/stablehlo/tests/BUILD.bazel b/stablehlo/tests/BUILD.bazel index bfc4a0dab25..be7b4f2f1e3 100644 --- a/stablehlo/tests/BUILD.bazel +++ b/stablehlo/tests/BUILD.bazel @@ -134,9 +134,15 @@ expand_template( testonly = True, out = "lit.site.cfg.py", substitutions = { - "\"@STABLEHLO_SOURCE_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "\"@STABLEHLO_TOOLS_DIR@\"": "os.path.join(os.environ['TEST_SRCDIR'], '_main')", - "@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.", + "\"@STABLEHLO_SOURCE_DIR@\"": "RUNFILES_DIR", + "\"@STABLEHLO_TOOLS_DIR@\"": "RUNFILES_DIR", + "@LIT_SITE_CFG_IN_HEADER@": '''# Autogenerated, do not edit. +from python.runfiles import Runfiles +from pathlib import Path + +r = Runfiles.Create() +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/tests/lite.cfg.py")) +RUNFILES_DIR = LITE_CFG_PY.parents[2].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, template = "lit.site.cfg.py.in", @@ -157,6 +163,7 @@ expand_template( "@llvm-project//llvm:not", ] + glob(["%s.bc" % src]), tags = ["stablehlo_tests"], + deps = ["@rules_python//python/runfiles"], ) for src in glob(["**/*.mlir"]) ] From 1933fdaace67c0486a522d3b329bdd64119ff5e3 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Wed, 14 Feb 2024 23:11:30 +0000 Subject: [PATCH 2/3] Add missing license --- build_tools/utils.bzl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/build_tools/utils.bzl b/build_tools/utils.bzl index 1b435234a51..81fd4bbbbd2 100644 --- a/build_tools/utils.bzl +++ b/build_tools/utils.bzl @@ -1,3 +1,17 @@ +# Copyright 2024 The StableHLO Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + """A collection of starlark functions for use in BUILD files that are useful for StableHLO.""" def is_bzlmod_enabled(): From 2a1d96c4b28c8195d47754cb13796814d9eb7396 Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 5 Mar 2024 23:06:32 +0000 Subject: [PATCH 3/3] lite -> lit for cfg files --- stablehlo/conversions/linalg/tests/BUILD.bazel | 2 +- stablehlo/conversions/tosa/tests/BUILD.bazel | 2 +- stablehlo/testdata/BUILD.bazel | 2 +- stablehlo/tests/BUILD.bazel | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/stablehlo/conversions/linalg/tests/BUILD.bazel b/stablehlo/conversions/linalg/tests/BUILD.bazel index a9fe899669a..9121430c164 100644 --- a/stablehlo/conversions/linalg/tests/BUILD.bazel +++ b/stablehlo/conversions/linalg/tests/BUILD.bazel @@ -32,7 +32,7 @@ from python.runfiles import Runfiles from pathlib import Path r = Runfiles.Create() -LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/linalg/tests/lite.cfg.py")) +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/linalg/tests/lit.cfg.py")) RUNFILES_DIR = LITE_CFG_PY.parents[4].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, diff --git a/stablehlo/conversions/tosa/tests/BUILD.bazel b/stablehlo/conversions/tosa/tests/BUILD.bazel index e95b02a1b90..281e38c4fc8 100644 --- a/stablehlo/conversions/tosa/tests/BUILD.bazel +++ b/stablehlo/conversions/tosa/tests/BUILD.bazel @@ -32,7 +32,7 @@ from python.runfiles import Runfiles from pathlib import Path r = Runfiles.Create() -LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/tosa/tests/lite.cfg.py")) +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/conversions/tosa/tests/lit.cfg.py")) RUNFILES_DIR = LITE_CFG_PY.parents[4].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, diff --git a/stablehlo/testdata/BUILD.bazel b/stablehlo/testdata/BUILD.bazel index f9fb0d0c2fe..c2c54a80480 100644 --- a/stablehlo/testdata/BUILD.bazel +++ b/stablehlo/testdata/BUILD.bazel @@ -32,7 +32,7 @@ from python.runfiles import Runfiles from pathlib import Path r = Runfiles.Create() -LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/testdata/lite.cfg.py")) +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/testdata/lit.cfg.py")) RUNFILES_DIR = LITE_CFG_PY.parents[2].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", }, diff --git a/stablehlo/tests/BUILD.bazel b/stablehlo/tests/BUILD.bazel index be7b4f2f1e3..517ae58e775 100644 --- a/stablehlo/tests/BUILD.bazel +++ b/stablehlo/tests/BUILD.bazel @@ -141,7 +141,7 @@ from python.runfiles import Runfiles from pathlib import Path r = Runfiles.Create() -LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/tests/lite.cfg.py")) +LITE_CFG_PY = Path(r.Rlocation("stablehlo/stablehlo/tests/lit.cfg.py")) RUNFILES_DIR = LITE_CFG_PY.parents[2].absolute().as_posix()''', "@LLVM_TOOLS_DIR@": "../llvm-project/llvm", },