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

Anastasia Stulova via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 10 07:15:59 PDT 2016


Hi Sam,

I agree the original implementation of sampler type in LLVM isn't ideal.

Your approach seems sensible to me. Would this mean that all sampler variables will be transformed to local by Clang?

Thanks,
Anastasia

From: Liu, Yaxun (Sam) [mailto:Yaxun.Liu at amd.com]
Sent: 09 June 2016 21:18
To: cfe-dev (cfe-dev at lists.llvm.org); Anastasia Stulova; Bader, Alexey (alexey.bader at intel.com); Pan, Xiuli
Cc: Tom Stellard; Sumner, Brian; Tye, Tony
Subject: [RFC][OpenCL] New representation for sampler_t and its literal initializer

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 --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160610/a0403723/attachment.html>


More information about the cfe-dev mailing list