Skip to content

[mlir] Fix getAPInt for non-power-of-2 widths and constant-fold ratio trunc/sext/zext#144

Open
PavelKopyl wants to merge 1 commit into
mainfrom
kpv-fix-cast-folder
Open

[mlir] Fix getAPInt for non-power-of-2 widths and constant-fold ratio trunc/sext/zext#144
PavelKopyl wants to merge 1 commit into
mainfrom
kpv-fix-cast-folder

Conversation

@PavelKopyl

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves MLIR codegen’s handling of integer constants by fixing getAPInt for non-power-of-2 bit widths and by constant-folding rational-number binary operations into immediate integer constants, reducing reliance on later folding and eliminating several previously failing semantic tests.

Changes:

  • Fix getAPInt(u256, numBits) to support arbitrary integer widths by parsing into 256 bits and truncating as needed.
  • Add constant-folding for binary operations whose expression type is a rational number, emitting a sol.constant directly.
  • Add new lit + semantic tests for non-power-of-2 integer widths and rational folding; update the MLIR semtest failure allowlist to reflect newly passing tests.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/mlirSemtestFailures.txt Removes several tests from the known-fail list (now expected to pass).
test/lit/mlirCodegen/int-const-fold.sol Adds FileCheck coverage for init-MLIR constant folding of non-power-of-2 integer widths.
test/lit/mlirCodegen/EVM/int-const-fold.sol Adds FileCheck coverage for EVM-target std-MLIR constant folding of the same cases.
test/libsolidity/semanticTests/mlir/int-const-fold.sol Adds semantic test expectations for MLIR compile path.
libsolidity/codegen/mlir/Util.h Updates getAPInt to correctly handle arbitrary widths via parse+truncate.
libsolidity/codegen/mlir/SolidityToMLIR.cpp Adds rational-number binary-op constant folding in MLIR lowering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1254 to +1260
if (binOp.annotation().commonType->category() ==
Type::Category::RationalNumber) {
auto intTy = mlir::cast<mlir::IntegerType>(argTy);
u256 val = binOp.annotation().commonType->literalValue(nullptr);
return b.create<mlir::sol::ConstantOp>(
loc, b.getIntegerAttr(intTy, getAPInt(val, intTy.getWidth())));
}
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