Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gardening: #52

Open
wants to merge 4 commits into
base: mlir-mux
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 61 additions & 52 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define P4MLIR_DIALECT_P4HIR_P4HIR_OPS_TD

include "mlir/IR/OpBase.td"
include "mlir/IR/OpAsmInterface.td"
include "mlir/IR/BuiltinAttributeInterfaces.td"
include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/FunctionInterfaces.td"
Expand All @@ -25,7 +26,8 @@ class P4HIR_Op<string mnemonic, list<Trait> traits = []> :
//===----------------------------------------------------------------------===//

def ConstOp : P4HIR_Op<"const",
[ConstantLike, Pure, AllTypesMatch<["value", "res"]>]> {
[ConstantLike, Pure, AllTypesMatch<["value", "res"]>,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let summary = "Defines a P4 constant";
let description = [{
The `p4hir.const` operation turns a literal into an SSA value.
Expand All @@ -35,37 +37,42 @@ def ConstOp : P4HIR_Op<"const",
Example:

```mlir
%0 = p4hir.const p4hir.int<-128> : !p4hir.int<8>
%0 = p4hir.const #p4hir.int<-128> : !p4hir.int<8>
```
}];

// The constant operation takes an attribute as the only input.
let arguments = (ins TypedAttrInterface:$value);
// The constant operation takes a constant value (via typed attributes) and
// optional name.

let arguments = (ins TypedAttrInterface:$value, OptionalAttr<StrAttr>:$name);

// The constant operation returns a single value of AnyP4Type.
let results = (outs AnyP4Type:$res);

let assemblyFormat = "attr-dict $value";
let assemblyFormat = [{
(` ` `[` $name^ `]`)? $value attr-dict
}];

let hasVerifier = 1;
}
// Allow building a ConstantOp with no name, etc.
let builders = [
OpBuilder<(ins "mlir::TypedAttr":$value), [{
build($_builder, $_state, value.getType(), value, {});
}]>,
OpBuilder<(ins "mlir::TypedAttr":$value, "const llvm::Twine &":$name), [{
build($_builder, $_state,
value.getType(), value, $_builder.getStringAttr(name));
}]>

];

class AllocaTypesMatchWith<string summary, string lhsArg, string rhsArg,
string transform, string comparator = "std::equal_to<>()">
: PredOpTrait<summary, CPred<
comparator # "(" #
!subst("$_self", "$" # lhsArg # ".getType()", transform) #
", $" # rhsArg # ")">> {
string lhs = lhsArg;
string rhs = rhsArg;
string transformer = transform;
let hasVerifier = 1;
}

def AllocaOp : P4HIR_Op<"alloca", [
AllocaTypesMatchWith<"'objectType' matches referenced type of 'object'",
"ref", "objectType",
"mlir::cast<P4HIR::ReferenceType>($_self).getObjectType()">/*,
DeclareOpInterfaceMethods<PromotableAllocationOpInterface>*/]> {
def VariableOp : P4HIR_Op<"variable", [
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>,
/*,
DeclareOpInterfaceMethods<PromotableAllocationOpInterface>,
DeclareOpInterfaceMethods<DestructurableAllocationOpInterface>*/]> {
let summary = "Defines a scope-local variable";
let description = [{
The `p4hir.alloca` operation defines a scope-local variable.
Expand All @@ -80,53 +87,47 @@ def AllocaOp : P4HIR_Op<"alloca", [

```mlir
// bit<32> count = 3;
%0 = p4hir.alloca !p4hir.bit<32> ["count", init] : !p4hir.ref<!p4hir.bit<32>>
%0 = p4hir.variable ["count", init] : <!p4hir.bit<32>>
...
```
}];

let arguments = (ins
TypeAttr:$objectType,
StrAttr:$name,
OptionalAttr<StrAttr>:$name,
UnitAttr:$init
);

let results = (outs Res<ReferenceType, ""/*,
[MemAlloc<AutomaticAllocationScopeResource>]*/>:$ref);

let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "mlir::Type":$ref, "mlir::Type":$objectType, "const llvm::Twine &":$name)>,
];

let assemblyFormat = [{
$objectType ` `
`[` $name
` ` `[` $name
(`,` `init` $init^)?
`]`
attr-dict `:` qualified(type($ref))
attr-dict `:` type($ref)
}];

let hasVerifier = 0;
}

def LoadOp : P4HIR_Op<"load", [
def ReadOp : P4HIR_Op<"read", [
TypesMatchWith<"type of 'result' matches object type of 'ref'",
"ref", "result",
"mlir::cast<P4HIR::ReferenceType>($_self).getObjectType()">/*,
DeclareOpInterfaceMethods<PromotableMemOpInterface>*/]> {
"mlir::cast<P4HIR::ReferenceType>($_self).getObjectType()">,/*,
DeclareOpInterfaceMethods<PromotableMemOpInterface>*/
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {

let summary = "Load value from variable";
let summary = "Read value from variable";
let description = [{
`p4hir.load` reads a value given an object reference
`p4hir.read` reads a value given a variable reference
backed up by a `p4hir.ref` type.

Example:

```mlir

// Read from local variable, address in %0.
%1 = p4hir.load %0 : !p4hir.ref<!p4hir.bit<32>>, !p4hir.bit<32>
// Read from local variable, reference in %0.
%1 = p4hir.read %0 : <!p4hir.bit<32>>

```
}];
Expand All @@ -137,28 +138,28 @@ def LoadOp : P4HIR_Op<"load", [
let results = (outs LoadableP4Type:$result);

let assemblyFormat = [{
$ref `:` qualified(type($ref)) `,` type($result) attr-dict
$ref `:` type($ref) attr-dict
}];

// FIXME: add verifier.
}

def StoreOp : P4HIR_Op<"store", [
def AssignOp : P4HIR_Op<"assign", [
TypesMatchWith<"type of 'value' matches object type of 'addr'",
"ref", "value",
"mlir::cast<ReferenceType>($_self).getObjectType()">/*,
"mlir::cast<ReferenceType>($_self).getObjectType()">,/*,
DeclareOpInterfaceMethods<PromotableMemOpInterface>*/]> {

let summary = "Store value to variable";
let summary = "Assign value to variable";
let description = [{
`p4hir.store` stores a value (first operand) to the object referenced
`p4hir.assign` stores a value (first operand) to the object referenced
in the second operand.

Example:

```mlir
// Store a function argument to local variable, address in %0.
p4hir.store %arg0, %0 : !p4hir.bit<32>, !p4hir.ref<!p4hir.bit<32>>
p4hir.assign %arg0, %0 : <!p4hir.bit<32>>
```
}];

Expand All @@ -167,7 +168,7 @@ def StoreOp : P4HIR_Op<"store", [
[MemWrite]*/>:$ref);

let assemblyFormat = [{
$value `,` $ref attr-dict `:` type($value) `,` qualified(type($ref))
$value `,` $ref attr-dict `:` type($ref)
}];

// FIXME: add verifier.
Expand All @@ -178,7 +179,9 @@ def StoreOp : P4HIR_Op<"store", [
// TODO: Add CastOpInterface
def CastOp : P4HIR_Op<"cast",
[Pure/*,
DeclareOpInterfaceMethods<PromotableOpInterface>*/]> {
DeclareOpInterfaceMethods<PromotableOpInterface>*/,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>
]> {
let summary = "Conversion between values of different types";
let description = [{
Apply P4 usual conversions rules between values.
Expand Down Expand Up @@ -210,7 +213,9 @@ def UnaryOpKind : I32EnumAttr<"UnaryOpKind",
let cppNamespace = "::P4::P4MLIR::P4HIR";
}

def UnaryOp : P4HIR_Op<"unary", [Pure, SameOperandsAndResultType]> {
def UnaryOp : P4HIR_Op<"unary",
[Pure, SameOperandsAndResultType,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let summary = "Unary operations";
let description = [{
`p4hir.unary` performs the unary operation according to
Expand Down Expand Up @@ -258,7 +263,8 @@ def BinOpKind : I32EnumAttr<"BinOpKind",
}

def BinOp : P4HIR_Op<"binop", [Pure,
SameTypeOperands, SameOperandsAndResultType]> {
SameTypeOperands, SameOperandsAndResultType,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {

let summary = "Binary operations (arith and logic)";
let description = [{
Expand Down Expand Up @@ -303,7 +309,9 @@ def CmpOpKind : I32EnumAttr<
let cppNamespace = "::P4::P4MLIR::P4HIR";
}

def CmpOp : P4HIR_Op<"cmp", [Pure, SameTypeOperands]> {
def CmpOp : P4HIR_Op<"cmp",
[Pure, SameTypeOperands,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let summary = "Compare values two values and produce a boolean result";
let description = [{
`p4hir.cmp` compares two input operands of the same type and produces a
Expand Down Expand Up @@ -730,7 +738,8 @@ def FuncOp : P4HIR_Op<"func", [

def CallOp : P4HIR_Op<"call",
[NoRegionArguments, CallOpInterface,
DeclareOpInterfaceMethods<SymbolUserOpInterface>]> {
DeclareOpInterfaceMethods<SymbolUserOpInterface>,
DeclareOpInterfaceMethods<OpAsmOpInterface, ["getAsmResultNames"]>]> {
let summary = "call operation";
let description = [{
The `call` operation represents a direct call to a method (action, function, etc.) that
Expand Down Expand Up @@ -788,7 +797,7 @@ def CallOp : P4HIR_Op<"call",
operand_iterator arg_operand_end() { return operand_end(); }

/// Return the callee of this operation.
::mlir::CallInterfaceCallable getCallableForCallee() {
mlir::CallInterfaceCallable getCallableForCallee() {
return (*this)->getAttrOfType<mlir::SymbolRefAttr>("callee");
}

Expand Down
15 changes: 14 additions & 1 deletion include/p4mlir/Dialect/P4HIR/P4HIR_Types.td
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def BitsType : P4HIR_TypeNoMnemonic<"Bits", "bits"> {

static mlir::Type parse(mlir::AsmParser &parser, bool isSigned);
void print(mlir::AsmPrinter &printer) const;

std::string getAlias() const {
return (isSigned() ? 'i' : 'b') + std::to_string(getWidth()) + 'i';
};
}];
}

Expand Down Expand Up @@ -75,6 +79,11 @@ def InfIntType : P4HIR_Type<"InfInt", "infint"> {
let description = [{
`p4hir.infint` represents a P4 `int` type.
}];

let extraClassDeclaration = [{
llvm::StringRef getAlias() const { return "infint"; };
}];

}

//===----------------------------------------------------------------------===//
Expand All @@ -89,6 +98,10 @@ def BooleanType : P4HIR_Type<"Bool", "bool"> {
}];

let hasCustomAssemblyFormat = 1;
let extraClassDeclaration = [{
llvm::StringRef getAlias() const { return "bool"; };
}];

}

//===----------------------------------------------------------------------===//
Expand All @@ -106,7 +119,7 @@ def VoidType : P4HIR_Type<"Void", "void"> {
Represents absense of result of actions and methods, or `void` type for functions.
}];
let extraClassDeclaration = [{
std::string getAlias() const { return "void"; };
llvm::StringRef getAlias() const { return "void"; };
}];
}
//===----------------------------------------------------------------------===//
Expand Down
Loading