[llvm] [NFC][TableGen] Use `IfGuardEmitter` in CallingConvEmitter (PR #168763)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 19 11:36:33 PST 2025


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/168763

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

>From 24dda0e0c2644457a362d16fd0d867b98421c376 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 19 Nov 2025 11:34:13 -0800
Subject: [PATCH] [NFC][TableGen] Use `IfGuardEmitter` in CallingConvEmitter

Use `IfGuardEmitter` in CallingConvEmitter. Additionally refactor
the code a bit to extract duplicated code to emit the CC function
prototype into a helper function.
---
 llvm/utils/TableGen/CallingConvEmitter.cpp | 52 +++++++++-------------
 1 file changed, 21 insertions(+), 31 deletions(-)

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);



More information about the llvm-commits mailing list