Skip to content

Commit b25236d

Browse files
committed
Add cmake function to simplify shader tests
1 parent cb06a3c commit b25236d

File tree

1 file changed

+45
-60
lines changed

1 file changed

+45
-60
lines changed

ggml/src/ggml-vulkan/CMakeLists.txt

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ function(detect_host_compiler)
1515
set(HOST_CXX_COMPILER "${HOST_CXX_COMPILER}" PARENT_SCOPE)
1616
endfunction()
1717

18+
# Function to test shader extension support
19+
# Parameters:
20+
# EXTENSION_NAME - Name of the extension to test (e.g., "GL_EXT_integer_dot_product")
21+
# TEST_SHADER_FILE - Path to the test shader file
22+
# RESULT_VARIABLE - Name of the variable to set (ON/OFF) based on test result
23+
function(test_shader_extension_support EXTENSION_NAME TEST_SHADER_FILE RESULT_VARIABLE)
24+
# Compile the test shader to determine whether the extension is supported
25+
execute_process(
26+
COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${TEST_SHADER_FILE}"
27+
OUTPUT_VARIABLE glslc_output
28+
ERROR_VARIABLE glslc_error
29+
)
30+
31+
# Check if error message contains extension not supported
32+
if (${glslc_error} MATCHES ".*extension not supported: ${EXTENSION_NAME}.*")
33+
message(STATUS "${EXTENSION_NAME} not supported by glslc")
34+
set(${RESULT_VARIABLE} OFF PARENT_SCOPE)
35+
else()
36+
message(STATUS "${EXTENSION_NAME} supported by glslc")
37+
set(${RESULT_VARIABLE} ON PARENT_SCOPE)
38+
add_compile_definitions(${RESULT_VARIABLE})
39+
endif()
40+
endfunction()
41+
1842
if (Vulkan_FOUND)
1943
message(STATUS "Vulkan found")
2044

@@ -23,69 +47,30 @@ if (Vulkan_FOUND)
2347
../../include/ggml-vulkan.h
2448
)
2549

26-
# Compile a test shader to determine whether GL_KHR_cooperative_matrix is supported.
27-
# If it's not, there will be an error to stderr.
28-
# If it's supported, set a define to indicate that we should compile those shaders
29-
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat_support.comp"
30-
OUTPUT_VARIABLE glslc_output
31-
ERROR_VARIABLE glslc_error)
32-
33-
if (${glslc_error} MATCHES ".*extension not supported: GL_KHR_cooperative_matrix.*")
34-
message(STATUS "GL_KHR_cooperative_matrix not supported by glslc")
35-
set(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT OFF)
36-
else()
37-
message(STATUS "GL_KHR_cooperative_matrix supported by glslc")
38-
set(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT ON)
39-
add_compile_definitions(GGML_VULKAN_COOPMAT_GLSLC_SUPPORT)
40-
endif()
41-
42-
# Compile a test shader to determine whether GL_NV_cooperative_matrix2 is supported.
43-
# If it's not, there will be an error to stderr.
44-
# If it's supported, set a define to indicate that we should compile those shaders
45-
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat2_support.comp"
46-
OUTPUT_VARIABLE glslc_output
47-
ERROR_VARIABLE glslc_error)
48-
49-
if (${glslc_error} MATCHES ".*extension not supported: GL_NV_cooperative_matrix2.*")
50-
message(STATUS "GL_NV_cooperative_matrix2 not supported by glslc")
51-
set(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT OFF)
52-
else()
53-
message(STATUS "GL_NV_cooperative_matrix2 supported by glslc")
54-
set(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT ON)
55-
add_compile_definitions(GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT)
56-
endif()
57-
58-
# Compile a test shader to determine whether GL_EXT_integer_dot_product is supported.
59-
# If it's not, there will be an error to stderr.
60-
# If it's supported, set a define to indicate that we should compile those shaders
61-
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_integer_dot_support.comp"
62-
OUTPUT_VARIABLE glslc_output
63-
ERROR_VARIABLE glslc_error)
50+
# Test all shader extensions
51+
test_shader_extension_support(
52+
"GL_KHR_cooperative_matrix"
53+
"${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat_support.comp"
54+
"GGML_VULKAN_COOPMAT_GLSLC_SUPPORT"
55+
)
6456

65-
if (${glslc_error} MATCHES ".*extension not supported: GL_EXT_integer_dot_product.*")
66-
message(STATUS "GL_EXT_integer_dot_product not supported by glslc")
67-
set(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT OFF)
68-
else()
69-
message(STATUS "GL_EXT_integer_dot_product supported by glslc")
70-
set(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT ON)
71-
add_compile_definitions(GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT)
72-
endif()
57+
test_shader_extension_support(
58+
"GL_NV_cooperative_matrix2"
59+
"${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_coopmat2_support.comp"
60+
"GGML_VULKAN_COOPMAT2_GLSLC_SUPPORT"
61+
)
7362

74-
# Compile a test shader to determine whether GL_EXT_bfloat16 is supported.
75-
# If it's not, there will be an error to stderr.
76-
# If it's supported, set a define to indicate that we should compile those shaders
77-
execute_process(COMMAND ${Vulkan_GLSLC_EXECUTABLE} -o - -fshader-stage=compute --target-env=vulkan1.3 "${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_bfloat16_support.comp"
78-
OUTPUT_VARIABLE glslc_output
79-
ERROR_VARIABLE glslc_error)
63+
test_shader_extension_support(
64+
"GL_EXT_integer_dot_product"
65+
"${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_integer_dot_support.comp"
66+
"GGML_VULKAN_INTEGER_DOT_GLSLC_SUPPORT"
67+
)
8068

81-
if (${glslc_error} MATCHES ".*extension not supported: GL_EXT_bfloat16.*")
82-
message(STATUS "GL_EXT_bfloat16 not supported by glslc")
83-
set(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT OFF)
84-
else()
85-
message(STATUS "GL_EXT_bfloat16 supported by glslc")
86-
set(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT ON)
87-
add_compile_definitions(GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT)
88-
endif()
69+
test_shader_extension_support(
70+
"GL_EXT_bfloat16"
71+
"${CMAKE_CURRENT_SOURCE_DIR}/vulkan-shaders/test_bfloat16_support.comp"
72+
"GGML_VULKAN_BFLOAT16_GLSLC_SUPPORT"
73+
)
8974

9075
target_link_libraries(ggml-vulkan PRIVATE Vulkan::Vulkan)
9176
target_include_directories(ggml-vulkan PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

0 commit comments

Comments
 (0)