[mlir] Fix printing of EmitC attrs/types with escape characters

Attributes and types were not escaped when printing.

Reviewed By: jpienaar, marbre

Differential Revision: https://reviews.llvm.org/D109143
This commit is contained in:
Simon Camphausen 2021-09-15 19:54:57 +02:00 committed by Marius Brehler
parent 96ec0ff2b7
commit 1b79efdc72
5 changed files with 52 additions and 7 deletions

View File

@ -9,6 +9,7 @@
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
@ -201,7 +202,9 @@ void EmitCDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
}
void emitc::OpaqueAttr::print(DialectAsmPrinter &printer) const {
printer << "opaque<\"" << getValue() << "\">";
printer << "opaque<\"";
llvm::printEscapedString(getValue(), printer.getStream());
printer << "\">";
}
//===----------------------------------------------------------------------===//
@ -245,5 +248,7 @@ void EmitCDialect::printType(Type type, DialectAsmPrinter &os) const {
}
void emitc::OpaqueType::print(DialectAsmPrinter &printer) const {
printer << "opaque<\"" << getValue() << "\">";
printer << "opaque<\"";
llvm::printEscapedString(getValue(), printer.getStream());
printer << "\">";
}

View File

@ -0,0 +1,12 @@
// RUN: mlir-opt -verify-diagnostics %s | FileCheck %s
// check parser
// RUN: mlir-opt -verify-diagnostics %s | mlir-opt -verify-diagnostics | FileCheck %s
// CHECK-LABEL: func @opaque_attrs() {
func @opaque_attrs() {
// CHECK-NEXT: #emitc.opaque<"attr">
emitc.call "f"() {args = [#emitc.opaque<"attr">]} : () -> ()
// CHECK-NEXT: #emitc.opaque<"\22quoted_attr\22">
emitc.call "f"() {args = [#emitc.opaque<"\"quoted_attr\"">]} : () -> ()
return
}

View File

@ -5,14 +5,15 @@
// CHECK-LABEL: func @opaque_types() {
func @opaque_types() {
// CHECK-NEXT: !emitc.opaque<"int">
emitc.call "f"() {args = [!emitc<"opaque<\"int\">">]} : () -> ()
emitc.call "f"() {template_args = [!emitc<"opaque<\"int\">">]} : () -> ()
// CHECK-NEXT: !emitc.opaque<"byte">
emitc.call "f"() {args = [!emitc<"opaque<\"byte\">">]} : () -> ()
emitc.call "f"() {template_args = [!emitc<"opaque<\"byte\">">]} : () -> ()
// CHECK-NEXT: !emitc.opaque<"unsigned">
emitc.call "f"() {args = [!emitc<"opaque<\"unsigned\">">]} : () -> ()
emitc.call "f"() {template_args = [!emitc<"opaque<\"unsigned\">">]} : () -> ()
// CHECK-NEXT: !emitc.opaque<"status_t">
emitc.call "f"() {args = [!emitc<"opaque<\"status_t\">">]} : () -> ()
emitc.call "f"() {template_args = [!emitc<"opaque<\"status_t\">">]} : () -> ()
// CHECK-NEXT: !emitc.opaque<"std::vector<std::string>">
emitc.call "f"() {args = [!emitc.opaque<"std::vector<std::string>">]} : () -> ()
emitc.call "f"() {template_args = [!emitc.opaque<"std::vector<std::string>">]} : () -> ()
return
}

View File

@ -0,0 +1,10 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
// CHECK-LABEL: void opaque_attrs() {
func @opaque_attrs() {
// CHECK-NEXT: f(OPAQUE_ENUM_VALUE);
emitc.call "f"() {args = [#emitc.opaque<"OPAQUE_ENUM_VALUE">]} : () -> ()
// CHECK-NEXT: f("some string");
emitc.call "f"() {args = [#emitc.opaque<"\"some string\"">]} : () -> ()
return
}

View File

@ -0,0 +1,17 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
// CHECK-LABEL: void opaque_template_args() {
func @opaque_template_args() {
// CHECK-NEXT: f<int>();
emitc.call "f"() {template_args = [!emitc<"opaque<\"int\">">]} : () -> ()
// CHECK-NEXT: f<byte>();
emitc.call "f"() {template_args = [!emitc<"opaque<\"byte\">">]} : () -> ()
// CHECK-NEXT: f<unsigned>();
emitc.call "f"() {template_args = [!emitc<"opaque<\"unsigned\">">]} : () -> ()
// CHECK-NEXT: f<status_t>();
emitc.call "f"() {template_args = [!emitc<"opaque<\"status_t\">">]} : () -> ()
// CHECK-NEXT: f<std::vector<std::string>>();
emitc.call "f"() {template_args = [!emitc.opaque<"std::vector<std::string>">]} : () -> ()
return
}