Skip to content

[MLIR][LLVM] Print LLVMStructType name using printEscapedString #139652

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 2 commits into from
May 13, 2025

Conversation

bcardosolopes
Copy link
Member

@bcardosolopes bcardosolopes commented May 13, 2025

LLVM struct type names need to be escaped when printed in order to allow interesting name choices.

@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-mlir-llvm

Author: Bruno Cardoso Lopes (bcardosolopes)

Changes

LLVM type names need to be escaped when printed in order to allow interesting name choices.


Full diff: https://github.com/llvm/llvm-project/pull/139652.diff

3 Files Affected:

  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp (+4-1)
  • (modified) mlir/test/Dialect/LLVMIR/roundtrip.mlir (+8)
  • (added) mlir/test/Target/LLVMIR/Import/struct.ll (+11)
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
index 319bb90d9b601..0acd5c7fd80e3 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
@@ -11,6 +11,7 @@
 #include "mlir/IR/DialectImplementation.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/TypeSwitch.h"
 
 using namespace mlir;
@@ -58,7 +59,9 @@ void LLVMStructType::print(AsmPrinter &printer) const {
   if (isIdentified()) {
     cyclicPrint = printer.tryStartCyclicPrint(*this);
 
-    printer << '"' << getName() << '"';
+    printer << '"';
+    llvm::printEscapedString(getName(), printer.getStream());
+    printer << '"';
     // 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)) {
diff --git a/mlir/test/Dialect/LLVMIR/roundtrip.mlir b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
index 2e6acc13d1627..a0273fb1e1bf4 100644
--- a/mlir/test/Dialect/LLVMIR/roundtrip.mlir
+++ b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
@@ -1045,3 +1045,11 @@ llvm.func @llvm.aarch64.neon.st3.v8i8.p0(vector<8xi8>, vector<8xi8>, vector<8xi8
 
 llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
 // CHECK: llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
+
+// CHECK-LABEL: llvm.func @escapedtypename
+llvm.func @escapedtypename() {
+  %0 = llvm.mlir.constant(1 : i32) : i32
+  // CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)>
+  %1 = llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
+  llvm.return
+}
diff --git a/mlir/test/Target/LLVMIR/Import/struct.ll b/mlir/test/Target/LLVMIR/Import/struct.ll
new file mode 100644
index 0000000000000..8ce845f648ede
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/struct.ll
@@ -0,0 +1,11 @@
+
+; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
+
+%"bucket<string, double, '\\b'>::Iterator" = type { ptr, i64, i64 }
+
+; CHECK-LABEL: llvm.func @g
+define void @g() {
+  %item.i = alloca %"bucket<string, double, '\\b'>::Iterator", align 8
+  ; CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
+  ret void
+}

@llvmbot
Copy link
Member

llvmbot commented May 13, 2025

@llvm/pr-subscribers-mlir

Author: Bruno Cardoso Lopes (bcardosolopes)

Changes

LLVM type names need to be escaped when printed in order to allow interesting name choices.


Full diff: https://github.com/llvm/llvm-project/pull/139652.diff

3 Files Affected:

  • (modified) mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp (+4-1)
  • (modified) mlir/test/Dialect/LLVMIR/roundtrip.mlir (+8)
  • (added) mlir/test/Target/LLVMIR/Import/struct.ll (+11)
diff --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
index 319bb90d9b601..0acd5c7fd80e3 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypeSyntax.cpp
@@ -11,6 +11,7 @@
 #include "mlir/IR/DialectImplementation.h"
 #include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/TypeSwitch.h"
 
 using namespace mlir;
@@ -58,7 +59,9 @@ void LLVMStructType::print(AsmPrinter &printer) const {
   if (isIdentified()) {
     cyclicPrint = printer.tryStartCyclicPrint(*this);
 
-    printer << '"' << getName() << '"';
+    printer << '"';
+    llvm::printEscapedString(getName(), printer.getStream());
+    printer << '"';
     // 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)) {
diff --git a/mlir/test/Dialect/LLVMIR/roundtrip.mlir b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
index 2e6acc13d1627..a0273fb1e1bf4 100644
--- a/mlir/test/Dialect/LLVMIR/roundtrip.mlir
+++ b/mlir/test/Dialect/LLVMIR/roundtrip.mlir
@@ -1045,3 +1045,11 @@ llvm.func @llvm.aarch64.neon.st3.v8i8.p0(vector<8xi8>, vector<8xi8>, vector<8xi8
 
 llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
 // CHECK: llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
+
+// CHECK-LABEL: llvm.func @escapedtypename
+llvm.func @escapedtypename() {
+  %0 = llvm.mlir.constant(1 : i32) : i32
+  // CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)>
+  %1 = llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
+  llvm.return
+}
diff --git a/mlir/test/Target/LLVMIR/Import/struct.ll b/mlir/test/Target/LLVMIR/Import/struct.ll
new file mode 100644
index 0000000000000..8ce845f648ede
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/Import/struct.ll
@@ -0,0 +1,11 @@
+
+; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
+
+%"bucket<string, double, '\\b'>::Iterator" = type { ptr, i64, i64 }
+
+; CHECK-LABEL: llvm.func @g
+define void @g() {
+  %item.i = alloca %"bucket<string, double, '\\b'>::Iterator", align 8
+  ; CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
+  ret void
+}

Copy link
Contributor

@gysit gysit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing.

LGTM modulo nit.

Copy link
Contributor

@Dinistro Dinistro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@bcardosolopes bcardosolopes merged commit 61272b5 into llvm:main May 13, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants