1//===- LLVMTypeSyntax.cpp - Parsing/printing for MLIR LLVM Dialect types --===//
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#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
10#include "mlir/IR/Builders.h"
11#include "mlir/IR/DialectImplementation.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/ADT/TypeSwitch.h"
14
15using namespace mlir;
16using namespace mlir::LLVM;
17
18//===----------------------------------------------------------------------===//
19// Printing.
20//===----------------------------------------------------------------------===//
21
22/// If the given type is compatible with the LLVM dialect, prints it using
23/// internal functions to avoid getting a verbose `!llvm` prefix. Otherwise
24/// prints it as usual.
25static void dispatchPrint(AsmPrinter &printer, Type type) {
26 if (isCompatibleType(type) &&
27 !(llvm::isa<IntegerType, FloatType, VectorType>(Val: type) ||
28 (llvm::isa<PtrLikeTypeInterface>(Val: type) &&
29 !llvm::isa<LLVMPointerType>(Val: type))))
30 return mlir::LLVM::detail::printType(type, printer);
31 printer.printType(type);
32}
33
34/// Returns the keyword to use for the given type.
35static StringRef getTypeKeyword(Type type) {
36 return TypeSwitch<Type, StringRef>(type)
37 .Case<LLVMVoidType>(caseFn: [&](Type) { return "void"; })
38 .Case<LLVMPPCFP128Type>(caseFn: [&](Type) { return "ppc_fp128"; })
39 .Case<LLVMLabelType>(caseFn: [&](Type) { return "label"; })
40 .Case<LLVMMetadataType>(caseFn: [&](Type) { return "metadata"; })
41 .Case<LLVMFunctionType>(caseFn: [&](Type) { return "func"; })
42 .Case<LLVMPointerType>(caseFn: [&](Type) { return "ptr"; })
43 .Case<LLVMArrayType>(caseFn: [&](Type) { return "array"; })
44 .Case<LLVMByteType>(caseFn: [&](Type) { return "byte"; })
45 .Case<LLVMStructType>(caseFn: [&](Type) { return "struct"; })
46 .Case<LLVMTargetExtType>(caseFn: [&](Type) { return "target"; })
47 .Case<LLVMX86AMXType>(caseFn: [&](Type) { return "x86_amx"; })
48 .DefaultUnreachable(message: "unexpected 'llvm' type kind");
49}
50
51/// Prints a structure type. Keeps track of known struct names to handle self-
52/// or mutually-referring structs without falling into infinite recursion.
53void LLVMStructType::print(AsmPrinter &printer) const {
54 FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrint;
55
56 printer << "<";
57 if (isIdentified()) {
58 cyclicPrint = printer.tryStartCyclicPrint(attrOrType: *this);
59
60 printer << '"';
61 llvm::printEscapedString(Name: getName(), Out&: printer.getStream());
62 printer << '"';
63 // If we are printing a reference to one of the enclosing structs, just
64 // print the name and stop to avoid infinitely long output.
65 if (failed(Result: cyclicPrint)) {
66 printer << '>';
67 return;
68 }
69 printer << ", ";
70 }
71
72 if (isIdentified() && isOpaque()) {
73 printer << "opaque>";
74 return;
75 }
76
77 if (isPacked())
78 printer << "packed ";
79
80 // Put the current type on stack to avoid infinite recursion.
81 printer << '(';
82 llvm::interleaveComma(c: getBody(), os&: printer.getStream(),
83 each_fn: [&](Type subtype) { dispatchPrint(printer, type: subtype); });
84 printer << ')';
85 printer << '>';
86}
87
88/// Prints the given LLVM dialect type recursively. This leverages closedness of
89/// the LLVM dialect type system to avoid printing the dialect prefix
90/// repeatedly. For recursive structures, only prints the name of the structure
91/// when printing a self-reference. Note that this does not apply to sibling
92/// references. For example,
93/// struct<"a", (ptr<struct<"a">>)>
94/// struct<"c", (ptr<struct<"b", (ptr<struct<"c">>)>>,
95/// ptr<struct<"b", (ptr<struct<"c">>)>>)>
96/// note that "b" is printed twice.
97void mlir::LLVM::detail::printType(Type type, AsmPrinter &printer) {
98 if (!type) {
99 printer << "<<NULL-TYPE>>";
100 return;
101 }
102
103 printer << getTypeKeyword(type);
104
105 llvm::TypeSwitch<Type>(type)
106 .Case<LLVMPointerType, LLVMArrayType, LLVMByteType, LLVMFunctionType,
107 LLVMTargetExtType, LLVMStructType>(
108 caseFn: [&](auto type) { type.print(printer); });
109}
110
111//===----------------------------------------------------------------------===//
112// Parsing.
113//===----------------------------------------------------------------------===//
114
115static ParseResult dispatchParse(AsmParser &parser, Type &type);
116
117/// Attempts to set the body of an identified structure type. Reports a parsing
118/// error at `subtypesLoc` in case of failure.
119static LLVMStructType trySetStructBody(LLVMStructType type,
120 ArrayRef<Type> subtypes, bool isPacked,
121 AsmParser &parser, SMLoc subtypesLoc) {
122 for (Type t : subtypes) {
123 if (!LLVMStructType::isValidElementType(type: t)) {
124 parser.emitError(loc: subtypesLoc)
125 << "invalid LLVM structure element type: " << t;
126 return LLVMStructType();
127 }
128 }
129
130 if (succeeded(Result: type.setBody(types: subtypes, isPacked)))
131 return type;
132
133 parser.emitError(loc: subtypesLoc)
134 << "identified type already used with a different body";
135 return LLVMStructType();
136}
137
138/// Parses an LLVM dialect structure type.
139/// llvm-type ::= `struct<` (string-literal `,`)? `packed`?
140/// `(` llvm-type-list `)` `>`
141/// | `struct<` string-literal `>`
142/// | `struct<` string-literal `, opaque>`
143Type LLVMStructType::parse(AsmParser &parser) {
144 Location loc = parser.getEncodedSourceLoc(loc: parser.getCurrentLocation());
145
146 if (failed(Result: parser.parseLess()))
147 return LLVMStructType();
148
149 // If we are parsing a self-reference to a recursive struct, i.e. the parsing
150 // stack already contains a struct with the same identifier, bail out after
151 // the name.
152 std::string name;
153 bool isIdentified = succeeded(Result: parser.parseOptionalString(string: &name));
154 if (isIdentified) {
155 SMLoc greaterLoc = parser.getCurrentLocation();
156 if (succeeded(Result: parser.parseOptionalGreater())) {
157 auto type = LLVMStructType::getIdentifiedChecked(
158 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), name);
159 if (succeeded(Result: parser.tryStartCyclicParse(attrOrType: type))) {
160 parser.emitError(
161 loc: greaterLoc,
162 message: "struct without a body only allowed in a recursive struct");
163 return nullptr;
164 }
165
166 return type;
167 }
168 if (failed(Result: parser.parseComma()))
169 return LLVMStructType();
170 }
171
172 // Handle intentionally opaque structs.
173 SMLoc kwLoc = parser.getCurrentLocation();
174 if (succeeded(Result: parser.parseOptionalKeyword(keyword: "opaque"))) {
175 if (!isIdentified)
176 return parser.emitError(loc: kwLoc, message: "only identified structs can be opaque"),
177 LLVMStructType();
178 if (failed(Result: parser.parseGreater()))
179 return LLVMStructType();
180 auto type = LLVMStructType::getOpaqueChecked(
181 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), name);
182 if (!type.isOpaque()) {
183 parser.emitError(loc: kwLoc, message: "redeclaring defined struct as opaque");
184 return LLVMStructType();
185 }
186 return type;
187 }
188
189 FailureOr<AsmParser::CyclicParseReset> cyclicParse;
190 if (isIdentified) {
191 cyclicParse =
192 parser.tryStartCyclicParse(attrOrType: LLVMStructType::getIdentifiedChecked(
193 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), name));
194 if (failed(Result: cyclicParse)) {
195 parser.emitError(loc: kwLoc,
196 message: "identifier already used for an enclosing struct");
197 return nullptr;
198 }
199 }
200
201 // Check for packedness.
202 bool isPacked = succeeded(Result: parser.parseOptionalKeyword(keyword: "packed"));
203 if (failed(Result: parser.parseLParen()))
204 return LLVMStructType();
205
206 // Fast pass for structs with zero subtypes.
207 if (succeeded(Result: parser.parseOptionalRParen())) {
208 if (failed(Result: parser.parseGreater()))
209 return LLVMStructType();
210 if (!isIdentified)
211 return LLVMStructType::getLiteralChecked(emitError: [loc] { return emitError(loc); },
212 context: loc.getContext(), types: {}, isPacked);
213 auto type = LLVMStructType::getIdentifiedChecked(
214 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), name);
215 return trySetStructBody(type, subtypes: {}, isPacked, parser, subtypesLoc: kwLoc);
216 }
217
218 // Parse subtypes. For identified structs, put the identifier of the struct on
219 // the stack to support self-references in the recursive calls.
220 SmallVector<Type, 4> subtypes;
221 SMLoc subtypesLoc = parser.getCurrentLocation();
222 do {
223 Type type;
224 if (dispatchParse(parser, type))
225 return LLVMStructType();
226 subtypes.push_back(Elt: type);
227 } while (succeeded(Result: parser.parseOptionalComma()));
228
229 if (parser.parseRParen() || parser.parseGreater())
230 return LLVMStructType();
231
232 // Construct the struct with body.
233 if (!isIdentified)
234 return LLVMStructType::getLiteralChecked(
235 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), types: subtypes, isPacked);
236 auto type = LLVMStructType::getIdentifiedChecked(
237 emitError: [loc] { return emitError(loc); }, context: loc.getContext(), name);
238 return trySetStructBody(type, subtypes, isPacked, parser, subtypesLoc);
239}
240
241/// Parses a type appearing inside another LLVM dialect-compatible type. This
242/// will try to parse any type in full form (including types with the `!llvm`
243/// prefix), and on failure fall back to parsing the short-hand version of the
244/// LLVM dialect types without the `!llvm` prefix.
245static Type dispatchParse(AsmParser &parser, bool allowAny = true) {
246 SMLoc keyLoc = parser.getCurrentLocation();
247
248 // Try parsing any MLIR type.
249 Type type;
250 OptionalParseResult result = parser.parseOptionalType(result&: type);
251 if (result.has_value()) {
252 if (failed(Result: result.value()))
253 return nullptr;
254 if (!allowAny) {
255 parser.emitError(loc: keyLoc) << "unexpected type, expected keyword";
256 return nullptr;
257 }
258 return type;
259 }
260
261 // If no type found, fallback to the shorthand form.
262 StringRef key;
263 if (failed(Result: parser.parseKeyword(keyword: &key)))
264 return Type();
265
266 MLIRContext *ctx = parser.getContext();
267 return StringSwitch<function_ref<Type()>>(key)
268 .Case(S: "void", Value: [&] { return LLVMVoidType::get(ctx); })
269 .Case(S: "ppc_fp128", Value: [&] { return LLVMPPCFP128Type::get(ctx); })
270 .Case(S: "label", Value: [&] { return LLVMLabelType::get(ctx); })
271 .Case(S: "metadata", Value: [&] { return LLVMMetadataType::get(ctx); })
272 .Case(S: "func", Value: [&] { return LLVMFunctionType::parse(odsParser&: parser); })
273 .Case(S: "ptr", Value: [&] { return LLVMPointerType::parse(odsParser&: parser); })
274 .Case(S: "array", Value: [&] { return LLVMArrayType::parse(odsParser&: parser); })
275 .Case(S: "byte", Value: [&] { return LLVMByteType::parse(odsParser&: parser); })
276 .Case(S: "struct", Value: [&] { return LLVMStructType::parse(parser); })
277 .Case(S: "target", Value: [&] { return LLVMTargetExtType::parse(odsParser&: parser); })
278 .Case(S: "x86_amx", Value: [&] { return LLVMX86AMXType::get(ctx); })
279 .Default(Value: [&] {
280 parser.emitError(loc: keyLoc) << "unknown LLVM type: " << key;
281 return Type();
282 })();
283}
284
285/// Helper to use in parse lists.
286static ParseResult dispatchParse(AsmParser &parser, Type &type) {
287 type = dispatchParse(parser);
288 return success(IsSuccess: type != nullptr);
289}
290
291/// Parses one of the LLVM dialect types.
292Type mlir::LLVM::detail::parseType(DialectAsmParser &parser) {
293 SMLoc loc = parser.getCurrentLocation();
294 Type type = dispatchParse(parser, /*allowAny=*/false);
295 if (!type)
296 return type;
297 if (!isCompatibleOuterType(type)) {
298 parser.emitError(loc) << "unexpected type, expected keyword";
299 return nullptr;
300 }
301 return type;
302}
303
304ParseResult LLVM::parsePrettyLLVMType(AsmParser &p, Type &type) {
305 return dispatchParse(parser&: p, type);
306}
307
308void LLVM::printPrettyLLVMType(AsmPrinter &p, Type type) {
309 return dispatchPrint(printer&: p, type);
310}
311