[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 (GitHub - KhronosGroup/SPIR at 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

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

Anastasia,

Thanks for the feedback.

Right all the sampler variables will become local (function scope).

I have some updates to the proposal by Brian’s suggestions to accommodate OpenCL C++’s needs since they represent the sampler initializer by struct.

Basically,

  1. Change the builtin function name from __initialize_sampler to __translate_sampler_initializer.

  2. Keep the concrete struct type for sampler initializer in Khronos Clang.

  3. Change the argument of __translate_sampler_initializer from int to the sampler initializer struct. Clang will translate the OpenCL sampler literal to the sampler initializer struct first, then pass it to __translate_sampler_initializer.

Here is the updated example:

sampler_t s = ADDR | NORM | FILT;

void f() {

g(s);

}

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

// opaque struct type for sampler

struct __sampler;

// concrete sampler initializer struct type

struct __sampler_initializer {

int addr;

int normalization;

int filter;

};

constant __sampler *attribute((always_inline)) __translate_sampler_initializer(struct __sampler_initializer); // a builtin function for translating sampler initializer

constant struct __sampler_initializer _SI = {ADDR, NORM, FILT};

void f() {

constant __sampler *_s = __translate_sampler_initializer(_SI);

g(_s);

}

Thanks.

Sam

Hi Sam,

I am just not sure whether having a struct solution is generic enough, certainly less generic than just having __initialize_sampler call.

On the other hand, having this struct offers more functionality (avoid splitting sampler fields later on).

Cheers,

Anastasia

Hi Anastasia,

The sampler initializer struct has a one-to-one mapping to an OpenCL sampler literal, so it has the same expressiveness as the original representation.

As you said, it also keeps a unique copy of initializer for each OpenCL sampler literal.

Sam

I’d like to request that, if we take this approach, we keep the current definitions of CLK_ADDRESS_, CLK_NORMALIZED_COORDS_, and CLK_FILTER_* that are currently in opencl-c.h. I believe these permit efficient translation to the target dependent sampler pointer. We could actually add 3 more addressing modes without adjusting the normalized and filter values, and so these definitions should be sufficient for quite some time.

Brian

Hi Brian,

I think it should be alright to keep the definitions of those constants either way we take.

I wouldn’t argue that leaving integer representation rather than using struct is more generic as ideally we are missing a distinct LLVM type for this.

However, my general concern here is that going for a bigger change (i.e. using struct) has a risk of higher impact on implementations that has originally and since long time relied on integer representation of sampler type.

Cheers,

Anastasia

Hi Anastasia,

Regards,

Brian

Hi Brian,

Perhaps without specific examples it’s hard to judge, but as I said I don’t see big difference between these two representations.

May be we could setup the review and see if anyone has strong preference.

Thanks,

Anastasia

I will create a patch for that. Thanks.

Sam