[llvm] [TableGen] Fix OperandMap table size (PR #125424)

Mariusz Sikora via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 2 09:22:31 PST 2025


https://github.com/mariusz-sikora-at-amd created https://github.com/llvm/llvm-project/pull/125424

- add missing value for OPERAND_LAST type. If function 'getNamedOperandIdx' is called with NamedIdx=OPERAND_LAST then we are accessing first element of next row in 2dim table.
- in most cases this will work unnoticed because 2dim OperandMap is sparse with most elements set to '-1'.

>From 2ed19c40a29db9b374021c69ebae82b125ecf983 Mon Sep 17 00:00:00 2001
From: Mariusz Sikora <mariusz.sikora at amd.com>
Date: Sun, 2 Feb 2025 12:03:18 -0500
Subject: [PATCH] [TableGen] Fix OperandMap table size

- add missing value for OPERAND_LAST type. If function
'getNamedOperandIdx' is called with NamedIdx=OPERAND_LAST then
we are accessing first element of next row in 2dim table.
- in most cases this will work unnoticed because 2dim OperandMap
is sparse with most elements set to '-1'.
---
 llvm/utils/TableGen/InstrInfoEmitter.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 97c00ad49241978..ecf30c95f4b76b6 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -300,8 +300,8 @@ void InstrInfoEmitter::emitOperandNameMappings(
     assert(MaxOperandNo <= INT16_MAX &&
            "Too many operands for the operand name -> index table");
     StringRef Type = MaxOperandNo <= INT8_MAX ? "int8_t" : "int16_t";
-    OS << "  static constexpr " << Type << " OperandMap[][" << NumOperandNames
-       << "] = {\n";
+    OS << "  static constexpr " << Type << " OperandMap[]["
+       << NumOperandNames + 1 << "] = {\n";
     for (const auto &Entry : OperandMap) {
       const std::map<unsigned, unsigned> &OpList = Entry.first;
 
@@ -311,7 +311,8 @@ void InstrInfoEmitter::emitOperandNameMappings(
         auto Iter = OpList.find(ID);
         OS << (Iter != OpList.end() ? (int)Iter->second : -1) << ", ";
       }
-      OS << "},\n";
+      // value for OPERAND_LAST
+      OS << "-1 },\n";
     }
     OS << "  };\n";
 



More information about the llvm-commits mailing list