[llvm] [SPIRV] Support selection of G_CONCAT_VECTORS (PR #201686)

Nick Sarnie via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 13:50:16 PDT 2026


https://github.com/sarnex updated https://github.com/llvm/llvm-project/pull/201686

>From 624e96912d269d9020a34f25ce79104c52184b8c Mon Sep 17 00:00:00 2001
From: Nick Sarnie <nick.sarnie at intel.com>
Date: Thu, 4 Jun 2026 12:32:03 -0700
Subject: [PATCH] [SPIRV] Support selection of G_CONCAT_VECTORS

Signed-off-by: Nick Sarnie <nick.sarnie at intel.com>

Co-Authored-By: Claude Opus 4.8 <noreply at anthropic.com>
---
 .../Target/SPIRV/SPIRVInstructionSelector.cpp | 25 +++++++++++++++++++
 llvm/test/CodeGen/SPIRV/concat-vectors.ll     | 25 +++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 llvm/test/CodeGen/SPIRV/concat-vectors.ll

diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index c3f21fe025bd5..9f8859ba6b400 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -210,6 +210,8 @@ class SPIRVInstructionSelector : public InstructionSelector {
                          MachineInstr &I) const;
   bool selectSplatVector(Register ResVReg, SPIRVTypeInst ResType,
                          MachineInstr &I) const;
+  bool selectConcatVectors(Register ResVReg, SPIRVTypeInst ResType,
+                           MachineInstr &I) const;
 
   bool selectCmp(Register ResVReg, SPIRVTypeInst ResType,
                  unsigned comparisonOpcode, MachineInstr &I) const;
@@ -1015,6 +1017,8 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
     return selectBuildVector(ResVReg, ResType, I);
   case TargetOpcode::G_SPLAT_VECTOR:
     return selectSplatVector(ResVReg, ResType, I);
+  case TargetOpcode::G_CONCAT_VECTORS:
+    return selectConcatVectors(ResVReg, ResType, I);
 
   case TargetOpcode::G_SHUFFLE_VECTOR: {
     MachineBasicBlock &BB = *I.getParent();
@@ -3967,6 +3971,27 @@ bool SPIRVInstructionSelector::selectSplatVector(Register ResVReg,
   return true;
 }
 
+bool SPIRVInstructionSelector::selectConcatVectors(Register ResVReg,
+                                                   SPIRVTypeInst ResType,
+                                                   MachineInstr &I) const {
+  // Implement G_CONCAT_VECTORS using OpCompositeConstruct, which allows vector
+  // constituents that share the result's component type to be
+  // concatenated in operand order.
+  if (ResType->getOpcode() != SPIRV::OpTypeVector)
+    report_fatal_error(
+        "Cannot select G_CONCAT_VECTORS with a non-vector result");
+
+  auto MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(),
+                     TII.get(SPIRV::OpCompositeConstruct))
+                 .addDef(ResVReg)
+                 .addUse(GR.getSPIRVTypeID(ResType));
+  for (unsigned OpIdx = I.getNumExplicitDefs();
+       OpIdx < I.getNumExplicitOperands(); ++OpIdx)
+    MIB.addUse(I.getOperand(OpIdx).getReg());
+  MIB.constrainAllUses(TII, TRI, RBI);
+  return true;
+}
+
 bool SPIRVInstructionSelector::selectDiscard(Register ResVReg,
                                              SPIRVTypeInst ResType,
                                              MachineInstr &I) const {
diff --git a/llvm/test/CodeGen/SPIRV/concat-vectors.ll b/llvm/test/CodeGen/SPIRV/concat-vectors.ll
new file mode 100644
index 0000000000000..cc7d593dbacb0
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/concat-vectors.ll
@@ -0,0 +1,25 @@
+; RUN: llc -O0 -global-isel -verify-machineinstrs -mtriple=spirv64 %s -o - | FileCheck %s
+; spirv-val errors about a 7 element vector.
+; TODO: %if spirv-tools %{ llc -O0 -mtriple=spirv64 < %s -o - -filetype=obj | spirv-val %}
+
+; G_CONCAT_VECTORS should select to OpCompositeConstruct, which
+; concatenates its vector constituents (each sharing the result component type).
+
+; Note we have to use a non-power of 2 vector length (7 here) that cannot be legalized to actually
+; generate the G_CONCAT_VECTOR Opcode. Usually it would be legalized but here it can't because it's
+; part of the ABI.
+
+; CHECK: %[[#I8:]] = OpTypeInt 8 0
+; CHECK: %[[#V4:]] = OpTypeVector %[[#I8]] 4
+; CHECK: %[[#V8:]] = OpTypeVector %[[#I8]] 8
+; CHECK: %[[#UNDEF:]] = OpUndef %[[#V4]]
+; CHECK: OpFunction
+; CHECK: %[[#A:]] = OpFunctionParameter %[[#V4]]
+; CHECK: %[[#CONCAT:]] = OpCompositeConstruct %[[#V8]] %[[#A]] %[[#UNDEF]]
+
+define spir_func <7 x i8> @extend_vec4_to_vec7(<4 x i8> %x) {
+
+entry:
+  %r = shufflevector <4 x i8> %x, <4 x i8> poison, <7 x i32> <i32 0, i32 1, i32 2, i32 3, i32 poison, i32 poison, i32 poison>
+  ret <7 x i8> %r
+}



More information about the llvm-commits mailing list