Skip to content

[SPIR-V] Add SPIRV-Tools for testing #73044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ llvm_canonicalize_cmake_booleans(
LLVM_ENABLE_REVERSE_ITERATION
LLVM_INCLUDE_DXIL_TESTS
LLVM_TOOL_LLVM_DRIVER_BUILD
LLVM_INCLUDE_SPIRV_TOOLS_TESTS
)

configure_lit_site_cfg(
Expand Down Expand Up @@ -222,6 +223,11 @@ if(LLVM_ENABLE_HTTPLIB)
list(APPEND LLVM_TEST_DEPENDS llvm-debuginfod)
endif()

if (LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
list(APPEND LLVM_TEST_DEPENDS spirv-dis)
list(APPEND LLVM_TEST_DEPENDS spirv-val)
endif()

add_custom_target(llvm-test-depends DEPENDS ${LLVM_TEST_DEPENDS})
set_target_properties(llvm-test-depends PROPERTIES FOLDER "Tests")

Expand Down
17 changes: 17 additions & 0 deletions llvm/test/CodeGen/SPIRV/OpVariable_order.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; REQUIRES: spirv-tools
; RUN: llc -O0 -mtriple=spirv-unknown-linux %s -o - -filetype=obj | not spirv-val 2>&1 | FileCheck %s

; TODO(#66261): The SPIR-V backend should reorder OpVariable instructions so this doesn't fail,
; but in the meantime it's a good example of the spirv-val tool working as intended.

; CHECK: All OpVariable instructions in a function must be the first instructions in the first block.

define void @main() #1 {
entry:
%0 = alloca <2 x i32>, align 4
%1 = getelementptr <2 x i32>, ptr %0, i32 0, i32 0
%2 = alloca float, align 4
ret void
}

attributes #1 = { "hlsl.numthreads"="4,8,16" "hlsl.shader"="compute" }
1 change: 1 addition & 0 deletions llvm/test/CodeGen/SPIRV/basic_int_types.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}

define void @main() {
entry:
Expand Down
45 changes: 45 additions & 0 deletions llvm/test/CodeGen/SPIRV/basic_int_types_spirvdis.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
; REQUIRES: spirv-tools
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - --filetype=obj | spirv-dis | FileCheck %s

define void @main() {
entry:
; CHECK: %int16_t_Val = OpVariable %_ptr_Function_ushort Function
%int16_t_Val = alloca i16, align 2

; CHECK: %int_Val = OpVariable %_ptr_Function_uint Function
%int_Val = alloca i32, align 4

; CHECK: %int64_t_Val = OpVariable %_ptr_Function_ulong Function
%int64_t_Val = alloca i64, align 8

; CHECK: %int16_t2_Val = OpVariable %_ptr_Function_v2ushort Function
%int16_t2_Val = alloca <2 x i16>, align 4

; CHECK: %int16_t3_Val = OpVariable %_ptr_Function_v3ushort Function
%int16_t3_Val = alloca <3 x i16>, align 8

; CHECK: %int16_t4_Val = OpVariable %_ptr_Function_v4ushort Function
%int16_t4_Val = alloca <4 x i16>, align 8

; CHECK: %int2_Val = OpVariable %_ptr_Function_v2uint Function
%int2_Val = alloca <2 x i32>, align 8

; CHECK: %int3_Val = OpVariable %_ptr_Function_v3uint Function
%int3_Val = alloca <3 x i32>, align 16

; CHECK: %int4_Val = OpVariable %_ptr_Function_v4uint Function
%int4_Val = alloca <4 x i32>, align 16

; CHECK: %int64_t2_Val = OpVariable %_ptr_Function_v2ulong Function
%int64_t2_Val = alloca <2 x i64>, align 16

; CHECK: %int64_t3_Val = OpVariable %_ptr_Function_v3ulong Function
%int64_t3_Val = alloca <3 x i64>, align 32

; CHECK: %int64_t4_Val = OpVariable %_ptr_Function_v4ulong Function
%int64_t4_Val = alloca <4 x i64>, align 32

ret void
}
5 changes: 5 additions & 0 deletions llvm/test/CodeGen/SPIRV/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
if not "SPIRV" in config.root.targets:
config.unsupported = True

if config.spirv_tools_tests:
config.available_features.add("spirv-tools")
config.substitutions.append(("spirv-dis", os.path.join(config.llvm_tools_dir, "spirv-dis")))
config.substitutions.append(("spirv-val", os.path.join(config.llvm_tools_dir, "spirv-val")))
1 change: 1 addition & 0 deletions llvm/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ config.expensive_checks = @LLVM_ENABLE_EXPENSIVE_CHECKS@
config.reverse_iteration = @LLVM_ENABLE_REVERSE_ITERATION@
config.dxil_tests = @LLVM_INCLUDE_DXIL_TESTS@
config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@

import lit.llvm
lit.llvm.initialize(lit_config, config)
Expand Down
65 changes: 65 additions & 0 deletions llvm/tools/spirv-tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
option(LLVM_INCLUDE_SPIRV_TOOLS_TESTS "Include tests that use SPIRV-Tools" Off)
mark_as_advanced(LLVM_INCLUDE_SPIRV_TOOLS_TESTS)

if (NOT LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
return()
endif ()

if (NOT "SPIRV" IN_LIST LLVM_TARGETS_TO_BUILD)
message(FATAL_ERROR "Building SPIRV-Tools tests is unsupported without the SPIR-V target")
endif ()

# SPIRV_DIS and SPIRV_VAL variables can be used to provide paths to existing
# spirv-dis and spirv-val binaries, respectively. Otherwise, build them from
# SPIRV-Tools source.
if (NOT SPIRV_DIS OR NOT SPIRV_VAL)
include(ExternalProject)

set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/SPIRVTools-bin)

ExternalProject_Add(SPIRVTools
GIT_REPOSITORY https://github.com/KhronosGroup/SPIRV-Tools.git
GIT_TAG main
BINARY_DIR ${BINARY_DIR}
BUILD_COMMAND ${CMAKE_COMMAND} --build ${BINARY_DIR} --target spirv-dis spirv-val
BUILD_BYPRODUCTS ${BINARY_DIR}/tools/spirv-dis ${BINARY_DIR}/tools/spirv-val
DOWNLOAD_COMMAND git clone https://github.com/KhronosGroup/SPIRV-Tools.git SPIRVTools &&
cd SPIRVTools &&
${Python3_EXECUTABLE} utils/git-sync-deps
UPDATE_COMMAND git pull origin main &&
${Python3_EXECUTABLE} utils/git-sync-deps
# Don't auto-update on every build.
UPDATE_DISCONNECTED 1
# Allow manual updating with an explicit SPIRVTools-update target.
STEP_TARGETS update
# Install handled below.
INSTALL_COMMAND ""
)
endif ()

if (CMAKE_HOST_UNIX)
set(LLVM_LINK_OR_COPY create_symlink)
else ()
set(LLVM_LINK_OR_COPY copy)
endif ()

# Link the provided or just built spirv-dis and spirv-val binaries.
if (SPIRV_DIS)
add_custom_target(spirv-dis
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${SPIRV_DIS}" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-dis")
else ()
add_custom_target(spirv-dis
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${BINARY_DIR}/tools/spirv-dis" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-dis"
DEPENDS SPIRVTools
)
endif ()

if (SPIRV_VAL)
add_custom_target(spirv-val
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${SPIRV_VAL}" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-val")
else ()
add_custom_target(spirv-val
COMMAND ${CMAKE_COMMAND} -E ${LLVM_LINK_OR_COPY} "${BINARY_DIR}/tools/spirv-val" "${LLVM_RUNTIME_OUTPUT_INTDIR}/spirv-val"
DEPENDS SPIRVTools
)
endif ()