KhronosGroup / SPIRV-LLVM-Translator

A tool and a library for bi-directional translation between SPIR-V and LLVM IR
Other
488 stars 219 forks source link

Execution Mode VecTypeHint Encoded Incorrectly for Scalar Types #593

Open FranklandJack opened 4 years ago

FranklandJack commented 4 years ago

For a scalar type e.g. char, int, float etc. the length of the vector in the SPIRV encoding of the hint is 0. The spec says:

The 16 high-order bits of the Vector Type operand specify the number of components of the vector.

implying that when the hint type is scalar the upper 16 bits should be have a value of 0x0001 rather than 0x0000. (see https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#Execution_Mode).

An example of this can be reproduced as follows: For an OpenCL kernel in file kernel.cl:

__kernel __attribute((vec_type_hint(char))) void foo(){}

calling clang with

clang -cc1 -emit-llvm-bc -triple spir64-unknown-unknown -O0 -finclude-default-header -cl-std=CL1.2 -o  kernel.bc kernel.cl

(where clang --version is version 11.0.0 (https://github.com/llvm/llvm-project.git d812efb121f72958cc88c866f3ed3b80ed052856))

then

llvm-spirv kernel.bc

(where llvm-spirv --version is LLVM version 11.0.0git)

and

spirv-dis kernel.spv

(where spirv-dis --version is SPIRV-Tools v2020.2 unknown hash, 2020-04-01T21:21:27)

will produce OpExecutionMode %4 VecTypeHint 0x0 rather than OpExecutionMode %4 VecTypeHint 0x10000.

This isn't a problem for the llvm-spirv tool since decoding is consistent with encoding and the zero length vectors in the SPIRV hint are correctly converted back to IR vectors of length one. However, this encoding still seems inconsistent with the spec wording above.

FranklandJack commented 4 years ago

PR: https://github.com/KhronosGroup/SPIRV-LLVM-Translator/pull/594