[Mlir-commits] [mlir] 88b9d1a - [mlir][emitc] Add a pointer type

Marius Brehler llvmlistbot at llvm.org
Mon Feb 14 08:44:56 PST 2022


Author: Marius Brehler
Date: 2022-02-14T16:42:21Z
New Revision: 88b9d1a49aba54171804da355f00c8fe0483f428

URL: https://github.com/llvm/llvm-project/commit/88b9d1a49aba54171804da355f00c8fe0483f428
DIFF: https://github.com/llvm/llvm-project/commit/88b9d1a49aba54171804da355f00c8fe0483f428.diff

LOG: [mlir][emitc] Add a pointer type

Adds a pointer type to EmitC. The emission of pointers is so far only
possible by using the `emitc.opaque` type

Co-authored-by: Simon Camphausen <simon.camphausen at iml.fraunhofer.de>

Reviewed By: jpienaar

Differential Revision: https://reviews.llvm.org/D119337

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
    mlir/lib/Dialect/EmitC/IR/EmitC.cpp
    mlir/lib/Target/Cpp/TranslateToCpp.cpp
    mlir/test/Dialect/EmitC/types.mlir
    mlir/test/Target/Cpp/types.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
index d6fdd0fbf82df..499fe3178d73e 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitCTypes.td
@@ -35,7 +35,7 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
 
     ```mlir
     !emitc.opaque<"int">
-    !emitc.opaque<"float *">
+    !emitc.opaque<"mytype">
     !emitc.opaque<"std::vector<std::string>">
     ```
   }];
@@ -43,4 +43,31 @@ def EmitC_OpaqueType : EmitC_Type<"Opaque", "opaque"> {
   let parameters = (ins StringRefParameter<"the opaque value">:$value);
 }
 
+def EmitC_PointerType : EmitC_Type<"Pointer", "ptr"> {
+  let summary = "EmitC pointer type";
+
+  let description = [{
+    A pointer data type.
+
+    Example:
+
+    ```mlir
+    // Pointer emitted as `int32_t*`
+    !emitc.ptr<i32>
+    // Pointer emitted as `float*`
+    !emitc.ptr<f32>
+    // Pointer emitted as `int*`
+    !emitc.ptr<!emitc.opaque<"int">>
+    ```
+  }];
+
+  let parameters = (ins "Type":$pointee);
+  let builders = [
+    TypeBuilderWithInferredContext<(ins "Type":$pointee), [{
+      return $_get(pointee.getContext(), pointee);
+    }]>
+  ];
+  let assemblyFormat = "`<` qualified($pointee) `>`";
+}
+
 #endif // MLIR_DIALECT_EMITC_IR_EMITCTYPES

diff  --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index d654c05d290d6..6a101fc1eb96e 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -193,6 +193,10 @@ void emitc::OpaqueAttr::print(AsmPrinter &printer) const {
 #define GET_TYPEDEF_CLASSES
 #include "mlir/Dialect/EmitC/IR/EmitCTypes.cpp.inc"
 
+//===----------------------------------------------------------------------===//
+// OpaqueType
+//===----------------------------------------------------------------------===//
+
 Type emitc::OpaqueType::parse(AsmParser &parser) {
   if (parser.parseLess())
     return Type();

diff  --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 1215e4473f4ea..eabda3234395d 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -976,6 +976,12 @@ LogicalResult CppEmitter::emitType(Location loc, Type type) {
     os << oType.getValue();
     return success();
   }
+  if (auto pType = type.dyn_cast<emitc::PointerType>()) {
+    if (failed(emitType(loc, pType.getPointee())))
+      return failure();
+    os << "*";
+    return success();
+  }
   return emitError(loc, "cannot emit type ") << type;
 }
 

diff  --git a/mlir/test/Dialect/EmitC/types.mlir b/mlir/test/Dialect/EmitC/types.mlir
index 26ddf19e3953b..a8aebf058f890 100644
--- a/mlir/test/Dialect/EmitC/types.mlir
+++ b/mlir/test/Dialect/EmitC/types.mlir
@@ -17,3 +17,23 @@ func @opaque_types() {
 
   return
 }
+
+// CHECK-LABEL: func @pointer_types() {
+func @pointer_types() {
+  // CHECK-NEXT: !emitc.ptr<i32>
+  emitc.call "f"() {template_args = [!emitc<"ptr<i32>">]} : () -> ()
+  // CHECK-NEXT: !emitc.ptr<i64>
+  emitc.call "f"() {template_args = [!emitc.ptr<i64>]} : () -> ()
+  // CHECK-NEXT: !emitc.ptr<f32>
+  emitc.call "f"() {template_args = [!emitc<"ptr<f32>">]} : () -> ()
+  // CHECK-NEXT: !emitc.ptr<f64>
+  emitc.call "f"() {template_args = [!emitc.ptr<f64>]} : () -> ()
+  // CHECK-NEXT: !emitc.ptr<i32>
+  %0 = emitc.call "f"() : () -> (!emitc.ptr<i32>)
+  // CHECK-NEXT: (!emitc.ptr<i32>) -> !emitc.ptr<!emitc.ptr<i32>>
+  %1 = emitc.call "f"(%0) : (!emitc.ptr<i32>) -> (!emitc.ptr<!emitc.ptr<i32>>)
+  // CHECK-NEXT: !emitc.ptr<!emitc.opaque<"int">>
+  emitc.call "f"() {template_args = [!emitc.ptr<!emitc.opaque<"int">>]} : () -> ()
+
+  return
+}

diff  --git a/mlir/test/Target/Cpp/types.mlir b/mlir/test/Target/Cpp/types.mlir
index 70febba61140a..25e0fc4171286 100644
--- a/mlir/test/Target/Cpp/types.mlir
+++ b/mlir/test/Target/Cpp/types.mlir
@@ -1,7 +1,7 @@
 // RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
 
-// CHECK-LABEL: void opaque_template_args() {
-func @opaque_template_args() {
+// CHECK-LABEL: void opaque_types() {
+func @opaque_types() {
   // CHECK-NEXT: f<int>();
   emitc.call "f"() {template_args = [!emitc<"opaque<\"int\">">]} : () -> ()
   // CHECK-NEXT: f<byte>();
@@ -15,3 +15,23 @@ func @opaque_template_args() {
 
   return
 }
+
+// CHECK-LABEL: void ptr_types() {
+func @ptr_types() {
+  // CHECK-NEXT: f<int32_t*>();
+  emitc.call "f"() {template_args = [!emitc<"ptr<i32>">]} : () -> ()
+  // CHECK-NEXT: f<int64_t*>();
+  emitc.call "f"() {template_args = [!emitc.ptr<i64>]} : () -> ()
+  // CHECK-NEXT: f<float*>();
+  emitc.call "f"() {template_args = [!emitc<"ptr<f32>">]} : () -> ()
+  // CHECK-NEXT: f<double*>();
+  emitc.call "f"() {template_args = [!emitc.ptr<f64>]} : () -> ()
+  // CHECK-NEXT: int32_t* [[V0:[^ ]*]] = f();
+  %0 = emitc.call "f"() : () -> (!emitc.ptr<i32>)
+  // CHECK-NEXT: int32_t** [[V1:[^ ]*]] = f([[V0:[^ ]*]]);
+  %1 = emitc.call "f"(%0) : (!emitc.ptr<i32>) -> (!emitc.ptr<!emitc.ptr<i32>>)
+  // CHECK-NEXT: f<int*>();
+  emitc.call "f"() {template_args = [!emitc.ptr<!emitc.opaque<"int">>]} : () -> ()
+
+  return
+}


        


More information about the Mlir-commits mailing list