Skip to content

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 4, 2025

  • Move standalone functions and variables out of anonymous namespace and make them static.
  • Restrict the scope of anonymous namespace to just class declarations.
  • Remove llvm/gi namespace enclosing the entire .cpp files and instead use using namespace.
  • Use nested namespaces when possible.

@jurahul jurahul force-pushed the nfc_fix_gisel_namespace_usage branch from 9e6488e to 21302dc Compare September 5, 2025 00:11
@jurahul jurahul marked this pull request as ready for review September 5, 2025 01:59
@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-llvm-globalisel

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes
  • Move standalone functions and variables out of anonymous namespace and make them static.
  • Restrict the scope of anonymous namespace to just class declarations.
  • Remove llvm/gi namespace enclosing the entire .cpp files and instead use using namespace.
  • Use nested namespaces when possible.

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

8 Files Affected:

  • (modified) llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp (+2-5)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.h (+2-4)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.cpp (+2-4)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp (+17-22)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp (+3-5)
  • (modified) llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp (+2-5)
  • (modified) llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp (+21-22)
  • (modified) llvm/utils/TableGen/GlobalISelEmitter.cpp (+6-6)
diff --git a/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp b/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp
index cbf70278a2813..10002a45289c0 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.cpp
@@ -12,8 +12,8 @@
 #include "CXXPredicates.h"
 #include "llvm/ADT/STLExtras.h"
 
-namespace llvm {
-namespace gi {
+using namespace llvm;
+using namespace gi;
 
 std::vector<const CXXPredicateCode *>
 CXXPredicateCode::getSorted(const CXXPredicateCodePool &Pool) {
@@ -46,6 +46,3 @@ CXXPredicateCode::CXXPredicateCode(std::string Code, unsigned ID)
 
 CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXMatchCode;
 CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXCustomActionCode;
-
-} // namespace gi
-} // namespace llvm
diff --git a/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.h b/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.h
index 0cfdf702c78b1..d4d1f39757525 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.h
+++ b/llvm/utils/TableGen/Common/GlobalISel/CXXPredicates.h
@@ -22,8 +22,7 @@
 #include <string>
 #include <vector>
 
-namespace llvm {
-namespace gi {
+namespace llvm::gi {
 
 /// Entry into the static pool of all CXX Predicate code. This contains
 /// fully expanded C++ code.
@@ -80,7 +79,6 @@ class CXXPredicateCode {
   }
 };
 
-} // namespace gi
-} // end namespace llvm
+} // namespace llvm::gi
 
 #endif // LLVM_UTILS_TABLEGEN_COMMON_GLOBALISEL_CXXPREDICATES_H
diff --git a/llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.cpp b/llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.cpp
index 37e6306050951..7049ab6f9725f 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/CombinerUtils.cpp
@@ -9,9 +9,9 @@
 #include "CombinerUtils.h"
 #include "llvm/ADT/StringSet.h"
 
