Skip to content

[OM] Add a folder for IntegerBinaryArithmeticOp and use it in Evaluator, extend StringConcat folder#10269

Merged
uenoku merged 3 commits intollvm:mainfrom
uenoku:dev/hidetou/evaluator-folder
Apr 23, 2026
Merged

[OM] Add a folder for IntegerBinaryArithmeticOp and use it in Evaluator, extend StringConcat folder#10269
uenoku merged 3 commits intollvm:mainfrom
uenoku:dev/hidetou/evaluator-folder

Conversation

@uenoku
Copy link
Copy Markdown
Member

@uenoku uenoku commented Apr 18, 2026

This extends IntegerBinaryArithmeticOp folder using evaluation rule in Evaluator. Also changes Evaluator to use folder to avoid code duplication. Also extends a folder of StringConcat.

Separated from #10265, which proposes to a pattern to use Op folder not only IntegerArithmeticOp.

Assisted-by: Codex: GPT 5.4

@uenoku uenoku force-pushed the dev/hidetou/evaluator-folder branch from b9313bd to 839ba80 Compare April 18, 2026 19:15
@uenoku uenoku force-pushed the dev/hidetou/evaluator-folder branch from 839ba80 to 50bade4 Compare April 18, 2026 19:17
Copy link
Copy Markdown
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

Only nits.

Switching to the folders here makes a ton of sense, like how we can reuse folders in FIRRTL IMCP. Nice idea.

Comment thread lib/Dialect/OM/Evaluator/Evaluator.cpp Outdated
SmallVector<mlir::OpFoldResult, 1> results;
om::IntegerAttr resultAttr;
auto foldResult = op->fold(operandAttrs, results);
if (failed(foldResult) || results.size() != 1 ||
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I forget how this works. If the folder doesn't apply, then that returns {}. That won't trigger the failure here, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If folder doesn't apply, it returns LogicalResult::failure so it returns an error below (results will be empty)

Comment on lines +499 to +500
!(resultAttr = llvm::dyn_cast_or_null<om::IntegerAttr>(
results[0].dyn_cast<Attribute>())))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are these checks necessary/reachable given the constraints of the fold function?

Copy link
Copy Markdown
Member Author

@uenoku uenoku Apr 18, 2026

Choose a reason for hiding this comment

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

For the current implementation of folders that implement IntegerBinaryArithmetic, yes it's certainly not reachable.

But I don't think this check is unnecessary since folder could return a value (even when all operands are attributes), and without this check we would get assertion failure or nullptr deference (e.g. string_concat didn't return an attribute when it had single operand without this PR).

This part https://github.com/llvm/circt/pull/10265/changes#r3105827722 is more generic version of this with a slightly better error message.

Comment thread lib/Dialect/OM/OMOps.cpp
Comment thread test/Dialect/OM/canonicalizers.mlir Outdated
// CHECK-DAG: [[WIDEADD:%.+]] = om.constant #om.integer<9 : si6> : !om.integer
// CHECK: [[DYN:%.+]] = om.integer.add %x, %{{.+}} : !om.integer
%0 = om.integer.add %i3, %i4 : !om.integer
%1 = om.integer.mul %i3, %i4 : !om.integer
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This folding to -4 is right in twos-complement, but may produce unexpected behavior if we're not careful as I think we view OM integers as arbitrary precision. I think the answer to this is just that this is how this works.

@uenoku uenoku merged commit 23c5669 into llvm:main Apr 23, 2026
8 of 12 checks passed
@uenoku uenoku deleted the dev/hidetou/evaluator-folder branch April 23, 2026 00:37
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.

2 participants