Skip to content

[mlir][LLVM][NFC] Implement print/parse for LLVMStructType #117930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def LLVMStructType : LLVMType<"LLVMStruct", "struct", [
bool isIdentified() const;

/// Checks if a struct is opaque.
bool isOpaque();
bool isOpaque() const;

/// Checks if a struct is initialized.
bool isInitialized();
Expand Down
25 changes: 11 additions & 14 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ static StringRef getTypeKeyword(Type type) {

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

printer << "<";
if (type.isIdentified()) {
cyclicPrint = printer.tryStartCyclicPrint(type);
if (isIdentified()) {
cyclicPrint = printer.tryStartCyclicPrint(*this);

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

if (type.isIdentified() && type.isOpaque()) {
if (isIdentified() && isOpaque()) {
printer << "opaque>";
return;
}

if (type.isPacked())
if (isPacked())
printer << "packed ";

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

llvm::TypeSwitch<Type>(type)
.Case<LLVMPointerType, LLVMArrayType, LLVMFixedVectorType,
LLVMScalableVectorType, LLVMFunctionType, LLVMTargetExtType>(
[&](auto type) { type.print(printer); })
.Case([&](LLVMStructType structType) {
printStructType(printer, structType);
});
LLVMScalableVectorType, LLVMFunctionType, LLVMTargetExtType,
LLVMStructType>([&](auto type) { type.print(printer); });
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -182,7 +179,7 @@ static LLVMStructType trySetStructBody(LLVMStructType type,
/// `(` llvm-type-list `)` `>`
/// | `struct<` string-literal `>`
/// | `struct<` string-literal `, opaque>`
static LLVMStructType parseStructType(AsmParser &parser) {
Type LLVMStructType::parse(AsmParser &parser) {
Location loc = parser.getEncodedSourceLoc(parser.getCurrentLocation());

if (failed(parser.parseLess()))
Expand Down Expand Up @@ -316,7 +313,7 @@ static Type dispatchParse(AsmParser &parser, bool allowAny = true) {
.Case("ptr", [&] { return LLVMPointerType::parse(parser); })
.Case("vec", [&] { return parseVectorType(parser); })
.Case("array", [&] { return LLVMArrayType::parse(parser); })
.Case("struct", [&] { return parseStructType(parser); })
.Case("struct", [&] { return LLVMStructType::parse(parser); })
.Case("target", [&] { return LLVMTargetExtType::parse(parser); })
.Case("x86_amx", [&] { return LLVMX86AMXType::get(ctx); })
.Default([&] {
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ LogicalResult LLVMStructType::setBody(ArrayRef<Type> types, bool isPacked) {

bool LLVMStructType::isPacked() const { return getImpl()->isPacked(); }
bool LLVMStructType::isIdentified() const { return getImpl()->isIdentified(); }
bool LLVMStructType::isOpaque() {
bool LLVMStructType::isOpaque() const {
return getImpl()->isIdentified() &&
(getImpl()->isOpaque() || !getImpl()->isInitialized());
}
Expand Down