[Beignet] [PATCH 2/2] GBE: eliminate duplicate GEP handling logic.

Zhigang Gong zhigang.gong at intel.com
Tue Dec 2 01:42:29 PST 2014


Part of GEP lowering logic in constant expression is the
same as the normal GEP instruction lowering pass. This patch
extract the common logic and reduce the redundant code.

Signed-off-by: Zhigang Gong <zhigang.gong at intel.com>
---
 backend/src/llvm/llvm_gen_backend.cpp | 37 +++++---------------
 backend/src/llvm/llvm_gen_backend.hpp |  8 +++++
 backend/src/llvm/llvm_passes.cpp      | 66 +++++++++++++++++------------------
 3 files changed, 50 insertions(+), 61 deletions(-)

diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index 3d74a0a..fa5f6a1 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -1126,38 +1126,18 @@ namespace gbe
       return pointer_reg;
     }
     else if (expr->getOpcode() == Instruction::GetElementPtr) {
-      int32_t TypeIndex;
       uint32_t constantOffset = 0;
 
       Value *pointer = val;
       CompositeType* CompTy = cast<CompositeType>(pointer->getType());
       for(uint32_t op=1; op<expr->getNumOperands(); ++op) {
-        uint32_t offset = 0;
+        int32_t TypeIndex;
         ConstantInt* ConstOP = dyn_cast<ConstantInt>(expr->getOperand(op));
-        GBE_ASSERT(ConstOP);
+        if (ConstOP == NULL)
+          goto error;
         TypeIndex = ConstOP->getZExtValue();
         GBE_ASSERT(TypeIndex >= 0);
-        if (op == 1) {
-          if (TypeIndex != 0) {
-            Type *elementType = (cast<PointerType>(pointer->getType()))->getElementType();
-            uint32_t elementSize = getTypeByteSize(unit, elementType);
-            uint32_t align = getAlignmentByte(unit, elementType);
-            elementSize += getPadding(elementSize, align);
-            offset += elementSize * TypeIndex;
-          }
-        } else {
-          for(int32_t ty_i=0; ty_i<TypeIndex; ty_i++)
-          {
-            Type* elementType = CompTy->getTypeAtIndex(ty_i);
-            uint32_t align = getAlignmentByte(unit, elementType);
-            offset += getPadding(offset, align);
-            offset += getTypeByteSize(unit, elementType);
-          }
-          const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
-          offset += getPadding(offset, align);
-        }
-
-        constantOffset += offset;
+        constantOffset += getGEPConstOffset(unit, CompTy, TypeIndex);
         CompTy = dyn_cast<CompositeType>(CompTy->getTypeAtIndex(TypeIndex));
       }
 
@@ -1173,10 +1153,11 @@ namespace gbe
       ctx.ADD(ir::Type::TYPE_S32, reg, pointer_reg, offset_reg);
       return reg;
     }
-    else {
-      GBE_ASSERT(0 && "Unsupported constant expression");
-      return regTranslator.getScalar(val, elemID);
-    }
+
+error:
+    expr->dump();
+    GBE_ASSERT(0 && "Unsupported constant expression");
+    return regTranslator.getScalar(val, elemID);
   }
 
   ir::Register GenWriter::getConstantRegister(Constant *c, uint32_t elemID) {
diff --git a/backend/src/llvm/llvm_gen_backend.hpp b/backend/src/llvm/llvm_gen_backend.hpp
index ae0a818..528b3c8 100644
--- a/backend/src/llvm/llvm_gen_backend.hpp
+++ b/backend/src/llvm/llvm_gen_backend.hpp
@@ -29,6 +29,11 @@
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Pass.h"
 #include "llvm/Analysis/LoopPass.h"
+#if LLVM_VERSION_MINOR <= 2
+#include "llvm/Instructions.h"
+#else
+#include "llvm/IR/Instructions.h"
+#endif
 #include "sys/platform.hpp"
 #include "sys/map.hpp"
 #include "sys/hash_map.hpp"
@@ -77,6 +82,9 @@ namespace gbe
   /*! Get the type size in bytes */
   uint32_t getTypeByteSize(const ir::Unit &unit, llvm::Type* Ty);
 
+  /*! Get GEP constant offset for the specified operand.*/
+  int32_t getGEPConstOffset(const ir::Unit &unit, llvm::CompositeType *CompTy, int32_t TypeIndex);
+
   /*! whether this is a kernel function */
   bool isKernelFunction(const llvm::Function &f);
 
diff --git a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp
index cb68fe6..d315d53 100644
--- a/backend/src/llvm/llvm_passes.cpp
+++ b/backend/src/llvm/llvm_passes.cpp
@@ -220,6 +220,35 @@ namespace gbe
     return size_bit/8;
   }
 
