[cfe-dev] [RFC][OpenCL] New representation for sampler_t and its literal initializer

Liu, Yaxun (Sam) via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 9 13:18:22 PDT 2016


Hi,

Currently Clang use int32 to represent sampler_t, which have been a source of issue for some backends, because in some backends sampler_t cannot be represented by int32. They have to depend on kernel argument metadata and use IPA to find the sampler arguments and global variables and transform them to target specific sampler type.

Khronos Clang spirv-1.0 branch (https://github.com/KhronosGroup/SPIR/tree/spirv-1.0 ) represents sampler_t as an opaque struct pointer. Also it represents sampler literal as a concrete struct type, and cast its pointer to a pointer to the opaque struct type for sampler_t. However there are two issues with the way how sampler literal is represented:


1.       Backends still need to translate the sampler initializer struct to target specific sampler initializer, and this transformation cannot be implemented by library functions. Instead, it needs to be done by passes.

2.       Optimizer may try to optimize the sampler global variable initialized with a sampler initializer struct. The optimizer will assume the sampler contains the bits of the initializer struct and may do memory optimizations on it, which will cause difficulty for backends to transform the samplers.

We think representing sampler as an opaque type is a good idea, which we suggest Clang trunk to adopt. And we propose a another way to represent sampler literal. Basically in Clang codegen we generate a function call for each reference of a sampler global variable initialized with a sampler literal, e.g.

sampler_t s = 0;

void f() {
  g(s);
}


ð  Llvm bitcode equivalent to (assuming __sampler is the opaque struct type to represent sampler_t):

constant __sampler *__attribute__((always_inline)) __initialize_sampler(int); // a builtin function for initialize a sampler

void f() {
  constant __sampler *_s = __initialize_sampler(0);
  g(_s);
}

Each builtin library can implement its own __initialize_sampler(). Since the real sampler type tends to be architecture dependent, allowing it to be initialized by a library function simplifies backend design. A typical implementation of __initialize_sampler could be a table lookup of real sampler literal values. Since its argument is always a literal, the returned pointer is known at compile time and easily optimized to finally become some literal values directly put into image read instructions.

The advantage of this representation is:


1.       Robust - can be optimized without losing information

2.       Easy to implement - can be implemented by library instead of pass

Your feedbacks are welcome. Thanks.

Sam






-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 21599 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160609/b78b0e79/attachment.bin>


More information about the cfe-dev mailing list