OpenCL extensions with Apple's openclc - opencl

I'm interested in using an optional extension to OpenCL which adds certain functions to the OpenCL language (in particular, cl_khr_gl_msaa_sharing). I'm using Apple's openclc to compile my OpenCL sources at build-time, however, openclc fails to compile my source (see below) because of calls to these new functions. The machine I'm running on does, indeed, support the extensions, and if I use clCreateProgramWithSource() and clBuildProgram() at runtime, everything works great.
Obviously, any build-time tool can't know which extensions are supported at run-time. However, I'd like to be able to compile my source at build-time assuming the extension exists, then at run-time query for the presence for the extension and degrade gracefully if the extension isn't present. Is there a mechanism for doing anything like this?
The top answer to OpenCL half4 type Apple OS X suggests using preprocessor defines inside the OpenCL program to detect extensions, but that won't help me as those defines are evaluated at build-time.
The particular build-time compiler error is this: error: target does not support depth and MSAA textures

I had a similar problem; I wanted the compiler to assume that the extension cl_khr_fp64 existed so I could use the boilerplate code
#ifdef cl_khr_fp64
#pragma OPENCL EXTENSION cl_khr_fp64 : enable
#elif defined(cl_amd_fp64)
#pragma OPENCL EXTENSION cl_amd_fp64 : enable
#else
#error "Double precision floating point not supported by OpenCL implementation."
#endif
and have it compile as if double precision was available.
The solution (as of XCode 6.3.1) was to select Target > Build Settings > OpenCL - Preprocessing, and add cl_khr_fp64 to the OPENCL_PREPROCESSOR_DEFINITIONS section.
The code then compiled without complaining within XCode, assuming there were no syntax errors in my source code (which is what I wanted to check).

Related

What compiles .cl files when calling clBuildProgram() API?

I wonder what kind of compiler compiles .cl files when we call clBuildProgram() API during the runtime? Is that depends on the device?
When you create a program from source and call clBuildProgram(), OpenCL runtime performs on-line compilation of the source. Each OpenCL runtime from the vendor includes OpenCL C compiler. Usually, the compiler is implemented as a shared library and supports only certain type of devices. For example, Intel OpenCL runtime for GPU uses Intel Graphics Compiler library to compile the source for Intel GPU devices.

How to use OpenCL 2.0 kernel language in Qt Creator?

Several days ago, when I started to learn OpenCL, I needed a good IDE to write OpenCL C kernel language, which provides some basic functions like syntax highlighting and static analysis. I was surprised to find out that Qt Creator is able to do the job.
Everything was fine until today I started to use some feature in OpenCL 2.0:
It looks like Qt Creator use libclang to do the parsing and libclang treats my source files as OpenCL 1.0. But My machine supports OpenCL 3.0 and the code was ok to compile and run. So I doubt it only supports OpenCL 1.0.
My questions are:
How to make Qt Creator (or the libclang plugin in it) support OpenCL 2.0?
How do you guys write OpenCL 2.0? Is there any better IDEs or editors?
Thank you guys, I am really a newbie here.
I tried to rename the file's postfix to ".cl2", but it doesn't work.
I read the OpenCL support documentation of LLVM, but failed to find out the solution.
QtCreator uses KDE syntax highlighting engine. The definitions are simple XML files. You can find them here, but opencl.xml doesn't look updated for 2.0/3.0 language. You can try to change it, and then put it into $HOME/.config/QtProject/qtcreator/generic-highlighter/syntax and it should pick it up.
In QtCreator, you can download newer definitions by going to Tools -> Options, select "Text editor" in left, then "Generic highlighter" tab. This will put the updated files in $HOME.local/share/org.kde.syntax-highlighting/

How to interface my own LLVM backend with LLVM IR

