Skip to content

Conversation

@jurahul
Copy link
Contributor

@jurahul jurahul commented Nov 19, 2025

Use IfGuardEmitter in CallingConvEmitter. Additionally refactor the code a bit to extract duplicated code to emit the CC function prototype into a helper function.

@github-actions
Copy link

github-actions bot commented Nov 19, 2025

🐧 Linux x64 Test Results

  • 186407 tests passed
  • 4858 tests skipped

@jurahul jurahul marked this pull request as ready for review November 19, 2025 21:10
@llvmbot
Copy link
Member

llvmbot commented Nov 19, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes

Use IfGuardEmitter in CallingConvEmitter. Additionally refactor the code a bit to extract duplicated code to emit the CC function prototype into a helper function.


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

1 Files Affected:

  • (modified) llvm/utils/TableGen/CallingConvEmitter.cpp (+21-31)
diff --git a/llvm/utils/TableGen/CallingConvEmitter.cpp b/llvm/utils/TableGen/CallingConvEmitter.cpp
index e0d933723ad66..5b3a97623ddf8 100644
--- a/llvm/utils/TableGen/CallingConvEmitter.cpp
+++ b/llvm/utils/TableGen/CallingConvEmitter.cpp
@@ -15,6 +15,7 @@
 #include "Common/CodeGenTarget.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/InterleavedRange.h"
+#include "llvm/TableGen/CodeGenHelpers.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 #include "llvm/TableGen/TGTimer.h"
@@ -54,6 +55,21 @@ class CallingConvEmitter {
 };
 } // End anonymous namespace
 
+static void emitCCHeader(raw_ostream &O, const Record *CC, StringRef Suffix) {
+  unsigned Pad = CC->getName().size();
+  if (CC->getValueAsBit("Entry")) {
+    O << "bool llvm::";
+    Pad += 12;
+  } else {
+    O << "static bool ";
+    Pad += 13;
+  }
+  O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
+    << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
+    << std::string(Pad, ' ')
+    << "ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State)" << Suffix;
+}
+
 void CallingConvEmitter::run(raw_ostream &O) {
   emitSourceFileHeader("Calling Convention Implementation Fragment", O);
 
@@ -63,35 +79,20 @@ void CallingConvEmitter::run(raw_ostream &O) {
   // Emit prototypes for all of the non-custom CC's so that they can forward ref
   // each other.
   Records.getTimer().startTimer("Emit prototypes");
-  O << "#ifndef GET_CC_REGISTER_LISTS\n\n";
+  IfGuardEmitter IfGuard(O, "!defined(GET_CC_REGISTER_LISTS)");
   for (const Record *CC : CCs) {
-    if (!CC->getValueAsBit("Custom")) {
-      unsigned Pad = CC->getName().size();
-      if (CC->getValueAsBit("Entry")) {
-        O << "bool llvm::";
-        Pad += 12;
-      } else {
-        O << "static bool ";
-        Pad += 13;
-      }
-      O << CC->getName() << "(unsigned ValNo, MVT ValVT,\n"
-        << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
-        << std::string(Pad, ' ')
-        << "ISD::ArgFlagsTy ArgFlags, Type *OrigTy, CCState &State);\n";
-    }
+    if (!CC->getValueAsBit("Custom"))
+      emitCCHeader(O, CC, ";\n");
   }
 
   // Emit each non-custom calling convention description in full.
   Records.getTimer().startTimer("Emit full descriptions");
   for (const Record *CC : CCs) {
-    if (!CC->getValueAsBit("Custom")) {
+    if (!CC->getValueAsBit("Custom"))
       emitCallingConv(CC, O);
-    }
   }
 
   emitArgRegisterLists(O);
-
-  O << "\n#endif // CC_REGISTER_LIST\n";
 }
 
 void CallingConvEmitter::emitCallingConv(const Record *CC, raw_ostream &O) {
@@ -105,18 +106,7 @@ void CallingConvEmitter::emitCallingConv(const Record *CC, raw_ostream &O) {
   AssignedRegsMap[CurrentAction] = {};
 
   O << "\n\n";
-  unsigned Pad = CurrentAction.size();
-  if (CC->getValueAsBit("Entry")) {
-    O << "bool llvm::";
-    Pad += 12;
-  } else {
-    O << "static bool ";
-    Pad += 13;
-  }
-  O << CurrentAction << "(unsigned ValNo, MVT ValVT,\n"
-    << std::string(Pad, ' ') << "MVT LocVT, CCValAssign::LocInfo LocInfo,\n"
-    << std::string(Pad, ' ') << "ISD::ArgFlagsTy ArgFlags, Type *OrigTy, "
-    << "CCState &State) {\n";
+  emitCCHeader(O, CC, " {\n");
   // Emit all of the actions, in order.
   for (unsigned I = 0, E = CCActions->size(); I != E; ++I) {
     const Record *Action = CCActions->getElementAsRecord(I);

Copy link
Contributor

@s-barannikov s-barannikov 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 force-pushed the nfc_ifguard_cc_emitter branch from 24dda0e to d33c38c Compare November 19, 2025 21:23
@github-actions
Copy link

github-actions bot commented Nov 19, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Use `IfGuardEmitter` in CallingConvEmitter. Additionally refactor
the code a bit to extract duplicated code to emit the CC function
prototype into a helper function.
@jurahul jurahul force-pushed the nfc_ifguard_cc_emitter branch from d33c38c to 6c7edd7 Compare November 19, 2025 21:29
@jurahul jurahul merged commit 308185e into llvm:main Nov 19, 2025
8 of 9 checks passed
@jurahul jurahul deleted the nfc_ifguard_cc_emitter branch November 19, 2025 22:40
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