[llvm] AMDGPU/MC: Fix emitting absolute expressions (PR #136789)

Nicolai Hähnle via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 22 16:47:36 PDT 2025


https://github.com/nhaehnle created https://github.com/llvm/llvm-project/pull/136789

When absolute MCExprs appear in normal instruction operands, we have to emit them like a normal inline constant or literal. More generally, an MCExpr that happens to have an absolute evaluation should be treated exactly like an immediate operand here.

No test; I found this downstream, and I don't think it can be triggered upstream yet.

Fixes: 16238669 ("[AMDGPU][MC] Support UC_VERSION_* constants. (#95618)")

>From d7ee443e7814f0de02cae1b88d26882e3905324f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= <nicolai.haehnle at amd.com>
Date: Tue, 22 Apr 2025 16:29:31 -0700
Subject: [PATCH] AMDGPU/MC: Fix emitting absolute expressions

When absolute MCExprs appear in normal instruction operands, we have to
emit them like a normal inline constant or literal. More generally, an
MCExpr that happens to have an absolute evaluation should be treated
exactly like an immediate operand here.

No test; I found this downstream, and I don't think it can be triggered
upstream yet.

Fixes: 16238669 ("[AMDGPU][MC] Support UC_VERSION_* constants. (#95618)")
---
 .../MCTargetDesc/AMDGPUMCCodeEmitter.cpp      | 20 ++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCCodeEmitter.cpp b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCCodeEmitter.cpp
index 1e82ee36dc0eb..9cf712318bfa1 100644
--- a/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCCodeEmitter.cpp
+++ b/llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCCodeEmitter.cpp
@@ -647,13 +647,15 @@ void AMDGPUMCCodeEmitter::getMachineOpValueT16Lo128(
 void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
     const MCInst &MI, const MCOperand &MO, unsigned OpNo, APInt &Op,
     SmallVectorImpl<MCFixup> &Fixups, const MCSubtargetInfo &STI) const {
+  bool isLikeImm = false;
   int64_t Val;
-  if (MO.isExpr() && MO.getExpr()->evaluateAsAbsolute(Val)) {
-    Op = Val;
-    return;
-  }
 
-  if (MO.isExpr() && MO.getExpr()->getKind() != MCExpr::Constant) {
+  if (MO.isImm()) {
+    Val = MO.getImm();
+    isLikeImm = true;
+  } else if (MO.isExpr() && MO.getExpr()->evaluateAsAbsolute(Val)) {
+    isLikeImm = true;
+  } else if (MO.isExpr()) {
     // FIXME: If this is expression is PCRel or not should not depend on what
     // the expression looks like. Given that this is just a general expression,
     // it should probably be FK_Data_4 and whatever is producing
@@ -683,8 +685,12 @@ void AMDGPUMCCodeEmitter::getMachineOpValueCommon(
       Op = *Enc;
       return;
     }
-  } else if (MO.isImm()) {
-    Op = MO.getImm();
+
+    llvm_unreachable("Operand not supported for SISrc");
+  }
+
+  if (isLikeImm) {
+    Op = Val;
     return;
   }
 



More information about the llvm-commits mailing list