[llvm] [AMDGPU] Compare VOP literals by their encoded value (PR #218752)

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 12:13:04 PDT 2026


https://github.com/yxsamliu created https://github.com/llvm/llvm-project/pull/218752

VOP instructions can share one 32-bit literal between operands of different
widths. The assembler compared the full operand values, so it rejected a
32-bit value and its sign-extended 64-bit form even though both use the same
literal word.

Keep the printer's full 64-bit spelling because it describes the operand's
value and type. Instead, make literal validation compare the bits that will
actually be encoded. Preserve full values for explicit lit64 operands.



>From 3b59882e18d114fc9a064c114b93350274ca6e21 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Tue, 25 Aug 2026 11:40:00 -0400
Subject: [PATCH] [AMDGPU] Compare VOP literals by their encoded value

VOP instructions can share one 32-bit literal between operands of different
widths. The assembler compared the full operand values, so it rejected a
32-bit value and its sign-extended 64-bit form even though both use the same
literal word.

Keep the printer's full 64-bit spelling because it describes the operand's
value and type. Instead, make literal validation compare the bits that will
actually be encoded. Preserve full values for explicit lit64 operands.
---
 llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp   | 10 +++++++---
 .../MC/AMDGPU/gfx11-vop3-literal-canonicalization.s    |  5 +++++
 .../MC/AMDGPU/gfx13-vop3-literal-canonicalization.s    |  5 +++++
 3 files changed, 17 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/MC/AMDGPU/gfx11-vop3-literal-canonicalization.s
 create mode 100644 llvm/test/MC/AMDGPU/gfx13-vop3-literal-canonicalization.s

diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 9ae621fdc6b71..fbc89ff4ea91d 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -5118,7 +5118,8 @@ bool AMDGPUAsmParser::validateVOPLiteral(const MCInst &Inst,
           Desc.operands()[OpIdx].OperandType == AMDGPU::OPERAND_KIMM64 ||
           (Desc.operands()[OpIdx].OperandType == AMDGPU::OPERAND_REG_IMM_FP64 &&
            HasMandatoryLiteral);
-      unsigned OpTy = Desc.operands()[OpIdx].OperandType;
+      AMDGPU::OperandType OpTy =
+          static_cast<AMDGPU::OperandType>(Desc.operands()[OpIdx].OperandType);
       bool IsFP64 =
           (IsForcedFP64 || (AMDGPU::isSISrcFPOperand(Desc, OpIdx) &&
                             OpTy != AMDGPU::OPERAND_REG_IMM_V2INT64)) &&
@@ -5143,8 +5144,11 @@ bool AMDGPUAsmParser::validateVOPLiteral(const MCInst &Inst,
         return false;
       }
 
-      if (IsFP64 && IsValid32Op && !IsForcedFP64)
-        Value = Hi_32(Value);
+      // Compare values using the word encoded by a 32-bit literal.
+      if (IsValid32Op && !IsForcedFP64 && !IsForcedLit64) {
+        Value = static_cast<uint32_t>(
+            AMDGPU::encode32BitLiteral(Value, OpTy, IsForcedLit));
+      }
 
       IsAnotherLiteral = !LiteralValue || *LiteralValue != Value;
       LiteralValue = Value;
diff --git a/llvm/test/MC/AMDGPU/gfx11-vop3-literal-canonicalization.s b/llvm/test/MC/AMDGPU/gfx11-vop3-literal-canonicalization.s
new file mode 100644
index 0000000000000..bed79e9336fd1
--- /dev/null
+++ b/llvm/test/MC/AMDGPU/gfx11-vop3-literal-canonicalization.s
@@ -0,0 +1,5 @@
+// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 6
+// RUN: llvm-mc -triple=amdgpu11.00 -mattr=-real-true16 -show-encoding < %s | FileCheck %s
+
+v_fma_f16 v0, -17, 0xffef, v1
+// CHECK: v_fma_f16 v0, 0xffef, 0xffef, v1        ; encoding: [0x00,0x00,0x48,0xd6,0xff,0xfe,0x05,0x04,0xef,0xff,0x00,0x00]
diff --git a/llvm/test/MC/AMDGPU/gfx13-vop3-literal-canonicalization.s b/llvm/test/MC/AMDGPU/gfx13-vop3-literal-canonicalization.s
new file mode 100644
index 0000000000000..c8708c999ae5c
--- /dev/null
+++ b/llvm/test/MC/AMDGPU/gfx13-vop3-literal-canonicalization.s
@@ -0,0 +1,5 @@
+// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 6
+// RUN: llvm-mc -triple=amdgpu13.10 -show-encoding < %s | FileCheck %s
+
+v_mad_co_u64_u32 v[3:4], null, 0xb6db6db7, v1, 0xffffffffb6db6db7
+// CHECK: v_mad_co_u64_u32 v[3:4], null, 0xb6db6db7, v1, 0xffffffffb6db6db7 ; encoding: [0x03,0x7c,0xf8,0xd6,0xff,0x02,0xfe,0x03,0xb7,0x6d,0xdb,0xb6]



More information about the llvm-commits mailing list