Skip to content

Commit

Permalink
[FIRRTL] Add BigIntConstantOp (llvm#5397)
Browse files Browse the repository at this point in the history
  • Loading branch information
youngar authored Jun 13, 2023
1 parent 5271822 commit 87897b7
Show file tree
Hide file tree
Showing 3 changed files with 41 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 @@ -1009,6 +1009,21 @@ def StringConstantOp : FIRRTLOp<"string", [Pure, ConstantLike]> {
let assemblyFormat = "$value attr-dict";
}

def BigIntConstantOp : FIRRTLOp<"bigint", [Pure, ConstantLike]> {
let summary = "Produce a constant bigint value";
let description = [{
Produces a constant value of bigint type.

Example:
```mlir
%0 = firrtl.bigint 42
```
}];
let arguments = (ins APSIntAttr:$value);
let results = (outs BigIntType:$result);
let hasCustomAssemblyFormat = true;
}

//===----------------------------------------------------------------------===//
// RefOperations: Operations on the RefType.
//
Expand Down
21 changes: 21 additions & 0 deletions lib/Dialect/FIRRTL/FIRRTLOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3024,6 +3024,27 @@ Attribute AggregateConstantOp::getAttributeFromFieldID(uint64_t fieldID) {
return value;
}

void BigIntConstantOp::print(OpAsmPrinter &p) {
p << " ";
p.printAttributeWithoutType(getValueAttr());
p.printOptionalAttrDict((*this)->getAttrs(), /*elidedAttrs=*/{"value"});
}

ParseResult BigIntConstantOp::parse(OpAsmParser &parser,
OperationState &result) {
auto *context = parser.getContext();
APInt value;
if (parser.parseInteger(value) ||
parser.parseOptionalAttrDict(result.attributes))
return failure();
result.addTypes(BigIntType::get(context));
auto intType =
IntegerType::get(context, value.getBitWidth(), IntegerType::Signed);
auto valueAttr = parser.getBuilder().getIntegerAttr(intType, value);
result.addAttribute("value", valueAttr);
return success();
}

LogicalResult BundleCreateOp::verify() {
if (getType().getNumElements() != getFields().size())
return emitOpError("number of fields doesn't match type");
Expand Down
5 changes: 5 additions & 0 deletions test/Dialect/FIRRTL/test.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,10 @@ firrtl.module @StringTest(in %in: !firrtl.string, out %out: !firrtl.string) {
// CHECK-SAME: (in %in: !firrtl.bigint, out %out: !firrtl.bigint)
firrtl.module @BigIntTest(in %in: !firrtl.bigint, out %out: !firrtl.bigint) {
firrtl.propassign %out, %in : !firrtl.bigint

// CHECK: %0 = firrtl.bigint 4
%0 = firrtl.bigint 4
// CHECK: %1 = firrtl.bigint -4
%1 = firrtl.bigint -4
}
}

0 comments on commit 87897b7

Please sign in to comment.