Skip to content

[NFC][TableGen] Adopt scaled indent in PredicateExpander #109801

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 25, 2024

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 24, 2024

Adopt scaled indent in PredicateExpander.
Added pre/post inc/dec operators to indent and related unit tests.
Verified by comparing *.inc files generated by LLVM build with/without the change.

@jurahul jurahul marked this pull request as ready for review September 24, 2024 19:22
@llvmbot
Copy link
Member

llvmbot commented Sep 24, 2024

@llvm/pr-subscribers-tablegen

@llvm/pr-subscribers-llvm-support

Author: Rahul Joshi (jurahul)

Changes

Adopt scaled indent in PredicateExpander.
Added pre/post inc/dec operators to indent and related unit tests.
Verified by comparing *.inc files generated by LLVM build with/without the change.


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

7 Files Affected:

  • (modified) llvm/include/llvm/Support/raw_ostream.h (+24)
  • (modified) llvm/unittests/Support/raw_ostream_test.cpp (+20)
  • (modified) llvm/utils/TableGen/Common/PredicateExpander.cpp (+40-67)
  • (modified) llvm/utils/TableGen/Common/PredicateExpander.h (+9-10)
  • (modified) llvm/utils/TableGen/InstrInfoEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/SubtargetEmitter.cpp (+9-11)
diff --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index c2f2299ed96455..d3b411590e7fd7 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -797,6 +797,30 @@ struct indent {
     assert(NumIndents >= N && "Indentation undeflow");
     return indent(NumIndents - N, Scale);
   }
