Skip to content

Commit 982a88c

Browse files
committed
verifier
Signed-off-by: Pietro Ghiglio <[email protected]>
1 parent 57c8164 commit 982a88c

File tree

8 files changed

+83
-11
lines changed

8 files changed

+83
-11
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef P4MLIR_DIALECT_BMv2IR_BMv2IR_OPINTERFACES_H
2+
#define P4MLIR_DIALECT_BMv2IR_BMv2IR_OPINTERFACES_H
3+
4+
#include "mlir/IR/OpDefinition.h"
5+
6+
#include "p4mlir/Dialect/BMv2IR/BMv2IR_OpInterfaces.h.inc"
7+
8+
#endif // P4MLIR_DIALECT_BMv2IR_BMv2IR_OPINTERFACES_H
9+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef BMv2IR_OP_INTERFACES_TD
2+
#define BMv2IR_OP_INTERFACES_TD
3+
4+
include "mlir/IR/OpBase.td"
5+
include "mlir/IR/Interfaces.td"
6+
7+
def AllowedTransitionKey : OpInterface<"AllowedTransitionKey"> {
8+
let description = [{
9+
Common interfaces for operations that could be used as transition keys
10+
}];
11+
let methods = [];
12+
13+
14+
let cppNamespace = "::P4::P4MLIR::BMv2IR";
15+
}
16+
17+
def AllowedParserOp : OpInterface<"AllowedParserOp"> {
18+
let description = [{
19+
Common interfaces for operations that could be used as parser ops
20+
}];
21+
let methods = [];
22+
23+
24+
let cppNamespace = "::P4::P4MLIR::BMv2IR";
25+
}
26+
27+
#endif

include/p4mlir/Dialect/BMv2IR/BMv2IR_Ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "mlir/IR/OpDefinition.h"
1212
#include "mlir/IR/SymbolTable.h"
1313
#include "p4mlir/Dialect/BMv2IR/BMv2IR_Attrs.h"
14+
#include "p4mlir/Dialect/BMv2IR/BMv2IR_OpInterfaces.h"
1415
#include "p4mlir/Dialect/BMv2IR/BMv2IR_Types.h"
15-
1616
#define GET_OP_CLASSES
1717
#include "p4mlir/Dialect/BMv2IR/BMv2IR_Ops.h.inc"
1818

include/p4mlir/Dialect/BMv2IR/BMv2IR_Ops.td

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include "mlir/IR/SymbolInterfaces.td"
88
include "p4mlir/Dialect/BMv2IR/BMv2IR_Dialect.td"
99
include "p4mlir/Dialect/BMv2IR/BMv2IR_Types.td"
1010
include "p4mlir/Dialect/BMv2IR/BMv2IR_Attrs.td"
11+
include "p4mlir/Dialect/BMv2IR/BMv2IR_OpInterfaces.td"
1112

1213
//===----------------------------------------------------------------------===//
1314
// Base BMv2IR operation definition.
@@ -69,6 +70,8 @@ def BMv2IR_ParserStateOp : BMv2IR_Op<"state",
6970
`\n``transitions` $transitions
7071
`\n``parser_ops` $parser_ops attr-dict
7172
}];
73+
74+
let hasVerifier = 1;
7275
}
7376

7477
def BMv2IR_TransitionOp : BMv2IR_Op<"transition", [HasParent<"ParserStateOp">]> {
@@ -102,7 +105,7 @@ def BMv2IR_TransitionOp : BMv2IR_Op<"transition", [HasParent<"ParserStateOp">]>
102105
}
103106

104107

