Problem with a pipeline for converting OpenCL into SPIRV

Hello everyone,

I want to make a generic program to transform an OpenCL project into SPIRV format with the files as optimized as I can for making them faster at execution time. I am following these steps in my pipeline:

First, after finding an OpenCL project, I execute clang for convert the OpenCL files into bytecode format (.cl into .bc), using the following command:
clang -cc1 -triple spir64 -emit-llvm-bc -finclude-default-header -cl-single-precision-constant -o EXAMPLE.bc

After that, I execute the command llvm-spirv (from SPIRV-LLVM) to convert the mentioned bytecode into SPIRV format (.bc into .spv), executing the following command:
llvm-spirv EXAMPLE.bc -o EXAMPLE.spv

After that, I finally execute the spirv-opt (from SPIRV-Tools Project) for finding how different optimizations affect to different projects, using the command like:
spirv-opt EXAMPLE.spv -o EXAMPLE_0.spv

I am having an error in some projects when running this pipeline. When I get the code from a project and it runs the optimizator, it gives me an error. For example, when I applied all of the project called backprop from Rodinia benchmark, I got the following error:
error line 313: Block 26[%if_then65] appears in the binary before its dominator 35[%if_end55_4]
%if_end55_4 = OpLabel

After a little research, I have found the problem comes from one of the first to commands, clang or llvm-spirv. I have tried several things but I do not found exactly where and why the problem is generated. Any suggestions? I would like to continue this project for finishing my final degree project and if it works, give to the community an automatic OpenCL optimizer.

I could not put the links to the projects and benchmark, if you need them or you can help me with this problem, please email me:, I would appreciate it.

Thanks in advance!

1 Like

Hi @Keryen, I would take a look at this GitHub issue and see if it solves the issue.

Hi @miker256, I have applied the flag the issue you sent said, it solves some error cases like the one I specifically post before:

Even so, I still get some errors like the following ones:

error: line 1032: Function definition (id 192) may not be decorated with Import Linkage type.
%compute_velocity = OpFunction %void None %191

error: line 271: Invalid use of function result id 108[%inCube].
OpGroupDecorate %373 %inCube %inSphere

error: line 279: 3rd operand of LoopMerge: operand PartialCount(256) requires SPIR-V version 1.4 or later
OpLoopMerge %for_cond_cleanup %for_inc PartialCount 8

The first one is from a different project, it is called cfd, also from Rodinia benchmark, it fails at

The second one is from a GItHub project called c-opencl-raymarcher, it fails at

The last one is another GitHub project called GRATER, it fails at

Are you compiling with or without optimizations?

It feels like

Could be a spirv-opt issue. You could create the issue against their repo and attached a test case to demonstrate a problem.

This might be a SPIRV-LLVM Translator issue. Again I would suggest to open an issue with a test case to make sure someone looks into this.

I have validated that SPIR-V files, then I have tried to apply optimizations. That errors are given in both cases, in the validator command (spirv-val) and also in the optimizations (spirv-opt), that is because the optimizator applies also the validator before trying to optimize anything.

That makes me think that the problem cannot come from the optimizer, only from the Clang command or the SPIRV-LLVM Translator, because the error is given with no optimizations applied. I will make an issue in SPIRV-LLVM Translator project then.