+  indent &operator++() { // Prefix ++.
+    ++NumIndents;
+    return *this;
+  }
+  indent operator++(int) { // Postfix ++.
+    indent Old = *this;
+    ++NumIndents;
+    return Old;
+  }
+  indent &operator--() { // Prefix --.
+    assert(NumIndents >= 1);
+    --NumIndents;
+    return *this;
+  }
+  indent operator--(int) { // Postfix --.
+    indent Old = *this;
+    assert(NumIndents >= 1);
+    --NumIndents;
+    return Old;
+  }
+  indent &operator=(unsigned N) {
+    NumIndents = N;
+    return *this;
+  }
 };
 
 inline raw_ostream &operator<<(raw_ostream &OS, const indent &Indent) {
diff --git a/llvm/unittests/Support/raw_ostream_test.cpp b/llvm/unittests/Support/raw_ostream_test.cpp
index a35edd61685296..fbeff37d26a354 100644
--- a/llvm/unittests/Support/raw_ostream_test.cpp
+++ b/llvm/unittests/Support/raw_ostream_test.cpp
@@ -198,6 +198,26 @@ TEST(raw_ostreamTest, Indent) {
   EXPECT_EQ(Spaces(10), printToString(Scaled));
   Scaled -= 1;
   EXPECT_EQ(Spaces(8), printToString(Scaled));
+
+  // Operators.
+  Indent = 10;
+  EXPECT_EQ(Spaces(10), printToString(Indent));
+
+  indent Temp = Indent++;
+  EXPECT_EQ(Spaces(11), printToString(Indent));
+  EXPECT_EQ(Spaces(10), printToString(Temp));
+
+  Temp = Indent--;
+  EXPECT_EQ(Spaces(10), printToString(Indent));
+  EXPECT_EQ(Spaces(11), printToString(Temp));
+
+  Temp = ++Indent;
+  EXPECT_EQ(Spaces(11), printToString(Indent));
+  EXPECT_EQ(Spaces(11), printToString(Temp));
+
+  Temp = --Indent;
+  EXPECT_EQ(Spaces(10), printToString(Indent));
+  EXPECT_EQ(Spaces(10), printToString(Temp));
 }
 
 TEST(raw_ostreamTest, FormatHex) {  
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.cpp b/llvm/utils/TableGen/Common/PredicateExpander.cpp
index 2afaa8cc21aa66..314e563ba90bb4 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.cpp
+++ b/llvm/utils/TableGen/Common/PredicateExpander.cpp
@@ -153,10 +153,9 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
   }
 
   OS << '(';
-  increaseIndentLevel();
+  ++Indent;
   for (const Record *Rec : Opcodes) {
-    OS << '\n';
-    OS.indent(getIndentLevel() * 2);
+    OS << '\n' << Indent;
     if (!First)
       OS << (shouldNegate() ? "&& " : "|| ");
 
@@ -164,10 +163,8 @@ void PredicateExpander::expandCheckOpcode(raw_ostream &OS,
     First = false;
   }
 
-  OS << '\n';
-  decreaseIndentLevel();
-  OS.indent(getIndentLevel() * 2);
-  OS << ')';
+  --Indent;
+  OS << '\n' << Indent << ')';
 }
 
 void PredicateExpander::expandCheckPseudo(raw_ostream &OS,
@@ -187,22 +184,19 @@ void PredicateExpander::expandPredicateSequence(
   // Okay, there is more than one predicate in the set.
   bool First = true;
   OS << (shouldNegate() ? "!(" : "(");
-  increaseIndentLevel();
+  ++Indent;
 
   bool OldValue = shouldNegate();
   setNegatePredicate(false);
   for (const Record *Rec : Sequence) {
-    OS << '\n';
-    OS.indent(getIndentLevel() * 2);
+    OS << '\n' << Indent;
     if (!First)
       OS << (IsCheckAll ? "&& " : "|| ");
     expandPredicate(OS, Rec);
     First = false;
   }
-  OS << '\n';
-  decreaseIndentLevel();
-  OS.indent(getIndentLevel() * 2);
-  OS << ')';
+  --Indent;
+  OS << '\n' << Indent << ')';
   setNegatePredicate(OldValue);
 }
 
@@ -269,15 +263,14 @@ void PredicateExpander::expandReturnStatement(raw_ostream &OS,
 void PredicateExpander::expandOpcodeSwitchCase(raw_ostream &OS,
                                                const Record *Rec) {
   for (const Record *Opcode : Rec->getValueAsListOfDefs("Opcodes")) {
-    OS.indent(getIndentLevel() * 2);
-    OS << "case " << Opcode->getValueAsString("Namespace")
+    OS << Indent << "case " << Opcode->getValueAsString("Namespace")
        << "::" << Opcode->getName() << ":\n";
   }
 
-  increaseIndentLevel();
-  OS.indent(getIndentLevel() * 2);
+  ++Indent;
+  OS << Indent;
   expandStatement(OS, Rec->getValueAsDef("CaseStmt"));
-  decreaseIndentLevel();
+  --Indent;
 }
 
 void PredicateExpander::expandOpcodeSwitchStatement(
@@ -292,17 +285,12 @@ void PredicateExpander::expandOpcodeSwitchStatement(
   }
 
   // Expand the default case.
-  SS.indent(getIndentLevel() * 2);
-  SS << "default:\n";
+  SS << Indent << "default:\n";
 
-  increaseIndentLevel();
-  SS.indent(getIndentLevel() * 2);
+  ++Indent;
+  SS << Indent;
   expandStatement(SS, Default);
-  decreaseIndentLevel();
-  SS << '\n';
-
-  SS.indent(getIndentLevel() * 2);
-  SS << "} // end of switch-stmt";
+  SS << '\n' << Indent << "} // end of switch-stmt";
   OS << Buffer;
 }
 
@@ -436,8 +424,7 @@ void STIPredicateExpander::expandHeader(raw_ostream &OS,
   const Record *Rec = Fn.getDeclaration();
   StringRef FunctionName = Rec->getValueAsString("Name");
 
-  OS.indent(getIndentLevel() * 2);
-  OS << "bool ";
+  OS << Indent << "bool ";
   if (shouldExpandDefinition())
     OS << getClassPrefix() << "::";
   OS << FunctionName << "(";
@@ -463,26 +450,22 @@ void STIPredicateExpander::expandPrologue(raw_ostream &OS,
   bool UpdatesOpcodeMask =
       Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
 
-  increaseIndentLevel();
-  unsigned IndentLevel = getIndentLevel();
+  ++Indent;
   for (const Record *Delegate :
        Fn.getDeclaration()->getValueAsListOfDefs("Delegates")) {
-    OS.indent(IndentLevel * 2);
-    OS << "if (" << Delegate->getValueAsString("Name") << "(MI";
+    OS << Indent << "if (" << Delegate->getValueAsString("Name") << "(MI";
     if (UpdatesOpcodeMask)
       OS << ", Mask";
     if (shouldExpandForMC())
       OS << ", ProcessorID";
     OS << "))\n";
-    OS.indent((1 + IndentLevel) * 2);
-    OS << "return true;\n\n";
+    OS << Indent + 1 << "return true;\n\n";
   }
 
   if (shouldExpandForMC())
     return;
 
-  OS.indent(IndentLevel * 2);
-  OS << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
+  OS << Indent << "unsigned ProcessorID = getSchedModel().getProcessorID();\n";
 }
 
 void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
@@ -497,8 +480,7 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
         continue;
 
       if (FirstProcID) {
-        OS.indent(getIndentLevel() * 2);
-        OS << "if (ProcessorID == " << I;
+        OS << Indent << "if (ProcessorID == " << I;
       } else {
         OS << " || ProcessorID == " << I;
       }
@@ -507,21 +489,20 @@ void STIPredicateExpander::expandOpcodeGroup(raw_ostream &OS,
 
     OS << ") {\n";
 
-    increaseIndentLevel();
-    OS.indent(getIndentLevel() * 2);
+    ++Indent;
+    OS << Indent;
     if (ShouldUpdateOpcodeMask) {
       if (PI.OperandMask.isZero())
         OS << "Mask.clearAllBits();\n";
       else
         OS << "Mask = " << PI.OperandMask << ";\n";
-      OS.indent(getIndentLevel() * 2);
+      OS << Indent;
     }
     OS << "return ";
     expandPredicate(OS, PI.Predicate);
     OS << ";\n";
-    decreaseIndentLevel();
-    OS.indent(getIndentLevel() * 2);
-    OS << "}\n";
+    --Indent;
+    OS << Indent << "}\n";
   }
 }
 
@@ -530,46 +511,38 @@ void STIPredicateExpander::expandBody(raw_ostream &OS,
   bool UpdatesOpcodeMask =
       Fn.getDeclaration()->getValueAsBit("UpdatesOpcodeMask");
 
-  unsigned IndentLevel = getIndentLevel();
-  OS.indent(IndentLevel * 2);
-  OS << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
-  OS.indent(IndentLevel * 2);
-  OS << "default:\n";
-  OS.indent(IndentLevel * 2);
-  OS << "  break;";
+  OS << Indent << "switch(MI" << (isByRef() ? "." : "->") << "getOpcode()) {\n";
+  OS << Indent << "default:\n";
+  OS << Indent << "  break;";
 
   for (const OpcodeGroup &Group : Fn.getGroups()) {
     for (const Record *Opcode : Group.getOpcodes()) {
-      OS << '\n';
-      OS.indent(IndentLevel * 2);
-      OS << "case " << getTargetName() << "::" << Opcode->getName() << ":";
+      OS << '\n'
+         << Indent << "case " << getTargetName() << "::" << Opcode->getName()
+         << ":";
     }
 
     OS << '\n';
-    increaseIndentLevel();
+    ++Indent;
     expandOpcodeGroup(OS, Group, UpdatesOpcodeMask);
 
-    OS.indent(getIndentLevel() * 2);
-    OS << "break;\n";
-    decreaseIndentLevel();
+    OS << Indent << "break;\n";
+    --Indent;
   }
 
-  OS.indent(IndentLevel * 2);
-  OS << "}\n";
+  OS << Indent << "}\n";
 }
 
 void STIPredicateExpander::expandEpilogue(raw_ostream &OS,
                                           const STIPredicateFunction &Fn) {
-  OS << '\n';
-  OS.indent(getIndentLevel() * 2);
+  OS << '\n' << Indent;
   OS << "return ";
   expandPredicate(OS, Fn.getDefaultReturnPredicate());
   OS << ";\n";
 
-  decreaseIndentLevel();
-  OS.indent(getIndentLevel() * 2);
+  --Indent;
   StringRef FunctionName = Fn.getDeclaration()->getValueAsString("Name");
-  OS << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
+  OS << Indent << "} // " << ClassPrefix << "::" << FunctionName << "\n\n";
 }
 
 void STIPredicateExpander::expandSTIPredicate(raw_ostream &OS,
diff --git a/llvm/utils/TableGen/Common/PredicateExpander.h b/llvm/utils/TableGen/Common/PredicateExpander.h
index c0cd69e3cb1f85..0c3a8718a473f1 100644
--- a/llvm/utils/TableGen/Common/PredicateExpander.h
+++ b/llvm/utils/TableGen/Common/PredicateExpander.h
@@ -18,39 +18,38 @@
 
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/raw_ostream.h"
 
 namespace llvm {
 
-class raw_ostream;
 class Record;
 
 class PredicateExpander {
   bool EmitCallsByRef;
   bool NegatePredicate;
   bool ExpandForMC;
-  unsigned IndentLevel;
   StringRef TargetName;
 
   PredicateExpander(const PredicateExpander &) = delete;
   PredicateExpander &operator=(const PredicateExpander &) = delete;
 
+protected:
+  indent Indent;
+
 public:
-  PredicateExpander(StringRef Target)
+  explicit PredicateExpander(StringRef Target, unsigned Indent = 1)
       : EmitCallsByRef(true), NegatePredicate(false), ExpandForMC(false),
-        IndentLevel(1U), TargetName(Target) {}
+        TargetName(Target), Indent(Indent, 2) {}
   bool isByRef() const { return EmitCallsByRef; }
   bool shouldNegate() const { return NegatePredicate; }
   bool shouldExpandForMC() const { return ExpandForMC; }
-  unsigned getIndentLevel() const { return IndentLevel; }
+  indent &getIndent() { return Indent; }
   StringRef getTargetName() const { return TargetName; }
 
   void setByRef(bool Value) { EmitCallsByRef = Value; }
   void flipNegatePredicate() { NegatePredicate = !NegatePredicate; }
   void setNegatePredicate(bool Value) { NegatePredicate = Value; }
   void setExpandForMC(bool Value) { ExpandForMC = Value; }
-  void setIndentLevel(unsigned Level) { IndentLevel = Level; }
-  void increaseIndentLevel() { ++IndentLevel; }
-  void decreaseIndentLevel() { --IndentLevel; }
 
   void expandTrue(raw_ostream &OS);
   void expandFalse(raw_ostream &OS);
@@ -116,8 +115,8 @@ class STIPredicateExpander : public PredicateExpander {
   void expandEpilogue(raw_ostream &OS, const STIPredicateFunction &Fn);
 
 public:
-  STIPredicateExpander(StringRef Target)
-      : PredicateExpander(Target), ExpandDefinition(false) {}
+  explicit STIPredicateExpander(StringRef Target, unsigned Indent = 1)
+      : PredicateExpander(Target, Indent), ExpandDefinition(false) {}
 
   bool shouldExpandDefinition() const { return ExpandDefinition; }
   StringRef getClassPrefix() const { return ClassPrefix; }
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index cc5ef49385bb86..46605095ba85f8 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -711,7 +711,7 @@ void InstrInfoEmitter::emitMCIIHelperMethods(raw_ostream &OS,
     OS << "bool " << Rec->getValueAsString("FunctionName");
     OS << "(const MCInst &MI) {\n";
 
-    OS.indent(PE.getIndentLevel() * 2);
+    OS << PE.getIndent();
     PE.expandStatement(OS, Rec->getValueAsDef("Body"));
     OS << "\n}\n\n";
   }
@@ -914,7 +914,7 @@ void InstrInfoEmitter::emitTIIHelperMethods(raw_ostream &OS,
     }
 
     OS << " {\n";
-    OS.indent(PE.getIndentLevel() * 2);
+    OS << PE.getIndent();
     PE.expandStatement(OS, Rec->getValueAsDef("Body"));
     OS << "\n}\n\n";
   }
diff --git a/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp b/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
index c4f238b67476a7..6ca2fea41230b8 100644
--- a/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
+++ b/llvm/utils/TableGen/MacroFusionPredicatorEmitter.cpp
@@ -160,7 +160,7 @@ void MacroFusionPredicatorEmitter::emitFirstPredicate(const Record *Predicate,
     OS.indent(4) << "const MachineInstr *MI = FirstMI;\n";
     OS.indent(4) << "if (";
     PE.setNegatePredicate(true);
-    PE.setIndentLevel(3);
+    PE.getIndent() = 3;
     PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
     OS << ")\n";
     OS.indent(4) << "  return false;\n";
@@ -181,7 +181,7 @@ void MacroFusionPredicatorEmitter::emitSecondPredicate(const Record *Predicate,
     OS.indent(4) << "const MachineInstr *MI = &SecondMI;\n";
     OS.indent(4) << "if (";
     PE.setNegatePredicate(true);
-    PE.setIndentLevel(3);
+    PE.getIndent() = 3;
     PE.expandPredicate(OS, Predicate->getValueAsDef("Predicate"));
     OS << ")\n";
     OS.indent(4) << "  return false;\n";
diff --git a/llvm/utils/TableGen/SubtargetEmitter.cpp b/llvm/utils/TableGen/SubtargetEmitter.cpp
index 78d80ff82d6a4f..376475403f4e35 100644
--- a/llvm/utils/TableGen/SubtargetEmitter.cpp
+++ b/llvm/utils/TableGen/SubtargetEmitter.cpp
@@ -1576,13 +1576,13 @@ static void emitPredicates(const CodeGenSchedTransition &T,
   unsigned NumNonTruePreds =
       T.PredTerm.size() - count_if(T.PredTerm, isTruePredicate);
 
-  SS.indent(PE.getIndentLevel() * 2);
+  SS << PE.getIndent();
 
   if (NumNonTruePreds) {
     bool FirstNonTruePredicate = true;
     SS << "if (";
 
-    PE.setIndentLevel(PE.getIndentLevel() + 2);
+    PE.getIndent() += 2;
 
     for (const Record *Rec : T.PredTerm) {
       // Skip predicates that evaluate to "true".
@@ -1593,7 +1593,7 @@ static void emitPredicates(const CodeGenSchedTransition &T,
         FirstNonTruePredicate = false;
       } else {
         SS << "\n";
-        SS.indent(PE.getIndentLevel() * 2);
+        SS << PE.getIndent();
         SS << "&& ";
       }
 
@@ -1610,9 +1610,9 @@ static void emitPredicates(const CodeGenSchedTransition &T,
     }
 
     SS << ")\n"; // end of if-stmt
-    PE.decreaseIndentLevel();
-    SS.indent(PE.getIndentLevel() * 2);
-    PE.decreaseIndentLevel();
+    PE.getIndent()--;
+    SS << PE.getIndent();
+    PE.getIndent()--;
   }
 
   SS << "return " << T.ToClassIdx << "; // " << SC.Name << '\n';
@@ -1736,7 +1736,7 @@ void SubtargetEmitter::emitSchedModelHelpersImpl(
           FinalT = &T;
           continue;
         }
-        PE.setIndentLevel(3);
+        PE.getIndent() = 3;
         emitPredicates(T, SchedModels.getSchedClass(T.ToClassIdx), PE, OS);
       }
       if (FinalT)
@@ -1780,11 +1780,10 @@ void SubtargetEmitter::EmitSchedModelHelpers(const std::string &ClassName,
      << "::resolveVariantSchedClassImpl(SchedClass, MI, MCII, CPUID);\n"
      << "} // " << ClassName << "::resolveVariantSchedClass\n\n";
 
-  STIPredicateExpander PE(Target);
+  STIPredicateExpander PE(Target, /*Indent=*/0);
   PE.setClassPrefix(ClassName);
   PE.setExpandDefinition(true);
   PE.setByRef(false);
-  PE.setIndentLevel(0);
 
   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
     PE.expandSTIPredicate(OS, Fn);
@@ -1962,7 +1961,7 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
   OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
   OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
 
-  STIPredicateExpander PE(Target);
+  STIPredicateExpander PE(Target, /*Indent=*/0);
   PE.setExpandForMC(true);
   PE.setByRef(true);
   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
@@ -1976,7 +1975,6 @@ void SubtargetEmitter::EmitMCInstrAnalysisPredicateFunctions(raw_ostream &OS) {
   std::string ClassPrefix = Target + "MCInstrAnalysis";
   PE.setExpandDefinition(true);
   PE.setClassPrefix(ClassPrefix);
-  PE.setIndentLevel(0);
   for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
     PE.expandSTIPredicate(OS, Fn);
 

@jurahul jurahul force-pushed the adopt_scaled_indent branch from 4da3e21 to d8e4948 Compare September 24, 2024 23:53
@jurahul jurahul requested a review from topperc September 24, 2024 23:53
Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM

@jurahul jurahul merged commit c92137e into llvm:main Sep 25, 2024
8 checks passed
@jurahul jurahul deleted the adopt_scaled_indent branch September 25, 2024 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants