Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TableGen] Fix OperandMap table size #125424

Conversation

mariusz-sikora-at-amd
Copy link
Contributor

  • 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'.

- 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'.
@llvmbot
Copy link
Member

llvmbot commented Feb 2, 2025

@llvm/pr-subscribers-tablegen

Author: Mariusz Sikora (mariusz-sikora-at-amd)

Changes
  • 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'.

Full diff: https://github.com/llvm/llvm-project/pull/125424.diff

1 Files Affected:

  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+4-3)
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 97c00ad4924197..ecf30c95f4b76b 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";
 

@jurahul
Copy link
Contributor

jurahul commented Feb 2, 2025

It may be better to deal with this as a separate if in the generated code instead of adding one more row to the table.

@jurahul
Copy link
Contributor

jurahul commented Feb 2, 2025

Actually, I'd say this should be an assert? We should not be passing OPERAND_LAST into this function ever

@@ -300,8 +300,8 @@ void InstrInfoEmitter::emitOperandNameMappings(
assert(MaxOperandNo <= INT16_MAX &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS << "assert(NamesIdx < OPERAND_LAST);"; ?

@jurahul
Copy link
Contributor

jurahul commented Feb 2, 2025 via email

@mariusz-sikora-at-amd
Copy link
Contributor Author

mariusz-sikora-at-amd commented Feb 3, 2025

Yeah that will work. Though I am changing the code here in #125313 If you want I can incorporate that change in the PR (since both names are changing in that PR)

Please do. Thanks. I will close this PR.

@jurahul
Copy link
Contributor

jurahul commented Feb 3, 2025

Will do, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants