[llvm] [AMDGPU] SIOptimizeExecMaskingPreRA: Fix crash on exec copy fold into INLINEASM (PR #172481)

Frederik Harwath via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 16 05:57:39 PST 2025


https://github.com/frederik-h created https://github.com/llvm/llvm-project/pull/172481

The optimization crashed attempting to fix a fold of a COPY $exec instruction into a use in an INLINEASM instruction because it attempts to call isOperandLegal which crashes since the index is out of the MCInstrDesc's operands array bounds.

Change SIOptimizeExecMaskingPreRA to skip the optimization if the operand index is out of bounds.

>From 8e173e62f337c19f445afc17513dacd0d055250e Mon Sep 17 00:00:00 2001
From: Frederik Harwath <fharwath at amd.com>
Date: Tue, 16 Dec 2025 08:45:08 -0500
Subject: [PATCH] [AMDGPU] SIOptimizeExecMaskingPreRA: Fix crash on exec copy
 fold into inline assembly

The optimization crashed attempting to fix a fold of a COPY $exec
instruction into a use in an INLINEASM operand because it attempts to
call isOperandLegal which crashes since the index is out of the
MCInstrDesc's operands array.

Change SIOptimizeExecMaskingPreRA to skip the optimization if the
operand index is out of bounds.
---
 .../AMDGPU/SIOptimizeExecMaskingPreRA.cpp     |  1 +
 ...ize-exec-mask-pre-ra-no-fold-exec-copy.mir | 32 +++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 llvm/test/CodeGen/AMDGPU/optimize-exec-mask-pre-ra-no-fold-exec-copy.mir

diff --git a/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp b/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp
index c186f5af78b7f..115c5bfc616c9 100644
--- a/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp
+++ b/llvm/lib/Target/AMDGPU/SIOptimizeExecMaskingPreRA.cpp
@@ -473,6 +473,7 @@ bool SIOptimizeExecMaskingPreRA::run(MachineFunction &MF) {
         assert(Idx != -1);
         if (SingleExecUser->getParent() == I->getParent() &&
             !SingleExecUser->getOperand(Idx).isImplicit() &&
+            SingleExecUser->getDesc().getNumOperands() > (unsigned)Idx &&
             TII->isOperandLegal(*SingleExecUser, Idx, &I->getOperand(1))) {
           LLVM_DEBUG(dbgs() << "Redundant EXEC COPY: " << *I << '\n');
           LIS->RemoveMachineInstrFromMaps(*I);
diff --git a/llvm/test/CodeGen/AMDGPU/optimize-exec-mask-pre-ra-no-fold-exec-copy.mir b/llvm/test/CodeGen/AMDGPU/optimize-exec-mask-pre-ra-no-fold-exec-copy.mir
new file mode 100644
index 0000000000000..8cdc0833cb0be
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/optimize-exec-mask-pre-ra-no-fold-exec-copy.mir
@@ -0,0 +1,32 @@
+# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 6
+# RUN: llc -mtriple=amdgcn -mcpu=gfx900 -run-pass=si-optimize-exec-masking-pre-ra -o - %s | FileCheck %s
+
+# SIOptimizeExecMaskingPreRA should not attempt to fold the exec COPY
+# into an instruction if it cannot check legality like for the
+# INLINEASM instruction in this example.
+
+---
+name: fold-exec-copy-operand-index-out-of-bounds
+tracksRegLiveness: true
+
+body:             |
+  ; CHECK-LABEL: name: fold-exec-copy-operand-index-out-of-bounds
+  ; CHECK: bb.0:
+  ; CHECK-NEXT:   successors: %bb.1(0x80000000)
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT:   [[COPY:%[0-9]+]]:sgpr_64 = COPY $exec
+  ; CHECK-NEXT:   INLINEASM &"", 25 /* sideeffect mayload maystore attdialect */, 4653065 /* reguse:SGPR_64 */, [[COPY]]
+  ; CHECK-NEXT: {{  $}}
+  ; CHECK-NEXT: bb.1:
+  ; CHECK-NEXT:   SI_RETURN
+  bb.0:
+    successors: %bb.1(0x80000000)
+
+    %0:sgpr_64 = COPY $exec
+    INLINEASM &"", 25 /* sideeffect mayload maystore attdialect */, 4653065 /* reguse:SGPR_64 */, %0
+
+  bb.1:
+    SI_RETURN
+...
+
+



More information about the llvm-commits mailing list