[llvm] [MIR] Support +inf and -inf literals (PR #221127)

Taufiq Rahman via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 3 20:56:26 PDT 2026


https://github.com/Inconnu08 created https://github.com/llvm/llvm-project/pull/221127

Support parsing `+inf` and `-inf` as floating-point literals in MIR.

The MIR lexer previously tokenized the leading sign separately, causing
`G_FCONSTANT float +inf` and `G_FCONSTANT float -inf` to fail with
"expected a floating point literal".

Recognize signed infinity spellings as `FloatingPointLiteral` tokens so
they can be handled by the existing floating-point constant parsing path.

Add a regression test covering both positive and negative infinity.

Fixes #221040


>From 45e10a7c775f25d66fe5adbcbb4cde2ff36cbc20 Mon Sep 17 00:00:00 2001
From: inconnu08 <taufiqrx8 at gmail.com>
Date: Thu, 3 Sep 2026 23:52:05 -0400
Subject: [PATCH] [MIR] Support +inf and -inf literals

---
 llvm/lib/CodeGen/MIRParser/MILexer.cpp        | 14 ++++++++++++++
 llvm/test/CodeGen/MIR/Generic/fp-infinity.mir | 11 +++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 llvm/test/CodeGen/MIR/Generic/fp-infinity.mir

diff --git a/llvm/lib/CodeGen/MIRParser/MILexer.cpp b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
index 67fbc00edd9db..f0dfab9f05046 100644
--- a/llvm/lib/CodeGen/MIRParser/MILexer.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
@@ -639,6 +639,18 @@ static Cursor maybeLexFloatHexBits(Cursor C, MIToken &Token) {
   return C;
 }
 
+static Cursor maybeLexInfinityLiteral(Cursor C, MIToken &Token) {
+  if ((C.peek() != '+' && C.peek() != '-') || C.peek(1) != 'i' ||
+      C.peek(2) != 'n' || C.peek(3) != 'f' ||
+      isIdentifierChar(C.peek(4)))
+    return std::nullopt;
+
+  auto Range = C;
+  C.advance(4);
+  Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
+  return C;
+}
+
 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
   if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
     return std::nullopt;
@@ -779,6 +791,8 @@ StringRef llvm::lexMIToken(StringRef Source, MIToken &Token,
     return R.remaining();
   if (Cursor R = maybeLexFloatHexBits(C, Token))
     return R.remaining();
+  if (Cursor R = maybeLexInfinityLiteral(C, Token))
+    return R.remaining();
   if (Cursor R = maybeLexIdentifier(C, Token))
     return R.remaining();
   if (Cursor R = maybeLexJumpTableIndex(C, Token))
diff --git a/llvm/test/CodeGen/MIR/Generic/fp-infinity.mir b/llvm/test/CodeGen/MIR/Generic/fp-infinity.mir
new file mode 100644
index 0000000000000..50dc5f5fe7e9e
--- /dev/null
+++ b/llvm/test/CodeGen/MIR/Generic/fp-infinity.mir
@@ -0,0 +1,11 @@
+# RUN: llc -run-pass none -o - %s | FileCheck %s
+
+---
+name: fp_infinity
+body: |
+  bb.0:
+    ; CHECK: %0:_(s32) = G_FCONSTANT float +inf
+    ; CHECK: %1:_(s32) = G_FCONSTANT float -inf
+    %0:_(s32) = G_FCONSTANT float +inf
+    %1:_(s32) = G_FCONSTANT float -inf
+...



More information about the llvm-commits mailing list