[llvm] [AMDGPU] Add basic verification for source modifiers in VOP3/VOP3P instructions (PR #186733)

Lleu Yang via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 15 21:23:46 PDT 2026


https://github.com/megakite created https://github.com/llvm/llvm-project/pull/186733

Source modifiers in VOP3/VOP3P instructions should always be immediates.
This commit made machine verifier reject non-immediate source modifiers.

Closes #182243

>From 2a52f1b3bf92df37927fd18f16e7b8c8fe37d895 Mon Sep 17 00:00:00 2001
From: Lleu Yang <hello at megakite.icu>
Date: Mon, 16 Mar 2026 12:06:36 +0800
Subject: [PATCH] [AMDGPU] Add basic verification for source modifiers in
 VOP3/VOP3P instructions

Source modifiers in VOP3/VOP3P instructions should always be immediates.
This commit made machine verifier reject non-immediate source modifiers.

Closes #182243
---
 llvm/lib/Target/AMDGPU/SIInstrInfo.cpp        | 21 +++++++++++++
 .../AMDGPU/invalid-vop3-source-modifiers.mir  | 30 +++++++++++++++++++
 2 files changed, 51 insertions(+)
 create mode 100644 llvm/test/CodeGen/AMDGPU/invalid-vop3-source-modifiers.mir

diff --git a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
index 0ab3f079c4299..e5eabdd5ee76a 100644
--- a/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
+++ b/llvm/lib/Target/AMDGPU/SIInstrInfo.cpp
@@ -5549,6 +5549,27 @@ bool SIInstrInfo::verifyInstruction(const MachineInstr &MI,
       ErrInfo = "VOP3 instruction uses literal";
       return false;
     }
+
+    // Verify VOP3/VOP3P source modifiers.
+    if (isVOP3(MI) || isVOP3P(MI)) {
+      int Src0ModIdx =
+          AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0_modifiers);
+      int Src1ModIdx =
+          AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1_modifiers);
+      int Src2ModIdx =
+          AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2_modifiers);
+
+      for (int ModIdx : {Src0ModIdx, Src1ModIdx, Src2ModIdx}) {
+        if (ModIdx == -1)
+          continue;
+
+        const MachineOperand &MO = MI.getOperand(ModIdx);
+        if (!MO.isImm()) {
+          ErrInfo = "Source modifier of VOP3/VOP3P instruction should be immediate";
+          return false;
+        }
+      }
+    }
   }
 
   // Special case for writelane - this can break the multiple constant bus rule,
diff --git a/llvm/test/CodeGen/AMDGPU/invalid-vop3-source-modifiers.mir b/llvm/test/CodeGen/AMDGPU/invalid-vop3-source-modifiers.mir
new file mode 100644
index 0000000000000..a13c7cf589ff0
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/invalid-vop3-source-modifiers.mir
@@ -0,0 +1,30 @@
+# RUN: not --crash llc -mtriple=amdgcn-- -mcpu=gfx1030 -run-pass=none %s 2>&1 | FileCheck -check-prefix=ERR %s
+
+# ERR: *** Bad machine code: Source modifier of VOP3/VOP3P instruction should be immediate ***
+# ERR: - instruction: $vgpr1 = V_CNDMASK_B32_e64 %stack.0, $vgpr0, 0, 0, $sgpr0, implicit $exec
+# ERR: *** Bad machine code: Source modifier of VOP3/VOP3P instruction should be immediate ***
+# ERR: - instruction: $vgpr1 = V_CNDMASK_B32_e64 0, 0, $vgpr0, $vgpr0, $sgpr0, implicit $exec
+# ERR: *** Bad machine code: Source modifier of VOP3/VOP3P instruction should be immediate ***
+# ERR: - instruction: $vgpr2 = V_FMA_MIX_F32 %stack.0, $vgpr0, 0, $vgpr0, 0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+# ERR: *** Bad machine code: Source modifier of VOP3/VOP3P instruction should be immediate ***
+# ERR: - instruction: $vgpr2 = V_FMA_MIX_F32 0, $vgpr0, $vgpr0, $vgpr0, 0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+# ERR: *** Bad machine code: Source modifier of VOP3/VOP3P instruction should be immediate ***
+# ERR: - instruction: $vgpr2 = V_FMA_MIX_F32 0, $vgpr0, $vgpr0, $vgpr0, %stack.0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+
+---
+name:            invalid_vop3_source_modifiers
+tracksRegLiveness: true
+stack:
+  - { id: 0, size: 8 }
+body:             |
+  bb.0:
+    liveins: $sgpr0, $sgpr1
+    $vgpr0 = V_MOV_B32_e32 0, implicit $exec
+    $vgpr1 = V_CNDMASK_B32_e64 %stack.0, $vgpr0, 0, 0, $sgpr0, implicit $exec
+    $vgpr1 = V_CNDMASK_B32_e64 0, 0, $vgpr0, $vgpr0, $sgpr0, implicit $exec
+    $vgpr2 = V_FMA_MIX_F32 %stack.0, $vgpr0, 0, $vgpr0, 0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+    $vgpr2 = V_FMA_MIX_F32 0, $vgpr0, $vgpr0, $vgpr0, 0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+    $vgpr2 = V_FMA_MIX_F32 0, $vgpr0, $vgpr0, $vgpr0, %stack.0, $vgpr0, 0, 0, 0, implicit $mode, implicit $exec
+
+    SI_RETURN
+...



More information about the llvm-commits mailing list