Skip to content

Commit 78ffddf

Browse files
Add UnaryOp verifier checks
Signed-off-by: Lavanyampalli <lavanyampalli@gmail.com>
1 parent 09bc0ca commit 78ffddf

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/Dialect/P4HIR/P4HIR_Ops.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,19 @@ static P4HIR::BinOp getDefiningBinop(P4HIR::BinOpKind kind, mlir::Value val) {
290290
}
291291

292292
LogicalResult P4HIR::UnaryOp::verify() {
293+
auto type = getInput().getType();
294+
293295
switch (getKind()) {
294296
case P4HIR::UnaryOpKind::Neg:
295297
case P4HIR::UnaryOpKind::UPlus:
296298
case P4HIR::UnaryOpKind::Cmpl:
299+
if (!mlir::isa<P4HIR::BitsType>(type))
300+
return emitOpError("arithmetic and bitwise unary operations require bit or int type");
301+
return success();
302+
297303
case P4HIR::UnaryOpKind::LNot:
298-
// Nothing to verify.
304+
if (!mlir::isa<P4HIR::BoolType>(type))
305+
return emitOpError("logical not requires boolean type");
299306
return success();
300307
}
301308

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: not p4mlir-opt %s 2>&1 | FileCheck %s --check-prefix=ERR
2+
3+
module {
4+
%0 = p4hir.const #p4hir.int<1> : !p4hir.int<8>
5+
%1 = p4hir.unary(not, %0) : !p4hir.int<8>
6+
// ERR: 'p4hir.unary' op logical not requires boolean type
7+
}
8+
9+
module {
10+
%0 = p4hir.const #p4hir.bool<true> : !p4hir.bool
11+
%1 = p4hir.unary(minus, %0) : !p4hir.bool
12+
// ERR: 'p4hir.unary' op arithmetic and bitwise unary operations require bit or int type
13+
}

0 commit comments

Comments
 (0)