Add support for a NoneType.
authorRiver Riddle <riverriddle@google.com>
Sun, 28 Apr 2019 01:35:04 +0000 (18:35 -0700)
committerMehdi Amini <joker.eph@gmail.com>
Mon, 6 May 2019 15:19:20 +0000 (08:19 -0700)
      none-type ::= `none`

    The `none` type is a unit type, i.e. a type with exactly one possible value, where its value does not have a defined dynamic representation.

--

PiperOrigin-RevId: 245599248

mlir/g3doc/LangRef.md
mlir/include/mlir/IR/Builders.h
mlir/include/mlir/IR/StandardTypes.h
mlir/include/mlir/Support/LLVM.h
mlir/lib/IR/AsmPrinter.cpp
mlir/lib/IR/Builders.cpp
mlir/lib/IR/MLIRContext.cpp
mlir/lib/Parser/Parser.cpp
mlir/lib/Parser/TokenKinds.def
mlir/test/IR/parser.mlir

index 542b2ef..84a1089 100644 (file)
@@ -513,6 +513,7 @@ non-function-type ::= integer-type
                     | type-alias
                     | complex-type
                     | tuple-type
+                    | none-type
 
 type-list-no-parens ::=  type (`,` type)*
 type-list-parens ::= `(` `)`
@@ -911,6 +912,17 @@ tuple<f32>
 tuple<i32, f32, tensor<i1>, i5>
 ```
 
+#### None Type
+
+Syntax:
+
+``` {.ebnf}
+none-type ::= `none`
+```
+
+The `none` type is a unit type, i.e. a type with exactly one possible value,
+where its value does not have a defined dynamic representation.
+
 ## Attributes
 
 Syntax:
index a2bfcf2..1ee6c48 100644 (file)
@@ -38,6 +38,7 @@ class VectorType;
 class RankedTensorType;
 class UnrankedTensorType;
 class TupleType;
+class NoneType;
 class BoolAttr;
 class IntegerAttr;
 class FloatAttr;
@@ -90,6 +91,7 @@ public:
   RankedTensorType getTensorType(ArrayRef<int64_t> shape, Type elementType);
   UnrankedTensorType getTensorType(Type elementType);
   TupleType getTupleType(ArrayRef<Type> elementTypes);
+  NoneType getNoneType();
 
   /// Get or construct an instance of the type 'ty' with provided arguments.
   template <typename Ty, typename... Args> Ty getType(Args... args) {
index 97062ab..849c6d2 100644 (file)
@@ -67,6 +67,7 @@ enum Kind {
   MemRef,
   Complex,
   Tuple,
+  None,
 };
 
 } // namespace StandardTypes
@@ -492,6 +493,19 @@ public:
   static bool kindof(unsigned kind) { return kind == StandardTypes::Tuple; }
 };
 
+/// NoneType is a unit type, i.e. a type with exactly one possible value, where
+/// its value does not have a defined dynamic representation.
+class NoneType : public Type::TypeBase<NoneType, Type> {
+public:
+  using Base::Base;
+
+  /// Get or create a NoneType instance.
+  static NoneType get(MLIRContext *context) {
+    return Base::get(context, StandardTypes::None);
+  }
+
+  static bool kindof(unsigned kind) { return kind == StandardTypes::None; }
+};
 } // end namespace mlir
 
 #endif // MLIR_IR_STANDARDTYPES_H
index 2f60d59..031dceb 100644 (file)
@@ -92,7 +92,6 @@ using llvm::Twine;
 // Other common classes.
 using llvm::APFloat;
 using llvm::APInt;
-using llvm::NoneType;
 using llvm::raw_ostream;
 } // namespace mlir
 
index 8b1410e..504bf68 100644 (file)
@@ -909,6 +909,9 @@ void ModulePrinter::printType(Type type) {
     os << '>';
     return;
   }
+  case StandardTypes::None:
+    os << "none";
+    return;
   }
 }
 
index d999b14..af066ba 100644 (file)
@@ -101,6 +101,8 @@ TupleType Builder::getTupleType(ArrayRef<Type> elementTypes) {
   return TupleType::get(elementTypes, context);
 }
 
+NoneType Builder::getNoneType() { return NoneType::get(context); }
+
 //===----------------------------------------------------------------------===//
 // Attributes.
 //===----------------------------------------------------------------------===//
index 29ac989..7b88414 100644 (file)
@@ -44,7 +44,9 @@
 
 using namespace mlir;
 using namespace mlir::detail;
-using namespace llvm;
+
+using llvm::hash_combine;
+using llvm::hash_combine_range;
 
 /// A utility function to safely get or create a uniqued instance within the
 /// given set container.
@@ -135,7 +137,7 @@ struct BuiltinDialect : public Dialect {
   BuiltinDialect(MLIRContext *context) : Dialect(/*name=*/"", context) {
     addTypes<FunctionType, OpaqueType, FloatType, IndexType, IntegerType,
              VectorType, RankedTensorType, UnrankedTensorType, MemRefType,
-             ComplexType, TupleType>();
+             ComplexType, TupleType, NoneType>();
   }
 };
 
@@ -426,7 +428,7 @@ public:
 
   /// This is a mapping from operation name to AbstractOperation for registered
   /// operations.
-  StringMap<AbstractOperation> registeredOperations;
+  llvm::StringMap<AbstractOperation> registeredOperations;
 
   /// This is a mapping from type identifier to Dialect for registered types.
   DenseMap<const TypeID *, Dialect *> registeredTypes;
@@ -499,7 +501,7 @@ public:
   BoolAttributeStorage *boolAttrs[2] = {nullptr};
   DenseSet<IntegerAttributeStorage *, IntegerAttrKeyInfo> integerAttrs;
   DenseSet<FloatAttributeStorage *, FloatAttrKeyInfo> floatAttrs;
-  StringMap<StringAttributeStorage *> stringAttrs;
+  llvm::StringMap<StringAttributeStorage *> stringAttrs;
   using ArrayAttrSet = DenseSet<ArrayAttributeStorage *, ArrayAttrKeyInfo>;
   ArrayAttrSet arrayAttrs;
   DenseMap<AffineMap, AffineMapAttributeStorage *> affineMapAttrs;
@@ -670,7 +672,7 @@ std::vector<AbstractOperation *> MLIRContext::getRegisteredOperations() {
     // We just have the operations in a non-deterministic hash table order. Dump
     // into a temporary array, then sort it by operation name to get a stable
     // ordering.
-    StringMap<AbstractOperation> &registeredOps =
+    llvm::StringMap<AbstractOperation> &registeredOps =
         getImpl().registeredOperations;
 
     opsToSort.reserve(registeredOps.size());
@@ -822,7 +824,7 @@ Location FusedLoc::get(ArrayRef<Location> locs, MLIRContext *context) {
 Location FusedLoc::get(ArrayRef<Location> locs, Attribute metadata,
                        MLIRContext *context) {
   // Unique the set of locations to be fused.
-  SmallSetVector<Location, 4> decomposedLocs;
+  llvm::SmallSetVector<Location, 4> decomposedLocs;
   for (auto loc : locs) {
     // If the location is a fused location we decompose it if it has no
     // metadata or the metadata is the same as the top level metadata.
index 6dc6550..5d6dad2 100644 (file)
@@ -324,9 +324,11 @@ ParseResult Parser::parseCommaSeparatedListUntil(
 ///                       | memref-type
 ///                       | complex-type
 ///                       | tuple-type
+///                       | none-type
 ///
 ///   index-type ::= `index`
 ///   float-type ::= `f16` | `bf16` | `f32` | `f64`
+///   none-type ::= `none`
 ///
 Type Parser::parseNonFunctionType() {
   switch (getToken().getKind()) {
@@ -371,6 +373,11 @@ Type Parser::parseNonFunctionType() {
     consumeToken(Token::kw_index);
     return builder.getIndexType();
 
+  // none-type
+  case Token::kw_none:
+    consumeToken(Token::kw_none);
+    return builder.getNoneType();
+
   // extended type
   case Token::exclamation_identifier:
     return parseExtendedType();
index 52a7fdc..98ab481 100644 (file)
@@ -108,6 +108,7 @@ TOK_KEYWORD(max)
 TOK_KEYWORD(memref)
 TOK_KEYWORD(min)
 TOK_KEYWORD(mod)
+TOK_KEYWORD(none)
 TOK_KEYWORD(opaque)
 TOK_KEYWORD(size)
 TOK_KEYWORD(sparse)
index 77a4889..e56ad97 100644 (file)
@@ -883,3 +883,10 @@ func @pretty_dialect_type() {
 
   return
 }
+
+// CHECK-LABEL: func @none_type
+func @none_type() {
+  // CHECK: "foo.unknown_op"() : () -> none
+  %none_val = "foo.unknown_op"() : () -> none
+  return
+}