Skip to content

Commit 3d63762

Browse files
committed
[NFC][TableGen] Adopt CodeGenHelpers in SubtargetEmitter
1 parent 1adbae9 commit 3d63762

File tree

2 files changed

+106
-77
lines changed

2 files changed

+106
-77
lines changed

llvm/include/llvm/TableGen/CodeGenHelpers.h

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,37 @@
2020
#include <string>
2121

2222
namespace llvm {
23-
// Simple RAII helper for emitting ifdef-undef-endif scope.
23+
// Simple RAII helper for emitting ifdef-undef-endif scope. `LateUndef` controls
24+
// whether the undef is emitted at the start of the scope (false) or at the end
25+
// of the scope (true).
2426
class IfDefEmitter {
2527
public:
26-
IfDefEmitter(raw_ostream &OS, StringRef Name) : Name(Name.str()), OS(OS) {
27-
OS << "#ifdef " << Name << "\n"
28-
<< "#undef " << Name << "\n\n";
28+
IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef = false)
29+
: Name(Name.str()), OS(OS), LateUndef(LateUndef) {
30+
OS << "#ifdef " << Name << "\n";
31+
if (!LateUndef)
32+
OS << "#undef " << Name << "\n";
33+
OS << "\n";
34+
}
35+
~IfDefEmitter() { close(); }
36+
37+
// Explicit function to close the ifdef scopes.
38+
void close() {
39+
if (Closed)
40+
return;
41+
42+
OS << "\n";
43+
if (LateUndef)
44+
OS << "#undef " << Name << "\n";
45+
OS << "#endif // " << Name << "\n\n";
46+
Closed = true;
2947
}
30-
~IfDefEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
3148

3249
private:
3350
std::string Name;
3451
raw_ostream &OS;
52+
bool LateUndef;
53+
bool Closed = false;
3554
};
3655

3756
// Simple RAII helper for emitting header include guard (ifndef-define-endif).
@@ -42,11 +61,20 @@ class IncludeGuardEmitter {
4261
OS << "#ifndef " << Name << "\n"
4362
<< "#define " << Name << "\n\n";
4463
}
45-
~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }
64+
~IncludeGuardEmitter() { close(); }
65+
66+
// Explicit function to close the ifdef scopes.
67+
void close() {
68+
if (Closed)
69+
return;
70+
OS << "\n#endif // " << Name << "\n\n";
71+
Closed = true;
72+
}
4673

4774
private:
4875
std::string Name;
4976
raw_ostream &OS;
77+
bool Closed = false;
5078
};
5179

