Skip to content

Commit 445f2a5

Browse files
authored
[CIR][CIRGen][TBAA] Replace hardcoded TBAA names with getTBAAName (#1242)
This patch follows #1220 (comment) by augmenting `CIR_Type` with a new field, `tbaaName`. Specifically, it enables TableGen support for the `-gen-cir-tbaa-name-lowering` option, allowing for the generation of `getTBAAName` functions based on the `tbaaName`. This enhancement enables us to replace the hardcoded TBAA names in the `getTypeName` function with the newly generated `getTBAAName`.
1 parent 2ec1a24 commit 445f2a5

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ include "mlir/Interfaces/DataLayoutInterfaces.td"
2020
include "mlir/IR/AttrTypeBase.td"
2121
include "mlir/IR/EnumAttr.td"
2222

23+
// Specify the TBAA name of CIR_type
24+
class TBAALoweringInfo {
25+
string tbaaName = "";
26+
}
27+
2328
//===----------------------------------------------------------------------===//
2429
// CIR Types
2530
//===----------------------------------------------------------------------===//
2631

2732
class CIR_Type<string name, string typeMnemonic, list<Trait> traits = [],
2833
string baseCppClass = "::mlir::Type">
29-
: TypeDef<CIR_Dialect, name, traits, baseCppClass> {
34+
: TypeDef<CIR_Dialect, name, traits, baseCppClass>, TBAALoweringInfo {
3035
let mnemonic = typeMnemonic;
3136
}
3237

@@ -162,6 +167,7 @@ class CIR_FloatType<string name, string mnemonic>
162167
]> {}
163168

164169
def CIR_Single : CIR_FloatType<"Single", "float"> {
170+
let tbaaName = "float";
165171
let summary = "CIR single-precision float type";
166172
let description = [{
167173
Floating-point type that represents the `float` type in C/C++. Its
@@ -170,6 +176,7 @@ def CIR_Single : CIR_FloatType<"Single", "float"> {
170176
}
171177

172178
def CIR_Double : CIR_FloatType<"Double", "double"> {
179+
let tbaaName = "double";
173180
let summary = "CIR double-precision float type";
174181
let description = [{
175182
Floating-point type that represents the `double` type in C/C++. Its
@@ -206,6 +213,7 @@ def CIR_FP128 : CIR_FloatType<"FP128", "f128"> {
206213
}
207214

208215
def CIR_LongDouble : CIR_FloatType<"LongDouble", "long_double"> {
216+
let tbaaName = "long double";
209217
let summary = "CIR extended-precision float type";
210218
let description = [{
211219
Floating-point type that represents the `long double` type in C/C++.
@@ -263,7 +271,7 @@ def CIR_ComplexType : CIR_Type<"Complex", "complex",
263271

264272
def CIR_PointerType : CIR_Type<"Pointer", "ptr",
265273
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
266-
274+
let tbaaName = "any pointer";
267275
let summary = "CIR pointer type";
268276
let description = [{
269277
`CIR.ptr` is a type returned by any op generating a pointer in C++.
@@ -339,7 +347,7 @@ def CIR_DataMemberType : CIR_Type<"DataMember", "data_member",
339347
def CIR_BoolType :
340348
CIR_Type<"Bool", "bool",
341349
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
342-
350+
let tbaaName = "bool";
343351
let summary = "CIR bool type";
344352
let description = [{
345353
`cir.bool` represent's C++ bool type.

clang/include/clang/CIR/Dialect/IR/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ add_public_tablegen_target(MLIRCIREnumsGen)
3131
clang_tablegen(CIRBuiltinsLowering.inc -gen-cir-builtins-lowering
3232
SOURCE CIROps.td
3333
TARGET CIRBuiltinsLowering)
34+
35+
clang_tablegen(CIRTBAANameLowering.inc -gen-cir-tbaa-name-lowering
36+
SOURCE CIRTypes.td
37+
TARGET CIRTBAANameLowering)

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -689,19 +689,18 @@ mlir::LLVM::TBAATypeDescriptorAttr getChar(mlir::MLIRContext *ctx) {
689689
return createScalarTypeNode(ctx, "omnipotent char", getRoot(ctx), 0);
690690
}
691691

692-
// FIXME(cir): This should be moved and use tablegen approach
693-
// see https://github.com/llvm/clangir/pull/1220#discussion_r1889187867
692+
#define GET_TBAANAME_LOWERING_FUNCTIONS_DEF
693+
#include "clang/CIR/Dialect/IR/CIRTBAANameLowering.inc"
694+
#undef GET_TBAANAME_LOWERING_FUNCTIONS_DEF
695+
694696
StringRef getTypeName(mlir::Type type) {
695697
return TypeSwitch<mlir::Type, StringRef>(type)
696698
.Case<cir::IntType>([](cir::IntType ty) { return ty.getTBAATypeName(); })
697-
.Case<cir::SingleType>([](cir::SingleType) { return "float"; })
698-
.Case<cir::DoubleType>([](cir::DoubleType) { return "double"; })
699-
.Case<cir::FP80Type>([](cir::FP80Type) { return "f80"; })
700-
.Case<cir::FP128Type>([](cir::FP128Type) { return "f128"; })
701-
.Case<cir::LongDoubleType>(
702-
[](cir::LongDoubleType) { return "long double"; })
703-
.Case<cir::BoolType>([](cir::BoolType) { return "bool"; })
704-
.Case<cir::PointerType>([](cir::PointerType) { return "any pointer"; })
699+
.Case<
700+
#define GET_TBAANAME_LOWERING_LIST
701+
#include "clang/CIR/Dialect/IR/CIRTBAANameLowering.inc"
702+
#undef GET_TBAANAME_LOWERING_LIST
703+
>([](auto ty) { return getTBAAName(ty); })
705704
.Default([](auto ty) {
706705
llvm::errs() << "unknown type: " << ty << "\n";
707706
return "unknown";

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,5 +1099,9 @@ class CIRToLLVMSignBitOpLowering
10991099
#include "clang/CIR/Dialect/IR/CIRBuiltinsLowering.inc"
11001100
#undef GET_BUILTIN_LOWERING_CLASSES_DECLARE
11011101

1102+
#define GET_TBAANAME_LOWERING_FUNCTIONS_DECLARE
1103+
#include "clang/CIR/Dialect/IR/CIRTBAANameLowering.inc"
1104+
#undef GET_TBAANAME_LOWERING_FUNCTIONS_DECLARE
1105+
11021106
} // namespace direct
11031107
} // namespace cir

clang/utils/TableGen/CIRLoweringEmitter.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ std::string ClassDeclaration;
1616
std::string ClassDefinitions;
1717
std::string ClassList;
1818

19+
std::string TBAANameFunctionDeclaration;
20+
std::string TBAANameFunctionDefinitions;
21+
std::string TBAANameClassList;
22+
1923
void GenerateLowering(const Record *Operation) {
2024
using namespace std::string_literals;
2125
std::string Name = Operation->getName().str();
@@ -68,6 +72,24 @@ CIR)C++" +
6872

6973
ClassList += ", CIR" + Name + "Lowering\n";
7074
}
75+
76+
void GenerateTBAANameLowering(const Record *def) {
77+
using namespace std::string_literals;
78+
std::string Name = def->getValueAsString("cppClassName").str();
79+
std::string TBAAName = def->getValueAsString("tbaaName").str();
80+
TBAANameFunctionDeclaration += "llvm::StringRef getTBAAName(cir::";
81+
TBAANameFunctionDeclaration += Name + " ty);";
82+
TBAANameFunctionDeclaration += "\n";
83+
TBAANameFunctionDefinitions += "llvm::StringRef getTBAAName(cir::";
84+
TBAANameFunctionDefinitions += Name + " ty) {";
85+
TBAANameFunctionDefinitions += " return \"" + TBAAName + "\";";
86+
TBAANameFunctionDefinitions += "}";
87+
TBAANameFunctionDefinitions += "\n";
88+
TBAANameClassList += "\n";
89+
TBAANameClassList += "cir::";
90+
TBAANameClassList += Name;
91+
TBAANameClassList += ", ";
92+
}
7193
} // namespace
7294

7395
void clang::EmitCIRBuiltinsLowering(const RecordKeeper &Records,
@@ -85,3 +107,25 @@ void clang::EmitCIRBuiltinsLowering(const RecordKeeper &Records,
85107
<< ClassDefinitions << "\n#endif\n";
86108
OS << "#ifdef GET_BUILTIN_LOWERING_LIST\n" << ClassList << "\n#endif\n";
87109
}
110+
111+
void clang::EmitCIRTBAANameLowering(const RecordKeeper &Records,
112+
raw_ostream &OS) {
113+
emitSourceFileHeader("Lowering of ClangIR TBAA Name", OS);
114+
115+
for (const auto *Builtin :
116+
Records.getAllDerivedDefinitions("TBAALoweringInfo")) {
117+
if (!Builtin->getValueAsString("tbaaName").empty())
118+
GenerateTBAANameLowering(Builtin);
119+
}
120+
121+
OS << "#ifdef GET_TBAANAME_LOWERING_FUNCTIONS_DECLARE\n"
122+
<< TBAANameFunctionDeclaration << "\n#endif\n";
123+
OS << "#ifdef GET_TBAANAME_LOWERING_FUNCTIONS_DEF\n"
124+
<< TBAANameFunctionDefinitions << "\n#endif\n";
125+
// remove last `, `
126+
if (!TBAANameClassList.empty()) {
127+
TBAANameClassList.resize(TBAANameClassList.size() - 2);
128+
}
129+
OS << "#ifdef GET_TBAANAME_LOWERING_LIST\n"
130+
<< TBAANameClassList << "\n#endif\n";
131+
}

clang/utils/TableGen/TableGen.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum ActionType {
2626
PrintRecords,
2727
DumpJSON,
2828
GenCIRBuiltinsLowering,
29+
GenCIRTBAANameLowering,
2930
GenClangAttrClasses,
3031
GenClangAttrParserStringSwitches,
3132
GenClangAttrSubjectMatchRulesParserStringSwitches,
@@ -123,6 +124,8 @@ cl::opt<ActionType> Action(
123124
clEnumValN(GenCIRBuiltinsLowering, "gen-cir-builtins-lowering",
124125
"Generate lowering of ClangIR builtins to equivalent LLVM "
125126
"IR builtins"),
127+
clEnumValN(GenCIRTBAANameLowering, "gen-cir-tbaa-name-lowering",
128+
"Generate lowering of ClangIR TBAA Name"),
126129
clEnumValN(GenClangAttrClasses, "gen-clang-attr-classes",
127130
"Generate clang attribute clases"),
128131
clEnumValN(GenClangAttrParserStringSwitches,
@@ -331,6 +334,9 @@ bool ClangTableGenMain(raw_ostream &OS, const RecordKeeper &Records) {
331334
case GenCIRBuiltinsLowering:
332335
EmitCIRBuiltinsLowering(Records, OS);
333336
break;
337+
case GenCIRTBAANameLowering:
338+
EmitCIRTBAANameLowering(Records, OS);
339+
break;
334340
case GenClangAttrClasses:
335341
EmitClangAttrClass(Records, OS);
336342
break;

clang/utils/TableGen/TableGenBackends.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace clang {
2626

2727
void EmitCIRBuiltinsLowering(const llvm::RecordKeeper &RK,
2828
llvm::raw_ostream &OS);
29+
void EmitCIRTBAANameLowering(const llvm::RecordKeeper &RK,
30+
llvm::raw_ostream &OS);
2931
void EmitClangDeclContext(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
3032
/**
3133
@param PriorizeIfSubclassOf These classes should be prioritized in the output.

0 commit comments

Comments
 (0)