making the SPIR target work

Hi,

I’ve been looking into making the spir target work with the latest version of clang.

Comparing the current version to this spir branch of clang
https://github.com/KhronosGroup/SPIR
I found these pieces missing that are necessary to generate confirming spir code:

  1. the “-cl-kernel-arg-info” option currently suppresses the generation of all kernel metadata when it should only omit the “kernel_arg_name” name node

  2. The “spir_kernel” calling convention needs to be set on kernel functions

  3. Additional spir metadata needs to be added to the generated module. Done by this code: https://github.com/KhronosGroup/SPIR/blob/spir_12/lib/CodeGen/CGSPIRMetadataAdder.cpp

  4. An additional “cl-spir-compile-options” option is added to clang to allow options to be passed through in the SPIR metadata

Do anyone see any obstacles to checking in these pieces? or know why they haven’t been committed already?

Thanks

Hi,

Do anyone see any obstacles to checking in these pieces? or know why they
haven't been committed already?

I think the main problem is the fact that the SPIR format is fixed to
the IR produced by certain LLVM versions. So, if later LLVM versions
have changes in the IR output, it might not produce valid SPIR
binaries anymore.

SPIR 1.2 is fixed to LLVM 3.2 IR and SPIR 2.0 to LLVM 3.4.

However, if there are other non-SPIR-specific features in that branch,
I hope Someone breaks them up in smaller patches so they get reviewed and
upstreamed.

Hi,

I’ve been looking into making the spir target work with the latest version of clang.

Comparing the current version to this spir branch of clang
https://github.com/KhronosGroup/SPIR
I found these pieces missing that are necessary to generate confirming spir code:

  1. the “-cl-kernel-arg-info” option currently suppresses the generation of all kernel metadata when it should only omit the “kernel_arg_name” name node

  2. The “spir_kernel” calling convention needs to be set on kernel functions

  3. Additional spir metadata needs to be added to the generated module. Done by this code: https://github.com/KhronosGroup/SPIR/blob/spir_12/lib/CodeGen/CGSPIRMetadataAdder.cpp

  4. An additional “cl-spir-compile-options” option is added to clang to allow options to be passed through in the SPIR metadata

Do anyone see any obstacles to checking in these pieces? or know why they haven’t been committed already?

Thanks

spir_target.patch (1.57 KB)

opencl_arginfo.patch (1.11 KB)

Hello Ribulous,

From the attachments, it seems like you have already done some work to port the changes from the Khronos SPIR generator to Clang. But it is not clear if you want to have them reviewed and submitted. As far as I know, there is no reason why they can't be submitted ... it's just that no one has bothered to do it before. It would be useful if you submit each patch as a separate review request.

Like Pekka said, SPIR 1.2 is based on LLVM 3.2, and there is no guarantee that the generated IR will be valid SPIR. But there is no harm in making sure that the SPIR target is maintained so that the generated IR is as close to SPIR 1.2 as possible. One could write a side-project that translates the LLVM IR to version 3.2 in order to produce perfect SPIR 1.2 if necessary. On the other hand, SPIR 2.0 is still provisional, so it's too early to say if those changes should be submitted to Clang.

Sameer.

From the attachments, it seems like you have already done

    > some work to port the changes from the Khronos SPIR
    > generator to Clang. But it is not clear if you want to have
    > them reviewed and submitted. As far as I know, there is no
    > reason why they can't be submitted ... it's just that no one
    > has bothered to do it before. It would be useful if you
    > submit each patch as a separate review request.

+1

    > Like Pekka said, SPIR 1.2 is based on LLVM 3.2, and there is
    > no guarantee that the generated IR will be valid SPIR. But
    > there is no harm in making sure that the SPIR target is
    > maintained so that the generated IR is as close to SPIR 1.2
    > as possible.

+1
That would ease the work for people generating SPIR 1.2...

    > One could write a side-project that translates
    > the LLVM IR to version 3.2 in order to produce perfect SPIR
    > 1.2 if necessary. On the other hand, SPIR 2.0 is still
    > provisional, so it's too early to say if those changes
    > should be submitted to Clang.

And anyway we would have the same issue since SPIR 2.0 is based on LLVM
IR 3.4, which is older than the ToT, by definition of the release
concept.

But there are some tools based on ToT that are able to generate SPIR
1.2, such as the OpenCL SYCL compiler from Codeplay. But I do not know
how they do this, since it is not open source. A post-processor that
downgrades the post-modern LLVM IR to SPIR 1.2 or a direct SPIR 1.2
generator inside ToT LLVM?

Since everybody will have the same issue, it would be nice to have an
Open Source translator ToT LLVM IR -> SPIR 1.2 -> ToT LLVM IR backed by
the Khronos Group and upstreamed in ToT. Since LLVM IR is expected to be
upward compatible, the last translation may be useless or just easy to
implement...