Skip to content

Parse ModArith constants as modulus's bit-width#3126

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
crockeea:modarithconsts
Jun 26, 2026
Merged

Parse ModArith constants as modulus's bit-width#3126
copybara-service[bot] merged 1 commit into
google:mainfrom
crockeea:modarithconsts

Conversation

@crockeea

@crockeea crockeea commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

This simple test fails on both lines, for different reasons.

func.func @test_large_decimal_constant_syntax() {
  %scalar = mod_arith.constant 12535824225335233 : !Zp_i64
  %dense = mod_arith.constant dense<[12535824225335233, 0]> : !Zp_i64_vec
  return
}

For the first line, 12535824225335233 is a 53-bit constant, but it triggers an error in the ConstantOp verifier that the constant is too big to fit in the modulus. For some reason, MLIR's int parser makes it a 68-bit value.

For the second line, existing code in the ConstantOp parser sets 0 to 4 bits, but this is incompatible with a non-zero constant (of any size other than 4 bits).

This PR fixes these issues by forcing all ModArith constants to have the same bit-width as the modulus (after checking that it in fact fits).

Assisted by AI.

@j2kun

j2kun commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Wow, this led me down a bit of rabbit hole.

https://github.com/llvm/llvm-project/pull/205947/changes should tighten the bit width upstream, but I still think this code should be defensive and so I will review it now.

@j2kun j2kun added the pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing label Jun 26, 2026
@copybara-service copybara-service Bot merged commit ca4e129 into google:main Jun 26, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pull_ready Indicates whether a PR is ready to pull. The copybara worker will import for internal testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants