Skip to content

[FIRRTL][OM] Use signed integer attributes#9548

Merged
rwy7 merged 1 commit intollvm:mainfrom
rwy7:integer-property-sign-verifier
Feb 13, 2026
Merged

[FIRRTL][OM] Use signed integer attributes#9548
rwy7 merged 1 commit intollvm:mainfrom
rwy7:integer-property-sign-verifier

Conversation

@rwy7
Copy link
Contributor

@rwy7 rwy7 commented Jan 29, 2026

  • In the FIRRTL IntegerConstantOp, use a verifier to ensure that the underlying value (encoded as an attribute) has a signed type.
  • Similarly, in the OM IntegerAttr, use an attribute verifier to ensure the underlying value is signed, too.
  • Fix the OM shl/shr folders so they return OM IntegerAttrs with signed integers, and
  • Fix the python API so that, when converting from a python integer to an OM integer attribute, the OM IntegerAttr is signed.

@rwy7 rwy7 force-pushed the integer-property-sign-verifier branch from eb71ab9 to 99b77ff Compare January 30, 2026 01:31
Copy link
Member

@seldridge seldridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great.

Are there tests that can be included to check that the verifiers are locked in?

Edit: also, there's the om.py integration test failure which I didn't investigate.

Comment on lines +247 to +250
if (apInt.isNegative()) {
++width;
apInt = apInt.zext(width);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:

Suggested change
if (apInt.isNegative()) {
++width;
apInt = apInt.zext(width);
}
if (apInt.isNegative())
apInt = apInt.zext(++width);

}
type = IntegerType::get(context, width, IntegerType::Signed);
intConstant = IntegerAttr::get(type, apInt);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thanks for taking the time to do this correctly and get the width correct.

let results = (outs FIntegerType:$result);
let hasFolder = 1;
let hasCustomAssemblyFormat = true;
let hasVerifier = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this with the verifier is fine. Did you explore changing this from APSIntAttr to some other, stricter constraint? E.g., like how OMIntegerAttr does it? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't for this PR, but something along those lines would be great. Honestly I think we should build out a whole new bigint attribute, rather than using APInts and IntegerAttrs directly.

@uenoku
Copy link
Member

uenoku commented Jan 30, 2026

return omIntegerAttrGet(intAttr);

Not actually tested but I feel this line is problematic. I think it's ok to consider python integer to si64 (historically we are using i64 for python integer, not ideal thouigh...

@rwy7 rwy7 force-pushed the integer-property-sign-verifier branch 4 times, most recently from 0a40434 to 6566dc1 Compare February 3, 2026 22:22
@rwy7 rwy7 force-pushed the integer-property-sign-verifier branch from 1138577 to d53751e Compare February 4, 2026 00:08
@rwy7 rwy7 marked this pull request as ready for review February 4, 2026 00:48
@rwy7 rwy7 requested a review from darthscsi as a code owner February 4, 2026 00:48
return IntegerAttr::get(IntegerType::get(getContext(),
lhsCst->getBitWidth(),
IntegerType::Signed),
lhsCst->shl(*rhsCst));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there is a bug here, which I am not fixing in this PR. This folder should be extending the width before shifting. Probably, the evaluator has the same issue.

Demo

firrtl.circuit "Casts" {
  firrtl.module @Casts() {}
  firrtl.class @PropertyArithmetic(out %o1: !firrtl.integer) {
    %0 = firrtl.integer 4
    %1 = firrtl.integer.shl %0, %0 : (!firrtl.integer, !firrtl.integer) -> !firrtl.integer
    firrtl.propassign %o1, %1 : !firrtl.integer
  }
}
// ./bin/circt-opt -canonicalize='top-down=true region-simplify=aggressive' ../scratch/scratch.mlir  --mlir-print-ir-before-all
module {
  firrtl.circuit "Casts" {
    firrtl.module @Casts() {
    }
    firrtl.class @PropertyArithmetic(out %o1: !firrtl.integer) {
      %0 = firrtl.integer 0
      firrtl.propassign %o1, %0 : !firrtl.integer
    }
  }
}

Copy link
Contributor Author

@rwy7 rwy7 Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for filing this issue!

Copy link
Member

@seldridge seldridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Are there error tests that can be added?

type = IntegerType::get(context, width, IntegerType::Signed);
intConstant = IntegerAttr::get(type, apInt);
}
return FIntegerConstantOp::create(builderOM, intConstant);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this could invert the check and do an early return for less indentation.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for filing this issue!

@rwy7 rwy7 merged commit 3aea5ad into llvm:main Feb 13, 2026
7 checks passed
@rwy7 rwy7 deleted the integer-property-sign-verifier branch February 13, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants