[llvm-dev] Execute OpenCL

Sachkov, Alexey via llvm-dev llvm-dev at lists.llvm.org
Thu Sep 26 05:04:46 PDT 2019


Hi Enrique,

> 1) What do yourefer by OpenCL RT?

I mean OpenCL Runtime – some library which implements OpenCL API for you.

Basically, OpenCL consist of two parts: host code and device code.
Host code can be written at any language and actually it is a regular C/C++/Python/whatever application which uses OpenCL API.

OpenCL API allows you to select platform, device, create context and buffers for data, create and build program for your device, create command queue and kernel, set kernel arguments and then enqueue your kernel for execution on the selected device.
You can always read the spec [1] to understand all these concepts. I would recommend section 3 “The OpenCL Architecture”: in particular, section 3.1 “Platform Model” and 3.2 “Execution Model”.
Also you can find some videos [2], slides [3] or articles [4] about this topic.

So, the next part is device code: it can be written on OpenCL C, C++ for OpenCL in clang, or any other language which can be represented in SPIR-V.
As I said in previous emails, device code can be compiled online or offline and then it will be launched on selected device using API.

I encourage you to read the spec or search for videos/slides/articles on this topic to get familiar with main OpenCL concepts/architecture/ideas.

> 2) Could you give me some examples of an open-source OpenCL implementation and update optimization pipeline?

Sure, here you go (there might be more implementations, I’m just not aware of them):


  *   POCL [5]
  *   Intel graphics compute runtime and compiler [6] and [7]
  *   ROCm [8]. I guess compiler sources are located in corresponding forks of llvm [9] and clang [10]
  *   VC4CL [11]

Please note that each implementation targets specific device or set of devices from one or more particular vendors.
The most generic/portable one is POCL, I guess.


[1]: https://www.khronos.org/registry/OpenCL/specs/2.2/pdf/OpenCL_API.pdf
[2]: https://www.youtube.com/watch?v=hUiX8rBcNzw
[3]: https://www.fz-juelich.de/SharedDocs/Downloads/IAS/JSC/EN/slides/opencl/opencl-03-basics.pdf?__blob=publicationFile
[4]: https://www.sciencedirect.com/topics/computer-science/opencl

[5]: https://github.com/pocl/pocl
[6]: https://github.com/intel/compute-runtime
[7]: https://github.com/intel/intel-graphics-compiler
[8]: https://github.com/RadeonOpenCompute/ROCm-OpenCL-Runtime
[9]: https://github.com/RadeonOpenCompute/llvm
[10]: https://github.com/RadeonOpenCompute/clang
[11]: https://github.com/doe300/VC4CL


From: Enrique González <enrike.gonzalez.98 at gmail.com>
Sent: Thursday, September 26, 2019 2:41 PM
To: Sachkov, Alexey <alexey.sachkov at intel.com>; llvm-dev at lists.llvm.org
Subject: Re: [llvm-dev] Execute OpenCL

Hi Alexey,

Your reply has been a great help to me,your way of explain the different types of compilation is very detailed and easy to understand.

Even so, I have a couple of questions.

1) What do yourefer by OpenCL RT?

2) Could you give me some examples of an open-source OpenCL implementation and update optimization pipeline?

Thank you in advance. Regards

El vie., 20 sept. 2019 a las 12:34, Sachkov, Alexey (<alexey.sachkov at intel.com<mailto:alexey.sachkov at intel.com>>) escribió:
Hi Enrique,

> First, I only want to compile a project and execute it to see how it works, specifically this one: https://github.com/rsnemmen/OpenCL-examples/tree/master/add_numbers

As I can see, it has a Makefile which you could use. Anyway, on Linux you need to do something like:

clang -std=c99 add_numbers.c -lOpenCL -I/path/to/folder/with/CL/cl.h

Usually, libOpenCL.so and CL/cl.h are provided by OpenCL SDKs from HW/SW vendor, for example: [1], [2], [3], [4] and others [5]
But, you can also get them from KhronosGroup repos: OpenCL-ICD-Loader [6], OpenCL-Headers [7]

> I want to do that the .c file uses the .cl that i have compiled before with clang and i do not know how to do it.

This is an interesting question. Basically, in OpenCL you can build your device program in a separate ways:


  1.  Use clCreateProgramFromSource + clBuildProgram: this is so-called “online” compilation, when your device code is written in OpenCL C and its compilation is performed in runtime (all stages like front-end, middle-end optimizations and back-end, i.e. codegen to native code)
  2.  Use clCreateProgramWithBinary + clBuildProgram: this is so-called “offline” compilation, where you build your device code manually prior launching the app. Unfortunately, OpenCL spec doesn’t define binary format, i.e. it is implementation-defined. Usually each vendor provides so-called “offline compiler” tool which allows you to get device binary for your OpenCL code – the binary is not portable across different devices/vendors
  3.  Use clCreateProgramWithIL + clBuildProgram: requires OpenCL 2.1 or cl_khr_il_program extension. Allows you to create program from SPIR-V. Basically, this is kind of combination of online and offline models: one the one side, you need to launch offline compiler to parse source code and get SPIR-V, on the other side, SPIR-V is an intermediate representation, i.e. partially-compiled program – that will save some runtime resources. Plus, SPIR-V is a standard and it is portable between devices/vendors.

Summarizing:

  *   There are several OpenCL implementations (both open-source and closed-source) which are LLVM-based
  *   Not sure that a lot of them provide possibility to create OpenCL program from pure LLVM IR produced by clang

If you want to optimize your OpenCL code by yourself you can do the following:

  *   Try to understand binary format that is accepted by OpenCL runtime you are using: try to contact support/forums to check if you can pass already optimized LLVM BC there (BTW, not sure that this functionality is widely supported)
  *   Get LLVM IR from clang, then optimize it as you want, then convert it to SPIR-V and pass to OpenCL RT
  *   Find open-source OpenCL implementation and update optimization pipeline in there with you own modifications

Personally, I would go with the last option.


[1]: https://software.intel.com/en-us/opencl-sdk
[2]: https://developer.nvidia.com/opencl
[3]: https://github.com/GPUOpen-LibrariesAndSDKs/OCL-SDK/releases
[4]: https://developer.arm.com/solutions/graphics/apis/opencl
[5]: https://www.iwocl.org/resources/opencl-implementations/

[6]: https://github.com/KhronosGroup/OpenCL-ICD-Loader
[7]: https://github.com/KhronosGroup/OpenCL-Headers


From: llvm-dev <llvm-dev-bounces at lists.llvm.org<mailto:llvm-dev-bounces at lists.llvm.org>> On Behalf Of Enrique Gonzalez via llvm-dev
Sent: Thursday, September 19, 2019 11:41 PM
To: llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>
Subject: [llvm-dev] Execute OpenCL

Dear all,

After a huge amount of time trying to install LLVM and Clang i could finally do it, so now im trying to use this tools for generating a bytecode, then apply it modular optimizations and then generate an executable to test the result.

First, I only want to compile a project and execute it to see how it works, specifically this one: https://github.com/rsnemmen/OpenCL-examples/tree/master/add_numbers

Which commands would you use?

I want to do that the .c file uses the .cl that i have compiled before with clang and i do not know how to do it.

Thanks in advance.

--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


--
Un saludo

Enrique González

--------------------------------------------------------------------
Joint Stock Company Intel A/O
Registered legal address: Krylatsky Hills Business Park,
17 Krylatskaya Str., Bldg 4, Moscow 121614,
Russian Federation

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190926/18187107/attachment.html>


More information about the llvm-dev mailing list