Skip to content

Commit

Permalink
Add MLIR operation for unary operations
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Korobeynikov <[email protected]>
  • Loading branch information
asl committed Feb 10, 2025
1 parent e60a5a8 commit fccad9a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/p4mlir/Dialect/P4HIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ mlir_tablegen(P4HIR_Ops.h.inc -gen-op-decls)
mlir_tablegen(P4HIR_Ops.cpp.inc -gen-op-defs)
mlir_tablegen(P4HIR_Types.h.inc -gen-typedef-decls -typedefs-dialect=p4hir)
mlir_tablegen(P4HIR_Types.cpp.inc -gen-typedef-defs -typedefs-dialect=p4hir)

# Generate extra headers for custom enum and attrs.
mlir_tablegen(P4HIR_OpsEnums.h.inc -gen-enum-decls)
mlir_tablegen(P4HIR_OpsEnums.cpp.inc -gen-enum-defs)
mlir_tablegen(P4HIR_Attrs.h.inc -gen-attrdef-decls -attrdefs-dialect=p4hir)
mlir_tablegen(P4HIR_Attrs.cpp.inc -gen-attrdef-defs -attrdefs-dialect=p4hir)

Expand Down
4 changes: 2 additions & 2 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/MemorySlotInterfaces.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_OpsEnums.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_Types.h"

#define GET_OP_CLASSES
#include "p4mlir/Dialect/P4HIR/P4HIR_Ops.h.inc"

#endif // P4MLIR_DIALECT_P4HIR_P4HIR_OPS_H
#endif // P4MLIR_DIALECT_P4HIR_P4HIR_OPS_H
39 changes: 39 additions & 0 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,43 @@ def CastOp : P4HIR_Op<"cast",
// FIXME: add verifier.
}

def UnaryOpKind_Neg : I32EnumAttrCase<"Neg", 1, "minus">; // unary minus (-)
def UnaryOpKind_UPlus : I32EnumAttrCase<"UPlus", 2, "plus">; // unary plus (+)
def UnaryOpKind_Cmpl : I32EnumAttrCase<"Cmpl", 3, "cmpl">; // complement (~)
def UnaryOpKind_LNot : I32EnumAttrCase<"LNot", 4, "not">; // logical not

def UnaryOpKind : I32EnumAttr<"UnaryOpKind",
"unary operation (arith and logic) kind",
[UnaryOpKind_Neg, UnaryOpKind_UPlus,
UnaryOpKind_Cmpl, UnaryOpKind_LNot]> {
let cppNamespace = "::P4::P4MLIR::P4HIR";
}

def UnaryOp : P4HIR_Op<"unary", [Pure, SameOperandsAndResultType]> {
let summary = "Unary operations";
let description = [{
`p4hir.unary` performs the unary operation according to
the specified opcode kind: [minus, plus, cmpl, not].

It requires one input operand and has one result, both types
should be the same.

```mlir
%7 = p4hir.unary(minus, %1) : i32
%8 = p4hir.unary(not, %2) : i32
```
}];

let results = (outs AnyP4Type:$result);
// FIXME: constraint types for unary operations
// FIXME: check type for lnot
let arguments = (ins Arg<UnaryOpKind, "unary op kind">:$kind, Arg<AnyP4Type>:$input);

let assemblyFormat = [{
`(` $kind `,` $input `)` `:` type($input) attr-dict
}];

let hasVerifier = 1;
}

#endif // P4MLIR_DIALECT_P4HIR_P4HIR_OPS_TD
11 changes: 11 additions & 0 deletions include/p4mlir/Dialect/P4HIR/P4HIR_OpsEnums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef P4MLIR_DIALECT_P4HIR_P4HIR_OPSENUMS_H
#define P4MLIR_DIALECT_P4HIR_P4HIR_OPSENUMS_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_OpsEnums.h.inc"

#endif // P4MLIR_DIALECT_P4HIR_P4HIR_OPSENUMS_H
15 changes: 15 additions & 0 deletions lib/Dialect/P4HIR/P4HIR_Ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
#include "mlir/IR/DialectImplementation.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_Attrs.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_Dialect.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_OpsEnums.h"
#include "p4mlir/Dialect/P4HIR/P4HIR_Types.h"

#define GET_OP_CLASSES
#include "p4mlir/Dialect/P4HIR/P4HIR_Dialect.cpp.inc"
#include "p4mlir/Dialect/P4HIR/P4HIR_Ops.cpp.inc"
#include "p4mlir/Dialect/P4HIR/P4HIR_OpsEnums.cpp.inc"

using namespace mlir;
using namespace P4::P4MLIR;
Expand Down Expand Up @@ -47,6 +49,19 @@ LogicalResult P4HIR::ConstOp::verify() {
return checkConstantTypes(getOperation(), getType(), getValue());
}

LogicalResult P4HIR::UnaryOp::verify() {
switch (getKind()) {
case P4HIR::UnaryOpKind::Neg:
case P4HIR::UnaryOpKind::UPlus:
case P4HIR::UnaryOpKind::Cmpl:
case P4HIR::UnaryOpKind::LNot:
// Nothing to verify.
return success();
}

llvm_unreachable("Unknown UnaryOp kind?");
}

//===----------------------------------------------------------------------===//
// AllocaOp
//===----------------------------------------------------------------------===//
Expand Down
13 changes: 13 additions & 0 deletions test/Dialect/P4HIR/unop.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: p4mlir-opt %s | FileCheck %s

// No need to check stuff. If it parses, it's fine.
// CHECK: module
module {
%0 = p4hir.const #p4hir.int<-128> : !p4hir.int<8>
%1 = p4hir.const #p4hir.bool<false> : !p4hir.bool

%2 = p4hir.unary(minus, %0) : !p4hir.int<8>
%3 = p4hir.unary(plus, %0) : !p4hir.int<8>
%4 = p4hir.unary(cmpl, %0) : !p4hir.int<8>
%5 = p4hir.unary(not, %1) : !p4hir.bool
}

0 comments on commit fccad9a

Please sign in to comment.