diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp --- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp +++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp @@ -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 emitc::OpaqueAttr::print(DialectAsmPrinter &printer) const { - printer << "opaque<\"" << getValue() << "\">"; + printer << "opaque<\""; + llvm::printEscapedString(getValue(), printer.getStream()); + printer << "\">"; } //===----------------------------------------------------------------------===// @@ -245,5 +248,7 @@ } void emitc::OpaqueType::print(DialectAsmPrinter &printer) const { - printer << "opaque<\"" << getValue() << "\">"; + printer << "opaque<\""; + llvm::printEscapedString(getValue(), printer.getStream()); + printer << "\">"; } diff --git a/mlir/test/Dialect/EmitC/attrs.mlir b/mlir/test/Dialect/EmitC/attrs.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Dialect/EmitC/attrs.mlir @@ -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 +} diff --git a/mlir/test/Dialect/EmitC/types.mlir b/mlir/test/Dialect/EmitC/types.mlir --- a/mlir/test/Dialect/EmitC/types.mlir +++ b/mlir/test/Dialect/EmitC/types.mlir @@ -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"> - emitc.call "f"() {args = [!emitc.opaque<"std::vector">]} : () -> () + emitc.call "f"() {template_args = [!emitc.opaque<"std::vector">]} : () -> () + return } diff --git a/mlir/test/Target/Cpp/attrs.mlir b/mlir/test/Target/Cpp/attrs.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Target/Cpp/attrs.mlir @@ -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 +} diff --git a/mlir/test/Target/Cpp/types.mlir b/mlir/test/Target/Cpp/types.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Target/Cpp/types.mlir @@ -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(); + emitc.call "f"() {template_args = [!emitc<"opaque<\"int\">">]} : () -> () + // CHECK-NEXT: f(); + emitc.call "f"() {template_args = [!emitc<"opaque<\"byte\">">]} : () -> () + // CHECK-NEXT: f(); + emitc.call "f"() {template_args = [!emitc<"opaque<\"unsigned\">">]} : () -> () + // CHECK-NEXT: f(); + emitc.call "f"() {template_args = [!emitc<"opaque<\"status_t\">">]} : () -> () + // CHECK-NEXT: f>(); + emitc.call "f"() {template_args = [!emitc.opaque<"std::vector">]} : () -> () + + return +}