From 898e7b2d2430543202e08402418ef8da63e8abd0 Mon Sep 17 00:00:00 2001 From: Anton Korobeynikov Date: Wed, 22 Jan 2025 01:12:42 -0800 Subject: [PATCH] Add special types: error, unknown and dontcare Signed-off-by: Anton Korobeynikov --- include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h | 4 +++ include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td | 6 ++--- include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td | 2 +- include/p4mlir/Dialect/P4HIR/P4HIR_Types.h | 4 +++ include/p4mlir/Dialect/P4HIR/P4HIR_Types.td | 29 ++++++++++++++------- test/Dialect/P4HIR/types.mlir | 10 +++++++ 6 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 test/Dialect/P4HIR/types.mlir diff --git a/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h b/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h index ced757e..5297d1c 100644 --- a/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h +++ b/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h @@ -1,6 +1,10 @@ #ifndef P4MLIR_DIALECT_P4HIR_P4HIR_ATTRS_H #define P4MLIR_DIALECT_P4HIR_P4HIR_ATTRS_H +// We explicitly do not use push / pop for diagnostic in +// order to propagate pragma further on +#pragma GCC diagnostic ignored "-Wunused-parameter" + #include "mlir/IR/BuiltinAttributes.h" #include "p4mlir/Dialect/P4HIR/P4HIR_Types.h" diff --git a/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td b/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td index f036eb9..0b4459d 100644 --- a/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td +++ b/include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td @@ -15,7 +15,7 @@ class P4HIR_Attr traits = []> // BoolAttr //===----------------------------------------------------------------------===// -def P4_BoolAttr : P4HIR_Attr<"Bool", "bool", [TypedAttrInterface]> { +def P4HIR_BoolAttr : P4HIR_Attr<"Bool", "bool", [TypedAttrInterface]> { let summary = "Represent true/false for !p4hir.bool types"; let description = [{ The BoolAttr represents a 'true' or 'false' value. @@ -30,10 +30,10 @@ def P4_BoolAttr : P4HIR_Attr<"Bool", "bool", [TypedAttrInterface]> { } //===----------------------------------------------------------------------===// -// IntegerAttr +// IntAttr //===----------------------------------------------------------------------===// -def IntAttr : P4HIR_Attr<"Int", "int", [TypedAttrInterface]> { +def P4HIR_IntAttr : P4HIR_Attr<"Int", "int", [TypedAttrInterface]> { let summary = "An Attribute containing a integer value"; let description = [{ An integer attribute is a literal attribute that represents an integral diff --git a/include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td b/include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td index 7b96039..2f745e1 100644 --- a/include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td +++ b/include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td @@ -21,7 +21,7 @@ class P4HIR_Op traits = []> : // P4HIR operation definitions. //===----------------------------------------------------------------------===// -def P4HIR_ConstOp : P4HIR_Op<"const", +def ConstOp : P4HIR_Op<"const", [ConstantLike, Pure, AllTypesMatch<["value", "res"]>]> { let summary = "Defines a P4 constant"; let description = [{ diff --git a/include/p4mlir/Dialect/P4HIR/P4HIR_Types.h b/include/p4mlir/Dialect/P4HIR/P4HIR_Types.h index 88174e8..5ef1197 100644 --- a/include/p4mlir/Dialect/P4HIR/P4HIR_Types.h +++ b/include/p4mlir/Dialect/P4HIR/P4HIR_Types.h @@ -1,6 +1,10 @@ #ifndef P4MLIR_DIALECT_P4HIR_P4HIR_TYPES_H #define P4MLIR_DIALECT_P4HIR_P4HIR_TYPES_H +// We explicitly do not use push / pop for diagnostic in +// order to propagate pragma further on +#pragma GCC diagnostic ignored "-Wunused-parameter" + #include "mlir/IR/BuiltinTypes.h" #define GET_TYPEDEF_CLASSES diff --git a/include/p4mlir/Dialect/P4HIR/P4HIR_Types.td b/include/p4mlir/Dialect/P4HIR/P4HIR_Types.td index f4fa866..c9cad63 100644 --- a/include/p4mlir/Dialect/P4HIR/P4HIR_Types.td +++ b/include/p4mlir/Dialect/P4HIR/P4HIR_Types.td @@ -21,7 +21,9 @@ class P4HIR_TypeNoMnemonic traits //===----------------------------------------------------------------------===// // Integer types: signed (int) and unsigned (bit) -def P4HIR_BitsType : P4HIR_TypeNoMnemonic<"Bits", "bits"> { +//===----------------------------------------------------------------------===// + +def BitsType : P4HIR_TypeNoMnemonic<"Bits", "bits"> { let summary = "fixed width integer type"; let description = [{ The `p4hir.bit` / `p4hir.int` type represents a P4 `bit` or `int` type. @@ -42,7 +44,7 @@ def P4HIR_BitsType : P4HIR_TypeNoMnemonic<"Bits", "bits"> { } // Unsigned integer type of a specific width (bits). -class P4HIR_Bit +class Bit : Type($_self)">, CPred<"::mlir::cast<::p4hir::BitsType>($_self).isUnsigned()">, @@ -55,7 +57,7 @@ class P4HIR_Bit } // Signed integer type of a specific width (int). -class P4HIR_Int +class Int : Type($_self)">, CPred<"::mlir::cast<::p4hir::IntType>($_self).isSigned()">, @@ -68,14 +70,11 @@ class P4HIR_Int } //===----------------------------------------------------------------------===// -// BoolType -// -// An alternative here is to represent bool as mlir::i1, but let's be more -// generic. -// +// BooleanType: An alternative here is to represent bool as mlir::i1, but +// let's be more generic, it could be lowered later on //===----------------------------------------------------------------------===// -def P4HIR_BooleanType : P4HIR_Type<"Bool", "bool"> { +def BooleanType : P4HIR_Type<"Bool", "bool"> { let summary = "boolean type"; let description = [{ `p4hir.bool` represents a P4 `bool` type. @@ -84,10 +83,20 @@ def P4HIR_BooleanType : P4HIR_Type<"Bool", "bool"> { let hasCustomAssemblyFormat = 1; } +//===----------------------------------------------------------------------===// +// "Singleton" types +//===----------------------------------------------------------------------===// + +def DontcareType : P4HIR_Type<"Dontcare", "dontcare"> {} +// FIXME: Add string for error here & declarations +def ErrorType : P4HIR_Type<"Error", "error"> {} +def UnknownType : P4HIR_Type<"Unknown", "unknown"> {} + //===----------------------------------------------------------------------===// // P4HIR type constraints. //===----------------------------------------------------------------------===// -def AnyP4Type : AnyTypeOf<[P4HIR_BitsType, P4HIR_BooleanType]> {} +def AnyP4Type : AnyTypeOf<[BitsType, BooleanType, + DontcareType, ErrorType, UnknownType]> {} #endif // P4MLIR_DIALECT_P4HIR_P4HIR_TYPES_TD diff --git a/test/Dialect/P4HIR/types.mlir b/test/Dialect/P4HIR/types.mlir new file mode 100644 index 0000000..6bc7217 --- /dev/null +++ b/test/Dialect/P4HIR/types.mlir @@ -0,0 +1,10 @@ +// RUN: p4mlir-opt %s | FileCheck %s + +!unknown = !p4hir.unknown +!error = !p4hir.error +!dontcare = !p4hir.dontcare + +// No need to check stuff. If it parses, it's fine. +// CHECK: module +module { +}