[llvm] [DirectX] legalize memset (PR #136244)
Chris B via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 21 07:32:29 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,
----------------
llvm-beanz wrote:
I'm not sure we should be deleting lifetime markers here. If the memory being set is still preserved, the lifetime intrinsics are correct.
DXIL does support lifetime markers, but we may not be correctly legalizing them.
https://github.com/llvm/llvm-project/pull/136244
More information about the llvm-commits
mailing list