Skip to content

Commit

Permalink
[FIRRTL] Add integer shift left property op
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart committed Oct 3, 2024
1 parent 3da788b commit 59e32bc
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/circt/Dialect/FIRRTL/FIRRTLExpressions.td
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,21 @@ def IntegerShrOp : IntegerBinaryPrimOp<"integer.shr"> {
}];
}

def IntegerShlOp : IntegerBinaryPrimOp<"integer.shl"> {
let summary = "Shift an FIntegerType value left by an FIntegerType value";
let description = [{
The shift left operation result is the arbitrary precision signed integer
shift left of the lhs operand by the rhs operand. The rhs operand must be
non-negative.

Example:
```mlir
%2 = firrtl.integer.shl %0, %1 : (!firrtl.integer, !firrtl.integer) ->
!firrtl.integer
```
}];
}

//===----------------------------------------------------------------------===//
// RefOperations: Operations on the RefType.
//
Expand Down
17 changes: 17 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLFolds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,23 @@ OpFoldResult IntegerShrOp::fold(FoldAdaptor adaptor) {
return {};
}

OpFoldResult IntegerShlOp::fold(FoldAdaptor adaptor) {
if (auto rhsCst = getConstant(adaptor.getRhs())) {
// Constant folding
if (auto lhsCst = getConstant(adaptor.getLhs()))

return IntegerAttr::get(
IntegerType::get(getContext(), lhsCst->getBitWidth()),
lhsCst->shl(*rhsCst));

// integer.shl(x, 0) -> x
if (rhsCst->isZero())
return getLhs();
}

return {};
}

//===----------------------------------------------------------------------===//
// Unary Operators
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5980,6 +5980,9 @@ void IntegerMulOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
void IntegerShrOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
genericAsmResultNames(*this, setNameFn);
}
void IntegerShlOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
genericAsmResultNames(*this, setNameFn);
}
void IsTagOp::getAsmResultNames(OpAsmSetValueNameFn setNameFn) {
genericAsmResultNames(*this, setNameFn);
}
Expand Down
15 changes: 15 additions & 0 deletions test/Dialect/FIRRTL/canonicalization.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3640,4 +3640,19 @@ firrtl.module private @Issue7562(in %sel : !firrtl.uint<1>, in %a : !firrtl.cons
firrtl.connect %out, %res : !firrtl.uint, !firrtl.uint<1>
}

// CHECK-LABEL: firrtl.class @PropertyArithmetic
firrtl.class @PropertyArithmetic(in %in: !firrtl.integer, out %out0: !firrtl.integer, out %out1: !firrtl.integer) {
// CHECK: [[C4:%.+]] = firrtl.integer 4
%0 = firrtl.integer 0
%1 = firrtl.integer 1
%2 = firrtl.integer 2

%3 = firrtl.integer.shl %1, %2 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%4 = firrtl.integer.shl %in, %0 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer

// CHECK: firrtl.propassign %out0, [[C4]]
// CHECK: firrtl.propassign %out1, %in
firrtl.propassign %out0, %3 : !firrtl.integer
firrtl.propassign %out1, %4 : !firrtl.integer
}
}
3 changes: 3 additions & 0 deletions test/Dialect/FIRRTL/round-trip.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ firrtl.module @PropertyArithmetic() {

// CHECK: firrtl.integer.shr %0, %1 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%4 = firrtl.integer.shr %0, %1 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer

// CHECK: firrtl.integer.shl %0, %1 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
%5 = firrtl.integer.shl %0, %1 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
}

// CHECK-LABEL: firrtl.module @PropertyListOps
Expand Down

0 comments on commit 59e32bc

Please sign in to comment.