+  int32_t getGEPConstOffset(const ir::Unit &unit, CompositeType *CompTy, int32_t TypeIndex) {
+    int32_t offset = 0;
+    SequentialType * seqType = dyn_cast<SequentialType>(CompTy);
+    if (seqType != NULL) {
+      if (TypeIndex != 0) {
+        Type *elementType = seqType->getElementType();
+        uint32_t elementSize = getTypeByteSize(unit, elementType);
+        uint32_t align = getAlignmentByte(unit, elementType);
+        elementSize += getPadding(elementSize, align);
+        offset = elementSize * TypeIndex;
+      }
+    } else {
+      int32_t step = TypeIndex > 0 ? 1 : -1;
+      GBE_ASSERT(CompTy->isStructTy());
+      for(int32_t ty_i=0; ty_i != TypeIndex; ty_i += step)
+      {
+        Type* elementType = CompTy->getTypeAtIndex(ty_i);
+        uint32_t align = getAlignmentByte(unit, elementType);
+        offset += getPadding(offset, align * step);
+        offset += getTypeByteSize(unit, elementType) * step;
+      }
+
+      //add getPaddingding for accessed type
+      const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
+      offset += getPadding(offset, align * step);
+    }
+    return offset;
+  }
+
   class GenRemoveGEPPasss : public BasicBlockPass
   {
 
@@ -268,41 +297,12 @@ namespace gbe
     for(uint32_t op=1; op<GEPInst->getNumOperands(); ++op)
     {
       int32_t TypeIndex;
-      //we have a constant struct/array acces
-      if(ConstantInt* ConstOP = dyn_cast<ConstantInt>(GEPInst->getOperand(op)))
-      {
-        int32_t offset = 0;
+      ConstantInt* ConstOP = dyn_cast<ConstantInt>(GEPInst->getOperand(op));
+      if (ConstOP != NULL) {
         TypeIndex = ConstOP->getZExtValue();
-        int32_t step = TypeIndex > 0 ? 1 : -1;
-        SequentialType * seqType = dyn_cast<SequentialType>(CompTy);
-        if (seqType != NULL) {
-          if (TypeIndex != 0) {
-            Type *elementType = seqType->getElementType();
-            uint32_t elementSize = getTypeByteSize(unit, elementType);
-            uint32_t align = getAlignmentByte(unit, elementType);
-            elementSize += getPadding(elementSize, align);
-            offset += elementSize * TypeIndex;
-          }
-        } else {
-          GBE_ASSERT(CompTy->isStructTy());
-          for(int32_t ty_i=0; ty_i != TypeIndex; ty_i += step)
-          {
-            Type* elementType = CompTy->getTypeAtIndex(ty_i);
-            uint32_t align = getAlignmentByte(unit, elementType);
-            offset += getPadding(offset, align * step);
-            offset += getTypeByteSize(unit, elementType) * step;
-          }
-
-          //add getPaddingding for accessed type
-          const uint32_t align = getAlignmentByte(unit, CompTy->getTypeAtIndex(TypeIndex));
-          offset += getPadding(offset, align * step);
-        }
-
-        constantOffset += offset;
+        constantOffset += getGEPConstOffset(unit, CompTy, TypeIndex);
       }
-      // none constant index (=> only array/verctor allowed)
-      else
-      {
+      else {
         // we only have array/vectors here, 
         // therefore all elements have the same size
         TypeIndex = 0;
-- 
1.8.3.2



More information about the Beignet mailing list