Skip to content

Commit

Permalink
Implement casts
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 30758d1 commit 404f787
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
24 changes: 24 additions & 0 deletions include/p4mlir/Dialect/P4HIR/P4HIR_Ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,28 @@ def StoreOp : P4HIR_Op<"store", [
// FIXME: add verifier.
}

// TODO: Decide if we'd want to be more precise and split cast into
// bitcast, trunc and extensions
def CastOp : P4HIR_Op<"cast",
[Pure/*,
DeclareOpInterfaceMethods<PromotableOpInterface>*/]> {
let summary = "Conversion between values of different types";
let description = [{
Apply P4 usual conversions rules between values.
```mlir
%4 = p4hir.cast (%3 : !p4hir:bit<32>) : !p4hir:int<32>
```
}];

let arguments = (ins AnyP4Type:$src);
let results = (outs AnyP4Type:$result);

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

// FIXME: add verifier.
}

#endif // P4MLIR_DIALECT_P4HIR_P4HIR_OPS_TD
12 changes: 8 additions & 4 deletions test/Translate/Ops/variables.p4
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ action foo() {
bit<8> b5 = 8w0b_1010_1010; // same value as above
bit<8> b6 = 8w170; // same value as above
bit<8> b7 = 8w0b1010_1010; // an 8-bit unsigned number with value 170
// FIXME: Implement casts and path resolution
// int<8> b8 = (int<8>)b7;
int<8> b8 = (int<8>)b7;
int<42> b9;
bit<8> b10 = b7;
bit<8> b10 = (bit<8>)b8;
}

// CHECK-LABEL: module
Expand All @@ -40,7 +39,12 @@ action foo() {
// CHECK: %[[VAL_12:.*]] = p4hir.const #p4hir.int<170> : !p4hir.bit<8>
// CHECK: %[[VAL_13:.*]] = p4hir.alloca !p4hir.bit<8> ["b7", init] : !p4hir.ref<!p4hir.bit<8>>
// CHECK: p4hir.store %[[VAL_12]], %[[VAL_13]] : !p4hir.bit<8>, !p4hir.ref<!p4hir.bit<8>>
// CHECK: %[[VAL_14_1:.*]] = p4hir.load %[[VAL_13]] : !p4hir.ref<!p4hir.bit<8>>, !p4hir.bit<8>
// CHECK: %[[VAL_15_1:.*]] = p4hir.cast(%[[VAL_14_1]] : !p4hir.bit<8>) : !p4hir.int<8>
// CHECK: %[[VAL_16_1:.*]] = p4hir.alloca !p4hir.int<8> ["b8", init] : !p4hir.ref<!p4hir.int<8>>
// CHECK: p4hir.store %[[VAL_15_1]], %[[VAL_16_1]] : !p4hir.int<8>, !p4hir.ref<!p4hir.int<8>>
// CHECK: %[[VAL_14:.*]] = p4hir.alloca !p4hir.int<42> ["b9"] : !p4hir.ref<!p4hir.int<42>>
// CHECK: %[[VAL_14_2:.*]] = p4hir.load %[[VAL_16_1]] : !p4hir.ref<!p4hir.int<8>>, !p4hir.int<8>
// CHECK: %[[VAL_16:.*]] = p4hir.cast(%[[VAL_14_2]] : !p4hir.int<8>) : !p4hir.bit<8>
// CHECK: %[[VAL_15:.*]] = p4hir.alloca !p4hir.bit<8> ["b10", init] : !p4hir.ref<!p4hir.bit<8>>
// CHECK: %[[VAL_16:.*]] = p4hir.load %[[VAL_13]] : !p4hir.ref<!p4hir.bit<8>>, !p4hir.bit<8>
// CHECK: p4hir.store %[[VAL_16]], %[[VAL_15]] : !p4hir.bit<8>, !p4hir.ref<!p4hir.bit<8>>
19 changes: 10 additions & 9 deletions tools/p4mlir-translate/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,13 @@ class P4HIRConverter : public P4::Inspector, public P4::ResolutionContext {
return false;
}

bool preorder(const P4::IR::Cast *cast) override {
// Cast could be used in constant initializers or as a separate
// operation. In former case resolve it to the constant
if (typeMap->isCompileTimeConstant(cast)) {
resolveConstantExpr(cast);
return false;
}
return true;
}
bool preorder(const P4::IR::Cast *cast) override { return true; }

bool preorder(const P4::IR::Declaration_Constant *decl) override;

bool preorder(const P4::IR::Declaration_Variable *) override { return true; }
void postorder(const P4::IR::Declaration_Variable *decl) override;
void postorder(const P4::IR::Cast *cast) override;
};

bool P4TypeConverter::preorder(const P4::IR::Type_Bits *type) {
Expand Down Expand Up @@ -402,6 +395,14 @@ void P4HIRConverter::postorder(const P4::IR::Declaration_Variable *decl) {
setValue(decl, alloca);
}

void P4HIRConverter::postorder(const P4::IR::Cast *cast) {
LOG4("Converting " << dbp(cast));
auto src = getValue(cast->expr);
auto destType = getOrCreateType(cast->destType);

setValue(cast, builder.create<P4HIR::CastOp>(getLoc(builder, cast), destType, src));
}

} // namespace

namespace P4::P4MLIR {
Expand Down

0 comments on commit 404f787

Please sign in to comment.