[llvm] [DirectX] Legalize memcpy (PR #139173)
Deric C. via llvm-commits
llvm-commits at lists.llvm.org
Thu May 15 13:16:01 PDT 2025
================
@@ -246,6 +246,61 @@ downcastI64toI32InsertExtractElements(Instruction &I,
}
}
+static void emitMemcpyExpansion(IRBuilder<> &Builder, Value *Dst, Value *Src,
+ ConstantInt *Length) {
+
+ uint64_t ByteLength = Length->getZExtValue();
+ if (ByteLength == 0)
+ return;
+
+ LLVMContext &Ctx = Builder.getContext();
+ const DataLayout &DL = Builder.GetInsertBlock()->getModule()->getDataLayout();
+
+ auto GetArrTyFromVal = [](Value *Val) -> ArrayType * {
+ assert(isa<AllocaInst>(Val) ||
+ isa<GlobalVariable>(Val) &&
+ "Expected Val to be an Alloca or Global Variable");
+ if (auto *Alloca = dyn_cast<AllocaInst>(Val))
+ return dyn_cast<ArrayType>(Alloca->getAllocatedType());
+ if (auto *GlobalVar = dyn_cast<GlobalVariable>(Val))
+ return dyn_cast<ArrayType>(GlobalVar->getValueType());
+ return nullptr;
+ };
+
+ ArrayType *ArrTy = GetArrTyFromVal(Dst);
+ assert(ArrTy && "Expected Dst of memcpy to be a Pointer to an Array Type");
+ if (auto *DstGlobalVar = dyn_cast<GlobalVariable>(Dst))
+ assert(!DstGlobalVar->isConstant() &&
+ "The Dst of memcpy must not be a constant Global Variable");
+
+ [[maybe_unused]] ArrayType *SrcArrTy = GetArrTyFromVal(Src);
+ assert(SrcArrTy && "Expected Src of memcpy to be a Pointer to an Array Type");
+
+ // This assumption simplifies implementation and covers currently-known
+ // use-cases for DXIL. It may be relaxed in the future if required.
+ assert(ArrTy == SrcArrTy &&
+ "Array Types of Src and Dst in memcpy must match");
+
+ Type *ElemTy = ArrTy->getElementType();
+ uint64_t ElemSize = DL.getTypeStoreSize(ElemTy);
+ assert(ElemSize > 0 && "Size must be set");
+
+ [[maybe_unused]] uint64_t Size = ArrTy->getArrayNumElements();
+ assert(ElemSize * Size >= ByteLength &&
+ "Array size must be at least as large as the memcpy length");
----------------
Icohedron wrote:
This assert is doing exactly that. It ensures the byte length of `dst` (`ElemSize * Size`) is greater or equal to the number of bytes copied via memcpy (`ByteLength`).
There is also a previous assert (`ArrTy == SrcArrTy`) that ensures the types of `src` and `dst` are identical -- they should be pointers to an array with the same element type and same number of elements.
https://github.com/llvm/llvm-project/pull/139173
More information about the llvm-commits
mailing list