[llvm] [DirectX] Implement `memcpy` in DXIL CBuffer Access pass (PR #144436)

Finn Plummer via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 19 16:43:27 PDT 2025


================
@@ -195,6 +195,85 @@ static void replaceLoad(LoadInst *LI, CBufferResource &CBR,
   DeadInsts.push_back(LI);
 }
 
+/// Replace memcpy from a cbuffer global with a memcpy from the cbuffer handle
+/// itself. Assumes the cbuffer global is an array, and the length of bytes to
+/// copy is divisible by array element allocation size.
+/// The memcpy source must also be a direct cbuffer global reference, not a GEP.
+static void replaceMemCpy(MemCpyInst *MCI, CBufferResource &CBR,
+                          SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
+
+  ArrayType *ArrTy = dyn_cast<ArrayType>(CBR.getValueType());
+  assert(ArrTy && "MemCpy lowering is only supported for array types");
+
+  // This assumption vastly simplifies the implementation
+  if (MCI->getSource() != CBR.Member)
+    reportFatalUsageError(
+        "Expected MemCpy source to be a cbuffer global variable");
+
+  const std::string Name = ("memcpy." + MCI->getDest()->getName() + "." +
+                            MCI->getSource()->getName())
+                               .str();
+
+  ConstantInt *Length = dyn_cast<ConstantInt>(MCI->getLength());
+  uint64_t ByteLength = Length->getZExtValue();
+
+  // If length to copy is zero, no memcpy is needed
+  if (ByteLength == 0) {
----------------
inbelic wrote:

We could maybe add a testcase to ensure it is removed as expected

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


More information about the llvm-commits mailing list