[Mlir-commits] [mlir] 83e9ef7 - [mlir][standalone] Extend sample with custom type

Marius Brehler llvmlistbot at llvm.org
Fri Feb 17 06:15:42 PST 2023


Author: Marius Brehler
Date: 2023-02-17T15:14:56+01:00
New Revision: 83e9ef7e519a2eac56609268a10e88b251d87e15

URL: https://github.com/llvm/llvm-project/commit/83e9ef7e519a2eac56609268a10e88b251d87e15
DIFF: https://github.com/llvm/llvm-project/commit/83e9ef7e519a2eac56609268a10e88b251d87e15.diff

LOG: [mlir][standalone] Extend sample with custom type

This extends the standalone example to illustrate how to structure the
files needed to create own types.

Reviewed By: jpienaar

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

Added: 
    mlir/examples/standalone/include/Standalone/StandaloneTypes.h
    mlir/examples/standalone/include/Standalone/StandaloneTypes.td
    mlir/examples/standalone/lib/Standalone/StandaloneTypes.cpp

Modified: 
    mlir/examples/standalone/include/Standalone/StandaloneDialect.td
    mlir/examples/standalone/include/Standalone/StandaloneOps.td
    mlir/examples/standalone/lib/Standalone/CMakeLists.txt
    mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
    mlir/examples/standalone/test/Standalone/dummy.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/examples/standalone/include/Standalone/StandaloneDialect.td b/mlir/examples/standalone/include/Standalone/StandaloneDialect.td
index 1b0882dfdee07..598a8dfe35f3b 100644
--- a/mlir/examples/standalone/include/Standalone/StandaloneDialect.td
+++ b/mlir/examples/standalone/include/Standalone/StandaloneDialect.td
@@ -25,6 +25,10 @@ def Standalone_Dialect : Dialect {
     }];
     let cppNamespace = "::mlir::standalone";
 
+    let useDefaultTypePrinterParser = 1;
+    let extraClassDeclaration = [{
+        void registerTypes();
+    }];
 }
 
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/standalone/include/Standalone/StandaloneOps.td b/mlir/examples/standalone/include/Standalone/StandaloneOps.td
index 6c9da3149cb39..9882a25558460 100644
--- a/mlir/examples/standalone/include/Standalone/StandaloneOps.td
+++ b/mlir/examples/standalone/include/Standalone/StandaloneOps.td
@@ -9,7 +9,7 @@
 #ifndef STANDALONE_OPS
 #define STANDALONE_OPS
 
-include "Standalone/StandaloneDialect.td"
+include "Standalone/StandaloneTypes.td"
 include "mlir/Interfaces/InferTypeOpInterface.td"
 include "mlir/Interfaces/SideEffectInterfaces.td"
 

diff  --git a/mlir/examples/standalone/include/Standalone/StandaloneTypes.h b/mlir/examples/standalone/include/Standalone/StandaloneTypes.h
new file mode 100644
index 0000000000000..681bb5e1a0b5c
--- /dev/null
+++ b/mlir/examples/standalone/include/Standalone/StandaloneTypes.h
@@ -0,0 +1,17 @@
+//===- StandaloneTypes.h - Standalone dialect types -------------*- C++ -*-===//
+//
+// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef STANDALONE_STANDALONETYPES_H
+#define STANDALONE_STANDALONETYPES_H
+
+#include "mlir/IR/BuiltinTypes.h"
+
+#define GET_TYPEDEF_CLASSES
+#include "Standalone/StandaloneOpsTypes.h.inc"
+
+#endif // STANDALONE_STANDALONETYPES_H

