[llvm] [SandboxVec][LoadStoreVec] Support constant vectors of mixed types (PR #224890)

Vasileios Porpodas via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 7 16:00:29 PDT 2026


================
@@ -74,49 +76,149 @@ LoadInst *LoadStoreVec::createVectorLoad(BndlRef<Instruction *> Loads) {
   if (!canVectorize(Loads))
     return nullptr;
 
-  Type *Ty = VecUtils::getCombinedVectorTypeFor(Loads, *DL);
+  Type *Ty = getCombinedVectorTypeFor(SmallVector<Value *>(Loads), *DL);
   Value *LdPtr = cast<LoadInst>(Loads[0])->getPointerOperand();
   // TODO: Compute alignment.
   Align LdAlign(1);
   auto LdWhereIt = std::next(VecUtils::getLowest(Loads)->getIterator());
   return LoadInst::create(Ty, LdPtr, LdAlign, LdWhereIt, *Ctx, "VecIinitL");
 }
 
-Constant *LoadStoreVec::createConstantVector(BndlRef<Constant *> Operands) {
-  SmallVector<Constant *, 8> Constants;
-  Constants.reserve(Operands.size());
-  for (Constant *COp : Operands) {
+Constant *LoadStoreVec::getEquivalentConstantWithType(Constant *C,
+                                                      Type *DestTy) {
+  assert((!DestTy->isPointerTy() ||
+           !DL->isNonIntegralAddressSpace(DestTy->getPointerAddressSpace())) && "DestTy has no fixed size");
+  Type *SrcTy = C->getType();
+  if (SrcTy == DestTy)
+    return C;
+
+  Constant *AsInt = C;
+  if (!SrcTy->isIntegerTy()) {
+    Type *IntTy = IntegerType::get(*Ctx, Utils::getNumBits(SrcTy, *DL));
+    AsInt = SrcTy->isPointerTy() ? ConstantExpr::getPtrToInt(C, IntTy)
+                                 : ConstantExpr::getBitCast(C, IntTy);
+  }
+  if (DestTy->isIntegerTy())
+    return AsInt;
+  return DestTy->isPointerTy() ? ConstantExpr::getIntToPtr(AsInt, DestTy)
+                               : ConstantExpr::getBitCast(AsInt, DestTy);
+}
+
+Constant *LoadStoreVec::createConstantVector(BndlRef<Constant *> Constants) {
+  auto *VecTy = cast<FixedVectorType>(
+      getCombinedVectorTypeFor(SmallVector<Value *>(Constants), *DL));
+  auto *LaneTy = VecTy->getScalarType();
+  unsigned LaneBits = Utils::getNumBits(LaneTy, *DL);
+  assert(all_of(Constants,
+                [&](Value *V) {
+                  return Utils::getNumBits(V, *DL) % LaneBits == 0;
+                }) &&
+         "Chain contains a constant which cannot be converted/splitted");
+
+  SmallVector<Constant *, 4> ConstantElements;
+  for (Constant *COp : Constants) {
     if (auto *AggrCOp = dyn_cast<ConstantAggregate>(COp)) {
       // If the operand is a constant aggregate, then append all its elements.
-      for (Value *Elm : AggrCOp->operands())
-        Constants.push_back(cast<Constant>(Elm));
+      for (Value *Elm : AggrCOp->operands()) {
+        auto *C = cast<Constant>(Elm);
----------------
vporpo wrote:

This looks like an unnecessary NFC. I don't thing I would prefer `auto *C = ` followed by `.push_back(C)` instead of `.push_back(cast<Constant>(Elm))`.

https://github.com/llvm/llvm-project/pull/224890


More information about the llvm-commits mailing list