Skip to content

add defensive guards for eval/coeff forms#2902

Open
toprakmurat wants to merge 1 commit intogoogle:mainfrom
toprakmurat:polynomial-to-mod-arith/eval-coeff-forms
Open

add defensive guards for eval/coeff forms#2902
toprakmurat wants to merge 1 commit intogoogle:mainfrom
toprakmurat:polynomial-to-mod-arith/eval-coeff-forms

Conversation

@toprakmurat
Copy link
Copy Markdown
Contributor

Fixes #2849

This PR adds strict getForm() checks to all polynomial-to-mod-arith lowerings that assume a specific polynomial form (COEFF or EVAL).

Previously, operations that only make mathematical sense in COEFF form (e.g., FromTensorOp, LeadingTermOp) would silently miscompile if fed an EVAL form polynomial. These conversion patterns now return a notifyMatchFailure if the input form is incorrect.

Changes made:

Added Form::COEFF checks to: ConvertFromTensor, ConvertConstant, ConvertMonomial, ConvertMonicMonomialMul, ConvertLeadingTerm, ConvertApplyCoefficientwise, and ConvertNTT.
Added a Form::EVAL check to: ConvertINTT.

(Note: I think that the first part of #2849 was previously implemented via the ConvertMulEvalForm struct, so no additional changes were needed for that requirement).

@asraa asraa requested a review from crockeea April 27, 2026 14:22
Copy link
Copy Markdown
Collaborator

@crockeea crockeea left a comment

Choose a reason for hiding this comment

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

I think a COEFF constraint on ConvertBasisOp is missing.


auto typeInfo = res.value();

if (typeInfo.polynomialType.getForm() != Form::COEFF) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

op, "failed to construct common conversion info");
auto typeInfo = res.value();

if (typeInfo.polynomialType.getForm() != Form::COEFF) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ditto here; see https://github.com/google/heir/blob/main/lib/Dialect/Polynomial/Transforms/PolyMulToNTT.cpp#L59. As a CONST op, this output a polynomial in either form.

op, "failed to construct common conversion info");
auto typeInfo = res.value();

if (typeInfo.polynomialType.getForm() != Form::COEFF) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LeadingTerm op doesn't output a poly, does it?

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.

PolynomialToModArith: branch lowerings based on eval form vs coeff form

2 participants