[llvm-branch-commits] [llvm] [AMDGPU] Error out on too large lit() constants (PR #197803)
Stanislav Mekhanoshin via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri May 15 01:02:31 PDT 2026
https://github.com/rampitec updated https://github.com/llvm/llvm-project/pull/197803
>From 0c8606c341f3667f0b984e5bb634ddf96dfd9f28 Mon Sep 17 00:00:00 2001
From: Stanislav Mekhanoshin <Stanislav.Mekhanoshin at amd.com>
Date: Thu, 14 May 2026 14:07:20 -0700
Subject: [PATCH] [AMDGPU] Error out on too large lit() constants
---
.../Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp | 10 +++++++++-
llvm/test/MC/AMDGPU/literals.s | 14 +++++++-------
2 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 1c4fe2c639572..80875c5bd9739 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -2540,8 +2540,12 @@ void AMDGPUOperand::addLiteralImmOperand(MCInst &Inst, int64_t Val, bool ApplyMo
// truncated to uint32_t), if the target doesn't support 64-bit literals, or
// the lit modifier is explicitly used, we need to truncate it to the 32
// LSBs.
- if (!AsmParser->has64BitLiterals() || Lit == LitModifier::Lit)
+ if (!AsmParser->has64BitLiterals() || Lit == LitModifier::Lit) {
+ if (!isInt<32>(Val) && !isUInt<32>(Val))
+ const_cast<AMDGPUAsmParser *>(AsmParser)->Error(
+ Inst.getLoc(), "literal value out of range");
Val = Lo_32(Val);
+ }
break;
case AMDGPU::OPERAND_REG_IMM_FP64:
@@ -2563,6 +2567,10 @@ void AMDGPUOperand::addLiteralImmOperand(MCInst &Inst, int64_t Val, bool ApplyMo
// 1) explicitly forced by using lit modifier;
// 2) the value is a valid 32-bit representation (signed or unsigned),
// meanwhile not forced by lit64 modifier.
+ if (Lit == LitModifier::Lit && !isInt<32>(Val) && !isUInt<32>(Val))
+ const_cast<AMDGPUAsmParser *>(AsmParser)->Error(
+ Inst.getLoc(), "literal value out of range");
+
if (Lit == LitModifier::Lit ||
(Lit != LitModifier::Lit64 && (isInt<32>(Val) || isUInt<32>(Val))))
Val = static_cast<uint64_t>(Val) << 32;
diff --git a/llvm/test/MC/AMDGPU/literals.s b/llvm/test/MC/AMDGPU/literals.s
index a6e6dfa5d6a88..af65636e8510a 100644
--- a/llvm/test/MC/AMDGPU/literals.s
+++ b/llvm/test/MC/AMDGPU/literals.s
@@ -2008,16 +2008,16 @@ v_add_nc_u64 v[0:1], v[0:1], 0x123456789
// NOSI: :[[@LINE-6]]:1: error: instruction not supported on this GPU (tahiti): v_add_nc_u64
// NOVI: :[[@LINE-7]]:1: error: instruction not supported on this GPU (tonga): v_add_nc_u64
-// FIXME: Literal is truncated but shall be rejected
v_add_nc_u64 v[0:1], v[0:1], lit(0x123456789)
// GFX1250-ASM: v_add_nc_u64_e64 v[0:1], v[0:1], lit(0x23456789) ; encoding: [0x00,0x00,0x28,0xd5,0x00,0xff,0x01,0x02,0x89,0x67,0x45,0x23]
// GFX1250-DIS: v_add_nc_u64_e64 v[0:1], v[0:1], 0x23456789 ; encoding: [0x00,0x00,0x28,0xd5,0x00,0xff,0x01,0x02,0x89,0x67,0x45,0x23]
// NOCI: :[[@LINE-3]]:1: error: instruction not supported on this GPU (bonaire): v_add_nc_u64
// NOGFX11: :[[@LINE-4]]:1: error: instruction not supported on this GPU (gfx1100): v_add_nc_u64
// NOGFX12: :[[@LINE-5]]:1: error: instruction not supported on this GPU (gfx1200): v_add_nc_u64
-// NOGFX9: :[[@LINE-6]]:1: error: instruction not supported on this GPU (gfx900): v_add_nc_u64
-// NOSI: :[[@LINE-7]]:1: error: instruction not supported on this GPU (tahiti): v_add_nc_u64
-// NOVI: :[[@LINE-8]]:1: error: instruction not supported on this GPU (tonga): v_add_nc_u64
+// NOGFX1250: :[[@LINE-6]]:1: error: literal value out of range
+// NOGFX9: :[[@LINE-7]]:1: error: instruction not supported on this GPU (gfx900): v_add_nc_u64
+// NOSI: :[[@LINE-8]]:1: error: instruction not supported on this GPU (tahiti): v_add_nc_u64
+// NOVI: :[[@LINE-9]]:1: error: instruction not supported on this GPU (tonga): v_add_nc_u64
v_add_f64 v[0:1], v[0:1], lit(1)
// GFX11: v_add_f64 v[0:1], v[0:1], lit(0x1) ; encoding: [0x00,0x00,0x27,0xd7,0x00,0xff,0x01,0x02,0x01,0x00,0x00,0x00]
@@ -2066,11 +2066,11 @@ v_add_f64 v[0:1], v[0:1], 0x3ff000001
// NOGFX89: :[[@LINE-4]]:19: error: invalid operand for instruction
// NOSICI: :[[@LINE-5]]:19: error: invalid operand for instruction
-// FIXME: Literal is truncated but shall be rejected
v_add_f64 v[0:1], v[0:1], lit(0x3ff000001)
// GFX1250-ASM: v_add_f64_e64 v[0:1], v[0:1], lit(0xff000001) ; encoding: [0x00,0x00,0x02,0xd5,0x00,0xff,0x01,0x02,0x01,0x00,0x00,0xff]
// GFX1250-DIS: v_add_f64_e64 v[0:1], v[0:1], 0xff000001 ; encoding: [0x00,0x00,0x02,0xd5,0x00,0xff,0x01,0x02,0x01,0x00,0x00,0xff]
// NOGFX11: :[[@LINE-3]]:19: error: invalid operand for instruction
// NOGFX12: :[[@LINE-4]]:19: error: invalid operand for instruction
-// NOGFX89: :[[@LINE-5]]:19: error: invalid operand for instruction
-// NOSICI: :[[@LINE-6]]:19: error: invalid operand for instruction
+// NOGFX1250: :[[@LINE-5]]:1: error: literal value out of range
+// NOGFX89: :[[@LINE-6]]:19: error: invalid operand for instruction
+// NOSICI: :[[@LINE-7]]:19: error: invalid operand for instruction
More information about the llvm-branch-commits
mailing list