Skip to content

Commit 549bc55

Browse files
[mlir][spirv] Fix int type declaration duplication when serializing (llvm#143108)
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 4cfe0d7 commit 549bc55

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

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 --test-spirv-roundtrip %s | FileCheck %s
2+
// RUN: %if spirv-tools %{ mlir-translate -no-implicit-module -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,4 +306,6 @@ 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
}

mlir/test/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ def find_real_python_interpreter():
332332
else:
333333
config.available_features.add("noasserts")
334334

335+
config.targets = frozenset(config.targets_to_build.split())
335336

336337
def have_host_jit_feature_support(feature_name):
337338
mlir_runner_exe = lit.util.which("mlir-runner", config.mlir_tools_dir)

mlir/test/lit.local.cfg

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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@
9+
config.targets_to_build = "@TARGETS_TO_BUILD@"
810
config.llvm_shlib_ext = "@SHLIBEXT@"
911
config.llvm_shlib_dir = lit_config.substitute(path(r"@SHLIBDIR@"))
1012
config.python_executable = "@Python3_EXECUTABLE@"
@@ -41,7 +43,7 @@ config.mlir_run_amx_tests = @MLIR_RUN_AMX_TESTS@
4143
config.mlir_run_arm_sve_tests = @MLIR_RUN_ARM_SVE_TESTS@
4244
# This is a workaround for the fact that LIT's:
4345
# %if <cond>
44-
# requires <cond> to be in the set of available features.
46+
# requires <cond> to be in the set of available features.
4547
# TODO: Update LIT's TestRunner so that this is not required.
4648
if config.mlir_run_arm_sve_tests:
4749
config.available_features.add("mlir_arm_sve_tests")

0 commit comments

Comments
 (0)