5280
// Simple RAII helper for emitting namespace scope. Name can be a single
@@ -64,7 +92,9 @@ class NamespaceEmitter {
6492

6593
// Explicit function to close the namespace scopes.
6694
void close() {
67-
if (!Closed && !Name.empty())
95+
if (Closed)
96+
return;
97+
if (!Name.empty())
6898
OS << "} // namespace " << Name << "\n";
6999
Closed = true;
70100
}

llvm/utils/TableGen/SubtargetEmitter.cpp

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/Debug.h"
2828
#include "llvm/Support/Format.h"
2929
#include "llvm/Support/raw_ostream.h"
30+
#include "llvm/TableGen/CodeGenHelpers.h"
3031
#include "llvm/TableGen/Error.h"
3132
#include "llvm/TableGen/Record.h"
3233
#include "llvm/TableGen/StringToOffsetTable.h"
@@ -75,7 +76,15 @@ class SubtargetEmitter : TargetFeaturesEmitter {
7576
CodeGenTarget TGT;
7677
CodeGenSchedModels &SchedModels;
7778

79+
FeatureMapTy emitEnums(raw_ostream &OS);
7880
void emitSubtargetInfoMacroCalls(raw_ostream &OS);
81+
std::tuple<unsigned, unsigned, unsigned>
82+
emitMCDesc(raw_ostream &OS, const FeatureMapTy &FeatureMap);
83+
void emitTargetDesc(raw_ostream &OS);
84+
void emitHeader(raw_ostream &OS);
85+
void emitCtor(raw_ostream &OS, unsigned NumNames, unsigned NumFeatures,
86+
unsigned NumProcs);
87+
7988
unsigned featureKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
8089
unsigned cpuKeyValues(raw_ostream &OS, const FeatureMapTy &FeatureMap);
8190
unsigned cpuNames(raw_ostream &OS);
@@ -141,7 +150,9 @@ class SubtargetEmitter : TargetFeaturesEmitter {
141150
/// Emit some information about the SubtargetFeature as calls to a macro so
142151
/// that they can be used from C++.
143152
void SubtargetEmitter::emitSubtargetInfoMacroCalls(raw_ostream &OS) {
144-
OS << "\n#ifdef GET_SUBTARGETINFO_MACRO\n";
153+
// Undef the GET_SUBTARGETINFO_MACRO macro and the end of the scope since its
154+
// used within the scope.
155+
IfDefEmitter IfDefMacro(OS, "GET_SUBTARGETINFO_MACRO", /*LateUndef=*/true);
145156

146157
std::vector<const Record *> FeatureList =
147158
Records.getAllDerivedDefinitions("SubtargetFeature");
@@ -167,14 +178,6 @@ void SubtargetEmitter::emitSubtargetInfoMacroCalls(raw_ostream &OS) {
167178
OS << "GET_SUBTARGETINFO_MACRO(" << FieldName << ", " << Default << ", "
168179
<< Getter << ")\n";
169180
}
170-
OS << "#undef GET_SUBTARGETINFO_MACRO\n";
171-
OS << "#endif // GET_SUBTARGETINFO_MACRO\n\n";
172-
173-
OS << "\n#ifdef GET_SUBTARGETINFO_MC_DESC\n";
174-
OS << "#undef GET_SUBTARGETINFO_MC_DESC\n\n";
175-
176-
if (Target == "AArch64")
177-
OS << "#include \"llvm/TargetParser/AArch64TargetParser.h\"\n\n";
178181
}
179182

180183
//
@@ -440,26 +443,24 @@ void SubtargetEmitter::emitStageAndOperandCycleData(
440443
continue;
441444

442445
StringRef Name = ProcModel.ItinsDef->getName();
443-
OS << "\n// Functional units for \"" << Name << "\"\n"
444-
<< "namespace " << Name << "FU {\n";
446+
OS << "\n// Functional units for \"" << Name << "\"\n";
447+
NamespaceEmitter FUNamespace(OS, (Name + Twine("FU")).str());
445448

446449
for (const auto &[Idx, FU] : enumerate(FUs))
447450
OS << " const InstrStage::FuncUnits " << FU->getName() << " = 1ULL << "
448451
<< Idx << ";\n";
449452

450-
OS << "} // end namespace " << Name << "FU\n";
453+
FUNamespace.close();
451454

452455
ConstRecVec BPs = ProcModel.ItinsDef->getValueAsListOfDefs("BP");
453456
if (BPs.empty())
454457
continue;
455-
OS << "\n// Pipeline forwarding paths for itineraries \"" << Name << "\"\n"
456-
<< "namespace " << Name << "Bypass {\n";
458+
OS << "\n// Pipeline forwarding paths for itineraries \"" << Name << "\"\n";
459+
NamespaceEmitter BypassNamespace(OS, (Name + Twine("Bypass")).str());
457460

458461
OS << " const unsigned NoBypass = 0;\n";
459462
for (const auto &[Idx, BP] : enumerate(BPs))
460463
OS << " const unsigned " << BP->getName() << " = 1 << " << Idx << ";\n";
461-
462-
OS << "} // end namespace " << Name << "Bypass\n";
463464
}
464465

465466
// Begin stages table
@@ -1940,13 +1941,13 @@ void SubtargetEmitter::parseFeaturesFunction(raw_ostream &OS) {
19401941
}
19411942

19421943
void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
1943-
OS << "namespace " << Target << "_MC {\n"
1944-
<< "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
1944+
NamespaceEmitter NS(OS, (Target + Twine("_MC")).str());
1945+
OS << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,\n"
19451946
<< " const MCInst *MI, const MCInstrInfo *MCII, "
19461947
<< "const MCSubtargetInfo &STI, unsigned CPUID) {\n";
19471948
emitSchedModelHelpersImpl(OS, /* OnlyExpandMCPredicates */ true);
19481949
OS << "}\n";
1949-
OS << "} // end namespace " << Target << "_MC\n\n";
1950+
NS.close();
19501951

19511952
OS << "struct " << Target
19521953
<< "GenMCSubtargetInfo : public MCSubtargetInfo {\n";
@@ -1982,46 +1983,37 @@ void SubtargetEmitter::emitGenMCSubtargetInfo(raw_ostream &OS) {
19821983
}
19831984

19841985
void SubtargetEmitter::emitMcInstrAnalysisPredicateFunctions(raw_ostream &OS) {
1985-
OS << "\n#ifdef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n";
1986-
OS << "#undef GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1986+
IfDefEmitter IfDefDecls(OS, "GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS");
19871987

19881988
STIPredicateExpander PE(Target, /*Indent=*/0);
19891989
PE.setExpandForMC(true);
19901990
PE.setByRef(true);
19911991
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
19921992
PE.expandSTIPredicate(OS, Fn);
19931993

1994-
OS << "#endif // GET_STIPREDICATE_DECLS_FOR_MC_ANALYSIS\n\n";
1995-
1996-
OS << "\n#ifdef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n";
1997-
OS << "#undef GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
1994+
IfDefDecls.close();
19981995

1996+
IfDefEmitter IfDefDefs(OS, "GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS");
19991997
std::string ClassPrefix = Target + "MCInstrAnalysis";
20001998
PE.setExpandDefinition(true);
20011999
PE.setClassPrefix(ClassPrefix);
20022000
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
20032001
PE.expandSTIPredicate(OS, Fn);
2004-
2005-
OS << "#endif // GET_STIPREDICATE_DEFS_FOR_MC_ANALYSIS\n\n";
20062002
}
20072003

2008-
//
2009-
// SubtargetEmitter::run - Main subtarget enumeration emitter.
2010-
//
2011-
void SubtargetEmitter::run(raw_ostream &OS) {
2012-
emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
2013-
2014-
OS << "\n#ifdef GET_SUBTARGETINFO_ENUM\n";
2015-
OS << "#undef GET_SUBTARGETINFO_ENUM\n\n";
2016-
2017-
OS << "namespace llvm {\n";
2018-
auto FeatureMap = enumeration(OS);
2019-
OS << "} // end namespace llvm\n\n";
2020-
OS << "#endif // GET_SUBTARGETINFO_ENUM\n\n";
2004+
FeatureMapTy SubtargetEmitter::emitEnums(raw_ostream &OS) {
2005+
IfDefEmitter IfDef(OS, "GET_SUBTARGETINFO_ENUM");
2006+
NamespaceEmitter NS(OS, "llvm");
2007+
return enumeration(OS);
2008+
}
20212009

2022-
emitSubtargetInfoMacroCalls(OS);
2010+
std::tuple<unsigned, unsigned, unsigned>
2011+
SubtargetEmitter::emitMCDesc(raw_ostream &OS, const FeatureMapTy &FeatureMap) {
2012+
IfDefEmitter IfDef(OS, "GET_SUBTARGETINFO_MC_DESC");
2013+
if (Target == "AArch64")
2014+
OS << "#include \"llvm/TargetParser/AArch64TargetParser.h\"\n\n";
2015+
NamespaceEmitter LlvmNS(OS, "llvm");
20232016

2024-
OS << "namespace llvm {\n";
20252017
unsigned NumFeatures = featureKeyValues(OS, FeatureMap);
20262018
OS << "\n";
20272019
emitSchedModel(OS);
@@ -2033,6 +2025,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
20332025

20342026
// MCInstrInfo initialization routine.
20352027
emitGenMCSubtargetInfo(OS);
2028+
LlvmNS.close();
20362029

20372030
OS << "\nstatic inline MCSubtargetInfo *create" << Target
20382031
<< "MCSubtargetInfoImpl("
@@ -2067,35 +2060,33 @@ void SubtargetEmitter::run(raw_ostream &OS) {
20672060
OS << "nullptr, nullptr, nullptr";
20682061
}
20692062
OS << ");\n}\n\n";
2063+
return {NumNames, NumFeatures, NumProcs};
2064+
}
20702065

2071-
OS << "} // end namespace llvm\n\n";
2072-
2073-
OS << "#endif // GET_SUBTARGETINFO_MC_DESC\n\n";
2074-
2075-
OS << "\n#ifdef GET_SUBTARGETINFO_TARGET_DESC\n";
2076-
OS << "#undef GET_SUBTARGETINFO_TARGET_DESC\n\n";
2066+
void SubtargetEmitter::emitTargetDesc(raw_ostream &OS) {
2067+
IfDefEmitter IfDef(OS, "GET_SUBTARGETINFO_TARGET_DESC");
20772068

20782069
OS << "#include \"llvm/ADT/BitmaskEnum.h\"\n";
20792070
OS << "#include \"llvm/Support/Debug.h\"\n";
20802071
OS << "#include \"llvm/Support/raw_ostream.h\"\n\n";
20812072
if (Target == "AArch64")
20822073
OS << "#include \"llvm/TargetParser/AArch64TargetParser.h\"\n\n";
20832074
parseFeaturesFunction(OS);
2075+
}
20842076

2085-
OS << "#endif // GET_SUBTARGETINFO_TARGET_DESC\n\n";
2086-
2077+
void SubtargetEmitter::emitHeader(raw_ostream &OS) {
20872078
// Create a TargetSubtargetInfo subclass to hide the MC layer initialization.
2088-
OS << "\n#ifdef GET_SUBTARGETINFO_HEADER\n";
2089-
OS << "#undef GET_SUBTARGETINFO_HEADER\n\n";
2079+
IfDefEmitter IfDef(OS, "GET_SUBTARGETINFO_HEADER");
2080+
NamespaceEmitter LLVMNS(OS, "llvm");
20902081

20912082
std::string ClassName = Target + "GenSubtargetInfo";
2092-
OS << "namespace llvm {\n";
20932083
OS << "class DFAPacketizer;\n";
2094-
OS << "namespace " << Target << "_MC {\n"
2095-
<< "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
2096-
<< " const MCInst *MI, const MCInstrInfo *MCII, "
2097-
<< "const MCSubtargetInfo &STI, unsigned CPUID);\n"
2098-
<< "} // end namespace " << Target << "_MC\n\n";
2084+
{
2085+
NamespaceEmitter MCNS(OS, (Target + Twine("_MC")).str());
2086+
OS << "unsigned resolveVariantSchedClassImpl(unsigned SchedClass,"
2087+
<< " const MCInst *MI, const MCInstrInfo *MCII, "
2088+
<< "const MCSubtargetInfo &STI, unsigned CPUID);\n";
2089+
}
20992090
OS << "struct " << ClassName << " : public TargetSubtargetInfo {\n"
21002091
<< " explicit " << ClassName << "(const Triple &TT, StringRef CPU, "
21012092
<< "StringRef TuneCPU, StringRef FS);\n"
@@ -2140,17 +2131,15 @@ void SubtargetEmitter::run(raw_ostream &OS) {
21402131
PE.setByRef(false);
21412132
for (const STIPredicateFunction &Fn : SchedModels.getSTIPredicates())
21422133
PE.expandSTIPredicate(OS, Fn);
2134+
OS << "};\n";
2135+
}
21432136

2144-
OS << "};\n"
2145-
<< "} // end namespace llvm\n\n";
2146-
2147-
OS << "#endif // GET_SUBTARGETINFO_HEADER\n\n";
2148-
2149-
OS << "\n#ifdef GET_SUBTARGETINFO_CTOR\n";
2150-
OS << "#undef GET_SUBTARGETINFO_CTOR\n\n";
2151-
2137+
void SubtargetEmitter::emitCtor(raw_ostream &OS, unsigned NumNames,
2138+
unsigned NumFeatures, unsigned NumProcs) {
2139+
IfDefEmitter IfDef(OS, "GET_SUBTARGETINFO_CTOR");
21522140
OS << "#include \"llvm/CodeGen/TargetSchedule.h\"\n\n";
2153-
OS << "namespace llvm {\n";
2141+
2142+
NamespaceEmitter LLVMNS(OS, "llvm");
21542143
OS << "extern const llvm::StringRef " << Target << "Names[];\n";
21552144
OS << "extern const llvm::SubtargetFeatureKV " << Target << "FeatureKV[];\n";
21562145
OS << "extern const llvm::SubtargetSubTypeKV " << Target << "SubTypeKV[];\n";
@@ -2167,6 +2156,7 @@ void SubtargetEmitter::run(raw_ostream &OS) {
21672156
OS << "extern const unsigned " << Target << "ForwardingPaths[];\n";
21682157
}
21692158

2159+
std::string ClassName = Target + "GenSubtargetInfo";
21702160
OS << ClassName << "::" << ClassName << "(const Triple &TT, StringRef CPU, "
21712161
<< "StringRef TuneCPU, StringRef FS)\n";
21722162

@@ -2204,11 +2194,20 @@ void SubtargetEmitter::run(raw_ostream &OS) {
22042194
emitSchedModelHelpers(ClassName, OS);
22052195
emitHwModeCheck(ClassName, OS, /*IsMC=*/false);
22062196
emitGetMacroFusions(ClassName, OS);
2197+
}
22072198

2208-
OS << "} // end namespace llvm\n\n";
2209-
2210-
OS << "#endif // GET_SUBTARGETINFO_CTOR\n\n";
2199+
//
2200+
// SubtargetEmitter::run - Main subtarget enumeration emitter.
2201+
//
2202+
void SubtargetEmitter::run(raw_ostream &OS) {
2203+
emitSourceFileHeader("Subtarget Enumeration Source Fragment", OS);
22112204

2205+
auto FeatureMap = emitEnums(OS);
2206+
emitSubtargetInfoMacroCalls(OS);
2207+
auto [NumNames, NumFeatures, NumProcs] = emitMCDesc(OS, FeatureMap);
2208+
emitTargetDesc(OS);
2209+
emitHeader(OS);
2210+
emitCtor(OS, NumNames, NumFeatures, NumProcs);
22122211
emitMcInstrAnalysisPredicateFunctions(OS);
22132212
}
22142213

0 commit comments

Comments
 (0)