[PATCH] D21567: [OpenCL] Generate struct type for sampler_t and function call for the initializer

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 21 13:02:11 PDT 2016


yaxunl created this revision.
yaxunl added reviewers: Anastasia, bader, pxli168.
yaxunl added subscribers: cfe-commits, tstellarAMD.

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.

This patch uses opaque struct pointer type __sampler* for sampler_t. For each sampler constant, it generates a global var of struct type __sampler_initializer.  It also generates a function call __translate_sampler_initializer for each reference of global vars of __sampler_initializer type. e.g.
 
  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);
  }

 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:

  # Robust - can be optimized without losing information
  # Easy to implement – can be implemented by library instead of pass

The implementation of the design is to introduce an internal sampler initializer type and translates each sampler variable to sampler initializer type in AST, and introduces cast from integer to sampler initializer and cast from sampler initializer to sampler. In codegen, sampler type is translated to opaque __sampler* type, sampler initializer type is translated to concrete __sampler_initializer type. The cast from sampler initializer to sampler is translated to function call __translate_sampler_initializer.

This patch is partly based on Alexey Sotkin's work in Khronos Clang (https://github.com/KhronosGroup/SPIR/commit/3d4eec61623502fc306e8c67c9868be2b136e42b).

http://reviews.llvm.org/D21567

Files:
  include/clang/AST/ASTContext.h
  include/clang/AST/BuiltinTypes.def
  include/clang/AST/OperationKinds.def
  include/clang/AST/Type.h
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Basic/LangOptions.def
  include/clang/Driver/CC1Options.td
  include/clang/Sema/Overload.h
  include/clang/Serialization/ASTBitCodes.h
  lib/AST/ASTContext.cpp
  lib/AST/Expr.cpp
  lib/AST/ExprConstant.cpp
  lib/AST/ItaniumMangle.cpp
  lib/AST/MicrosoftMangle.cpp
  lib/AST/NSAPI.cpp
  lib/AST/Type.cpp
  lib/AST/TypeLoc.cpp
  lib/CodeGen/CGDebugInfo.cpp
  lib/CodeGen/CGDebugInfo.h
  lib/CodeGen/CGExpr.cpp
  lib/CodeGen/CGExprAgg.cpp
  lib/CodeGen/CGExprComplex.cpp
  lib/CodeGen/CGExprConstant.cpp
  lib/CodeGen/CGExprScalar.cpp
  lib/CodeGen/CGOpenCLRuntime.cpp
  lib/CodeGen/CodeGenModule.cpp
  lib/CodeGen/CodeGenModule.h
  lib/CodeGen/CodeGenTypes.cpp
  lib/CodeGen/ItaniumCXXABI.cpp
  lib/Edit/RewriteObjCFoundationAPI.cpp
  lib/Frontend/CompilerInvocation.cpp
  lib/Index/USRGeneration.cpp
  lib/Sema/SemaDecl.cpp
  lib/Sema/SemaExpr.cpp
  lib/Sema/SemaExprCXX.cpp
  lib/Sema/SemaInit.cpp
  lib/Sema/SemaOverload.cpp
  lib/Serialization/ASTCommon.cpp
  lib/Serialization/ASTReader.cpp
  lib/StaticAnalyzer/Core/ExprEngineC.cpp
  test/CodeGenOpenCL/opencl_types.cl
  test/CodeGenOpenCL/sampler.cl
  test/SemaOpenCL/sampler_t.cl
  tools/libclang/CIndex.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21567.61422.patch
Type: text/x-patch
Size: 36063 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160621/adb9331e/attachment-0001.bin>


More information about the cfe-commits mailing list