Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIRRTL] Add integer shift left property op #7657

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading