Skip to content

Commit 61272b5

Browse files
[MLIR][LLVM] Print LLVMStructType name using printEscapedString (llvm#139652)
LLVM struct type names need to be escaped when printed in order to allow interesting name choices.
1 parent e4d21ae commit 61272b5

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "mlir/IR/DialectImplementation.h"
1212
#include "llvm/ADT/ScopeExit.h"
1313
#include "llvm/ADT/SetVector.h"
14+
#include "llvm/ADT/StringExtras.h"
1415
#include "llvm/ADT/TypeSwitch.h"
1516

1617
using namespace mlir;
@@ -58,7 +59,9 @@ void LLVMStructType::print(AsmPrinter &printer) const {
5859
if (isIdentified()) {
5960
cyclicPrint = printer.tryStartCyclicPrint(*this);
6061

61-
printer << '"' << getName() << '"';
62+
printer << '"';
63+
llvm::printEscapedString(getName(), printer.getStream());
64+
printer << '"';
6265
// If we are printing a reference to one of the enclosing structs, just
6366
// print the name and stop to avoid infinitely long output.
6467
if (failed(cyclicPrint)) {

mlir/test/Dialect/LLVMIR/roundtrip.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,11 @@ llvm.func @llvm.aarch64.neon.st3.v8i8.p0(vector<8xi8>, vector<8xi8>, vector<8xi8
10451045

10461046
llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
10471047
// CHECK: llvm.mlir.global internal thread_local unnamed_addr @myglobal(-1 : i32) {addr_space = 0 : i32, alignment = 4 : i64, dso_local} : i32
1048+
1049+
// CHECK-LABEL: llvm.func @escapedtypename
1050+
llvm.func @escapedtypename() {
1051+
%0 = llvm.mlir.constant(1 : i32) : i32
1052+
// CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)>
1053+
%1 = llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
1054+
llvm.return
1055+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
2+
3+
%"bucket<string, double, '\\b'>::Iterator" = type { ptr, i64, i64 }
4+
5+
; CHECK-LABEL: llvm.func @g
6+
define void @g() {
7+
%item.i = alloca %"bucket<string, double, '\\b'>::Iterator", align 8
8+
; CHECK: llvm.alloca %0 x !llvm.struct<"bucket<string, double, '\\b'>::Iterator", (ptr, i64, i64)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
9+
ret void
10+
}

0 commit comments

Comments
 (0)