Skip to content

Commit

Permalink
Add special types: error, unknown and dontcare
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Korobeynikov <anton@korobeynikov.info>
  • Loading branch information
asl committed Jan 22, 2025
1 parent 8763f05 commit 898e7b2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
4 changes: 4 additions & 0 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.h
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
6 changes: 3 additions & 3 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Attrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class P4HIR_Attr<string name, string attrMnemonic, list<Trait> 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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class P4HIR_Op<string mnemonic, list<Trait> 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 = [{
Expand Down
4 changes: 4 additions & 0 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Types.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 19 additions & 10 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Types.td
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class P4HIR_TypeNoMnemonic<string name, string typeMnemonic, list<Trait> 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.
Expand All @@ -42,7 +44,7 @@ def P4HIR_BitsType : P4HIR_TypeNoMnemonic<"Bits", "bits"> {
}

// Unsigned integer type of a specific width (bits<width>).
class P4HIR_Bit<int width>
class Bit<int width>
: Type<And<[
CPred<"::mlir::isa<::p4hir::BitsType>($_self)">,
CPred<"::mlir::cast<::p4hir::BitsType>($_self).isUnsigned()">,
Expand All @@ -55,7 +57,7 @@ class P4HIR_Bit<int width>
}

// Signed integer type of a specific width (int<width>).
class P4HIR_Int<int width>
class Int<int width>
: Type<And<[
CPred<"::mlir::isa<::p4hir::IntType>($_self)">,
CPred<"::mlir::cast<::p4hir::IntType>($_self).isSigned()">,
Expand All @@ -68,14 +70,11 @@ class P4HIR_Int<int width>
}

//===----------------------------------------------------------------------===//
// 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.
Expand All @@ -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
10 changes: 10 additions & 0 deletions test/Dialect/P4HIR/types.mlir
Original file line number Diff line number Diff line change
@@ -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 {
}

0 comments on commit 898e7b2

Please sign in to comment.