[Beignet] [PATCH v4 08/10] gbe: add vec_type_hint's type into functionAttributes.

xionghu.luo at intel.com xionghu.luo at intel.com
Mon Dec 7 22:11:09 PST 2015


From: Luo Xionghu <xionghu.luo at intel.com>

for SPIR kernel, user may call clGetKernelInfo with CL_KERNEL_ATTRIBUTES
to query the functionAttributes.

v2: remove debug code.
v3: fix llvm 3.5 build fail.
v4:  only llvm-3.6 or later has kernel_arg_base_type in metadata.

Signed-off-by: Luo Xionghu <xionghu.luo at intel.com>
---
 backend/src/ir/function.hpp           | 33 ++++++++++++++--
 backend/src/ir/type.cpp               |  6 +--
 backend/src/llvm/llvm_gen_backend.cpp | 72 +++++++++++++++++++++++++++++++++++
 3 files changed, 104 insertions(+), 7 deletions(-)

diff --git a/backend/src/ir/function.hpp b/backend/src/ir/function.hpp
index b7853c1..cbaa208 100644
--- a/backend/src/ir/function.hpp
+++ b/backend/src/ir/function.hpp
@@ -182,6 +182,31 @@ namespace ir {
       std::string typeQual;
       std::string argName; // My different from arg->getName()
 
+
+      // only llvm-3.6 or later has kernel_arg_base_type in metadata.
+#if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR <= 5)
+      bool isImage1dT() const {
+        return typeName.compare("image1d_t") == 0;
+      }
+      bool isImage1dArrayT() const {
+        return typeName.compare("image1d_array_t") == 0;
+      }
+      bool isImage1dBufferT() const {
+        return typeName.compare("image1d_buffer_t") == 0;
+      }
+      bool isImage2dT() const {
+        return typeName.compare("image2d_t") == 0;
+      }
+      bool isImage2dArrayT() const {
+        return typeName.compare("image2d_array_t") == 0;
+      }
+      bool isImage3dT() const {
+        return typeName.compare("image3d_t") == 0;
+      }
+      bool isSamplerType() const {
+        return typeName.compare("sampler_t") == 0;
+      }
+#else
       bool isImage1dT() const {
         return typeBaseName.compare("image1d_t") == 0;
       }
@@ -200,16 +225,16 @@ namespace ir {
       bool isImage3dT() const {
         return typeBaseName.compare("image3d_t") == 0;
       }
+      bool isSamplerType() const {
+        return typeBaseName.compare("sampler_t") == 0;
+      }
+#endif
 
       bool isImageType() const {
         return isImage1dT() || isImage1dArrayT() || isImage1dBufferT() ||
                isImage2dT() || isImage2dArrayT() || isImage3dT();
       }
 
-      bool isSamplerType() const {
-        return typeBaseName.compare("sampler_t") == 0;
-      }
-
     };
 
     /*! Create a function input argument */
diff --git a/backend/src/ir/type.cpp b/backend/src/ir/type.cpp
index 450ba61..682757b 100644
--- a/backend/src/ir/type.cpp
+++ b/backend/src/ir/type.cpp
@@ -32,11 +32,11 @@ namespace ir {
       case TYPE_S8: return out << "int8";
       case TYPE_U8: return out << "uint8";
       case TYPE_S16: return out << "int16";
-      case TYPE_U16: return out << "uin16";
+      case TYPE_U16: return out << "uint16";
       case TYPE_S32: return out << "int32";
-      case TYPE_U32: return out << "uin32";
+      case TYPE_U32: return out << "uint32";
       case TYPE_S64: return out << "int64";
-      case TYPE_U64: return out << "uin64";
+      case TYPE_U64: return out << "uint64";
       case TYPE_HALF: return out << "half";
       case TYPE_FLOAT: return out << "float";
       case TYPE_DOUBLE: return out << "double";
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index f011e1f..8d18c4a 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -114,6 +114,46 @@ namespace gbe
            type->isPointerTy();
   }
 
+  static std::string getTypeName(ir::Context &ctx, const Type *type, int sign)
+  {
+    GBE_ASSERT(isScalarType(type));
+    if (type->isFloatTy() == true)
+      return "float";
+    if (type->isHalfTy() == true)
+      return "half";
+    if (type->isDoubleTy() == true)
+      return "double";
+
+    GBE_ASSERT(type->isIntegerTy() == true);
+    if(sign) {
+      if (type == Type::getInt1Ty(type->getContext()))
+        return "char";
+      if (type == Type::getInt8Ty(type->getContext()))
+        return "char";
+      if (type == Type::getInt16Ty(type->getContext()))
+        return "short";
+      if (type == Type::getInt32Ty(type->getContext()))
+        return "int";
+      if (type == Type::getInt64Ty(type->getContext()))
+        return "long";
+    }
+    else
+    {
+      if (type == Type::getInt1Ty(type->getContext()))
+        return "uchar";
+      if (type == Type::getInt8Ty(type->getContext()))
+        return "uchar";
+      if (type == Type::getInt16Ty(type->getContext()))
+        return "ushort";
+      if (type == Type::getInt32Ty(type->getContext()))
+        return "uint";
+      if (type == Type::getInt64Ty(type->getContext()))
+        return "ulong";
+    }
+    GBE_ASSERTM(false, "Unsupported type.");
+    return "";
+  }
+
   /*! LLVM IR Type to Gen IR type translation */
   static ir::Type getType(ir::Context &ctx, const Type *type)
   {
@@ -1963,6 +2003,38 @@ namespace gbe
       } else if (attrName->getString() == "vec_type_hint") {
         GBE_ASSERT(attrNode->getNumOperands() == 3);
         functionAttributes += attrName->getString();
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
+        Value* V = attrNode->getOperand(1);
+#else
+        auto *Op1 = cast<ValueAsMetadata>(attrNode->getOperand(1));
+        Value *V = Op1 ? Op1->getValue() : NULL;
+#endif
+#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 5
+        ConstantInt *sign = dyn_cast<ConstantInt>(attrNode->getOperand(2));
+#else
+        ConstantInt *sign = mdconst::extract<ConstantInt>(attrNode->getOperand(2));
+#endif
+        size_t signValue = sign->getZExtValue();
+        Type* vtype = V->getType();
+        Type* stype = vtype;
+        uint32_t elemNum = 0;
+        if(vtype->isVectorTy()) {
+          VectorType *vectorType = cast<VectorType>(vtype);
+          stype = vectorType->getElementType();
+          elemNum = vectorType->getNumElements();
+        }
+
+        std::string typeName = getTypeName(ctx, stype, signValue);
+
+        std::stringstream param;
+        char buffer[100];
+        param <<"(";
+        param << typeName;
+        if(vtype->isVectorTy())
+          param << elemNum;
+        param <<")";
+        param >> buffer;
+        functionAttributes += buffer;
         functionAttributes += " ";
       } else if (attrName->getString() == "work_group_size_hint") {
         GBE_ASSERT(attrNode->getNumOperands() == 4);
-- 
1.9.1



More information about the Beignet mailing list