Skip to content

Commit 3327195

Browse files
authored
[mlir][LLVM][NFC] Implement print/parse for LLVMStructType (#117930)
The printing and parsing logic for struct types was still using ad-hoc functions instead of the more conventional `print` and `parse` methods whose declarations are automatically generated by TableGen. This PR effectively renames these functions and uses them directly as implementations for `print` and `parse` of `LLVMStructType`. This additionally fixes linking errors when users or auto generated code may call `print` and `parse` directly. Fixes #117927
1 parent 7173a7d commit 3327195

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def LLVMStructType : LLVMType<"LLVMStruct", "struct", [
233233
bool isIdentified() const;
234234

235235
/// Checks if a struct is opaque.
236-
bool isOpaque();
236+
bool isOpaque() const;
237237

238238
/// Checks if a struct is initialized.
239239
bool isInitialized();

mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ static StringRef getTypeKeyword(Type type) {
5353

5454
/// Prints a structure type. Keeps track of known struct names to handle self-
5555
/// or mutually-referring structs without falling into infinite recursion.
56-
static void printStructType(AsmPrinter &printer, LLVMStructType type) {
56+
void LLVMStructType::print(AsmPrinter &printer) const {
5757
FailureOr<AsmPrinter::CyclicPrintReset> cyclicPrint;
5858

5959
printer << "<";
60-
if (type.isIdentified()) {
61-
cyclicPrint = printer.tryStartCyclicPrint(type);
60+
if (isIdentified()) {
61+
cyclicPrint = printer.tryStartCyclicPrint(*this);
6262

63-
printer << '"' << type.getName() << '"';
63+
printer << '"' << getName() << '"';
6464
// If we are printing a reference to one of the enclosing structs, just
6565
// print the name and stop to avoid infinitely long output.
6666
if (failed(cyclicPrint)) {
@@ -70,17 +70,17 @@ static void printStructType(AsmPrinter &printer, LLVMStructType type) {
7070
printer << ", ";
7171
}
7272

73-
if (type.isIdentified() && type.isOpaque()) {
73+
if (isIdentified() && isOpaque()) {
7474
printer << "opaque>";
7575
return;
7676
}
7777

78-
if (type.isPacked())
78+
if (isPacked())
7979
printer << "packed ";
8080

8181
// Put the current type on stack to avoid infinite recursion.
8282
printer << '(';
83-
llvm::interleaveComma(type.getBody(), printer.getStream(),
83+
llvm::interleaveComma(getBody(), printer.getStream(),
8484
[&](Type subtype) { dispatchPrint(printer, subtype); });
8585
printer << ')';
8686
printer << '>';
@@ -105,11 +105,8 @@ void mlir::LLVM::detail::printType(Type type, AsmPrinter &printer) {
105105

106106
llvm::TypeSwitch<Type>(type)
107107
.Case<LLVMPointerType, LLVMArrayType, LLVMFixedVectorType,
108-
LLVMScalableVectorType, LLVMFunctionType, LLVMTargetExtType>(
109-
[&](auto type) { type.print(printer); })
110-
.Case([&](LLVMStructType structType) {
111-
printStructType(printer, structType);
112-
});
108+
LLVMScalableVectorType, LLVMFunctionType, LLVMTargetExtType,
109+
LLVMStructType>([&](auto type) { type.print(printer); });
113110
}
114111

115112
//===----------------------------------------------------------------------===//
@@ -182,7 +179,7 @@ static LLVMStructType trySetStructBody(LLVMStructType type,
182179
/// `(` llvm-type-list `)` `>`
183180
/// | `struct<` string-literal `>`
184181
/// | `struct<` string-literal `, opaque>`
185-
static LLVMStructType parseStructType(AsmParser &parser) {
182+
Type LLVMStructType::parse(AsmParser &parser) {
186183
Location loc = parser.getEncodedSourceLoc(parser.getCurrentLocation());
187184

188185
if (failed(parser.parseLess()))
@@ -316,7 +313,7 @@ static Type dispatchParse(AsmParser &parser, bool allowAny = true) {
316313
.Case("ptr", [&] { return LLVMPointerType::parse(parser); })
317314
.Case("vec", [&] { return parseVectorType(parser); })
318315
.Case("array", [&] { return LLVMArrayType::parse(parser); })
319-
.Case("struct", [&] { return parseStructType(parser); })
316+
.Case("struct", [&] { return LLVMStructType::parse(parser); })
320317
.Case("target", [&] { return LLVMTargetExtType::parse(parser); })
321318
.Case("x86_amx", [&] { return LLVMX86AMXType::get(ctx); })
322319
.Default([&] {

mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ LogicalResult LLVMStructType::setBody(ArrayRef<Type> types, bool isPacked) {
480480

481481
bool LLVMStructType::isPacked() const { return getImpl()->isPacked(); }
482482
bool LLVMStructType::isIdentified() const { return getImpl()->isIdentified(); }
483-
bool LLVMStructType::isOpaque() {
483+
bool LLVMStructType::isOpaque() const {
484484
return getImpl()->isIdentified() &&
485485
(getImpl()->isOpaque() || !getImpl()->isInitialized());
486486
}

0 commit comments

Comments
 (0)