-namespace llvm {
+using namespace llvm;
 
-StringRef insertStrRef(StringRef S) {
+StringRef llvm::insertStrRef(StringRef S) {
   if (S.empty())
     return {};
 
@@ -19,5 +19,3 @@ StringRef insertStrRef(StringRef S) {
   auto [It, Inserted] = Pool.insert(S);
   return It->getKey();
 }
-
-} // namespace llvm
diff --git a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
index 623fef973f989..bea782a47c990 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/GlobalISelMatchTable.cpp
@@ -20,17 +20,15 @@
 
 STATISTIC(NumPatternEmitted, "Number of patterns emitted");
 
-namespace llvm {
-namespace gi {
+using namespace llvm;
+using namespace gi;
 
-namespace {
-
-Error failUnsupported(const Twine &Reason) {
+static Error failUnsupported(const Twine &Reason) {
   return make_error<StringError>(Reason, inconvertibleErrorCode());
 }
 
 /// Get the name of the enum value used to number the predicate function.
-std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) {
+static std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) {
   if (Predicate.hasGISelPredicateCode())
     return "GICXXPred_MI_" + Predicate.getFnName();
   if (Predicate.hasGISelLeafPredicateCode())
@@ -39,18 +37,17 @@ std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) {
          Predicate.getFnName();
 }
 
-std::string getMatchOpcodeForImmPredicate(const TreePredicateFn &Predicate) {
+static std::string
+getMatchOpcodeForImmPredicate(const TreePredicateFn &Predicate) {
   return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate";
 }
 
 // GIMT_Encode2/4/8
 constexpr StringLiteral EncodeMacroName = "GIMT_Encode";
 
-} // namespace
-
 //===- Helpers ------------------------------------------------------------===//
 
-void emitEncodingMacrosDef(raw_ostream &OS) {
+void llvm::gi::emitEncodingMacrosDef(raw_ostream &OS) {
   OS << "#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__\n"
      << "#define " << EncodeMacroName << "2(Val)"
      << " uint8_t(Val), uint8_t((Val) >> 8)\n"
@@ -76,14 +73,15 @@ void emitEncodingMacrosDef(raw_ostream &OS) {
      << "#endif\n";
 }
 
-void emitEncodingMacrosUndef(raw_ostream &OS) {
+void llvm::gi::emitEncodingMacrosUndef(raw_ostream &OS) {
   OS << "#undef " << EncodeMacroName << "2\n"
      << "#undef " << EncodeMacroName << "4\n"
      << "#undef " << EncodeMacroName << "8\n";
 }
 
-std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
-                                    int HwModeIdx) {
+std::string
+llvm::gi::getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
+                                  int HwModeIdx) {
   std::string Name = "GIFBS";
   for (const Record *Feature : FeatureBitset)
     Name += ("_" + Feature->getName()).str();
@@ -94,8 +92,8 @@ std::string getNameForFeatureBitset(ArrayRef<const Record *> FeatureBitset,
 
 template <class GroupT>
 std::vector<Matcher *>
-optimizeRules(ArrayRef<Matcher *> Rules,
-              std::vector<std::unique_ptr<Matcher>> &MatcherStorage) {
+llvm::gi::optimizeRules(ArrayRef<Matcher *> Rules,
+                        std::vector<std::unique_ptr<Matcher>> &MatcherStorage) {
 
   std::vector<Matcher *> OptRules;
   std::unique_ptr<GroupT> CurrentGroup = std::make_unique<GroupT>();
@@ -142,11 +140,11 @@ optimizeRules(ArrayRef<Matcher *> Rules,
   return OptRules;
 }
 
-template std::vector<Matcher *> optimizeRules<GroupMatcher>(
+template std::vector<Matcher *> llvm::gi::optimizeRules<GroupMatcher>(
     ArrayRef<Matcher *> Rules,
     std::vector<std::unique_ptr<Matcher>> &MatcherStorage);
 
-template std::vector<Matcher *> optimizeRules<SwitchMatcher>(
+template std::vector<Matcher *> llvm::gi::optimizeRules<SwitchMatcher>(
     ArrayRef<Matcher *> Rules,
     std::vector<std::unique_ptr<Matcher>> &MatcherStorage);
 
@@ -158,7 +156,7 @@ static std::string getEncodedEmitStr(StringRef NamedValue, unsigned NumBytes) {
 
 //===- Global Data --------------------------------------------------------===//
 
-std::set<LLTCodeGen> KnownTypes;
+std::set<LLTCodeGen> llvm::gi::KnownTypes;
 
 //===- MatchTableRecord ---------------------------------------------------===//
 
@@ -437,7 +435,7 @@ bool LLTCodeGen::operator<(const LLTCodeGen &Other) const {
 
 //===- LLTCodeGen Helpers -------------------------------------------------===//
 
-std::optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) {
+std::optional<LLTCodeGen> llvm::gi::MVTToLLT(MVT::SimpleValueType SVT) {
   MVT VT(SVT);
 
   if (VT.isVector() && !VT.getVectorElementCount().isScalar())
@@ -2434,6 +2432,3 @@ void MakeTempRegisterAction::emitActionOpcodes(MatchTable &Table,
         << MatchTable::ULEB128Value(TempRegID) << MatchTable::Comment("TypeID")
         << Ty << MatchTable::LineBreak;
 }
-
-} // namespace gi
-} // namespace llvm
diff --git a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
index e62f63ae53ba4..ad5089d429c7d 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/PatternParser.cpp
@@ -17,8 +17,9 @@
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
-namespace llvm {
-namespace gi {
+using namespace llvm;
+using namespace gi;
+
 static constexpr StringLiteral MIFlagsEnumClassName = "MIFlagEnum";
 
 namespace {
@@ -445,6 +446,3 @@ const PatFrag *PatternParser::parsePatFrag(const Record *Def) {
   SeenPatFrags.insert(Res);
   return Res;
 }
-
-} // namespace gi
-} // namespace llvm
diff --git a/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp b/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
index c503c9a47b4ff..dfd37fd64a652 100644
--- a/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
+++ b/llvm/utils/TableGen/Common/GlobalISel/Patterns.cpp
@@ -18,8 +18,8 @@
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
-namespace llvm {
-namespace gi {
+using namespace llvm;
+using namespace gi;
 
 //===- PatternType --------------------------------------------------------===//
 
@@ -884,6 +884,3 @@ bool BuiltinPattern::checkSemantics(ArrayRef<SMLoc> Loc) {
 
   return true;
 }
-
-} // namespace gi
-} // namespace llvm
diff --git a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
index 83dc34896e6f6..28723bf7186df 100644
--- a/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelCombinerEmitter.cpp
@@ -60,23 +60,22 @@ using namespace llvm::gi;
 
 #define DEBUG_TYPE "gicombiner-emitter"
 
-namespace {
-cl::OptionCategory
+static cl::OptionCategory
     GICombinerEmitterCat("Options for -gen-global-isel-combiner");
-cl::opt<bool> StopAfterParse(
+static cl::opt<bool> StopAfterParse(
     "gicombiner-stop-after-parse",
     cl::desc("Stop processing after parsing rules and dump state"),
     cl::cat(GICombinerEmitterCat));
-cl::list<std::string>
+static cl::list<std::string>
     SelectedCombiners("combiners", cl::desc("Emit the specified combiners"),
                       cl::cat(GICombinerEmitterCat), cl::CommaSeparated);
-cl::opt<bool> DebugCXXPreds(
+static cl::opt<bool> DebugCXXPreds(
     "gicombiner-debug-cxxpreds",
     cl::desc("Add Contextual/Debug comments to all C++ predicates"),
     cl::cat(GICombinerEmitterCat));
-cl::opt<bool> DebugTypeInfer("gicombiner-debug-typeinfer",
-                             cl::desc("Print type inference debug logs"),
-                             cl::cat(GICombinerEmitterCat));
+static cl::opt<bool> DebugTypeInfer("gicombiner-debug-typeinfer",
+                                    cl::desc("Print type inference debug logs"),
+                                    cl::cat(GICombinerEmitterCat));
 
 constexpr StringLiteral CXXCustomActionPrefix = "GICXXCustomAction_";
 constexpr StringLiteral CXXPredPrefix = "GICXXPred_MI_Predicate_";
@@ -84,20 +83,20 @@ constexpr StringLiteral MatchDataClassName = "GIDefMatchData";
 
 //===- CodeExpansions Helpers  --------------------------------------------===//
 
-void declareInstExpansion(CodeExpansions &CE, const InstructionMatcher &IM,
-                          StringRef Name) {
+static void declareInstExpansion(CodeExpansions &CE,
+                                 const InstructionMatcher &IM, StringRef Name) {
   CE.declare(Name, "State.MIs[" + to_string(IM.getInsnVarID()) + "]");
 }
 
-void declareInstExpansion(CodeExpansions &CE, const BuildMIAction &A,
-                          StringRef Name) {
+static void declareInstExpansion(CodeExpansions &CE, const BuildMIAction &A,
+                                 StringRef Name) {
   // Note: we use redeclare here because this may overwrite a matcher inst
   // expansion.
   CE.redeclare(Name, "OutMIs[" + to_string(A.getInsnID()) + "]");
 }
 
-void declareOperandExpansion(CodeExpansions &CE, const OperandMatcher &OM,
-                             StringRef Name) {
+static void declareOperandExpansion(CodeExpansions &CE,
+                                    const OperandMatcher &OM, StringRef Name) {
   if (OM.isVariadic()) {
     CE.declare(Name, "getRemainingOperands(*State.MIs[" +
                          to_string(OM.getInsnVarID()) + "], " +
@@ -108,33 +107,34 @@ void declareOperandExpansion(CodeExpansions &CE, const OperandMatcher &OM,
   }
 }
 
-void declareTempRegExpansion(CodeExpansions &CE, unsigned TempRegID,
-                             StringRef Name) {
+static void declareTempRegExpansion(CodeExpansions &CE, unsigned TempRegID,
+                                    StringRef Name) {
   CE.declare(Name, "State.TempRegisters[" + to_string(TempRegID) + "]");
 }
 
 //===- Misc. Helpers  -----------------------------------------------------===//
 
-template <typename Container> auto keys(Container &&C) {
+template <typename Container> static auto keys(Container &&C) {
   return map_range(C, [](auto &Entry) -> auto & { return Entry.first; });
 }
 
-template <typename Container> auto values(Container &&C) {
+template <typename Container> static auto values(Container &&C) {
   return map_range(C, [](auto &Entry) -> auto & { return Entry.second; });
 }
 
-std::string getIsEnabledPredicateEnumName(unsigned CombinerRuleID) {
+static std::string getIsEnabledPredicateEnumName(unsigned CombinerRuleID) {
   return "GICXXPred_Simple_IsRule" + to_string(CombinerRuleID) + "Enabled";
 }
 
 //===- MatchTable Helpers  ------------------------------------------------===//
 
-LLTCodeGen getLLTCodeGen(const PatternType &PT) {
+static LLTCodeGen getLLTCodeGen(const PatternType &PT) {
   return *MVTToLLT(getValueType(PT.getLLTRecord()));
 }
 
 //===- PrettyStackTrace Helpers  ------------------------------------------===//
 
+namespace {
 class PrettyStackTraceParse : public PrettyStackTraceEntry {
   const Record &Def;
 
@@ -277,6 +277,7 @@ class CombineRuleOperandTypeChecker : private OperandTypeChecker {
 
   const OperandTable &MatchOpTable;
 };
+} // namespace
 
 bool CombineRuleOperandTypeChecker::processMatchPattern(InstructionPattern &P) {
   MatchPats.push_back(&P);
@@ -2822,8 +2823,6 @@ void GICombinerEmitter::run(raw_ostream &OS) {
   emitTemporariesInit(OS, MaxTemporaries, "GET_GICOMBINER_CONSTRUCTOR_INITS");
 }
 
-} // end anonymous namespace
-
 //===----------------------------------------------------------------------===//
 
 static void EmitGICombiner(const RecordKeeper &RK, raw_ostream &OS) {
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index fcfcb36a124a8..7ae6107b98554 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -86,8 +86,6 @@ static cl::opt<bool> OptimizeMatchTable(
     cl::desc("Generate an optimized version of the match table"),
     cl::init(true), cl::cat(GlobalISelEmitterCat));
 
-namespace {
-
 static std::string explainPredicates(const TreePatternNode &N) {
   std::string Explanation;
   StringRef Separator = "";
@@ -167,7 +165,7 @@ static std::string explainPredicates(const TreePatternNode &N) {
   return Explanation;
 }
 
-std::string explainOperator(const Record *Operator) {
+static std::string explainOperator(const Record *Operator) {
   if (Operator->isSubClassOf("SDNode"))
     return (" (" + Operator->getValueAsString("Opcode") + ")").str();
 
@@ -314,6 +312,7 @@ static Expected<LLTCodeGen> getInstResultType(const TreePatternNode &Dst,
   return *MaybeOpTy;
 }
 
+namespace {
 class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
 public:
   explicit GlobalISelEmitter(const RecordKeeper &RK);
@@ -493,8 +492,11 @@ class GlobalISelEmitter final : public GlobalISelMatchTableExecutorEmitter {
                        const TreePredicateFn &Predicate,
                        InstructionMatcher &InsnMatcher, bool &HasAddedMatcher);
 };
+} // namespace
 
-StringRef getPatFragPredicateEnumName(const Record *R) { return R->getName(); }
+static StringRef getPatFragPredicateEnumName(const Record *R) {
+  return R->getName();
+}
 
 void GlobalISelEmitter::gatherOpcodeValues() {
   InstructionOpcodeMatcher::initOpcodeValuesMap(Target);
@@ -2534,8 +2536,6 @@ unsigned GlobalISelEmitter::declareHwModeCheck(StringRef HwModeFeatures) {
   return HwModes.emplace(HwModeFeatures.str(), HwModes.size()).first->second;
 }
 
-} // end anonymous namespace
-
 //===----------------------------------------------------------------------===//
 
 static TableGen::Emitter::OptClass<GlobalISelEmitter>

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.

2 participants