Skip to content

[LLVM][TableGen] Use const Record pointers in PredicateExpander #109365

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

Merged
merged 1 commit into from
Sep 20, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 20, 2024

Use const Record pointers in PredicateExpander.

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089

@jurahul jurahul marked this pull request as ready for review September 20, 2024 04:32
@jurahul jurahul requested a review from arsenm September 20, 2024 04:32
@llvmbot
Copy link
Member

llvmbot commented Sep 20, 2024

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes

Use const Record pointers in PredicateExpander.

This is a part of effort to have better const correctness in TableGen backends:

https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089


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

2 Files Affected:

  • (modified) llvm/utils/TableGen/Common/PredicateExpander.cpp (+10-13)
  • (modified) llvm/utils/TableGen/Common/PredicateExpander.h (+7-6)
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.cpp b/llvm/utils/TableGen/Common/PredicateExpander.cpp
index d0a35ff82df649..456c4cf5d92279 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.cpp
+++ b/llvm/utils/TableGen/Common/PredicateExpander.cpp
@@ -139,7 +139,7 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS, const Record *Inst) {
 }
 
 void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
-                                          const RecVec &Opcodes) {
+                                          ArrayRef<const Record *> Opcodes) {
   assert(!Opcodes.empty() && "Expected at least one opcode to check!");
   bool First = true;
 
@@ -169,16 +169,15 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
 }
 
 void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
-                                          const RecVec &Opcodes) {
+                                          ArrayRef<const Record *> Opcodes) {
   if (shouldExpandForMC())
     expandFalse(OS);
   else
     expandCheckOpcode(OS, Opcodes);
 }
 
-void PredicateExpander::expandPredicateSequence(raw_ostream &OS,
-                                                const RecVec &Sequence,
-                                                bool IsCheckAll) {
+void PredicateExpander::expandPredicateSequence(
+    raw_ostream &OS, ArrayRef<const Record *> Sequence, bool IsCheckAll) {
   assert(!Sequence.empty() && "Found an invalid empty predicate set!");
   if (Sequence.size() == 1)
     return expandPredicate(OS, Sequence[0]);
@@ -267,8 +266,7 @@ void PredicateExpander::expandReturnStatement(raw_ostream &OS,
 
 void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
                                                const Record *Rec) {
-  const RecVec &Opcodes = Rec->getValueAsListOfDefs("Opcodes");
-  for (const Record *Opcode : Opcodes) {
+  for (const Record *Opcode : Rec->getValueAsListOfDefs("Opcodes")) {
     OS.indent(getIndentLevel() * 2);
     OS << "case " << Opcode->getValueAsString("Namespace")
        << "::" << Opcode->getName() << ":\n";
@@ -280,9 +278,8 @@ void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
   decreaseIndentLevel();
 }
 
-void PredicateExpander::expandOpcodeSwitchStatement(raw_ostream &OS,
-                                                    const RecVec &Cases,
-                                                    const Record *Default) {
+void PredicateExpander::expandOpcodeSwitchStatement(
+    raw_ostream &OS, ArrayRef<const Record *> Cases, const Record *Default) {
   std::string Buffer;
   raw_string_ostream SS(Buffer);
 
@@ -310,7 +307,7 @@ void PredicateExpander::expandOpcodeSwitchStatement(raw_ostream &OS,
 void PredicateExpander::expandStatement(raw_ostream &OS, const Record *Rec) {
   // Assume that padding has been added by the caller.
   if (Rec->isSubClassOf("MCOpcodeSwitchStatement")) {
-    expandOpcodeSwitchStatement(OS, Rec->getValueAsListOfDefs("Cases"),
+    expandOpcodeSwitchStatement(OS, Rec->getValueAsListOfConstDefs("Cases"),
                                 Rec->getValueAsDef("DefaultCase"));
     return;
   }
@@ -461,13 +458,13 @@ void STIPredicateExpander::expandHeader(raw_ostream &OS,
 
 void STIPredicateExpander::expandPrologue(raw_ostream &OS,
                                           const STIPredicateFunction &Fn) {
-  RecVec Delegates = Fn.getDeclaration()->getValueAsListOfDefs("Delegates");
   bool UpdatesOpcodeMask =
       Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
 
   increaseIndentLevel();
   unsigned IndentLevel = getIndentLevel();
-  for (const Record *Delegate : Delegates) {
+  for (const Record *Delegate :
+       Fn.getDeclaration()->getValueAsListOfDefs("Delegates")) {
     OS.indent(IndentLevel * 2);
     OS << "if (" << Delegate->getValueAsString("Name") << "(MI";
     if (UpdatesOpcodeMask)
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.h b/llvm/utils/TableGen/Common/PredicateExpander.h
index 333d1c561f9701..c0cd69e3cb1f85 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.h
+++ b/llvm/utils/TableGen/Common/PredicateExpander.h
@@ -16,8 +16,8 @@
 #ifndef LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
 #define LLVM_UTILS_TABLEGEN_COMMON_PREDICATEEXPANDER_H
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
-#include <vector>
 
 namespace llvm {
 
@@ -52,7 +52,6 @@ class PredicateExpander {
   void increaseIndentLevel() { ++IndentLevel; }
   void decreaseIndentLevel() { --IndentLevel; }
 
-  using RecVec = std::vector<Record *>;
   void expandTrue(raw_ostream &OS);
   void expandFalse(raw_ostream &OS);
   void expandCheckImmOperand(raw_ostream &OS, int OpIndex, int ImmVal,
@@ -73,9 +72,10 @@ class PredicateExpander {
   void expandCheckNumOperands(raw_ostream &OS, int NumOps);
   void expandCheckOpcode(raw_ostream &OS, const Record *Inst);
 
-  void expandCheckPseudo(raw_ostream &OS, const RecVec &Opcodes);
-  void expandCheckOpcode(raw_ostream &OS, const RecVec &Opcodes);
-  void expandPredicateSequence(raw_ostream &OS, const RecVec &Sequence,
+  void expandCheckPseudo(raw_ostream &OS, ArrayRef<const Record *> Opcodes);
+  void expandCheckOpcode(raw_ostream &OS, ArrayRef<const Record *> Opcodes);
+  void expandPredicateSequence(raw_ostream &OS,
+                               ArrayRef<const Record *> Sequence,
                                bool IsCheckAll);
   void expandTIIFunctionCall(raw_ostream &OS, StringRef MethodName);
   void expandCheckIsRegOperand(raw_ostream &OS, int OpIndex);
@@ -91,7 +91,8 @@ class PredicateExpander {
   void expandPredicate(raw_ostream &OS, const Record *Rec);
   void expandReturnStatement(raw_ostream &OS, const Record *Rec);
   void expandOpcodeSwitchCase(raw_ostream &OS, const Record *Rec);
-  void expandOpcodeSwitchStatement(raw_ostream &OS, const RecVec &Cases,
+  void expandOpcodeSwitchStatement(raw_ostream &OS,
+                                   ArrayRef<const Record *> Cases,
                                    const Record *Default);
   void expandStatement(raw_ostream &OS, const Record *Rec);
 };

@jurahul jurahul merged commit 2b01452 into llvm:main Sep 20, 2024
12 checks passed
@jurahul jurahul deleted the const_record_predicate_expander branch September 20, 2024 11:23
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