1//===-- CIRLoweringEmitter.cpp - Generate CIR lowering patterns -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This TableGen backend emits CIR operation lowering patterns.
10//
11//===----------------------------------------------------------------------===//
12
13#include "TableGenBackends.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/TableGen/Error.h"
16#include "llvm/TableGen/Record.h"
17#include "llvm/TableGen/TableGenBackend.h"
18#include <string>
19#include <utility>
20#include <vector>
21
22using namespace llvm;
23using namespace clang;
24
25namespace {
26std::vector<std::string> CXXABILoweringPatterns;
27std::vector<std::string> CXXABILoweringPatternsList;
28std::vector<std::string> CXXABILoweringAttrAlwaysLegal;
29std::vector<std::string> LLVMLoweringPatterns;
30std::vector<std::string> LLVMLoweringPatternsList;
31std::string CIRAttrToValueVisitFunc;
32std::vector<std::string> CIRAttrToValueVisitorCaseTypes;
33std::vector<std::string> CIRAttrToValueVisitorDecls;
34
35struct CustomLoweringCtor {
36 struct Param {
37 std::string Type;
38 std::string Name;
39 };
40
41 std::vector<Param> Params;
42};
43
44// Adapted from mlir/lib/TableGen/Operator.cpp
45// Returns the C++ class name of the operation, which is the name of the
46// operation with the dialect prefix removed and the first underscore removed.
47// If the operation name starts with an underscore, the underscore is considered
48// part of the class name.
49std::string GetOpCppClassName(const Record *OpRecord) {
50 StringRef Name = OpRecord->getName();
51 StringRef Prefix;
52 StringRef CppClassName;
53 std::tie(args&: Prefix, args&: CppClassName) = Name.split(Separator: '_');
54 if (Prefix.empty()) {
55 // Class name with a leading underscore and without dialect prefix
56 return Name.str();
57 }
58 if (CppClassName.empty()) {
59 // Class name without dialect prefix
60 return Prefix.str();
61 }
62
63 return CppClassName.str();
64}
65
66// Returns the namespace-qualified C++ class name of an attribute. Unlike
67// operations, attributes carry the authoritative name in cppClassName, so the
68// def name is free to differ from it.
69std::string GetAttrCppClassRef(const Record *AttrRecord) {
70 StringRef Ns =
71 AttrRecord->getValueAsDef(FieldName: "dialect")->getValueAsString(FieldName: "cppNamespace");
72 Ns.consume_front(Prefix: "::");
73 std::string CppClassRef = Ns.str();
74 CppClassRef += "::";
75 CppClassRef += AttrRecord->getValueAsString(FieldName: "cppClassName");
76 return CppClassRef;
77}
78
79std::string GetOpABILoweringPatternName(llvm::StringRef OpName) {
80 std::string Name = "CIR";
81 Name += OpName;
82 Name += "ABILowering";
83 return Name;
84}
85
86std::string GetOpLLVMLoweringPatternName(llvm::StringRef OpName) {
87 std::string Name = "CIRToLLVM";
88 Name += OpName;
89 Name += "Lowering";
90 return Name;
91}
92std::optional<CustomLoweringCtor> parseCustomLoweringCtor(const Record *R) {
93 if (!R)
94 return std::nullopt;
95
96 CustomLoweringCtor Ctor;
97 const DagInit *Args = R->getValueAsDag(FieldName: "dagParams");
98
99 for (const auto &[Arg, Name] : Args->getArgAndNames()) {
100 Ctor.Params.push_back(
101 x: {.Type: Arg->getAsUnquotedString(), .Name: Name->getAsUnquotedString()});
102 }
103
104 return Ctor;
105}
106
107void emitCustomParamList(raw_ostream &Code,
108 ArrayRef<CustomLoweringCtor::Param> Params) {
109 for (const CustomLoweringCtor::Param &Param : Params) {
110 Code << ", ";
111 Code << Param.Type << " " << Param.Name;
112 }
113}
114
115void emitCustomInitList(raw_ostream &Code,
116 ArrayRef<CustomLoweringCtor::Param> Params) {
117 for (const CustomLoweringCtor::Param &P : Params)
118 Code << ", " << P.Name << "(" << P.Name << ")";
119}
120
121void GenerateABILoweringPattern(llvm::StringRef OpName,
122 llvm::StringRef PatternName) {
123 std::string CodeBuffer;
124 llvm::raw_string_ostream Code(CodeBuffer);
125
126 Code << "class " << PatternName
127 << " : public mlir::OpConversionPattern<cir::" << OpName << "> {\n";
128 Code << " [[maybe_unused]] mlir::DataLayout *dataLayout;\n";
129 Code << " [[maybe_unused]] cir::LowerModule *lowerModule;\n";
130 Code << "\n";
131
132 Code << "public:\n";
133 Code << " " << PatternName
134 << "(mlir::MLIRContext *context, const mlir::TypeConverter "
135 "&typeConverter, mlir::DataLayout &dataLayout, cir::LowerModule "
136 "&lowerModule)\n";
137 Code << " : OpConversionPattern<cir::" << OpName
138 << ">(typeConverter, context), dataLayout(&dataLayout), "
139 "lowerModule(&lowerModule) {}\n";
140 Code << "\n";
141
142 Code << " mlir::LogicalResult matchAndRewrite(cir::" << OpName
143 << " op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) "
144 "const override;\n";
145
146 Code << "};\n";
147
148 CXXABILoweringPatterns.push_back(x: std::move(CodeBuffer));
149}
150
151void GenerateLLVMLoweringPattern(
152 llvm::StringRef OpName, llvm::StringRef PatternName, bool IsRecursive,
153 llvm::StringRef ExtraDecl, const Record *CustomCtorRec,
154 llvm::StringRef LLVMOp, const DagInit *LLVMOpProps,
155 llvm::StringRef ConstrainedLLVMIntrinsic, bool ConstrainedHasRoundingMode,
156 bool HasFastMathFlags, bool HasZeroResult) {
157 std::optional<CustomLoweringCtor> CustomCtor =
158 parseCustomLoweringCtor(R: CustomCtorRec);
159 std::string CodeBuffer;
160 llvm::raw_string_ostream Code(CodeBuffer);
161
162 Code << "class " << PatternName
163 << " : public mlir::OpConversionPattern<cir::" << OpName << "> {\n";
164 Code << " [[maybe_unused]] mlir::DataLayout const &dataLayout;\n";
165 Code << " [[maybe_unused]] mlir::SymbolTableCollection &symbolTables;\n";
166
167 if (CustomCtor) {
168 for (const CustomLoweringCtor::Param &P : CustomCtor->Params)
169 Code << " " << P.Type << " " << P.Name << ";\n";
170 }
171
172 Code << "\n";
173
174 Code << "public:\n";
175 Code << " using mlir::OpConversionPattern<cir::" << OpName
176 << ">::OpConversionPattern;\n";
177
178 // Constructor
179 Code << " " << PatternName
180 << "(const mlir::TypeConverter &typeConverter, "
181 "mlir::MLIRContext *context, const mlir::DataLayout &dataLayout, "
182 "mlir::SymbolTableCollection &symbolTables";
183
184 if (CustomCtor)
185 emitCustomParamList(Code, Params: CustomCtor->Params);
186
187 Code << ")\n";
188
189 Code << " : OpConversionPattern<cir::" << OpName
190 << ">(typeConverter, context), dataLayout(dataLayout), "
191 "symbolTables(symbolTables)";
192
193 if (CustomCtor)
194 emitCustomInitList(Code, Params: CustomCtor->Params);
195
196 Code << " {\n";
197
198 if (IsRecursive)
199 Code << " setHasBoundedRewriteRecursion();\n";
200
201 Code << " }\n\n";
202
203 if (!ConstrainedLLVMIntrinsic.empty()) {
204 // Generate a matchAndRewrite body for a floating-point operation that
205 // carries an optional `fenv` attribute. The shared `lowerConstrainableFPOp`
206 // helper lowers to the plain `llvmOp` when no `fenv` attribute is present
207 // and to a call to the constrained floating-point intrinsic when one is.
208 assert(!LLVMOp.empty() && "constrainedLLVMIntrinsic requires llvmOp");
209 Code
210 << " mlir::LogicalResult matchAndRewrite(cir::" << OpName
211 << " op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) "
212 "const override {\n";
213 Code << " return lowerConstrainableFPOp<mlir::LLVM::" << LLVMOp
214 << ">(\n";
215 Code << " op, adaptor.getOperands(), op.getFenvAttr(),\n";
216 Code << " "
217 << (HasFastMathFlags ? "op.getFastmathFlagsAttr()"
218 : "cir::FastMathFlagsAttr{}")
219 << ",\n";
220 Code << " *getTypeConverter(), rewriter, \""
221 << ConstrainedLLVMIntrinsic << "\",\n";
222 Code << " /*hasRoundingMode=*/"
223 << (ConstrainedHasRoundingMode ? "true" : "false") << ");\n";
224 Code << " }\n";
225 } else if (!LLVMOp.empty() && HasFastMathFlags) {
226 assert(!HasZeroResult && "fast-math op must have a result");
227 Code
228 << " mlir::LogicalResult matchAndRewrite(cir::" << OpName
229 << " op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) "
230 "const override {\n";
231 Code << " return lowerFPOp<mlir::LLVM::" << LLVMOp
232 << ">(op, adaptor.getOperands(),\n";
233 Code << " op.getFastmathFlagsAttr(), *getTypeConverter(), "
234 "rewriter);\n";
235 Code << " }\n";
236 } else if (!LLVMOp.empty()) {
237 // Generate the matchAndRewrite body automatically.
238 Code
239 << " mlir::LogicalResult matchAndRewrite(cir::" << OpName
240 << " op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) "
241 "const override {\n";
242 Code << " auto props = cir::getDefaultProperties<mlir::LLVM::" << LLVMOp
243 << ">(op.getContext());\n";
244 // `$name` forwards the CIR argument to the LLVM property of the same name;
245 // `"llvm_name":$name` names a differently named LLVM property.
246 for (const auto &[Value, Name] : LLVMOpProps->getArgAndNames()) {
247 StringRef CIRName = Name->getValue();
248 StringRef LLVMName = CIRName;
249 if (const auto *Str = dyn_cast<StringInit>(Val: Value))
250 LLVMName = Str->getValue();
251 Code << " props.set"
252 << llvm::convertToCamelFromSnakeCase(input: LLVMName,
253 /*capitalizeFirst=*/true)
254 << "(convertToLLVMProperty(op.getContext(), op.get"
255 << llvm::convertToCamelFromSnakeCase(input: CIRName,
256 /*capitalizeFirst=*/true)
257 << "()));\n";
258 }
259 if (HasZeroResult) {
260 Code << " rewriter.replaceOpWithNewOp<mlir::LLVM::" << LLVMOp
261 << ">(op, mlir::TypeRange{}, adaptor.getOperands(), props);\n";
262 } else {
263 Code << " mlir::Type resTy = "
264 "typeConverter->convertType(op.getType());\n";
265 Code << " if (!resTy)\n";
266 Code << " return op.emitError() << \"ClangIR lowering NYI: cannot "
267 "convert \" << op.getType();\n";
268 Code << " rewriter.replaceOpWithNewOp<mlir::LLVM::" << LLVMOp
269 << ">(op, mlir::TypeRange{resTy}, adaptor.getOperands(), "
270 "props);\n";
271 }
272 Code << " return mlir::success();\n";
273 Code << " }\n";
274 } else {
275 Code
276 << " mlir::LogicalResult matchAndRewrite(cir::" << OpName
277 << " op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) "
278 "const override;\n";
279 }
280
281 if (!ExtraDecl.empty()) {
282 Code << "\nprivate:\n";
283 Code << ExtraDecl << "\n";
284 }
285
286 Code << "};\n";
287
288 LLVMLoweringPatterns.push_back(x: std::move(CodeBuffer));
289}
290
291void Generate(const Record *OpRecord) {
292 std::string OpName = GetOpCppClassName(OpRecord);
293
294 if (OpRecord->getValueAsBit(FieldName: "hasCXXABILowering")) {
295 std::string PatternName = GetOpABILoweringPatternName(OpName);
296 GenerateABILoweringPattern(OpName, PatternName);
297 CXXABILoweringPatternsList.push_back(x: std::move(PatternName));
298 }
299
300 if (OpRecord->getValueAsBit(FieldName: "hasLLVMLowering")) {
301 std::string PatternName = GetOpLLVMLoweringPatternName(OpName);
302 bool IsRecursive = OpRecord->getValueAsBit(FieldName: "isLLVMLoweringRecursive");
303 const Record *CustomCtor =
304 OpRecord->getValueAsOptionalDef(FieldName: "customLLVMLoweringConstructorDecl");
305 llvm::StringRef ExtraDecl =
306 OpRecord->getValueAsString(FieldName: "extraLLVMLoweringPatternDecl");
307
308 llvm::StringRef LLVMOp = OpRecord->getValueAsString(FieldName: "llvmOp");
309 const DagInit *LLVMOpProps = OpRecord->getValueAsDag(FieldName: "llvmOpProps");
310 llvm::StringRef ConstrainedLLVMIntrinsic =
311 OpRecord->getValueAsString(FieldName: "constrainedLLVMIntrinsic");
312 bool ConstrainedHasRoundingMode =
313 OpRecord->getValueAsBit(FieldName: "constrainedLLVMIntrinsicHasRoundingMode");
314 const DagInit *ArgsDag = OpRecord->getValueAsDag(FieldName: "arguments");
315 bool HasFastMathFlags =
316 llvm::any_of(Range: ArgsDag->getArgNames(), P: [](const StringInit *Name) {
317 return Name && Name->getValue() == "fastmath_flags";
318 });
319
320 if (!LLVMOp.empty() && CustomCtor)
321 PrintFatalError(ErrorLoc: OpRecord->getLoc(),
322 Msg: "op '" + OpName +
323 "' has both llvmOp and a custom lowering "
324 "constructor, which is not supported");
325
326 if (!ConstrainedLLVMIntrinsic.empty() && LLVMOp.empty())
327 PrintFatalError(ErrorLoc: OpRecord->getLoc(),
328 Msg: "op '" + OpName +
329 "' has constrainedLLVMIntrinsic set but no llvmOp, "
330 "which is not supported");
331
332 if (!LLVMOpProps->arg_empty() &&
333 (LLVMOp.empty() || !ConstrainedLLVMIntrinsic.empty()))
334 PrintFatalError(ErrorLoc: OpRecord->getLoc(),
335 Msg: "op '" + OpName +
336 "' has llvmOpProps, which requires llvmOp and is "
337 "not supported with constrainedLLVMIntrinsic");
338
339 for (const auto &[Value, Name] : LLVMOpProps->getArgAndNames()) {
340 if (!Name || !isa<UnsetInit, StringInit>(Val: Value))
341 PrintFatalError(ErrorLoc: OpRecord->getLoc(),
342 Msg: "op '" + OpName +
343 "' has an llvmOpProps entry that is neither "
344 "`$name` nor `\"llvm_name\":$name`");
345 }
346
347 const DagInit *ResultsDag = OpRecord->getValueAsDag(FieldName: "results");
348 bool IsZeroResult = ResultsDag->getNumArgs() == 0;
349 GenerateLLVMLoweringPattern(
350 OpName, PatternName, IsRecursive, ExtraDecl, CustomCtorRec: CustomCtor, LLVMOp,
351 LLVMOpProps, ConstrainedLLVMIntrinsic, ConstrainedHasRoundingMode,
352 HasFastMathFlags, HasZeroResult: IsZeroResult);
353 // Only automatically register patterns that use the default constructor.
354 // Patterns with a custom constructor must be manually registered by the
355 // lowering pass.
356 if (!CustomCtor)
357 LLVMLoweringPatternsList.push_back(x: std::move(PatternName));
358 }
359}
360
361void GenerateCIREnumAttrs(const Record *Record) {
362 // EnumAttr is in a separate hierarchy, so we have to set these separately, as
363 // they never have an 'illegal' CXXABI type in them.
364 CXXABILoweringAttrAlwaysLegal.push_back(x: GetAttrCppClassRef(AttrRecord: Record));
365}
366
367void GenerateAttrToValueVisitor(const Record *Rec) {
368 std::string CppClassRef = GetAttrCppClassRef(AttrRecord: Rec);
369
370 std::string CodeBuffer;
371 llvm::raw_string_ostream Code(CodeBuffer);
372 Code << " mlir::Value visitCirAttr(" << CppClassRef << " attr);";
373 CIRAttrToValueVisitorDecls.push_back(x: std::move(CodeBuffer));
374 CIRAttrToValueVisitorCaseTypes.push_back(x: std::move(CppClassRef));
375}
376
377void GenerateAttrToValueVisitFunc() {
378 std::string CodeBuffer;
379 llvm::raw_string_ostream Code(CodeBuffer);
380 Code << " mlir::Value visit(mlir::Attribute attr) {\n"
381 << " return llvm::TypeSwitch<mlir::Attribute, mlir::Value>(attr)\n"
382 << " .Case<\n "
383 << llvm::join(R&: CIRAttrToValueVisitorCaseTypes, Separator: ",\n ")
384 << ">(\n"
385 << " [&](auto attrT) { return visitCirAttr(attrT); })\n"
386 << " .Default([this](mlir::Attribute attr) {\n"
387 << " mlir::emitError(parentLoc, \"unsupported CIR "
388 "attribute in LLVM constant lowering\")\n"
389 << " << attr;\n"
390 << " return mlir::Value();\n"
391 << " });\n"
392 << " }\n";
393 CIRAttrToValueVisitFunc = std::move(CodeBuffer);
394}
395
396void GenerateCIRAttrs(const Record *Record) {
397 if (!Record->getValueAsBit(FieldName: "canHaveIllegalCXXABIType"))
398 CXXABILoweringAttrAlwaysLegal.push_back(x: GetAttrCppClassRef(AttrRecord: Record));
399 if (Record->getValueAsBit(FieldName: "hasAttrToValueLowering"))
400 GenerateAttrToValueVisitor(Rec: Record);
401}
402} // namespace
403
404void clang::EmitCIRLowering(const llvm::RecordKeeper &RK,
405 llvm::raw_ostream &OS) {
406 emitSourceFileHeader(Desc: "Lowering patterns for CIR operations", OS);
407 for (const auto *OpRecord : RK.getAllDerivedDefinitions(ClassName: "CIR_Op"))
408 Generate(OpRecord);
409 for (const auto *OpRecord : RK.getAllDerivedDefinitions(ClassName: "EnumAttr"))
410 GenerateCIREnumAttrs(Record: OpRecord);
411 for (const auto *OpRecord : RK.getAllDerivedDefinitions(ClassName: "CIR_Attr"))
412 GenerateCIRAttrs(Record: OpRecord);
413 GenerateAttrToValueVisitFunc();
414
415 OS << "#ifdef GET_CIR_ATTR_TO_VALUE_VISITOR_DECLS\n"
416 << CIRAttrToValueVisitFunc << "\n"
417 << llvm::join(R&: CIRAttrToValueVisitorDecls, Separator: "\n") << "\n"
418 << "#endif // GET_CIR_ATTR_TO_VALUE_VISITOR_DECLS\n\n";
419
420 OS << "#ifdef GET_ABI_LOWERING_PATTERNS\n"
421 << llvm::join(R&: CXXABILoweringPatterns, Separator: "\n") << "#endif\n\n";
422 OS << "#ifdef GET_ABI_LOWERING_PATTERNS_LIST\n"
423 << llvm::join(R&: CXXABILoweringPatternsList, Separator: ",\n") << "\n#endif\n\n";
424
425 OS << "#ifdef GET_LLVM_LOWERING_PATTERNS\n"
426 << llvm::join(R&: LLVMLoweringPatterns, Separator: "\n") << "#endif\n\n";
427 OS << "#ifdef GET_LLVM_LOWERING_PATTERNS_LIST\n"
428 << llvm::join(R&: LLVMLoweringPatternsList, Separator: ",\n") << "\n#endif\n\n";
429
430 OS << "#ifdef CXX_ABI_ALWAYS_LEGAL_ATTRS\n"
431 << llvm::join(R&: CXXABILoweringAttrAlwaysLegal, Separator: ",\n") << "\n#endif\n\n";
432}
433