From 92dcd53dba46016dfc69bb783492c3af127f4f3f Mon Sep 17 00:00:00 2001
From: Arina Neshlyaeva <arina.neshlyaeva@intel.com>
Date: Thu, 8 Sep 2022 16:42:32 -0700
Subject: [PATCH] StoreInst doesn't need isUniformData argument anymore

---
 src/ctx.cpp  | 29 +++++++----------------------
 src/ctx.h    |  2 +-
 src/expr.cpp | 50 ++++++++++++++++++++++++--------------------------
 src/func.cpp |  2 +-
 4 files changed, 33 insertions(+), 50 deletions(-)

diff --git a/src/ctx.cpp b/src/ctx.cpp
index fcd96dfe4..05ba3aeae 100644
--- a/src/ctx.cpp
+++ b/src/ctx.cpp
@@ -1279,7 +1279,7 @@ void FunctionEmitContext::CurrentLanesReturned(Expr *expr, bool doCoherenceCheck
             llvm::Value *retVal = expr->GetValue(this);
             if (retVal != NULL) {
                 if (returnType->IsUniformType() || CastType<ReferenceType>(returnType) != NULL)
-                    StoreInst(retVal, returnValueAddressInfo, returnType, returnType->IsUniformType());
+                    StoreInst(retVal, returnValueAddressInfo, returnType);
                 else {
                     // Use a masked store to store the value of the expression
                     // in the return value memory; this preserves the return
@@ -2970,7 +2970,7 @@ void FunctionEmitContext::scatter(llvm::Value *value, llvm::Value *ptr, const Ty
         addGSMetadata(inst, currentPos);
 }
 
-void FunctionEmitContext::StoreInst(llvm::Value *value, AddressInfo *ptrInfo, const Type *ptrType, bool isUniformData) {
+void FunctionEmitContext::StoreInst(llvm::Value *value, AddressInfo *ptrInfo, const Type *ptrType) {
     if (value == NULL || ptrInfo == NULL) {
         // may happen due to error elsewhere
         AssertPos(currentPos, m->errorCount > 0);
@@ -2997,16 +2997,6 @@ void FunctionEmitContext::StoreInst(llvm::Value *value, AddressInfo *ptrInfo, co
         inst->setAlignment(llvm::MaybeAlign(g->target->getNativeVectorAlignment()).valueOrOne());
     }
 
-#ifdef ISPC_XE_ENABLED
-    // For uniform data like short vectors we need to add ISPC-Uniform metadata
-    // to exclude these instructions from predication in SIMDCFLowering pass.
-    llvm::VectorType *ty = llvm::dyn_cast<llvm::VectorType>(value->getType());
-    if (ty != NULL) {
-        if (emitXeHardwareMask() && isUniformData) {
-            XeUniformMetadata(inst);
-        }
-    }
-#endif
     AddDebugPos(inst);
 }
 
@@ -3034,11 +3024,11 @@ void FunctionEmitContext::StoreInst(llvm::Value *value, llvm::Value *ptr, llvm::
             storeUniformToSOA(value, ptr, mask, valueType, ptrType);
         else if (ptrType->GetBaseType()->IsUniformType())
             // the easy case
-            StoreInst(value, ptrInfo, valueType, true);
+            StoreInst(value, ptrInfo, valueType);
         else if (mask == LLVMMaskAllOn && !g->opt.disableMaskAllOnOptimizations)
             // Otherwise it is a masked store unless we can determine that the
             // mask is all on...  (Unclear if this check is actually useful.)
-            StoreInst(value, ptrInfo, valueType, false);
+            StoreInst(value, ptrInfo, valueType);
         else {
             maskedStore(value, ptr, ptrType, mask);
         }
@@ -3071,7 +3061,7 @@ void FunctionEmitContext::storeUniformToSOA(llvm::Value *value, llvm::Value *ptr
         // then we can do a final regular store
         AssertPos(currentPos, Type::IsBasicType(valueType));
         ptr = lFinalSliceOffset(this, ptr, &ptrType);
-        StoreInst(value, new AddressInfo(ptr, ptrType), valueType, valueType->IsUniformType());
+        StoreInst(value, new AddressInfo(ptr, ptrType), valueType);
     }
 }
 
@@ -3437,12 +3427,11 @@ llvm::Value *FunctionEmitContext::CallInst(llvm::Value *func, const FunctionType
 #ifdef ISPC_XE_ENABLED
             // Current mask will be calculated according to EM mask
             oldFullMask = XeSimdCFPredicate(LLVMMaskAllOn);
-            StoreInst(oldFullMask, maskPtrInfo, NULL, true);
 #endif
         } else {
             oldFullMask = GetFullMask();
-            StoreInst(oldFullMask, maskPtrInfo);
         }
+        StoreInst(oldFullMask, maskPtrInfo);
 
         // Mask wasn't initialized
         Assert(oldFullMask != NULL && "Mask is not initialized");
@@ -3541,11 +3530,7 @@ llvm::Value *FunctionEmitContext::CallInst(llvm::Value *func, const FunctionType
             // currentMask = currentMask & ~callmask
             llvm::Value *notCallMask = BinaryOperator(llvm::Instruction::Xor, callMask, LLVMMaskAllOn, "~callMask");
             currentMask = BinaryOperator(llvm::Instruction::And, currentMask, notCallMask, "currentMask&~callMask");
-            if (emitXeHardwareMask()) {
-                StoreInst(currentMask, maskPtrInfo, NULL, true);
-            } else {
-                StoreInst(currentMask, maskPtrInfo);
-            }
+            StoreInst(currentMask, maskPtrInfo);
 
             // And go back to the test to see if we need to do another
             // call.
diff --git a/src/ctx.h b/src/ctx.h
index d669932d9..c9058be4a 100644
--- a/src/ctx.h
+++ b/src/ctx.h
@@ -525,7 +525,7 @@ class FunctionEmitContext {
         single pointer, not a varying lvalue.
         'ptrType' needs to be provided when storage type is different from IR type. For example,
      * 'unform bool' is 'i1' in IR but stored as 'i8'. */
-    void StoreInst(llvm::Value *value, AddressInfo *ptrInfo, const Type *ptrType = NULL, bool isUniformData = false);
+    void StoreInst(llvm::Value *value, AddressInfo *ptrInfo, const Type *ptrType = NULL);
 
     /** In this variant of StoreInst(), the lvalue may be varying.  If so,
         this corresponds to a scatter.  Whether the lvalue is uniform of
diff --git a/src/expr.cpp b/src/expr.cpp
index 02435e053..95031c540 100644
--- a/src/expr.cpp
+++ b/src/expr.cpp
@@ -611,7 +611,8 @@ bool ispc::PossiblyResolveFunctionOverloads(Expr *expr, const Type *type) {
     @param ctx       FunctionEmitContext to use for generating instructions
     @param pos       Source file position of the variable being initialized
 */
-void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr, FunctionEmitContext *ctx, SourcePos pos) {
+void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr, FunctionEmitContext *ctx,
+                      SourcePos pos) {
     if (initExpr == NULL)
         // leave it uninitialized
         return;
@@ -633,7 +634,7 @@ void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr,
         }
 
         if (Type::IsBasicType(symType))
-            ctx->StoreInst(constValue, ptrInfo, symType, symType->IsUniformType());
+            ctx->StoreInst(constValue, ptrInfo, symType);
         else {
             llvm::Value *constPtr =
                 new llvm::GlobalVariable(*m->module, llvmType, true /* const */, llvm::GlobalValue::InternalLinkage,
@@ -659,8 +660,7 @@ void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr,
         llvm::Value *initializerValue = initExpr->GetValue(ctx);
         if (initializerValue != NULL)
             // Bingo; store the value in the variable's storage
-            ctx->StoreInst(initializerValue, ptrInfo, symType,
-                           symType->IsUniformType());
+            ctx->StoreInst(initializerValue, ptrInfo, symType);
         return;
     }
 
@@ -696,8 +696,7 @@ void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr,
 
         llvm::Value *initializerValue = initExpr->GetValue(ctx);
         if (initializerValue)
-            ctx->StoreInst(initializerValue, ptrInfo, initExpr->GetType(),
-                           symType->IsUniformType());
+            ctx->StoreInst(initializerValue, ptrInfo, initExpr->GetType());
         return;
     }
 
@@ -762,9 +761,9 @@ void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr,
                 if (CastType<StructType>(symType) != NULL)
                     ep = ctx->AddElementOffset(ptrInfo->getPointer(), i, NULL, "element");
                 else
-                    ep = ctx->GetElementPtrInst(ptrInfo->getPointer(), LLVMInt32(0), LLVMInt32(i), PointerType::GetUniform(elementType),
-                                                "gep");
-                AddressInfo* epInfo = new AddressInfo(ep, ptrInfo->getElementType());
+                    ep = ctx->GetElementPtrInst(ptrInfo->getPointer(), LLVMInt32(0), LLVMInt32(i),
+                                                PointerType::GetUniform(elementType), "gep");
+                AddressInfo *epInfo = new AddressInfo(ep, ptrInfo->getElementType());
                 if (i < nInits)
                     InitSymbol(epInfo, elementType, exprList->exprs[i], ctx, pos);
                 else {
@@ -777,7 +776,7 @@ void ispc::InitSymbol(AddressInfo *ptrInfo, const Type *symType, Expr *initExpr,
                     }
 
                     llvm::Constant *zeroInit = llvm::Constant::getNullValue(llvmType);
-                    ctx->StoreInst(zeroInit, epInfo, elementType, elementType->IsUniformType());
+                    ctx->StoreInst(zeroInit, epInfo, elementType);
                 }
             }
         } else if (collectionType) {
@@ -1774,7 +1773,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             // uniform or varying)
             ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
             llvm::Value *trueValue = retType->IsUniformType() ? LLVMTrue : LLVMMaskAllOn;
-            ctx->StoreInst(trueValue, retPtrInfo, retType, retType->IsUniformType());
+            ctx->StoreInst(trueValue, retPtrInfo, retType);
             ctx->BranchInst(bbLogicalDone);
         } else {
             AssertPos(pos, op == BinaryExpr::LogicalAnd);
@@ -1787,7 +1786,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             // uniform or varying false).
             ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
             llvm::Value *falseValue = retType->IsUniformType() ? LLVMFalse : LLVMMaskAllOff;
-            ctx->StoreInst(falseValue, retPtrInfo, retType, retType->IsUniformType());
+            ctx->StoreInst(falseValue, retPtrInfo, retType);
             ctx->BranchInst(bbLogicalDone);
         }
 
@@ -1806,7 +1805,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             AssertPos(pos, m->errorCount > 0);
             return NULL;
         }
-        ctx->StoreInst(value1, retPtrInfo, arg1->GetType(), retType->IsUniformType());
+        ctx->StoreInst(value1, retPtrInfo, arg1->GetType());
         ctx->BranchInst(bbLogicalDone);
 
         // In all cases, we end up at the bbLogicalDone basic block;
@@ -1847,7 +1846,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             // value0 is true for all running lanes, so it can be used for
             // the final result
             ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
-            ctx->StoreInst(value0, retPtrInfo, arg0->GetType(), retType->IsUniformType());
+            ctx->StoreInst(value0, retPtrInfo, arg0->GetType());
             ctx->BranchInst(bbLogicalDone);
 
             // Otherwise, we need to valuate arg1. However, first we need
@@ -1871,7 +1870,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             llvm::Value *value1AndMask =
                 ctx->BinaryOperator(llvm::Instruction::And, value1, ctx->GetInternalMask(), "op&mask");
             llvm::Value *result = ctx->BinaryOperator(llvm::Instruction::Or, value0AndMask, value1AndMask, "or_result");
-            ctx->StoreInst(result, retPtrInfo, retType, retType->IsUniformType());
+            ctx->StoreInst(result, retPtrInfo, retType);
             ctx->BranchInst(bbLogicalDone);
         } else {
             AssertPos(pos, op == BinaryExpr::LogicalAnd);
@@ -1896,7 +1895,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
             // value0 was false for all running lanes, so use its value as
             // the overall result.
             ctx->SetCurrentBasicBlock(bbSkipEvalValue1);
-            ctx->StoreInst(value0, retPtrInfo, arg0->GetType(), retType->IsUniformType());
+            ctx->StoreInst(value0, retPtrInfo, arg0->GetType());
             ctx->BranchInst(bbLogicalDone);
 
             // Otherwise we need to evaluate value1, but again with the
@@ -1920,7 +1919,7 @@ llvm::Value *lEmitLogicalOp(BinaryExpr::Op op, Expr *arg0, Expr *arg1, FunctionE
                 ctx->BinaryOperator(llvm::Instruction::And, value1, ctx->GetInternalMask(), "value1&mask");
             llvm::Value *result =
                 ctx->BinaryOperator(llvm::Instruction::And, value0AndMask, value1AndMask, "or_result");
-            ctx->StoreInst(result, retPtrInfo, retType, retType->IsUniformType());
+            ctx->StoreInst(result, retPtrInfo, retType);
             ctx->BranchInst(bbLogicalDone);
         }
 
@@ -3274,7 +3273,7 @@ static llvm::Value *lEmitVaryingSelect(FunctionEmitContext *ctx, llvm::Value *te
     AddressInfo *resultPtrInfo = ctx->AllocaInst(type, "selectexpr_tmp");
     Assert(resultPtrInfo != NULL);
     // Don't need to worry about masking here
-    ctx->StoreInst(expr2, resultPtrInfo, type, type->IsUniformType());
+    ctx->StoreInst(expr2, resultPtrInfo, type);
     // Use masking to conditionally store the expr1 values
     Assert(resultPtrInfo->getType() == PointerType::GetUniform(type)->LLVMStorageType(g->ctx));
     ctx->StoreInst(expr1, resultPtrInfo->getPointer(), test, type, PointerType::GetUniform(type));
@@ -3296,7 +3295,7 @@ static void lEmitSelectExprCode(FunctionEmitContext *ctx, llvm::Value *testVal,
     llvm::Value *testAndMask = ctx->BinaryOperator(llvm::Instruction::And, testVal, oldMask, "test&mask");
     ctx->SetInternalMask(testAndMask);
     llvm::Value *exprVal = expr->GetValue(ctx);
-    ctx->StoreInst(exprVal, exprPtrInfo, expr->GetType(), expr->GetType()->IsUniformType());
+    ctx->StoreInst(exprVal, exprPtrInfo, expr->GetType());
     ctx->BranchInst(bbDone);
 
     ctx->SetCurrentBasicBlock(bbDone);
@@ -3403,7 +3402,7 @@ llvm::Value *SelectExpr::GetValue(FunctionEmitContext *ctx) const {
         else {
             ctx->SetInternalMaskAnd(oldMask, testVal);
             llvm::Value *expr1Val = expr1->GetValue(ctx);
-            ctx->StoreInst(expr1Val, expr1PtrInfo, expr1->GetType(), expr1->GetType()->IsUniformType());
+            ctx->StoreInst(expr1Val, expr1PtrInfo, expr1->GetType());
         }
 
         if (shortCircuit2) {
@@ -3412,7 +3411,7 @@ llvm::Value *SelectExpr::GetValue(FunctionEmitContext *ctx) const {
         } else {
             ctx->SetInternalMaskAndNot(oldMask, testVal);
             llvm::Value *expr2Val = expr2->GetValue(ctx);
-            ctx->StoreInst(expr2Val, expr2PtrInfo, expr2->GetType(), expr2->GetType()->IsUniformType());
+            ctx->StoreInst(expr2Val, expr2PtrInfo, expr2->GetType());
         }
 
         ctx->SetInternalMask(oldMask);
@@ -4376,7 +4375,7 @@ llvm::Value *IndexExpr::GetValue(FunctionEmitContext *ctx) const {
         }
         ctx->SetDebugPos(pos);
         AddressInfo *tmpPtrInfo = ctx->AllocaInst(baseExprType, "array_tmp");
-        ctx->StoreInst(val, tmpPtrInfo, baseExprType, baseExprType->IsUniformType());
+        ctx->StoreInst(val, tmpPtrInfo, baseExprType);
 
         // Get a pointer type to the underlying elements
         const SequentialType *st = CastType<SequentialType>(baseExprType);
@@ -5059,7 +5058,7 @@ llvm::Value *VectorMemberExpr::GetValue(FunctionEmitContext *ctx) const {
                 AssertPos(pos, m->errorCount > 0);
                 return NULL;
             }
-            ctx->StoreInst(exprValue, basePtrInfo, expr->GetType(), expr->GetType()->IsUniformType());
+            ctx->StoreInst(exprValue, basePtrInfo, expr->GetType());
         }
 
         // Allocate temporary memory to store the result
@@ -5093,7 +5092,6 @@ llvm::Value *VectorMemberExpr::GetValue(FunctionEmitContext *ctx) const {
             // TODO: when we have swizzle on bool type, it breaks here on StoreInst.
             // The condition in StoreInst checking that SwitchBoolSize is needed doesn't detect it.
             ctx->StoreInst(elementValue, new AddressInfo(ptmp, exprVectorType->GetElementType()), elementPtrType);
-
         }
 
         return ctx->LoadInst(resultPtrInfo->getPointer(), memberType, llvm::Twine(basePtr->getName()) + "_swizzle");
@@ -5215,7 +5213,7 @@ llvm::Value *MemberExpr::GetValue(FunctionEmitContext *ctx) const {
         ctx->SetDebugPos(pos);
         const Type *exprType = expr->GetType();
         AddressInfo *ptrInfo = ctx->AllocaInst(exprType, "struct_tmp");
-        ctx->StoreInst(val, ptrInfo, exprType, exprType->IsUniformType());
+        ctx->StoreInst(val, ptrInfo, exprType);
 
         int elementNumber = getElementNumber();
         if (elementNumber == -1)
@@ -7268,7 +7266,7 @@ llvm::Value *ReferenceExpr::GetValue(FunctionEmitContext *ctx) const {
     }
 
     AddressInfo *ptrInfo = ctx->AllocaInst(type);
-    ctx->StoreInst(value, ptrInfo, type, expr->GetType()->IsUniformType());
+    ctx->StoreInst(value, ptrInfo, type);
     return ptrInfo->getPointer();
 }
 
diff --git a/src/func.cpp b/src/func.cpp
index e23ed18fb..9036c002d 100644
--- a/src/func.cpp
+++ b/src/func.cpp
@@ -265,7 +265,7 @@ static void lCopyInTaskParameter(int i, AddressInfo *structArgPtrInfo, const std
     // and copy the value from the struct and into the local alloca'ed
     // memory
     llvm::Value *ptrval = ctx->LoadInst(ptr, sym->type, sym->name.c_str());
-    ctx->StoreInst(ptrval, sym->storageInfo, sym->type, sym->type->IsUniformType());
+    ctx->StoreInst(ptrval, sym->storageInfo, sym->type);
     ctx->EmitFunctionParameterDebugInfo(sym, i);
 }
 
