Skip to content

Commit f368e64

Browse files
authored
[SPIR-V] Add SPIRV-Tools for testing (#73044)
Add spirv-dis (disassembler) and spirv-val (validator) from SPIRV-Tools as external dependencies for testing the SPIR-V backend. These tools are test dependencies only. SPIR-V backend tests now have a dependency on the spirv-dis and spirv-val targets when the `LLVM_INCLUDE_SPIRV_TOOLS_TESTS` cmake variable is set, which allows additional test files with the `REQUIRES: spirv-tools` constraint to run, along with additional `RUN: %if spirv-tools ...` lines in existing tests. All other SPIR-V backend tests will run normally when `LLVM_INCLUDE_SPIRV_TOOLS_TESTS` is not set. Several tests are included to show these tools' use, however more tests will be migrated and added later. * OpVariable_order.ll shows how spirv-val can catch bugs in the backend. * basic_int_types_spirvdis.ll shows how tests can be much shorter and more readable by FileChecking the spirv-dis output. * basic_int_types.ll shows how an additional RUN line can add validation to existing tests. RFC: https://discourse.llvm.org/t/rfc-add-a-test-dependency-on-spirv-tools/75135
1 parent dff2f59 commit f368e64

File tree

7 files changed

+140
-0
lines changed

7 files changed

+140
-0
lines changed

llvm/test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ llvm_canonicalize_cmake_booleans(
2424
LLVM_ENABLE_REVERSE_ITERATION
2525
LLVM_INCLUDE_DXIL_TESTS
2626
LLVM_TOOL_LLVM_DRIVER_BUILD
27+
LLVM_INCLUDE_SPIRV_TOOLS_TESTS
2728
)
2829

2930
configure_lit_site_cfg(
@@ -222,6 +223,11 @@ if(LLVM_ENABLE_HTTPLIB)
222223
list(APPEND LLVM_TEST_DEPENDS llvm-debuginfod)
223224
endif()
224225

226+
if (LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
227+
list(APPEND LLVM_TEST_DEPENDS spirv-dis)
228+
list(APPEND LLVM_TEST_DEPENDS spirv-val)
229+
endif()
230+
225231
add_custom_target(llvm-test-depends DEPENDS ${LLVM_TEST_DEPENDS})
226232
set_target_properties(llvm-test-depends PROPERTIES FOLDER "Tests")
227233

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; REQUIRES: spirv-tools
2+
; RUN: llc -O0 -mtriple=spirv-unknown-linux %s -o - -filetype=obj | not spirv-val 2>&1 | FileCheck %s
3+
4+
; TODO(#66261): The SPIR-V backend should reorder OpVariable instructions so this doesn't fail,
5+
; but in the meantime it's a good example of the spirv-val tool working as intended.
6+
7+
; CHECK: All OpVariable instructions in a function must be the first instructions in the first block.
8+
9+
define void @main() #1 {
10+
entry:
11+
%0 = alloca <2 x i32>, align 4
12+
%1 = getelementptr <2 x i32>, ptr %0, i32 0, i32 0
13+
%2 = alloca float, align 4
14+
ret void
15+
}
16+
17+
attributes #1 = { "hlsl.numthreads"="4,8,16" "hlsl.shader"="compute" }

llvm/test/CodeGen/SPIRV/basic_int_types.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
22
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
33
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
45

56
define void @main() {
67
entry:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; REQUIRES: spirv-tools
2+
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s
3+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s
4+
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s
5+
6+
define void @main() {
7+
entry:
8+
; CHECK: %int16_t_Val = OpVariable %_ptr_Function_ushort Function
9+
%int16_t_Val = alloca i16, align 2
10+
11+
; CHECK: %int_Val = OpVariable %_ptr_Function_uint Function
12+
%int_Val = alloca i32, align 4
13+
14+
; CHECK: %int64_t_Val = OpVariable %_ptr_Function_ulong Function
15+
%int64_t_Val = alloca i64, align 8
16+
17+
; CHECK: %int16_t2_Val = OpVariable %_ptr_Function_v2ushort Function
18+
%int16_t2_Val = alloca <2 x i16>, align 4
19+
20+
; CHECK: %int16_t3_Val = OpVariable %_ptr_Function_v3ushort Function
21+
%int16_t3_Val = alloca <3 x i16>, align 8
22+
23+
; CHECK: %int16_t4_Val = OpVariable %_ptr_Function_v4ushort Function
24+
%int16_t4_Val = alloca <4 x i16>, align 8
25+
26+
; CHECK: %int2_Val = OpVariable %_ptr_Function_v2uint Function
27+
%int2_Val = alloca <2 x i32>, align 8
28+
29+
; CHECK: %int3_Val = OpVariable %_ptr_Function_v3uint Function
30+
%int3_Val = alloca <3 x i32>, align 16
31+
32+
; CHECK: %int4_Val = OpVariable %_ptr_Function_v4uint Function
33+
%int4_Val = alloca <4 x i32>, align 16
34+
35+
; CHECK: %int64_t2_Val = OpVariable %_ptr_Function_v2ulong Function
36+
%int64_t2_Val = alloca <2 x i64>, align 16
37+
38+
; CHECK: %int64_t3_Val = OpVariable %_ptr_Function_v3ulong Function
39+
%int64_t3_Val = alloca <3 x i64>, align 32
40+
41+
; CHECK: %int64_t4_Val = OpVariable %_ptr_Function_v4ulong Function
42+
%int64_t4_Val = alloca <4 x i64>, align 32
43+
44+
ret void
45+
}

llvm/test/CodeGen/SPIRV/lit.local.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
if not "SPIRV" in config.root.targets:
22
config.unsupported = True
3+
4+
if config.spirv_tools_tests:
5+
config.available_features.add("spirv-tools")
6+
config.substitutions.append(("spirv-dis", os.path.join(config.llvm_tools_dir, "spirv-dis")))
7+
config.substitutions.append(("spirv-val", os.path.join(config.llvm_tools_dir, "spirv-val")))

llvm/test/lit.site.cfg.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ config.expensive_checks = @LLVM_ENABLE_EXPENSIVE_CHECKS@
6060
config.reverse_iteration = @LLVM_ENABLE_REVERSE_ITERATION@
6161
config.dxil_tests = @LLVM_INCLUDE_DXIL_TESTS@
6262
config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
63+
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
6364

6465
import lit.llvm
6566
lit.llvm.initialize(lit_config, config)

llvm/tools/spirv-tools/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
option(LLVM_INCLUDE_SPIRV_TOOLS_TESTS "Include tests that use SPIRV-Tools" Off)
2+
mark_as_advanced(LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
3+
4+
if (NOT LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
5+
return()
6+
endif ()
7+
8+
if (NOT "SPIRV" IN_LIST LLVM_TARGETS_TO_BUILD)
9+
message(FATAL_ERROR "Building SPIRV-Tools tests is unsupported without the SPIR-V target")
10+
endif ()
11+
12+
# SPIRV_DIS and SPIRV_VAL variables can be used to provide paths to existing
13+
# spirv-dis and spirv-val binaries, respectively. Otherwise, build them from
14+
# SPIRV-Tools source.
15+
if (NOT SPIRV_DIS OR NOT SPIRV_VAL)
16+
include(ExternalProject)
17+
18+
set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/SPIRVTools-bin)
19+
20+
ExternalProject_Add(SPIRVTools
21+
GIT_REPOSITORY https://github.com/KhronosGroup/SPIRV-Tools.git
22+
GIT_TAG main
23+
BINARY_DIR ${BINARY_DIR}
24+
BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR} --target spirv-dis spirv-val
25+
BUILD_BYPRODUCTS ${BINARY_DIR}/tools/spirv-dis ${BINARY_DIR}/tools/spirv-val
26+
DOWNLOAD_COMMAND git clone https://github.com/KhronosGroup/SPIRV-Tools.git SPIRVTools &&
27+
cd SPIRVTools &&
28+
${Python3_EXECUTABLE} utils/git-sync-deps
29+
UPDATE_COMMAND git pull origin main &&
30+
${Python3_EXECUTABLE} utils/git-sync-deps
31+
# Don't auto-update on every build.
32+
UPDATE_DISCONNECTED 1
33+
# Allow manual updating with an explicit SPIRVTools-update target.
34+
STEP_TARGETS update
35+
# Install handled below.
36+
INSTALL_COMMAND ""
37+
)
38+
endif ()
39+
40+
if (CMAKE_HOST_UNIX)
41+
set(LLVM_LINK_OR_COPY create_symlink)
42+
else ()
43+
set(LLVM_LINK_OR_COPY copy)
44+
endif ()
45+
46+
# Link the provided or just built spirv-dis and spirv-val binaries.
47+
if (SPIRV_DIS)
48+
add_custom_target(spirv-dis
49+
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${SPIRV_DIS}" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-dis")
50+
else ()
51+
add_custom_target(spirv-dis
52+
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${BINARY_DIR}/tools/spirv-dis" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-dis"
53+
DEPENDS SPIRVTools
54+
)
55+
endif ()
56+
57+
if (SPIRV_VAL)
58+
add_custom_target(spirv-val
59+
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${SPIRV_VAL}" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-val")
60+
else ()
61+
add_custom_target(spirv-val
62+
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${BINARY_DIR}/tools/spirv-val" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-val"
63+
DEPENDS SPIRVTools
64+
)
65+
endif ()

0 commit comments

Comments
 (0)