[llvm] [DirectX] legalize memset (PR #136244)

Farzon Lotfi via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 21 15:42:06 PDT 2025


================
@@ -151,6 +152,82 @@ downcastI64toI32InsertExtractElements(Instruction &I,
   }
 }
 
+void emitMemset(IRBuilder<> &Builder, Value *Dst, Value *Val,
+                ConstantInt *SizeCI) {
+  LLVMContext &Ctx = Builder.getContext();
+  [[maybe_unused]] DataLayout DL =
+      Builder.GetInsertBlock()->getModule()->getDataLayout();
+  [[maybe_unused]] uint64_t OrigSize = SizeCI->getZExtValue();
+
+  AllocaInst *Alloca = dyn_cast<AllocaInst>(Dst);
+
+  assert(Alloca && "Expected memset on an Alloca");
+  assert(OrigSize == Alloca->getAllocationSize(DL)->getFixedValue() &&
+         "Expected for memset size to match DataLayout size");
+
+  Type *AllocatedTy = Alloca->getAllocatedType();
+  ArrayType *ArrTy = dyn_cast<ArrayType>(AllocatedTy);
+  assert(ArrTy && "Expected Alloca for an Array Type");
+
+  Type *ElemTy = ArrTy->getElementType();
+  uint64_t Size = ArrTy->getArrayNumElements();
+
+  [[maybe_unused]] uint64_t ElemSize = DL.getTypeStoreSize(ElemTy);
+
+  assert(ElemSize > 0 && "Size must be set");
+  assert(OrigSize == ElemSize * Size && "Size in bytes must match");
+
+  Value *TypedVal = Val;
+  if (Val->getType() != ElemTy)
+    TypedVal = Builder.CreateIntCast(Val, ElemTy,
+                                     false); // Or use CreateBitCast for float
+
+  for (uint64_t I = 0; I < Size; ++I) {
+    Value *Offset = ConstantInt::get(Type::getInt32Ty(Ctx), I);
+    Value *Ptr = Builder.CreateGEP(ElemTy, Dst, Offset, "gep");
+    Builder.CreateStore(TypedVal, Ptr);
+  }
+}
+
+void removeLifetimesForMemset(CallInst *Memset,
----------------
farzonl wrote:

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

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


More information about the llvm-commits mailing list