105-
def BMv2_ExtractOp : BMv2IR_Op<"extract", [HasParent<"ParserStateOp">]> {
108+
def BMv2_ExtractOp : BMv2IR_Op<"extract", [HasParent<"ParserStateOp">, AllowedParserOp]> {
106109
let summary = "Extract header operation";
107110
let description = [{
108111
Extracts the field of a header instance, header stack, or union stack element.
@@ -129,7 +132,7 @@ def BMv2_ExtractOp : BMv2IR_Op<"extract", [HasParent<"ParserStateOp">]> {
129132
// see https://github.com/p4lang/behavioral-model/blob/main/docs/JSON_format.md#the-type-value-object
130133
//===-------------------------------------------------------------------------------------------===//
131134

132-
def BMv2IR_FieldOp : BMv2IR_Op<"field"> {
135+
def BMv2IR_FieldOp : BMv2IR_Op<"field", [AllowedTransitionKey]> {
133136
let summary = "BMv2 header field access";
134137
let description = [{
135138
Represents access to a field within a header instance.
@@ -144,7 +147,7 @@ def BMv2IR_FieldOp : BMv2IR_Op<"field"> {
144147
let assemblyFormat = "`<` $headerInstance `,` $fieldMember `>` attr-dict";
145148
}
146149

147-
def BMv2_StackFieldOp : BMv2IR_Op<"stack_field"> {
150+
def BMv2_StackFieldOp : BMv2IR_Op<"stack_field", [AllowedTransitionKey]> {
148151
let summary = "BMv2 header stack field access";
149152
let description = [{
150153
Represents access to a field in the last valid header instance in a stack.
@@ -160,7 +163,7 @@ def BMv2_StackFieldOp : BMv2IR_Op<"stack_field"> {
160163
}
161164

162165

163-
def BMv2IR_LookaheadOp : BMv2IR_Op<"lookahead"> {
166+
def BMv2IR_LookaheadOp : BMv2IR_Op<"lookahead", [AllowedTransitionKey]> {
164167
let summary = "BMv2 parser lookahead";
165168
let description = [{
166169
Represents a lookahead operation in a parser.
@@ -175,7 +178,7 @@ def BMv2IR_LookaheadOp : BMv2IR_Op<"lookahead"> {
175178
let assemblyFormat = "`<` $bitOffset `,` $bitwidth `>` attr-dict";
176179
}
177180

178-
def BMv2_UnionStackFieldOp : BMv2IR_Op<"union_stack_field"> {
181+
def BMv2_UnionStackFieldOp : BMv2IR_Op<"union_stack_field", [AllowedTransitionKey]> {
179182
let summary = "BMv2 header union stack field access";
180183
let description = [{
181184
Represents access to a field in the last valid union instance in a stack.

include/p4mlir/Dialect/BMv2IR/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ mlir_tablegen(BMv2IR_EnumAttrs.h.inc -gen-enum-decls)
1414
mlir_tablegen(BMv2IR_EnumAttrs.cpp.inc -gen-enum-defs)
1515
add_public_tablegen_target(P4MLIR_BMv2IR_AttrIncGen)
1616
add_dependencies(mlir-headers P4MLIR_BMv2IR_AttrIncGen)
17+
18+
set(LLVM_TARGET_DEFINITIONS BMv2IR_OpInterfaces.td)
19+
mlir_tablegen(BMv2IR_OpInterfaces.h.inc -gen-op-interface-decls)
20+
mlir_tablegen(BMv2IR_OpInterfaces.cpp.inc -gen-op-interface-defs)
21+
add_public_tablegen_target(P4MLIR_BMv2IR_OpsInterfacesIncGen)
22+
add_dependencies(mlir-headers P4MLIR_BMv2IR_OpsInterfacesIncGen)

lib/Conversion/P4HIRToBMv2IR/P4HIRToBMv2IR.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct ParserStateOpConversionPattern : public OpConversionPattern<P4HIR::Parser
9797
op.walk([&](P4HIR::ParserTransitionSelectOp transitionSelectOp) {
9898
for (auto operand : transitionSelectOp.getArgs()) {
9999
auto transitionKey =
100-
insertTransitionKey(operand.getDefiningOp(), rewriter, &keysBlock);
100+
insertTransitionKey(operand.getDefiningOp(), rewriter, &keysBlock, eraseList);
101101
transitionInserted &= transitionKey != nullptr;
102102
}
103103

@@ -145,7 +145,7 @@ struct ParserStateOpConversionPattern : public OpConversionPattern<P4HIR::Parser
145145

146146
private:
147147
static Operation *insertTransitionKey(Operation *op, ConversionPatternRewriter &rewriter,
148-
Block *block) {
148+
Block *block, SmallVector<Operation*>& eraseList) {
149149
auto loc = op->getLoc();
150150
if (auto lookAheadOp = dyn_cast<P4CoreLib::PacketLookAheadOp>(op)) {
151151
// TODO: not sure how to handle offsets
@@ -155,6 +155,7 @@ struct ParserStateOpConversionPattern : public OpConversionPattern<P4HIR::Parser
155155
auto width = rewriter.getI32IntegerAttr(bitTy.getWidth());
156156
ConversionPatternRewriter::InsertionGuard guard(rewriter);
157157
rewriter.setInsertionPointToEnd(block);
158+
eraseList.push_back(lookAheadOp);
158159
return rewriter.create<BMv2IR::LookaheadOp>(loc, offset, width);
159160
}
160161
llvm_unreachable("Unsupported operand");

lib/Dialect/BMv2IR/BMv2IR_Ops.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,37 @@
22

33
#include "mlir/IR/Builders.h"
44
#include "p4mlir/Dialect/BMv2IR/BMv2IR_Dialect.h"
5+
#include "p4mlir/Dialect/BMv2IR/BMv2IR_OpInterfaces.h"
56

6-
using namespace P4::P4MLIR;
7+
using namespace mlir;
8+
using namespace P4::P4MLIR::BMv2IR;
79

8-
void BMv2IR::BMv2IRDialect::initialize() {
10+
LogicalResult ParserStateOp::verify() {
11+
// Check that only allowed ops are used as transition keys, transitions and parser ops
12+
13+
for (auto &block : getTransitionKeys()) {
14+
for (auto &op : block) {
15+
if (!isa<AllowedTransitionKey>(op)) {
16+
return emitError("Op not allowed as transition key");
17+
}
18+
}
19+
}
20+
21+
for (auto &block : getTransitions()) {
22+
for (auto &op : block) {
23+
if (!isa<TransitionOp>(op)) return emitError("Op not allowed as transition");
24+
}
25+
}
26+
27+
for (auto &block : getParserOps()) {
28+
for (auto &op : block) {
29+
if (!isa<AllowedParserOp>(op)) return emitError("Op not allowed as parser op");
30+
}
31+
}
32+
return success();
33+
}
34+
35+
void BMv2IRDialect::initialize() {
936
registerTypes();
1037
registerAttributes();
1138
addOperations<

test/Conversion/BMv2IR/parser.mlir

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ module {
5858
// CHECK: bmv2ir.transition type default, @prs::@parse_bottom
5959
// CHECK: }
6060
// CHECK: parser_ops {
61-
// CHECK: %lookahead = p4corelib.packet_lookahead %arg0 : !p4corelib.packet_in -> !b8i
6261
// CHECK: }
6362
p4hir.state @parse_one {
6463
%one_field_ref = p4hir.struct_field_ref %arg1["one"] : <!Headers_t>

0 commit comments

Comments
 (0)