From ab32e3f803a369bf9ec3eed24cdd2888fe7c3994 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Tue, 18 Nov 2025 13:41:26 -0800 Subject: [PATCH 1/2] [NFC][TableGen] Add IfGuardEmitter and adopt it in InstrInfoEmitter Add a RAII `IfGuardEmitter` to insert simple #if guards and adopt it in InstrInfoEmitter. --- llvm/include/llvm/TableGen/CodeGenHelpers.h | 18 +++++++++++ llvm/utils/TableGen/InstrInfoEmitter.cpp | 35 ++++++++++----------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h index 1b1b5e63a8fc4..f7e732a4b232d 100644 --- a/llvm/include/llvm/TableGen/CodeGenHelpers.h +++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h @@ -46,6 +46,24 @@ class IfDefEmitter { bool LateUndef; }; +// Simple RAII helper for emitting #if guard. It emits: +// +// #endif // +class IfGuardEmitter { +public: + IfGuardEmitter(raw_ostream &OS, StringRef Condition, + StringRef IfGuard = "#if") + : Condition(Condition.str()), OS(OS) { + OS << IfGuard << " " << Condition << "\n\n"; + } + + ~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; } + +private: + std::string Condition; + raw_ostream &OS; +}; + // Simple RAII helper for emitting header include guard (ifndef-define-endif). class IncludeGuardEmitter { public: diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp index 32994c12aa98b..0d13a6f173934 100644 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp @@ -911,24 +911,23 @@ void InstrInfoEmitter::run(raw_ostream &OS) { } } - OS << "#if defined(GET_INSTRINFO_MC_DESC) || " - "defined(GET_INSTRINFO_CTOR_DTOR)\n"; - - OS << "namespace llvm {\n\n"; - - OS << "struct " << TargetName << "InstrTable {\n"; - OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n"; - OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), " - "\"Unwanted padding between Insts and OperandInfo\");\n"; - OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n"; - OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), " - "\"Unwanted padding between OperandInfo and ImplicitOps\");\n"; - OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) << "];\n"; - OS << "};\n\n"; - - OS << "} // end namespace llvm\n"; - OS << "#endif // defined(GET_INSTRINFO_MC_DESC) || " - "defined(GET_INSTRINFO_CTOR_DTOR)\n\n"; + { + IfGuardEmitter IfGuard( + OS, + "defined(GET_INSTRINFO_MC_DESC) || defined(GET_INSTRINFO_CTOR_DTOR)"); + NamespaceEmitter NS(OS, "llvm"); + + OS << "struct " << TargetName << "InstrTable {\n"; + OS << " MCInstrDesc Insts[" << NumberedInstructions.size() << "];\n"; + OS << " static_assert(alignof(MCInstrDesc) >= alignof(MCOperandInfo), " + "\"Unwanted padding between Insts and OperandInfo\");\n"; + OS << " MCOperandInfo OperandInfo[" << OperandInfoSize << "];\n"; + OS << " static_assert(alignof(MCOperandInfo) >= alignof(MCPhysReg), " + "\"Unwanted padding between OperandInfo and ImplicitOps\");\n"; + OS << " MCPhysReg ImplicitOps[" << std::max(ImplicitListSize, 1U) + << "];\n"; + OS << "};"; + } const CodeGenRegBank &RegBank = Target.getRegBank(); const CodeGenHwModes &CGH = Target.getHwModes(); From ae51f6161ce72bc3c9cb9e43dddcb74a4237eac3 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Tue, 18 Nov 2025 15:42:41 -0800 Subject: [PATCH 2/2] Review feedback: always use #if --- llvm/include/llvm/TableGen/CodeGenHelpers.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/llvm/include/llvm/TableGen/CodeGenHelpers.h b/llvm/include/llvm/TableGen/CodeGenHelpers.h index f7e732a4b232d..41b0adc49d1ad 100644 --- a/llvm/include/llvm/TableGen/CodeGenHelpers.h +++ b/llvm/include/llvm/TableGen/CodeGenHelpers.h @@ -47,14 +47,13 @@ class IfDefEmitter { }; // Simple RAII helper for emitting #if guard. It emits: -// +// #if // #endif // class IfGuardEmitter { public: - IfGuardEmitter(raw_ostream &OS, StringRef Condition, - StringRef IfGuard = "#if") + IfGuardEmitter(raw_ostream &OS, StringRef Condition) : Condition(Condition.str()), OS(OS) { - OS << IfGuard << " " << Condition << "\n\n"; + OS << "#if " << Condition << "\n\n"; } ~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; }