Skip to content

Conversation

@bogner
Copy link
Contributor

@bogner bogner commented Dec 18, 2025

This introduces the DXILMemIntrinsics pass and moves memset and memcpy handling from DXILLegalize to here. We need to do this so that we can handle memory intrinsics before the DXILResourceAccess pass so that we can properly deal with arrays and large structures in resources.

This introduces the DXILMemIntrinsics pass and moves memset and memcpy handling
from DXILLegalize to here. We need to do this so that we can handle memory
intrinsics before the DXILResourceAccess pass so that we can properly deal with
arrays and large structures in resources.
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2025

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

Changes

This introduces the DXILMemIntrinsics pass and moves memset and memcpy handling from DXILLegalize to here. We need to do this so that we can handle memory intrinsics before the DXILResourceAccess pass so that we can properly deal with arrays and large structures in resources.


Patch is 22.79 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/172921.diff

10 Files Affected:

  • (modified) llvm/lib/Target/DirectX/CMakeLists.txt (+2-1)
  • (modified) llvm/lib/Target/DirectX/DXILLegalizePass.cpp (+2-166)
  • (added) llvm/lib/Target/DirectX/DXILMemIntrinsics.cpp (+188)
  • (added) llvm/lib/Target/DirectX/DXILMemIntrinsics.h (+25)
  • (modified) llvm/lib/Target/DirectX/DirectX.h (+6)
  • (modified) llvm/lib/Target/DirectX/DirectXPassRegistry.def (+1)
  • (modified) llvm/lib/Target/DirectX/DirectXTargetMachine.cpp (+3)
  • (renamed) llvm/test/CodeGen/DirectX/MemIntrinsics/memcpy.ll (+1-1)
  • (renamed) llvm/test/CodeGen/DirectX/MemIntrinsics/memset.ll (+5-4)
  • (modified) llvm/test/CodeGen/DirectX/llc-pipeline.ll (+1)
diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt
index 6c079517e22d6..9091cc0b3f6a5 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_target(DirectXCodeGen
   DXILForwardHandleAccesses.cpp
   DXILFlattenArrays.cpp
   DXILIntrinsicExpansion.cpp
+  DXILMemIntrinsics.cpp
   DXILOpBuilder.cpp
   DXILOpLowering.cpp
   DXILPostOptimizationValidation.cpp
@@ -37,7 +38,7 @@ add_llvm_target(DirectXCodeGen
   DXILTranslateMetadata.cpp
   DXILRootSignature.cpp
   DXILLegalizePass.cpp
-  
+
   LINK_COMPONENTS
   Analysis
   AsmPrinter
diff --git a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
index 3427968d199f6..71e52d608c8bb 100644
--- a/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
+++ b/llvm/lib/Target/DirectX/DXILLegalizePass.cpp
@@ -269,8 +269,8 @@ static bool upcastI8AllocasAndUses(Instruction &I,
       if (CastInst *Cast = dyn_cast<CastInst>(LU))
         Ty = Cast->getType();
       else if (CallInst *CI = dyn_cast<CallInst>(LU)) {
-        if (CI->getIntrinsicID() == Intrinsic::memset)
-          Ty = Type::getInt32Ty(CI->getContext());
+        assert(CI->getIntrinsicID() != Intrinsic::memset &&
+               "memset should have been eliminated in an earlier pass");
       }
 
       if (!Ty)
@@ -346,168 +346,6 @@ downcastI64toI32InsertExtractElements(Instruction &I,
   return false;
 }
 
-static void emitMemcpyExpansion(IRBuilder<> &Builder, Value *Dst, Value *Src,
-                                ConstantInt *Length) {
-
-  uint64_t ByteLength = Length->getZExtValue();
-  // If length to copy is zero, no memcpy is needed.
-  if (ByteLength == 0)
-    return;
-
-  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 *DstArrTy = GetArrTyFromVal(Dst);
-  assert(DstArrTy && "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");
-
-  Type *DstElemTy = DstArrTy->getElementType();
-  uint64_t DstElemByteSize = DL.getTypeStoreSize(DstElemTy);
-  assert(DstElemByteSize > 0 && "Dst element type store size must be set");
-  Type *SrcElemTy = SrcArrTy->getElementType();
-  [[maybe_unused]] uint64_t SrcElemByteSize = DL.getTypeStoreSize(SrcElemTy);
-  assert(SrcElemByteSize > 0 && "Src element type store size must be set");
-
-  // This assumption simplifies implementation and covers currently-known
-  // use-cases for DXIL. It may be relaxed in the future if required.
-  assert(DstElemTy == SrcElemTy &&
-         "The element types of Src and Dst arrays must match");
-
-  [[maybe_unused]] uint64_t DstArrNumElems = DstArrTy->getArrayNumElements();
-  assert(DstElemByteSize * DstArrNumElems >= ByteLength &&
-         "Dst array size must be at least as large as the memcpy length");
-  [[maybe_unused]] uint64_t SrcArrNumElems = SrcArrTy->getArrayNumElements();
-  assert(SrcElemByteSize * SrcArrNumElems >= ByteLength &&
-         "Src array size must be at least as large as the memcpy length");
-
-  uint64_t NumElemsToCopy = ByteLength / DstElemByteSize;
-  assert(ByteLength % DstElemByteSize == 0 &&
-         "memcpy length must be divisible by array element type");
-  for (uint64_t I = 0; I < NumElemsToCopy; ++I) {
-    SmallVector<Value *, 2> Indices = {Builder.getInt32(0),
-                                       Builder.getInt32(I)};
-    Value *SrcPtr = Builder.CreateInBoundsGEP(SrcArrTy, Src, Indices, "gep");
-    Value *SrcVal = Builder.CreateLoad(SrcElemTy, SrcPtr);
-    Value *DstPtr = Builder.CreateInBoundsGEP(DstArrTy, Dst, Indices, "gep");
-    Builder.CreateStore(SrcVal, DstPtr);
-  }
-}
-
-static void emitMemsetExpansion(IRBuilder<> &Builder, Value *Dst, Value *Val,
-                                ConstantInt *SizeCI,
-                                DenseMap<Value *, Value *> &ReplacedValues) {
-  [[maybe_unused]] const 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) {
-    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);
-    }
-  }
-
-  for (uint64_t I = 0; I < Size; ++I) {
-    Value *Zero = Builder.getInt32(0);
-    Value *Offset = Builder.getInt32(I);
-    Value *Ptr = Builder.CreateGEP(ArrTy, Dst, {Zero, Offset}, "gep");
-    Builder.CreateStore(TypedVal, Ptr);
-  }
-}
-
-// Expands the instruction `I` into corresponding loads and stores if it is a
-// memcpy call. In that case, the call instruction is added to the `ToRemove`
-// vector. `ReplacedValues` is unused.
-static bool legalizeMemCpy(Instruction &I,
-                           SmallVectorImpl<Instruction *> &ToRemove,
-                           DenseMap<Value *, Value *> &ReplacedValues) {
-
-  CallInst *CI = dyn_cast<CallInst>(&I);
-  if (!CI)
-    return false;
-
-  Intrinsic::ID ID = CI->getIntrinsicID();
-  if (ID != Intrinsic::memcpy)
-    return false;
-
-  IRBuilder<> Builder(&I);
-  Value *Dst = CI->getArgOperand(0);
-  Value *Src = CI->getArgOperand(1);
-  ConstantInt *Length = dyn_cast<ConstantInt>(CI->getArgOperand(2));
-  assert(Length && "Expected Length to be a ConstantInt");
-  [[maybe_unused]] ConstantInt *IsVolatile =
-      dyn_cast<ConstantInt>(CI->getArgOperand(3));
-  assert(IsVolatile && "Expected IsVolatile to be a ConstantInt");
-  assert(IsVolatile->getZExtValue() == 0 && "Expected IsVolatile to be false");
-  emitMemcpyExpansion(Builder, Dst, Src, Length);
-  ToRemove.push_back(CI);
-  return true;
-}
-
-static bool legalizeMemSet(Instruction &I,
-                           SmallVectorImpl<Instruction *> &ToRemove,
-                           DenseMap<Value *, Value *> &ReplacedValues) {
-
-  CallInst *CI = dyn_cast<CallInst>(&I);
-  if (!CI)
-    return false;
-
-  Intrinsic::ID ID = CI->getIntrinsicID();
-  if (ID != Intrinsic::memset)
-    return false;
-
-  IRBuilder<> Builder(&I);
-  Value *Dst = CI->getArgOperand(0);
-  Value *Val = CI->getArgOperand(1);
-  ConstantInt *Size = dyn_cast<ConstantInt>(CI->getArgOperand(2));
-  assert(Size && "Expected Size to be a ConstantInt");
-  emitMemsetExpansion(Builder, Dst, Val, Size, ReplacedValues);
-  ToRemove.push_back(CI);
-  return true;
-}
-
 static bool updateFnegToFsub(Instruction &I,
                              SmallVectorImpl<Instruction *> &ToRemove,
                              DenseMap<Value *, Value *> &) {
@@ -660,8 +498,6 @@ class DXILLegalizationPipeline {
     LegalizationPipeline[Stage1].push_back(fixI8UseChain);
     LegalizationPipeline[Stage1].push_back(legalizeGetHighLowi64Bytes);
     LegalizationPipeline[Stage1].push_back(legalizeFreeze);
-    LegalizationPipeline[Stage1].push_back(legalizeMemCpy);
-    LegalizationPipeline[Stage1].push_back(legalizeMemSet);
     LegalizationPipeline[Stage1].push_back(updateFnegToFsub);
     // Note: legalizeGetHighLowi64Bytes and
     // downcastI64toI32InsertExtractElements both modify extractelement, so they
diff --git a/llvm/lib/Target/DirectX/DXILMemIntrinsics.cpp b/llvm/lib/Target/DirectX/DXILMemIntrinsics.cpp
new file mode 100644
index 0000000000000..e5bf0f1af04e7
--- /dev/null
+++ b/llvm/lib/Target/DirectX/DXILMemIntrinsics.cpp
@@ -0,0 +1,188 @@
+//===- DXILMemIntrinsics.cpp - Eliminate Memory Intrinsics ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "DXILMemIntrinsics.h"
+#include "DirectX.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Module.h"
+
+#define DEBUG_TYPE "dxil-mem-intrinsics"
+
+using namespace llvm;
+
+void expandMemSet(MemSetInst *MemSet) {
+  IRBuilder<> Builder(MemSet);
+  Value *Dst = MemSet->getDest();
+  Value *Val = MemSet->getValue();
+  ConstantInt *LengthCI = dyn_cast<ConstantInt>(MemSet->getLength());
+  assert(LengthCI && "Expected length to be a ConstantInt");
+
+  [[maybe_unused]] const DataLayout &DL =
+      Builder.GetInsertBlock()->getModule()->getDataLayout();
+  [[maybe_unused]] uint64_t OrigLength = LengthCI->getZExtValue();
+
+  AllocaInst *Alloca = dyn_cast<AllocaInst>(Dst);
+
+  assert(Alloca && "Expected memset on an Alloca");
+  assert(OrigLength == 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(OrigLength == ElemSize * Size && "Size in bytes must match");
+
+  Value *TypedVal = Val;
+
+  if (Val->getType() != ElemTy)
+    TypedVal = Builder.CreateIntCast(Val, ElemTy, false);
+
+  for (uint64_t I = 0; I < Size; ++I) {
+    Value *Zero = Builder.getInt32(0);
+    Value *Offset = Builder.getInt32(I);
+    Value *Ptr = Builder.CreateGEP(ArrTy, Dst, {Zero, Offset}, "gep");
+    Builder.CreateStore(TypedVal, Ptr);
+  }
+
+  MemSet->eraseFromParent();
+}
+
+void expandMemCpy(MemCpyInst *MemCpy) {
+  IRBuilder<> Builder(MemCpy);
+  Value *Dst = MemCpy->getDest();
+  Value *Src = MemCpy->getSource();
+  ConstantInt *LengthCI = dyn_cast<ConstantInt>(MemCpy->getLength());
+  assert(LengthCI && "Expected Length to be a ConstantInt");
+  assert(!MemCpy->isVolatile() && "Handling for volatile not implemented");
+
+  uint64_t ByteLength = LengthCI->getZExtValue();
+  // If length to copy is zero, no memcpy is needed.
+  if (ByteLength == 0)
+    return;
+
+  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 *DstArrTy = GetArrTyFromVal(Dst);
+  assert(DstArrTy && "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");
+
+  Type *DstElemTy = DstArrTy->getElementType();
+  uint64_t DstElemByteSize = DL.getTypeStoreSize(DstElemTy);
+  assert(DstElemByteSize > 0 && "Dst element type store size must be set");
+  Type *SrcElemTy = SrcArrTy->getElementType();
+  [[maybe_unused]] uint64_t SrcElemByteSize = DL.getTypeStoreSize(SrcElemTy);
+  assert(SrcElemByteSize > 0 && "Src element type store size must be set");
+
+  // This assumption simplifies implementation and covers currently-known
+  // use-cases for DXIL. It may be relaxed in the future if required.
+  assert(DstElemTy == SrcElemTy &&
+         "The element types of Src and Dst arrays must match");
+
+  [[maybe_unused]] uint64_t DstArrNumElems = DstArrTy->getArrayNumElements();
+  assert(DstElemByteSize * DstArrNumElems >= ByteLength &&
+         "Dst array size must be at least as large as the memcpy length");
+  [[maybe_unused]] uint64_t SrcArrNumElems = SrcArrTy->getArrayNumElements();
+  assert(SrcElemByteSize * SrcArrNumElems >= ByteLength &&
+         "Src array size must be at least as large as the memcpy length");
+
+  uint64_t NumElemsToCopy = ByteLength / DstElemByteSize;
+  assert(ByteLength % DstElemByteSize == 0 &&
+         "memcpy length must be divisible by array element type");
+  for (uint64_t I = 0; I < NumElemsToCopy; ++I) {
+    SmallVector<Value *, 2> Indices = {Builder.getInt32(0),
+                                       Builder.getInt32(I)};
+    Value *SrcPtr = Builder.CreateInBoundsGEP(SrcArrTy, Src, Indices, "gep");
+    Value *SrcVal = Builder.CreateLoad(SrcElemTy, SrcPtr);
+    Value *DstPtr = Builder.CreateInBoundsGEP(DstArrTy, Dst, Indices, "gep");
+    Builder.CreateStore(SrcVal, DstPtr);
+  }
+
+  MemCpy->eraseFromParent();
+}
+
+void expandMemMove(MemMoveInst *MemMove) {
+  report_fatal_error("memmove expansion is not implemented yet.");
+}
+
+static bool eliminateMemIntrinsics(Module &M) {
+  bool HadMemIntrinsicUses = false;
+  for (auto &F : make_early_inc_range(M.functions())) {
+    Intrinsic::ID IID = F.getIntrinsicID();
+    switch (IID) {
+    case Intrinsic::memcpy:
+    case Intrinsic::memcpy_inline:
+    case Intrinsic::memmove:
+    case Intrinsic::memset:
+    case Intrinsic::memset_inline:
+      break;
+    default:
+      continue;
+    }
+    for (User *U : make_early_inc_range(F.users())) {
+      HadMemIntrinsicUses = true;
+      if (auto *MemSet = dyn_cast<MemSetInst>(U))
+        expandMemSet(MemSet);
+      else if (auto *MemCpy = dyn_cast<MemCpyInst>(U))
+        expandMemCpy(MemCpy);
+      else if (auto *MemMove = dyn_cast<MemMoveInst>(U))
+        expandMemMove(MemMove);
+      else
+        llvm_unreachable("Unhandled memory intrinsic");
+    }
+    assert(F.user_empty() && "Mem intrinsic not eliminated?");
+    F.eraseFromParent();
+  }
+  return HadMemIntrinsicUses;
+}
+
+PreservedAnalyses DXILMemIntrinsics::run(Module & M, ModuleAnalysisManager &) {
+  if (eliminateMemIntrinsics(M))
+      return PreservedAnalyses::none();
+  return PreservedAnalyses::all();
+}
+
+class DXILMemIntrinsicsLegacy : public ModulePass {
+public:
+  bool runOnModule(Module &M) override { return eliminateMemIntrinsics(M); }
+  DXILMemIntrinsicsLegacy() : ModulePass(ID) {}
+
+  static char ID; // Pass identification.
+};
+char DXILMemIntrinsicsLegacy::ID = 0;
+
+INITIALIZE_PASS_BEGIN(DXILMemIntrinsicsLegacy, DEBUG_TYPE,
+                      "DXIL Memory Intrinsic Elimination", false, false)
+INITIALIZE_PASS_END(DXILMemIntrinsicsLegacy, DEBUG_TYPE,
+                    "DXIL Memory Intrinsic Elimination", false, false)
+
+ModulePass *llvm::createDXILMemIntrinsicsLegacyPass() {
+  return new DXILMemIntrinsicsLegacy();
+}
diff --git a/llvm/lib/Target/DirectX/DXILMemIntrinsics.h b/llvm/lib/Target/DirectX/DXILMemIntrinsics.h
new file mode 100644
index 0000000000000..46f105026d909
--- /dev/null
+++ b/llvm/lib/Target/DirectX/DXILMemIntrinsics.h
@@ -0,0 +1,25 @@
+//===- DXILMemIntrinsics.h -  Eliminate Memory Intrinsics -----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TARGET_DIRECTX_DXILMEMINTRINSICS_H
+#define LLVM_TARGET_DIRECTX_DXILMEMINTRINSICS_H
+
+#include "llvm/IR/PassManager.h"
+#include "llvm/Pass.h"
+
+namespace llvm {
+
+/// Transform all llvm memory intrinsics to explicit loads and stores.
+class DXILMemIntrinsics : public PassInfoMixin<DXILMemIntrinsics> {
+public:
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
+};
+
+} // namespace llvm
+
+#endif // LLVM_TARGET_DIRECTX_DXILMEMINTRINSICS_H
diff --git a/llvm/lib/Target/DirectX/DirectX.h b/llvm/lib/Target/DirectX/DirectX.h
index e31c2ffa4f761..dfeb1ab12665d 100644
--- a/llvm/lib/Target/DirectX/DirectX.h
+++ b/llvm/lib/Target/DirectX/DirectX.h
@@ -66,6 +66,12 @@ void initializeDXILLegalizeLegacyPass(PassRegistry &);
 /// elements
 FunctionPass *createDXILLegalizeLegacyPass();
 
+/// Initializer for DXIL Mem Intrinsics.
+void initializeDXILMemIntrinsicsLegacyPass(PassRegistry &);
+
+/// Pass to transform all llvm memory intrinsics to explicit loads and stores.
+ModulePass *createDXILMemIntrinsicsLegacyPass();
+
 /// Initializer for DXILOpLowering
 void initializeDXILOpLoweringLegacyPass(PassRegistry &);
 
diff --git a/llvm/lib/Target/DirectX/DirectXPassRegistry.def b/llvm/lib/Target/DirectX/DirectXPassRegistry.def
index b4b48a166800e..f594546f98901 100644
--- a/llvm/lib/Target/DirectX/DirectXPassRegistry.def
+++ b/llvm/lib/Target/DirectX/DirectXPassRegistry.def
@@ -28,6 +28,7 @@ MODULE_PASS("dxil-finalize-linkage", DXILFinalizeLinkage())
 MODULE_PASS("dxil-data-scalarization", DXILDataScalarization())
 MODULE_PASS("dxil-flatten-arrays", DXILFlattenArrays())
 MODULE_PASS("dxil-intrinsic-expansion", DXILIntrinsicExpansion())
+MODULE_PASS("dxil-mem-intrinsics", DXILMemIntrinsics())
 MODULE_PASS("dxil-op-lower", DXILOpLowering())
 MODULE_PASS("dxil-pretty-printer", DXILPrettyPrinterPass(dbgs()))
 MODULE_PASS("dxil-translate-metadata", DXILTranslateMetadata())
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index fae9cbf9832fe..c0a92f92e1fba 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -19,6 +19,7 @@
 #include "DXILForwardHandleAccesses.h"
 #include "DXILIntrinsicExpansion.h"
 #include "DXILLegalizePass.h"
+#include "DXILMemIntrinsics.h"
 #include "DXILOpLowering.h"
 #include "DXILPostOptimizationValidation.h"
 #include "DXILPrettyPrinter.h"
@@ -58,6 +59,7 @@ LLVMInitializeDirectXTarget() {
   RegisterTargetMachine<DirectXTargetMachine> X(getTheDirectXTarget());
   auto *PR = PassRegistry::getPassRegistry();
   initializeDXILIntrinsicExpansionLegacyPass(*PR);
+  initializeDXILMemIntrinsicsLegacyPass(*PR);
   initializeDXILDataScalarizationLegacyPass(*PR);
   initializeDXILFlattenArraysLegacyPass(*PR);
   initializeScalarizerLegacyPassPass(*PR);
@@ -110,6 +112,7 @@ class DirectXPassConfig : public TargetPassConfig {
   void addCodeGenPrepare() override {
     addPass(createDXILFinalizeLinkageLegacyPass());
     addPass(createGlobalDCEPass());
+    addPass(createDXILMemIntrinsicsLegacyPass());
     addPass(createDXILCBufferAccessLegacyPass());
     addPass(createDXILResourceAccessLegacyPass());
     addPass(createDXILIntrinsicExpansionLegacyPass());
diff --git a/llvm/test/CodeGen/DirectX/legalize-memcpy.ll b/llvm/test/CodeGen/DirectX/MemIntrinsics/memcpy.ll
sim...
[truncated]

@github-actions
Copy link

github-actions bot commented Dec 18, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@bogner bogner merged commit b324c9f into llvm:main Dec 19, 2025
11 checks passed
@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 19, 2025

LLVM Buildbot has detected a new failure on builder clang-m68k-linux-cross running on suse-gary-m68k-cross while building llvm at step 5 "ninja check 1".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/20536

Here is the relevant piece of the build log for the reference
Step 5 (ninja check 1) failure: stage 1 checked (failure)
...
[77/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/GlobalCompilationDatabaseTests.cpp.o
[78/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/RIFFTests.cpp.o
[79/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/ExpectedTypeTest.cpp.o
[80/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/FeatureModulesTests.cpp.o
[81/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/ProjectAwareIndexTests.cpp.o
[82/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/BackgroundIndexTests.cpp.o
[83/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/LSPBinderTests.cpp.o
[84/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/FeatureModulesRegistryTests.cpp.o
[85/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/ClangdTests.cpp.o
[86/1249] Building CXX object tools/clang/tools/extra/include-cleaner/unittests/CMakeFiles/ClangIncludeCleanerTests.dir/FindHeadersTest.cpp.o
FAILED: tools/clang/tools/extra/include-cleaner/unittests/CMakeFiles/ClangIncludeCleanerTests.dir/FindHeadersTest.cpp.o 
/usr/bin/c++ -DGTEST_HAS_RTTI=0 -DLLVM_BUILD_STATIC -D_DEBUG -D_GLIBCXX_ASSERTIONS -D_GLIBCXX_USE_CXX11_ABI=1 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/tools/clang/tools/extra/include-cleaner/unittests -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/include-cleaner/unittests -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/tools/clang/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/include-cleaner/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/include-cleaner/unittests/../lib -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/third-party/unittest/googletest/include -I/var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/third-party/unittest/googlemock/include -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-dangling-reference -Wno-redundant-move -Wno-pessimizing-move -Wno-array-bounds -Wno-stringop-overread -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -O3 -DNDEBUG -std=c++17  -Wno-variadic-macros -fno-exceptions -funwind-tables -fno-rtti -UNDEBUG -Wno-suggest-override -MD -MT tools/clang/tools/extra/include-cleaner/unittests/CMakeFiles/ClangIncludeCleanerTests.dir/FindHeadersTest.cpp.o -MF tools/clang/tools/extra/include-cleaner/unittests/CMakeFiles/ClangIncludeCleanerTests.dir/FindHeadersTest.cpp.o.d -o tools/clang/tools/extra/include-cleaner/unittests/CMakeFiles/ClangIncludeCleanerTests.dir/FindHeadersTest.cpp.o -c /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
[87/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/FindTargetTests.cpp.o
[88/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/InsertionPointTests.cpp.o
[89/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/HeadersTests.cpp.o
[90/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/ModulesTests.cpp.o
[91/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/SymbolDocumentationTests.cpp.o
[92/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/HeaderSourceSwitchTests.cpp.o
[93/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/IncludeCleanerTests.cpp.o
[94/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/FindSymbolsTests.cpp.o
[95/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/SemanticSelectionTests.cpp.o
[96/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/PreambleTests.cpp.o
[97/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/IndexActionTests.cpp.o
[98/1249] Building CXX object tools/clang/tools/extra/clangd/unittests/CMakeFiles/ClangdTests.dir/SelectionTests.cpp.o
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/streambuf:43,
                 from /usr/include/c++/14/bits/streambuf_iterator.h:35,
                 from /usr/include/c++/14/iterator:66,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/include/llvm/ADT/ADL.h:13,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/include/llvm/ADT/iterator_range.h:21,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/include/llvm/ADT/StringRef.h:14,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/clangd/URI.h:12,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/clangd/Protocol.h:26,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/clangd/unittests/Annotations.h:15,
                 from /var/lib/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/clang-tools-extra/clangd/unittests/SelectionTests.cpp:8:
In static member function ‘static _Up* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(_Tp*, _Tp*, _Up*) [with _Tp = const clang::clangd::SelectionTree::Node* const; _Up = const clang::clangd::SelectionTree::Node*; bool _IsMove = false]’,
    inlined from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const clang::clangd::SelectionTree::Node* const*; _OI = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_algobase.h:521:30,
    inlined from ‘_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = const clang::clangd::SelectionTree::Node* const*; _OI = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_algobase.h:548:42,
    inlined from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const clang::clangd::SelectionTree::Node* const*; _OI = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_algobase.h:555:31,
    inlined from ‘_OI std::copy(_II, _II, _OI) [with _II = const clang::clangd::SelectionTree::Node* const*; _OI = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_algobase.h:651:7,
    inlined from ‘static _ForwardIterator std::__uninitialized_copy<true>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const clang::clangd::SelectionTree::Node* const*; _ForwardIterator = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_uninitialized.h:147:27,
    inlined from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const clang::clangd::SelectionTree::Node* const*; _ForwardIterator = const clang::clangd::SelectionTree::Node**]’ at /usr/include/c++/14/bits/stl_uninitialized.h:185:15,
    inlined from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = const clang::clangd::SelectionTree::Node* const*; _ForwardIterator = const clang::clangd::SelectionTree::Node**; _Tp = const clang::clangd::SelectionTree::Node*]’ at /usr/include/c++/14/bits/stl_uninitialized.h:373:37,
    inlined from ‘void std::vector<_Tp, _Alloc>::_M_range_insert(iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const clang::clangd::SelectionTree::Node* const*; _Tp = const clang::clangd::SelectionTree::Node*; _Alloc = std::allocator<const clang::clangd::SelectionTree::Node*>]’ at /usr/include/c++/14/bits/vector.tcc:1022:38,
    inlined from ‘std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(const_iterator, _InputIterator, _InputIterator) [with _InputIterator = const clang::clangd::SelectionTree::Node* const*; <template-parameter-2-2> = void; _Tp = const clang::clangd::SelectionTree::Node*; _Alloc = std::allocator<const clang::clangd::SelectionTree::Node*>]’ at /usr/include/c++/14/bits/stl_vector.h:1488:19,

@llvm-ci
Copy link
Collaborator

llvm-ci commented Dec 19, 2025

LLVM Buildbot has detected a new failure on builder reverse-iteration running on hexagon-build-03 while building llvm at step 6 "check_all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/110/builds/6882

Here is the relevant piece of the build log for the reference
Step 6 (check_all) failure: test (failure)
******************** TEST 'Clang :: Interpreter/dynamic-library.cpp' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
# RUN: at line 17
cat /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/dynamic-library.cpp | env LD_LIBRARY_PATH=/local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/Inputs:$LD_LIBRARY_PATH /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.obj/bin/clang-repl | /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.obj/bin/FileCheck /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/dynamic-library.cpp
# executed command: cat /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/dynamic-library.cpp
# .---command stdout------------
# | // REQUIRES: host-supports-jit, x86_64-linux
# | 
# | // To generate libdynamic-library-test.so :
# | // clang -xc++ -o libdynamic-library-test.so -fPIC -shared
# | //
# | // extern "C" {
# | //
# | // int ultimate_answer = 0;
# | // 
# | // int calculate_answer() {
# | //   ultimate_answer = 42;
# | //   return 5;
# | // }
# | //
# | // }
# | 
# | // RUN: cat %s | env LD_LIBRARY_PATH=%S/Inputs:$LD_LIBRARY_PATH clang-repl | FileCheck %s
# | 
# | extern "C" int printf(const char* format, ...);
# | 
# | extern "C" int ultimate_answer;
# | extern "C" int calculate_answer();
# | 
# | %lib libdynamic-library-test.so
# | 
# | printf("Return value: %d\n", calculate_answer());
# | // CHECK: Return value: 5
# | 
# | printf("Variable: %d\n", ultimate_answer);
# | // CHECK-NEXT: Variable: 42
# | 
# | %quit
# `-----------------------------
# executed command: env 'LD_LIBRARY_PATH=/local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/Inputs:$LD_LIBRARY_PATH' /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.obj/bin/clang-repl
# .---command stderr------------
# | /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.obj/bin/clang-repl: error while loading shared libraries: libc++.so.1: cannot open shared object file: No such file or directory
# `-----------------------------
# error: command failed with exit status: 127
# executed command: /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.obj/bin/FileCheck /local/mnt/workspace/bots/hexagon-build-03/reverse-iteration/llvm.src/clang/test/Interpreter/dynamic-library.cpp
# .---command stderr------------
# | FileCheck error: '<stdin>' is empty.
...

Type *Ty = nullptr;
if (CastInst *Cast = dyn_cast<CastInst>(LU))
Ty = Cast->getType();
else if (CallInst *CI = dyn_cast<CallInst>(LU)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we have just deleted this else if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Follow up here: #173040

bogner added a commit to bogner/llvm-project that referenced this pull request Dec 19, 2025
bogner added a commit that referenced this pull request Dec 19, 2025
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Dec 19, 2025
…2921)

This introduces the DXILMemIntrinsics pass and moves memset and memcpy
handling from DXILLegalize to here. We need to do this so that we can
handle memory intrinsics before the DXILResourceAccess pass so that we
can properly deal with arrays and large structures in resources.
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DirectX] Split memory intrinsic expansion into its own pass before lowering resource intrinsics

6 participants