diff  --git a/mlir/examples/standalone/include/Standalone/StandaloneTypes.td b/mlir/examples/standalone/include/Standalone/StandaloneTypes.td
new file mode 100644
index 0000000000000..05b4c45da476c
--- /dev/null
+++ b/mlir/examples/standalone/include/Standalone/StandaloneTypes.td
@@ -0,0 +1,31 @@
+//===- StandaloneTypes.td - Standalone dialect types -------*- tablegen -*-===//
+//
+// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef STANDALONE_TYPES
+#define STANDALONE_TYPES
+
+include "mlir/IR/AttrTypeBase.td"
+include "Standalone/StandaloneDialect.td"
+
+//===----------------------------------------------------------------------===//
+// Standalone type definitions
+//===----------------------------------------------------------------------===//
+
+class Standalone_Type<string name, string typeMnemonic, list<Trait> traits = []>
+    : TypeDef<Standalone_Dialect, name, traits> {
+  let mnemonic = typeMnemonic;
+}
+
+def Standalone_CustomType : Standalone_Type<"Custom", "custom"> {
+    let summary = "Standalone custom type";
+    let description = "Custom type in standalone dialect";
+    let parameters = (ins StringRefParameter<"the custom value">:$value);
+    let assemblyFormat = "`<` $value `>`";
+}
+
+#endif // STANDALONE_TYPES

diff  --git a/mlir/examples/standalone/lib/Standalone/CMakeLists.txt b/mlir/examples/standalone/lib/Standalone/CMakeLists.txt
index eadc695d39f35..599c1c55c7926 100644
--- a/mlir/examples/standalone/lib/Standalone/CMakeLists.txt
+++ b/mlir/examples/standalone/lib/Standalone/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_mlir_dialect_library(MLIRStandalone
+        StandaloneTypes.cpp
         StandaloneDialect.cpp
         StandaloneOps.cpp
 

diff  --git a/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp b/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
index cdd9337fcf98b..1ea69f9059321 100644
--- a/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
+++ b/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
@@ -8,6 +8,7 @@
 
 #include "Standalone/StandaloneDialect.h"
 #include "Standalone/StandaloneOps.h"
+#include "Standalone/StandaloneTypes.h"
 
 using namespace mlir;
 using namespace mlir::standalone;
@@ -23,4 +24,5 @@ void StandaloneDialect::initialize() {
 #define GET_OP_LIST
 #include "Standalone/StandaloneOps.cpp.inc"
       >();
+  registerTypes();
 }

diff  --git a/mlir/examples/standalone/lib/Standalone/StandaloneTypes.cpp b/mlir/examples/standalone/lib/Standalone/StandaloneTypes.cpp
new file mode 100644
index 0000000000000..4ce4e00150f64
--- /dev/null
+++ b/mlir/examples/standalone/lib/Standalone/StandaloneTypes.cpp
@@ -0,0 +1,26 @@
+//===- StandaloneTypes.cpp - Standalone dialect types -----------*- C++ -*-===//
+//
+// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Standalone/StandaloneTypes.h"
+
+#include "Standalone/StandaloneDialect.h"
+#include "mlir/IR/Builders.h"
+#include "mlir/IR/DialectImplementation.h"
+#include "llvm/ADT/TypeSwitch.h"
+
+using namespace mlir::standalone;
+
+#define GET_TYPEDEF_CLASSES
+#include "Standalone/StandaloneOpsTypes.cpp.inc"
+
+void StandaloneDialect::registerTypes() {
+  addTypes<
+#define GET_TYPEDEF_LIST
+#include "Standalone/StandaloneOpsTypes.cpp.inc"
+      >();
+}

diff  --git a/mlir/examples/standalone/test/Standalone/dummy.mlir b/mlir/examples/standalone/test/Standalone/dummy.mlir
index cb688d2734433..60b169e51a7b4 100644
--- a/mlir/examples/standalone/test/Standalone/dummy.mlir
+++ b/mlir/examples/standalone/test/Standalone/dummy.mlir
@@ -8,4 +8,9 @@ module {
         %res = standalone.foo %0 : i32
         return
     }
+
+    // CHECK-LABEL: func @standalone_types(%arg0: !standalone.custom<"10">)
+    func.func @standalone_types(%arg0: !standalone.custom<"10">) {
+        return
+    }
 }


        


More information about the Mlir-commits mailing list