Skip to content

Commit

Permalink
[FIRRTL] Add integer addition parser support. (#6701)
Browse files Browse the repository at this point in the history
This adds parser support for IntegerAddOp by defining the primitive
expression keyword. This is only supported in FIRRTL version 4.0.0 and
up.
  • Loading branch information
mikeurbach authored Feb 15, 2024
1 parent d0e332b commit a3e993c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLExpressions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,18 @@ class IntegerBinaryPrimOp<string mnemonic, list<Trait> traits = []> :
[Pure, SameOperandsAndResultType] # traits> {
// We use the standard inferReturnTypes from SameOperandsAndResultType.
let inferReturnTypesDecl = "";

// We use a static inferType that always returns FIntegerType.
let inferType = "getFIntegerType";

// Define the getFIntegerType inline in ODS.
let firrtlExtraClassDeclaration = [{
static FIntegerType getFIntegerType(FIRRTLType lhs,
FIRRTLType rhs,
std::optional<Location> loc) {
return FIntegerType::get(lhs.getContext());
}
}];
}

def IntegerAddOp : IntegerBinaryPrimOp<"integer.add", [Commutative]> {
Expand Down
4 changes: 4 additions & 0 deletions lib/Dialect/FIRRTL/Import/FIRParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2252,6 +2252,10 @@ ParseResult FIRStmtParser::parsePrimExp(Value &result) {
case FIRToken::lp_tail:
attrNames.push_back(getConstants().amountIdentifier);
break;
case FIRToken::lp_integer_add:
if (requireFeature({4, 0, 0}, "Integer arithmetic expressions", loc))
return failure();
break;
}

if (operands.size() != numOperandsExpected) {
Expand Down
1 change: 1 addition & 0 deletions lib/Dialect/FIRRTL/Import/FIRTokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ TOK_LPKEYWORD_PRIM(sub, SubPrimOp, 2)
TOK_LPKEYWORD_PRIM(tail, TailPrimOp, 1)
TOK_LPKEYWORD_PRIM(xor, XorPrimOp, 2)
TOK_LPKEYWORD_PRIM(xorr, XorRPrimOp, 1)
TOK_LPKEYWORD_PRIM(integer_add, IntegerAddOp, 2)

#undef TOK_MARKER
#undef TOK_IDENTIFIER
Expand Down
14 changes: 14 additions & 0 deletions test/Dialect/FIRRTL/parse-basic.fir
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,20 @@ circuit BasicProps :
; CHECK-NEXT: firrtl.propassign %nested, %[[NESTED]]
propassign nested, List<List<String>>(List<String>(), List<String>(String("test")), List<String>())

;// -----
FIRRTL version 4.0.0

; CHECK-LABEL: firrtl.circuit "IntegerArithmetic"
circuit IntegerArithmetic :
module IntegerArithmetic :
input a : Integer
input b : Integer
output c : Integer

; CHECK: [[C:%.+]] = firrtl.integer.add %a, %b
; CHECK: firrtl.propassign %c, [[C]]
propassign c, integer_add(a, b)

;// -----
FIRRTL version 3.1.0

Expand Down
11 changes: 11 additions & 0 deletions test/Dialect/FIRRTL/parse-errors.fir
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,17 @@ circuit Top:
output out : Integer
propassign out, in

;// -----
FIRRTL version 3.1.0

circuit Top:
module Top:
input a : Integer
input b : Integer
output c : Integer
; expected-error @below {{Integer arithmetic expressions are a FIRRTL 4.0.0+ feature, but the specified FIRRTL version was 3.1.0}}
propassign c, integer_add(a, b)

;// -----
FIRRTL version 3.3.0

Expand Down

0 comments on commit a3e993c

Please sign in to comment.