[llvm] [DirectX] legalize memset (PR #136244)
Justin Bogner via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 30 10:28:46 PDT 2025
================
@@ -239,6 +247,72 @@ downcastI64toI32InsertExtractElements(Instruction &I,
}
}
+void emitMemsetExpansion(IRBuilder<> &Builder, Value *Dst, Value *Val,
+ ConstantInt *SizeCI,
+ DenseMap<Value *, Value *> &ReplacedValues) {
+ 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) {
+ // Note for i8 replacements if we know them we should use them.
+ // Further if this is a constant ReplacedValues will return null
+ // so we will stick to TypedVal = Val
+ if (ReplacedValues[Val])
+ TypedVal = ReplacedValues[Val];
+ // This case Val is a ConstantInt so the cast folds away.
+ // However if we don't do the cast the store below ends up being
+ // an i8.
+ else
+ TypedVal = Builder.CreateIntCast(Val, ElemTy, false);
----------------
bogner wrote:
I find the comment block before the `else` kind of confusing. Maybe use braces and move the comments into the two blocks?
```suggestion
if (ReplacedValues[Val]) {
// Note for i8 replacements if we know them we should use them. Further if
// this is a constant ReplacedValues will return null so we will stick to
// TypedVal = Val
TypedVal = ReplacedValues[Val];
} else {
// This case Val is a ConstantInt so the cast folds away. However if we
// don't do the cast the store below ends up being an i8.
TypedVal = Builder.CreateIntCast(Val, ElemTy, false);
}
```
https://github.com/llvm/llvm-project/pull/136244
More information about the llvm-commits
mailing list