[LLVMdev] Do I need to add new intrinsic functions for the OpenGL shading language swizzle?

Wei wei.hu.tw at gmail.com
Mon Nov 17 18:51:34 PST 2008

OpenGL shading language (GLSL) is like a C subset language, but it contains
some special features, ex: native vector type & swizzle.
In GLSL, you can declare vector types:

void main()
  vec4 a;
  vec3 b;
  vec2 c;

You can access the element of vector by using .xyzw, it means the 1st, 2nd,
3rd, 4th element of the vector are x, y, z, w.

void main()
  float f;
  vec4 a = vec4(1.0, 2.0, 3.0, 4.0);
  vec4 b = vec4(3.0, 4.0, 5.0, 6.0);

  v4.xyw = vec2(2.5, 3.3. 7.7);
  f = v4.y;

  b.xy += a.xy;

Because my chip supports this facility in instruction & register level, I
think I have to preserve the semantic of this 'vector' and 'swizzle'. I
don't think this GLSL::vector can be represented directly by the
llvm::vector type, because I can just operate on some elements on a
GLSL::vector rather than 4 elements all the time. (Am I right about this

Hence, I think I would need to add some new 'intrinsic' function in LLVM,
and use llvm::vector to represent GLSL::vector.


vec4 a;
vec3 b
a.xyz = b;

will be translate to

llvm::vector<4> a;
llvm::vector<3> b;
intrinsic_vector3_to_vector4_assign(a, 0, 1, 2, b);

Then in the backend, I can see this intrinsic function, and do instruction
selections based on this intrinsic. Ex: intrinsic_vector3_to_vector4_assign
will be translated to single machine instruction: mov a.xyz, b.xyz.

Am I right on this thought? Does it really needs to add LLVM intrinsic
functions to support OpenGL shading language?
Or am I missing some really usful mechanism in LLVM to natively support

Comments are welcome.

Wei Hu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20081118/32443b11/attachment.html>

More information about the llvm-dev mailing list