I have my own linker and machine code converter.I am using my own assembly instruction for my machine.This machine is a software processor which executes machine code generated by asm to hex converter. Instead of assembly, i wan to use c language now.My question is that how to use LLVM for this purpose.
One approach could be that:
Create one parser which will read .s file (sort of asm file) generated by LLVM IR and map those instruction with my processor specific asm instruction.
I donot want to create linker and asm to machine code converter again.
Is my approach ok? or what could be the better way to do that.
The *.s file you read is not just "sort of asm", it is actually assembler that has already passed some LLVM backend, probably some X86 variant if you have not chosen a different target.
What you really want to do is to make LLVM emit assembly instructions for your own machine instead. This is what Writing an LLVM Backend and similar guides are about.
This is not exactly simple, but I expect that trying to translate some other machine's instruction set (let alone X86) to your own is probably even more difficult, as you would have to emulate each and every detail of a very complex machine.

Where can I compile the opengl source to generate libraries?

Where can I find a program which can be executed on Visual studio to compile openGL sources and generate libraries like opengl32.lib, glut32.lib, etc. In fact I have a problem of version with QT. I want to execute opengl using QT but without QT Opengl API.
Here's the error message :
lnk2026 module unsafe for safeseh image opengl32.lib
I'm really lost.
Thanks in advance for your help.
OpenGL is a specification, not a library with a defined source code. OpenGL implementations are what you use, and most of them are propietary. Some of them are open source (such as MESA), but in general you do not have public access to the source code.
The "Open" in "OpenGL" refers to the nature of the specification, not to the concept of "open source". OpenGL is "Open" in that the specification is freely available and can be implemented by anyone (modulo patent claims).
So while you can compile free-standing implementations of OpenGL, what your operating system provides (in your case, via opengl32.lib and opengl32.dll) is not something you can compile yourself.

Disassemble an OpenCL kernel?

I'm not sure if it's possible. I want to study OpenCL in-depth, so I was wondering if there is a tool to disassemble an compiled OpenCL kernel.
For normal x86 executable, I can use objdump to get a disassembly view. Is there a similar tool for OpenCL kernel, yet?
If you're using NVIDIA's OpenCL implementation for their GPUs, you can do the followings to disassemble an OpenCL kernel:
Use clGetEventProfilingInfo() to dump the ptx code to a file, say ptxfile.ptx. Please refer to the OpenCL specification to have more details on this function.
Use nvcc to compile ptx to cubin file, for example: nvcc -cubin -arch=sm_20 ptxfile.ptx will compile ptxfile.ptx onto a compute capability 2.0 device.
Use cuobjdump to disassemble the cubin file into GPU instructions. For example: cuobjdump -sass ptxfile.cubin
Hope this helps.
I know that this is an old question, but in case someone comes looking here for disassembling a AMD GPU kernel, you can do the following in linux:
export GPU_DUMP_DEVICE_KERNEL=3
This make any kernel that is compiled on your machine dump the assembled code to a file in the same directory.
Source:
http://dis.unal.edu.co/~gjhernandezp/TOS/GPU/ATI_Stream_SDK_OpenCL_Programming_Guide.pdf
Sections 4.2.1 and 4.2.2
The simplest solution, in my experience, is to use clangs OpenCL C compiler and emit SPIR.
It even works on Godbolt's compiler explorer:
https://godbolt.org/z/_JbXPb
Clang can also emit ptx (https://godbolt.org/z/4ARMqM) and amdhsa (https://godbolt.org/z/TduTZQ), but it may not correspond to the ptx and amdhsa assembly generated by the respective driver at runtime.
If you work with an AMD GPU, you can use the Analyzer tool. It is free, cross-platform, and comes in two forms:
Command line tool (ships as part of the CodeXL package, search for the CodeXLAnalyzer executable after installing).
CodeXL GUI application (just switch to the Analyzer mode in CodeXL).
Here is a short summary of what you can do with the Analyzer:
Compile OpenCL kernels, OpenGL shaders and D3D shaders for any GPU that is supported by the installed driver (even without having the GPU physically installed on your system), and get the ISA. Using CodeXL Analyzer (option #2 above), you can get additional information such as an estimation for the number of clock cycles that are required to execute the instruction.
View the compiler-generated statistics (SGPRs usage, VGPRs usage, etc.)
Generate the AMD IL code for the OpenCL kernel.
Export the compiled binaries (ELF, in binary format).
You can download the CodeXL tool suite from here: https://gpuopen.com/compute-product/codexl/
As AMD CodeXLAnalyzer not not supported anymore use
Radeon GPU Analyzer

Resources