Fix signed/unsigned comparison and arithmetic semantics in C codegen#1002
Draft
Fix signed/unsigned comparison and arithmetic semantics in C codegen#1002
Conversation
Agent-Logs-Url: https://github.com/inducer/loopy/sessions/7614b1e9-441e-41a6-9b81-98243a7b4d2b Co-authored-by: inducer <352067+inducer@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix and test signed/unsigned comparison semantics
Fix signed/unsigned comparison and arithmetic semantics in C codegen
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
C's implicit unsigned-promotion rule causes wrong results when mixing signed/unsigned integers — e.g.
(int32_t)-1 < (uint32_t)1evaluates tofalsebecause-1becomesUINT_MAX. Loopy's generated code was subject to this, diverging from Python semantics.Changes
loopy/target/c/codegen/expression.py—ExpressionToCExpressionMappermap_comparison: When operands are integers of opposite signedness, cast both to the narrowest common signed type (vianp.result_type; falls back toint64forint64/uint64).map_product(new override): When the inferred result is a signed integer and any factor is unsigned, cast those factors to the result type. Fixes subtraction'sa + (-1)*bform.map_sum(new override): Same treatment for addends — prevents an unsigned term from silently converting signed co-addends to unsigned arithmetic._is_mixed_sign_integer_pair/_common_signed_dtype(new helpers): Detect mixed-signedness and derive the appropriate signed cast target.Before / after for
int32 sv = -1, uint32 uv = 1:test/test_c_execution.pytest_mixed_sign_comparison: All six comparison operators (<,<=,>,>=,==,!=) acrossint8/uint8,int16/uint16,int32/uint32,int64/uint64pairs, three representative value combinations each.test_mixed_sign_subtraction: Both subtraction directions (sv - uv,uv - sv) for the same dtype pairs.