Skip to content

Commit 0121a8e

Browse files
Reland "[mlir][spirv] Fix int type declaration duplication when serializing" (#145687)
This relands PRs #143108 and #144538. The original PR was reverted due to a mistake that made all the mlir tests run only if SPIRV target was enabled. This is now resolved since enabling spirv-tools does not required SPIRV target any longer. spirv-tools are not required by default to run SPIRV mlir tests, but they can be optionally enabled in some SPIRV mlir test to verify that the produced SPIRV assembly pass validation. The other reverted PR #144685 is not longer needed and not part of this relanding. Original commit message: > At the MLIR level unsigned integer and signless integers are different types. Indeed when looking up the two types in type definition cache they do not match. > Hence when translating a SPIR-V module which contains both usign and signless integers will contain the same type declaration twice (something like OpTypeInt 32 0) which is not permitted in SPIR-V and such generated modules fail validation. > This patch solves the problem by mapping unisgned integer types to singless integer types before looking up in the type definition cache. --------- Signed-off-by: Davide Grohmann <[email protected]>
1 parent 496d31c commit 0121a8e

File tree

8 files changed

+30
-7
lines changed

8 files changed

+30
-7
lines changed

llvm/tools/spirv-tools/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ if (NOT LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
55
return()
66
endif ()
77

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-
128
# SPIRV_DIS, SPIRV_VAL, SPIRV_AS and SPIRV_LINK variables can be used to provide paths to existing
139
# spirv-dis, spirv-val, spirv-as, and spirv-link binaries, respectively. Otherwise, build them from
1410
# SPIRV-Tools source.

mlir/lib/Target/SPIRV/Serialization/Serializer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,19 @@ LogicalResult Serializer::processType(Location loc, Type type,
446446
LogicalResult
447447
Serializer::processTypeImpl(Location loc, Type type, uint32_t &typeID,
448448
SetVector<StringRef> &serializationCtx) {
449+
450+
// Map unsigned integer types to singless integer types.
451+
// This is needed otherwise the generated spirv assembly will contain
452+
// twice a type declaration (like OpTypeInt 32 0) which is no permitted and
453+
// such module fails validation. Indeed at MLIR level the two types are
454+
// different and lookup in the cache below misses.
455+
// Note: This conversion needs to happen here before the type is looked up in
456+
// the cache.
457+
if (type.isUnsignedInteger()) {
458+
type = IntegerType::get(loc->getContext(), type.getIntOrFloatBitWidth(),
459+
IntegerType::SignednessSemantics::Signless);
460+
}
461+
449462
typeID = getTypeID(type);
450463
if (typeID)
451464
return success();

mlir/test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ endif()
6868
llvm_canonicalize_cmake_booleans(
6969
LLVM_BUILD_EXAMPLES
7070
LLVM_HAS_NVPTX_TARGET
71+
LLVM_INCLUDE_SPIRV_TOOLS_TESTS
7172
MLIR_ENABLE_BINDINGS_PYTHON
7273
MLIR_ENABLE_CUDA_RUNNER
7374
MLIR_ENABLE_ROCM_CONVERSIONS
@@ -217,6 +218,11 @@ if(MLIR_ENABLE_BINDINGS_PYTHON)
217218
)
218219
endif()
219220

221+
if (LLVM_INCLUDE_SPIRV_TOOLS_TESTS)
222+
list(APPEND MLIR_TEST_DEPENDS spirv-as)
223+
list(APPEND MLIR_TEST_DEPENDS spirv-val)
224+
endif()
225+
220226
# This target can be used to just build the dependencies
221227
# for the check-mlir target without executing the tests.
222228
# This is useful for bots when splitting the build step

mlir/test/Target/SPIRV/constant.mlir

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// RUN: mlir-translate --no-implicit-module --split-input-file --test-spirv-roundtrip %s | FileCheck %s
2+
// RUN: %if spirv-tools %{ mlir-translate -no-implicit-module --split-input-file -serialize-spirv %s | spirv-val %}
23

3-
spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
4+
spirv.module Logical Vulkan requires #spirv.vce<v1.3, [VulkanMemoryModel, Shader, Int64, Int16, Int8, Float64, Float16, CooperativeMatrixKHR], [SPV_KHR_vulkan_memory_model, SPV_KHR_cooperative_matrix]> {
45
// CHECK-LABEL: @bool_const
56
spirv.func @bool_const() -> () "None" {
67
// CHECK: spirv.Constant true
@@ -305,6 +306,8 @@ spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader], []> {
305306
%coop = spirv.Constant dense<4> : !spirv.coopmatrix<16x16xi8, Subgroup, MatrixAcc>
306307
spirv.ReturnValue %coop : !spirv.coopmatrix<16x16xi8, Subgroup, MatrixAcc>
307308
}
309+
310+
spirv.EntryPoint "GLCompute" @bool_const
308311
}
309312

310313
// -----

mlir/test/Target/SPIRV/lit.local.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if config.spirv_tools_tests:
2+
config.available_features.add("spirv-tools")
3+
config.substitutions.append(("spirv-as", os.path.join(config.llvm_tools_dir, "spirv-as")))
4+
config.substitutions.append(("spirv-val", os.path.join(config.llvm_tools_dir, "spirv-val")))

mlir/test/lit.cfg.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ def find_real_python_interpreter():
343343
else:
344344
config.available_features.add("noasserts")
345345

346-
347346
def have_host_jit_feature_support(feature_name):
348347
mlir_runner_exe = lit.util.which("mlir-runner", config.mlir_tools_dir)
349348

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sys
55
config.target_triple = "@LLVM_TARGET_TRIPLE@"
66
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
77
config.llvm_tools_dir = lit_config.substitute("@LLVM_TOOLS_DIR@")
8+
config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
89
config.llvm_shlib_ext = "@SHLIBEXT@"
910
config.llvm_shlib_dir = lit_config.substitute(path(r"@SHLIBDIR@"))
1011
config.python_executable = "@Python3_EXECUTABLE@"
@@ -41,7 +42,7 @@ config.mlir_run_amx_tests = @MLIR_RUN_AMX_TESTS@
4142
config.mlir_run_arm_sve_tests = @MLIR_RUN_ARM_SVE_TESTS@
4243
# This is a workaround for the fact that LIT's:
4344
# %if <cond>
44-
# requires <cond> to be in the set of available features.
45+
# requires <cond> to be in the set of available features.
4546
# TODO: Update LIT's TestRunner so that this is not required.
4647
if config.mlir_run_arm_sve_tests:
4748
config.available_features.add("mlir_arm_sve_tests")

utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ expand_template(
3737
# All disabled, but required to substituted because they are not in quotes.
3838
"@LLVM_BUILD_EXAMPLES@": "0",
3939
"@LLVM_HAS_NVPTX_TARGET@": "0",
40+
"@LLVM_INCLUDE_SPIRV_TOOLS_TESTS@": "0",
4041
"@MLIR_ENABLE_CUDA_RUNNER@": "0",
4142
"@MLIR_ENABLE_ROCM_CONVERSIONS@": "0",
4243
"@MLIR_ENABLE_ROCM_RUNNER@": "0",

0 commit comments

Comments
 (0)