From ae3bfba61593515cb1e5e1342596f904aab497e5 Mon Sep 17 00:00:00 2001
From: Arina Neshlyaeva <arina.neshlyaeva@intel.com>
Date: Tue, 12 Jul 2022 16:09:58 -0700
Subject: [PATCH] Added RegularizePointer method

---
 src/ctx.cpp | 49 +++++++++++++++++--------------------------------
 src/ctx.h   |  3 +++
 2 files changed, 20 insertions(+), 32 deletions(-)

diff --git a/src/ctx.cpp b/src/ctx.cpp
index 1b9c6c3bc..72852998b 100644
--- a/src/ctx.cpp
+++ b/src/ctx.cpp
@@ -2039,6 +2039,17 @@ llvm::Value *FunctionEmitContext::MakeSlicePointer(llvm::Value *ptr, llvm::Value
     return ret;
 }
 
+const PointerType *FunctionEmitContext::RegularizePointer(const Type *ptrRefType) {
+    const PointerType *ptrType;
+    if (CastType<ReferenceType>(ptrRefType) != NULL)
+        ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
+    else {
+        ptrType = CastType<PointerType>(ptrRefType);
+    }
+    AssertPos(currentPos, ptrType != NULL);
+    return ptrType;
+}
+
 llvm::Value *FunctionEmitContext::GetElementPtrInst(llvm::Value *basePtr, llvm::Value *index, const Type *ptrRefType,
                                                     const llvm::Twine &name) {
     if (basePtr == NULL || index == NULL) {
@@ -2047,13 +2058,7 @@ llvm::Value *FunctionEmitContext::GetElementPtrInst(llvm::Value *basePtr, llvm::
     }
 
     // Regularize to a standard pointer type for basePtr's type
-    const PointerType *ptrType;
-    if (CastType<ReferenceType>(ptrRefType) != NULL)
-        ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
-    else {
-        ptrType = CastType<PointerType>(ptrRefType);
-    }
-    AssertPos(currentPos, ptrType != NULL);
+    const PointerType *ptrType = RegularizePointer(ptrRefType);
 
     if (ptrType->IsSlice()) {
         AssertPos(currentPos, llvm::isa<llvm::StructType>(basePtr->getType()));
@@ -2108,14 +2113,8 @@ llvm::Value *FunctionEmitContext::GetElementPtrInst(llvm::Value *basePtr, llvm::
         return NULL;
     }
 
-    // Regaularize the pointer type for basePtr
-    const PointerType *ptrType = NULL;
-    if (CastType<ReferenceType>(ptrRefType) != NULL)
-        ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
-    else {
-        ptrType = CastType<PointerType>(ptrRefType);
-        AssertPos(currentPos, ptrType != NULL);
-    }
+    // Regularize the pointer type for basePtr
+    const PointerType *ptrType = RegularizePointer(ptrRefType);
 
     if (ptrType->IsSlice()) {
         // Similar to the 1D GEP implementation above, for non-frozen slice
@@ -2183,12 +2182,7 @@ llvm::Value *FunctionEmitContext::AddElementOffset(llvm::Value *fullBasePtr, int
     // if we have one, regularize into a pointer type.
     const PointerType *ptrType = NULL;
     if (ptrRefType != NULL) {
-        // Normalize references to uniform pointers
-        if (CastType<ReferenceType>(ptrRefType) != NULL)
-            ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
-        else
-            ptrType = CastType<PointerType>(ptrRefType);
-        AssertPos(currentPos, ptrType != NULL);
+        ptrType = RegularizePointer(ptrRefType);
     }
 
     // Similarly, we have to see if the pointer type is a struct to see if
@@ -2397,14 +2391,11 @@ llvm::Value *FunctionEmitContext::LoadInst(llvm::Value *ptr, llvm::Value *mask,
 
     AssertPos(currentPos, ptrRefType != NULL && mask != NULL);
 
-    const PointerType *ptrType;
+    const PointerType *ptrType = RegularizePointer(ptrRefType);
     const Type *elType;
     if (CastType<ReferenceType>(ptrRefType) != NULL) {
-        ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
         elType = ptrRefType->GetReferenceTarget();
     } else {
-        ptrType = CastType<PointerType>(ptrRefType);
-        AssertPos(currentPos, ptrType != NULL);
         elType = ptrType->GetBaseType();
     }
 
@@ -3019,13 +3010,7 @@ void FunctionEmitContext::StoreInst(llvm::Value *value, llvm::Value *ptr, llvm::
         return;
     }
 
-    const PointerType *ptrType;
-    if (CastType<ReferenceType>(ptrRefType) != NULL)
-        ptrType = PointerType::GetUniform(ptrRefType->GetReferenceTarget());
-    else {
-        ptrType = CastType<PointerType>(ptrRefType);
-        AssertPos(currentPos, ptrType != NULL);
-    }
+    const PointerType *ptrType = RegularizePointer(ptrRefType);
 
     if (CastType<UndefinedStructType>(ptrType->GetBaseType())) {
         Error(currentPos, "Unable to store to undefined struct type \"%s\".",
diff --git a/src/ctx.h b/src/ctx.h
index a842d96d7..2d0bc59f0 100644
--- a/src/ctx.h
+++ b/src/ctx.h
@@ -446,6 +446,9 @@ class FunctionEmitContext {
         and an integer offset to a slice within that type. */
     llvm::Value *MakeSlicePointer(llvm::Value *ptr, llvm::Value *offset);
 
+    /* Regularize to a standard pointer type. */
+    const PointerType *RegularizePointer(const Type *ptrRefType);
+
     /** These GEP methods are generalizations of the standard ones in LLVM;
         they support both uniform and varying basePtr values as well as
         uniform and varying index values (arrays of indices).  Varying base
