Skip to content

Commit

Permalink
Add support for running FileCheck tests in the Bazel build (#1321)
Browse files Browse the repository at this point in the history
Add support for running FileCheck tests in the Bazel build

Before this PR, only CMake build could actually ran tests, and Bazel
build could only build things.

With this PR, Bazel build can now test everything except for a few
tests that involve Python bindings. This is relatively acceptable for
now, and we have #362 to keep track of specifically Python tests.

While learning about FileCheck support in Bazel and applying this
knowledge to our repositoru, I discovered that our CMake build for
FileCheck tests was vastly overcomplicated, so I simplified it
considerably.

Finally, I noticed that ci_build_llvm.sh builds mlir-cpu-runner.
This is something that we got rid of in README.md recently, and I don't
think we need this in CI either.

Fixes #1315
  • Loading branch information
Eugene Burmako authored Mar 13, 2023
1 parent 03e820f commit 551db9e
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 326 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions .github/workflows/buildAndTestCMake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ jobs:
- name: Configure and Build LLVM
shell: bash
run: |
./build_tools/github_actions/ci_build_llvm.sh "$LLVM_PROJECT_DIR" "$LLVM_BUILD_DIR"
./build_tools/github_actions/ci_build_cmake_llvm.sh "$LLVM_PROJECT_DIR" "$LLVM_BUILD_DIR"
- name: Build and Test StableHLO
shell: bash
run: |
./build_tools/github_actions/ci_build_stablehlo.sh "$LLVM_BUILD_DIR" "$STABLEHLO_BUILD_DIR"
./build_tools/github_actions/ci_build_cmake.sh "$LLVM_BUILD_DIR" "$STABLEHLO_BUILD_DIR"
- name: Build and Test StableHLO Python API
shell: bash
run: |
./build_tools/github_actions/ci_build_stablehlo_python_api.sh "$LLVM_PROJECT_DIR" "$STABLEHLO_PYTHON_BUILD_DIR" "$GITHUB_WORKSPACE"
./build_tools/github_actions/ci_build_cmake_python_api.sh "$LLVM_PROJECT_DIR" "$STABLEHLO_PYTHON_BUILD_DIR" "$GITHUB_WORKSPACE"
116 changes: 3 additions & 113 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -545,53 +545,6 @@ cc_library(
alwayslink = True,
)

cc_library(
name = "check_ops",
srcs = [
"stablehlo/tests/CheckOps.cpp",
],
hdrs = [
"stablehlo/tests/CheckOps.h",
],
strip_include_prefix = ".",
deps = [
":base",
":check_ops_inc_gen",
":reference_tensor",
],
)

gentbl_cc_library(
name = "check_ops_inc_gen",
tbl_outs = [
(
["-gen-op-decls"],
"stablehlo/tests/CheckOps.h.inc",
),
(
["-gen-op-defs"],
"stablehlo/tests/CheckOps.cpp.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "stablehlo/tests/CheckOps.td",
strip_include_prefix = ".",
deps = [
":check_ops_td_files",
],
)

td_library(
name = "check_ops_td_files",
srcs = [
"stablehlo/tests/CheckOps.td",
],
includes = ["."],
deps = [
":base_td_files",
],
)

gentbl_cc_library(
name = "stablehlo_enums_inc_gen",
strip_include_prefix = ".",
Expand Down Expand Up @@ -818,16 +771,16 @@ cc_binary(
"stablehlo/tools/StablehloTranslateMain.cpp",
],
deps = [
":check_ops",
":reference_errors",
":reference_ops",
":reference_scope",
":reference_tensor",
":register",
":stablehlo_ops",
":stablehlo_serialization",
":test_utils",
":vhlo_ops",
"//stablehlo/tests:check_ops",
"//stablehlo/tests:test_utils",
"@llvm-project//mlir:AllPassesAndDialects",
"@llvm-project//mlir:FuncDialect",
"@llvm-project//mlir:IR",
Expand All @@ -844,75 +797,12 @@ cc_binary(
deps = [
":register",
":stablehlo_passes",
":test_utils",
"//stablehlo/tests:test_utils",
"@llvm-project//mlir:AllPassesAndDialects",
"@llvm-project//mlir:MlirOptLib",
],
)

filegroup(
name = "test_data",
testonly = True,
data = [
":stablehlo-opt",
":stablehlo-translate",
"@llvm-project//llvm:FileCheck",
],
)

gentbl_cc_library(
name = "test_utils_inc_gen",
strip_include_prefix = ".",
tbl_outs = [
(
[
"-gen-pass-decls",
"-name=HloTest",
],
"stablehlo/tests/TestUtils.h.inc",
),
],
tblgen = "@llvm-project//mlir:mlir-tblgen",
td_file = "stablehlo/tests/TestUtils.td",
deps = [
":test_utils_td_files",
],
)

td_library(
name = "test_utils_td_files",
srcs = [
"stablehlo/tests/TestUtils.td",
],
includes = ["."],
deps = [
"@llvm-project//mlir:PassBaseTdFiles",
],
)

cc_library(
name = "test_utils",
srcs = [
"stablehlo/tests/TestUtils.cpp",
],
hdrs = [
"stablehlo/tests/TestUtils.h",
],
strip_include_prefix = ".",
deps = [
":stablehlo_assembly_format",
":test_utils_inc_gen",
"@llvm-project//llvm:Support",
"@llvm-project//mlir:FuncDialect",
"@llvm-project//mlir:IR",
"@llvm-project//mlir:InferTypeOpInterface",
"@llvm-project//mlir:Pass",
"@llvm-project//mlir:ShapeDialect",
"@llvm-project//mlir:Support",
"@llvm-project//mlir:Transforms",
],
)

cc_library(
name = "version",
srcs = [
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ endif()

set(STABLEHLO_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(STABLEHLO_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(STABLEHLO_TOOLS_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)

add_custom_target(check-stablehlo)

Expand Down
5 changes: 2 additions & 3 deletions build_tools/github_actions/ci_build_bazel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ if [[ $# -ne 0 ]] ; then
exit 1
fi

# Build StableHLO
echo "=== Building StableHLO ==="
bazel build //:all
# Build and Test StableHLO
bazel test //...
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ cmake -GNinja \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DSTABLEHLO_ENABLE_STRICT_BUILD=On

# Build and Check StableHLO
# Build and Test StableHLO
cd "$STABLEHLO_BUILD_DIR"
ninja check-stablehlo
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ cmake -GNinja \
-DCMAKE_C_COMPILER_LAUNCHER=ccache

# Build LLVM/MLIR
cmake --build "$LLVM_BUILD_DIR" --target all --target mlir-cpu-runner
cmake --build "$LLVM_BUILD_DIR" --target all
2 changes: 1 addition & 1 deletion build_tools/github_actions/llvm_gcov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
# limitations under the License.

# This is a helper script used by lcov in
# ci_build_stablehlo_code_coverage.sh
# ci_build_cmake_code_coverage.sh
exec llvm-cov-14 gcov "$@"
2 changes: 1 addition & 1 deletion docs/reference_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ After implementing the interpreter:
apply, keep the existing name.
1. Once the above step is complete, sort all the tests related to the op
under test alphabetically based on the function name.
1. Keep adding tests until the [ccov](https://github.com/openxla/stablehlo/blob/main/build_tools/github_actions/ci_build_stablehlo_code_coverage.sh)
1. Keep adding tests until the [ccov](https://github.com/openxla/stablehlo/blob/main/build_tools/github_actions/ci_build_cmake_code_coverage.sh)
shows >= 90% coverage for the op.
1. In [infer_stablehlo.mlir](https://github.com/openxla/stablehlo/blob/main/stablehlo/tests/infer_stablehlo.mlir):
1. Make sure all constraints related to shape inference tests are present
Expand Down
51 changes: 51 additions & 0 deletions stablehlo/testdata/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2023 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.
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@llvm-project//llvm:lit_test.bzl", "lit_test", "package_path")

package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)

# Equivalent of configure_lit_site_cfg from CMakeLists.txt.
expand_template(
name = "lit_site_cfg_py_gen",
testonly = True,
out = "lit.site.cfg.py",
substitutions = {
"@LIT_SITE_CFG_IN_HEADER@": "# Autogenerated, do not edit.",
"@LLVM_TOOLS_DIR@": package_path("@llvm-project//llvm:BUILD"),
"@STABLEHLO_TOOLS_DIR@": ".",
"@STABLEHLO_SOURCE_DIR@": ".",
},
template = "lit.site.cfg.py.in",
)

# Equivalent of add_lit_testsuite from CMakeLists.txt.
[
lit_test(
name = "%s.test" % src,
srcs = [src],
data = [
"lit.cfg.py",
"lit.site.cfg.py",
"//:stablehlo-opt",
"//:stablehlo-translate",
"@llvm-project//llvm:FileCheck",
] + glob(["%s.*.bc" % src]),
size = "small",
)
for src in glob(["**/*.mlir"])
]
26 changes: 10 additions & 16 deletions stablehlo/testdata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@
# limitations under the License.

configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
)

set(STABLEHLO_TEST_DEPENDS
FileCheck count not
stablehlo-opt
stablehlo-translate
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
MAIN_CONFIG
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
)
add_lit_testsuite(check-stablehlo-testdata "Running the testdata/ suite"
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${STABLEHLO_TEST_DEPENDS}
)
set_target_properties(check-stablehlo-testdata PROPERTIES FOLDER "Tests")
add_lit_testsuites(STABLEHLO_TESTDATA_SUITE ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${STABLEHLO_TEST_DEPENDS})

${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
FileCheck
stablehlo-opt
stablehlo-translate
)
add_dependencies(check-stablehlo check-stablehlo-testdata)

55 changes: 13 additions & 42 deletions stablehlo/testdata/lit.cfg.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Lit configuration to drive test in this repo."""
# Copyright 2020 The TensorFlow Authors. All Rights Reserved.
# Copyright 2022 The StableHLO Authors.
# Copyright 2023 The StableHLO Authors.
# 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
Expand All @@ -20,51 +20,22 @@

import lit.formats
from lit.llvm import llvm_config
import lit.util

# Configuration file for the 'lit' test runner.

# name: The name of this test suite.
config.name = 'STABLEHLO_TESTDATA_SUITE'

# Populate Lit configuration with the minimal required metadata.
# Some metadata is populated in lit.site.cfg.py.in.
config.name = 'STABLEHLO_TESTS_SUITE'
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)

# suffixes: A list of file extensions to treat as test files.
config.suffixes = ['.mlir', '.mlir.py']

# test_source_root: The root path where tests are located.
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(config.stablehlo_obj_root, 'test')

config.substitutions.append(('%PATH%', config.environment['PATH']))
config.substitutions.append(('%shlibext', config.llvm_shlib_ext))

llvm_config.with_system_environment(['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP'])

llvm_config.use_default_substitutions()

# excludes: A list of directories to exclude from the testsuite.
config.excludes = []

# test_source_root: The root path where tests are located.
config.suffixes = ['.mlir']
config.test_source_root = os.path.dirname(__file__)

# test_exec_root: The root path where tests should be run.
config.test_exec_root = os.path.join(config.stablehlo_obj_root, 'testdata')
config.stablehlo_tools_dir = os.path.join(config.stablehlo_obj_root, 'bin')

# Tweak the PATH to include the tools dir.
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)

tool_dirs = [
config.stablehlo_tools_dir,
config.llvm_tools_dir,
]
# Make LLVM and StableHLO tools available in RUN directives
tools = [
'stablehlo-opt',
'stablehlo-translate',
'FileCheck',
'stablehlo-opt',
'stablehlo-translate',
]
tool_dirs = [
config.llvm_tools_dir,
config.stablehlo_tools_dir,
]

llvm_config.add_tool_substitutions(tools, tool_dirs)
Loading

0 comments on commit 551db9e

Please sign in to comment.