[llvm] [SPIRV] Respect source alignment when lowering memory copies (PR #228859)

Alexander Shaposhnikov via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 4 01:19:44 PDT 2026


https://github.com/alexander-shaposhnikov created https://github.com/llvm/llvm-project/pull/228859

Lowering memcpy, memcpy.inline, and memmove to OpCopyMemory or OpCopyMemorySized only considers the destination alignment. A single Aligned memory operand applies to both pointers, so a destination alignment greater than the source alignment overstates the latter.

For SPIR-V 1.4 and later, emit separate destination and source alignments when they differ. For earlier versions, emit their minimum in the single permitted memory operand mask. Continue using one mask when the alignments match.

Add an alignment override to the memory operand helper so both accesses retain the existing flags and alias metadata while using the appropriate alignment.

Cover equal and unequal alignments in SPIR-V 1.3 and 1.4, and add an OpCopyMemory regression case with unequal alignments, volatility, and alias-scope metadata.

>From 3a3f10b5de8b251782dd6d2e765c7d23bd1d9430 Mon Sep 17 00:00:00 2001
From: Alexander Shaposhnikov <ashaposhnikov at google.com>
Date: Sat, 3 Oct 2026 20:13:47 -0700
Subject: [PATCH] [SPIRV] Respect source alignment when lowering memory copies

Lowering memcpy, memcpy.inline, and memmove to OpCopyMemory or
OpCopyMemorySized only considers the destination alignment. A single
Aligned memory operand applies to both pointers, so a destination
alignment greater than the source alignment overstates the latter.

For SPIR-V 1.4 and later, emit separate destination and source alignments
when they differ. For earlier versions, emit their minimum in the single
permitted memory operand mask. Continue using one mask when the
alignments match.

Add an alignment override to the memory operand helper so both accesses
retain the existing flags and alias metadata while using the appropriate
alignment.

Cover equal and unequal alignments in SPIR-V 1.3 and 1.4, and add an
OpCopyMemory regression case with unequal alignments, volatility, and
alias-scope metadata.
---
 .../Target/SPIRV/SPIRVInstructionSelector.cpp | 57 ++++++++++++++-----
 .../alias-copy-memory.ll                      |  6 +-
 .../SPIRV/llvm-intrinsics/memcpy.align.ll     | 16 ++++--
 3 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index 143e1c0e3ef2a..f132c9cdafbc9 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -1969,10 +1969,10 @@ bool SPIRVInstructionSelector::selectBitcast(Register ResVReg,
   return selectUnOp(ResVReg, ResType, I, SPIRV::OpBitcast);
 }
 
-static void addMemoryOperands(MachineMemOperand *MemOp,
-                              MachineInstrBuilder &MIB,
-                              MachineIRBuilder &MIRBuilder,
-                              SPIRVGlobalRegistry &GR) {
+static void
+addMemoryOperands(const MachineMemOperand *MemOp, MachineInstrBuilder &MIB,
+                  MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry &GR,
+                  std::optional<Align> AlignOverride = std::nullopt) {
   const SPIRVSubtarget *ST =
       static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
   uint32_t SpvMemOp = static_cast<uint32_t>(SPIRV::MemoryOperand::None);
@@ -2004,7 +2004,7 @@ static void addMemoryOperands(MachineMemOperand *MemOp,
   if (SpvMemOp != static_cast<uint32_t>(SPIRV::MemoryOperand::None)) {
     MIB.addImm(SpvMemOp);
     if (SpvMemOp & static_cast<uint32_t>(SPIRV::MemoryOperand::Aligned))
-      MIB.addImm(MemOp->getAlign().value());
+      MIB.addImm(AlignOverride.value_or(MemOp->getAlign()).value());
     if (AliasList)
       MIB.addUse(AliasList->getOperand(0).getReg());
     if (NoAliasList)
@@ -2012,6 +2012,43 @@ static void addMemoryOperands(MachineMemOperand *MemOp,
   }
 }
 
+// Appends the Memory Operands for OpCopyMemory / OpCopyMemorySized.
+// G_MEMCPY, G_MEMCPY_INLINE, and G_MEMMOVE carry two MachineMemOperands
+// (destination store followed by source load), whereas G_MEMSET and
+// G_MEMSET_INLINE carry only the destination store operand.
+//
+// In SPIR-V, a single Memory Operands mask on OpCopyMemory / OpCopyMemorySized
+// applies to both Target and Source. Starting with SPIR-V 1.4, two masks may be
+// provided (Target followed by Source); prior to SPIR-V 1.4 only one mask is
+// permitted, so its alignment must be the minimum of the destination and source
+// alignments.
+static void addCopyMemoryOperands(MachineInstr &I, MachineInstrBuilder &MIB,
+                                  SPIRVGlobalRegistry &GR) {
+  if (!I.getNumMemOperands())
+    return;
+  MachineIRBuilder MIRBuilder(I);
+  const MachineMemOperand *DstMemOp = *I.memoperands_begin();
+  const SPIRVSubtarget &ST = I.getMF()->getSubtarget<SPIRVSubtarget>();
+  if (I.getNumMemOperands() == 1 || ST.isShader()) {
+    addMemoryOperands(DstMemOp, MIB, MIRBuilder, GR);
+    return;
+  }
+
+  const MachineMemOperand *SrcMemOp = *std::next(I.memoperands_begin());
+  const Align DstAlign = DstMemOp->getAlign();
+  const Align SrcAlign = SrcMemOp->getAlign();
+  if (DstAlign != SrcAlign && ST.isAtLeastSPIRVVer(VersionTuple(1, 4))) {
+    // Keep the existing flags and metadata on both accesses, changing only
+    // the source alignment. Both masks contain Aligned, so neither is omitted.
+    addMemoryOperands(DstMemOp, MIB, MIRBuilder, GR);
+    addMemoryOperands(DstMemOp, MIB, MIRBuilder, GR, SrcAlign);
+    return;
+  }
+
+  addMemoryOperands(DstMemOp, MIB, MIRBuilder, GR,
+                    std::min(DstAlign, SrcAlign));
+}
+
 static void addMemoryOperands(uint64_t Flags, MachineInstrBuilder &MIB) {
   uint32_t SpvMemOp = static_cast<uint32_t>(SPIRV::MemoryOperand::None);
   if (Flags & MachineMemOperand::Flags::MOVolatile)
@@ -2553,10 +2590,7 @@ bool SPIRVInstructionSelector::selectCopyMemory(MachineInstr &I,
   auto MIB = BuildMI(BB, I, I.getDebugLoc(), TII.get(SPIRV::OpCopyMemory))
                  .addUse(DstReg)
                  .addUse(SrcReg);
-  if (I.getNumMemOperands()) {
-    MachineIRBuilder MIRBuilder(I);
-    addMemoryOperands(*I.memoperands_begin(), MIB, MIRBuilder, GR);
-  }
+  addCopyMemoryOperands(I, MIB, GR);
   MIB.constrainAllUses(TII, TRI, RBI);
   return true;
 }
@@ -2568,10 +2602,7 @@ bool SPIRVInstructionSelector::selectCopyMemorySized(MachineInstr &I,
                  .addUse(I.getOperand(0).getReg())
                  .addUse(SrcReg)
                  .addUse(I.getOperand(2).getReg());
-  if (I.getNumMemOperands()) {
-    MachineIRBuilder MIRBuilder(I);
-    addMemoryOperands(*I.memoperands_begin(), MIB, MIRBuilder, GR);
-  }
+  addCopyMemoryOperands(I, MIB, GR);
   MIB.constrainAllUses(TII, TRI, RBI);
   return true;
 }
diff --git a/llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_memory_access_aliasing/alias-copy-memory.ll b/llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_memory_access_aliasing/alias-copy-memory.ll
index ddd61bd9e0bbb..6ceddae59ba0d 100644
--- a/llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_memory_access_aliasing/alias-copy-memory.ll
+++ b/llvm/test/CodeGen/SPIRV/extensions/SPV_INTEL_memory_access_aliasing/alias-copy-memory.ll
@@ -9,14 +9,16 @@
 ; CHECK: %[[#]] = OpFunction
 define void @copy_aliased() {
 entry:
-; CHECK: OpCopyMemory %[[#]] %[[#]] Aligned|AliasScopeINTELMask 4 %[[#List]]
+; CHECK: OpCopyMemory %[[#]] %[[#]] Aligned|AliasScopeINTELMask 4 %[[#List]]{{$}}
   call void @llvm.memcpy.p1.p1.i64(ptr addrspace(1) align 4 @dst, ptr addrspace(1) align 4 @src, i64 20, i1 false), !alias.scope !1
+; CHECK: OpCopyMemory %[[#]] %[[#]] Volatile|Aligned|AliasScopeINTELMask 8 %[[#List]] Volatile|Aligned|AliasScopeINTELMask 4 %[[#List]]{{$}}
+  call void @llvm.memcpy.p1.p1.i64(ptr addrspace(1) align 8 @dst, ptr addrspace(1) align 4 @src, i64 20, i1 true), !alias.scope !1
   ret void
 }
 
 %struct.T = type <{ float, float, float, float, float }>
 @src = external dso_local addrspace(1) global %struct.T, align 4
- at dst = external dso_local addrspace(1) global %struct.T, align 4
+ at dst = external dso_local addrspace(1) global %struct.T, align 8
 
 declare void @llvm.memcpy.p1.p1.i64(ptr addrspace(1), ptr addrspace(1), i64, i1)
 
diff --git a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/memcpy.align.ll b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/memcpy.align.ll
index b4100019a8a0c..03f20f0176d2b 100644
--- a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/memcpy.align.ll
+++ b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/memcpy.align.ll
@@ -1,5 +1,7 @@
-; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
-; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64v1.3-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPV13
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64v1.3-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64v1.4-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK,SPV14
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64v1.4-unknown-unknown %s -o - -filetype=obj | spirv-val %}
 
 %struct.B = type { [2 x i32] }
 %struct.A = type { i64, %struct.B }
@@ -14,13 +16,14 @@ entry:
   call void @llvm.lifetime.start.p0(i64 8, ptr %0)
   %1 = bitcast ptr %b to ptr
   call void @llvm.memcpy.p0.p2.i32(ptr align 4 %1, ptr addrspace(2) align 4 @__const.foo.b, i32 8, i1 false)
-; CHECK: OpCopyMemorySized %[[#]] %[[#]] %[[#]] Aligned 4
+; CHECK: OpCopyMemorySized %[[#]] %[[#]] %[[#]] Aligned 4{{$}}
   %b1 = getelementptr inbounds %struct.A, ptr %agg.result, i32 0, i32 1
   %2 = bitcast ptr %b1 to ptr
   %3 = bitcast ptr %b to ptr
   call void @llvm.memcpy.p0.p0.i32(ptr align 8 %2, ptr align 4 %3, i32 8, i1 false)
 ; CHECK: %[[#PTR1:]] = OpInBoundsPtrAccessChain %[[#]] %[[#]] %[[#]] %[[#]]
-; CHECK: OpCopyMemorySized %[[#PTR1]] %[[#]] %[[#]] Aligned 8
+; SPV13: OpCopyMemorySized %[[#PTR1]] %[[#]] %[[#]] Aligned 4{{$}}
+; SPV14: OpCopyMemorySized %[[#PTR1]] %[[#]] %[[#]] Aligned 8 Aligned 4{{$}}
   %4 = bitcast ptr %b to ptr
   call void @llvm.lifetime.end.p0(i64 8, ptr %4)
   ret void
@@ -41,13 +44,14 @@ entry:
   call void @llvm.lifetime.start.p0(i64 16, ptr %0)
   %1 = bitcast ptr %a to ptr
   call void @llvm.memcpy.p0.p2.i32(ptr align 8 %1, ptr addrspace(2) align 8 @__const.bar.a, i32 16, i1 false)
-; CHECK: OpCopyMemorySized %[[#]] %[[#]] %[[#]] Aligned 8
+; CHECK: OpCopyMemorySized %[[#]] %[[#]] %[[#]] Aligned 8{{$}}
   %b = getelementptr inbounds %struct.A, ptr %a, i32 0, i32 1
   %2 = bitcast ptr %agg.result to ptr
   %3 = bitcast ptr %b to ptr
   call void @llvm.memcpy.p0.p0.i32(ptr align 4 %2, ptr align 8 %3, i32 8, i1 false)
 ; CHECK: %[[#PTR2:]] = OpInBoundsPtrAccessChain %[[#]] %[[#]] %[[#]] %[[#]]
-; CHECK: OpCopyMemorySized %[[#]] %[[#PTR2]] %[[#]] Aligned 4
+; SPV13: OpCopyMemorySized %[[#]] %[[#PTR2]] %[[#]] Aligned 4{{$}}
+; SPV14: OpCopyMemorySized %[[#]] %[[#PTR2]] %[[#]] Aligned 4 Aligned 8{{$}}
   %4 = bitcast ptr %a to ptr
   call void @llvm.lifetime.end.p0(i64 16, ptr %4)
   ret void



More information about the llvm-commits mailing list