This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Arith] Fix up integer range inference for truncation
ClosedPublic

Authored by krzysz00 on Jul 25 2022, 10:25 AM.

Details

Summary

Attempting to apply the range analysis to real code revealed that
trunci wasn't correctly handling the case where truncation would
create wider ranges - for example, if we truncate [255, 257] : i16 to
i8, the result can be 255, 0, or 1, which isn't a contiguous range of
values.

The previous implementation would naively map this to [255, 1], which
would cause issues with unsigned ranges and unification.

Diff Detail

Event Timeline

krzysz00 created this revision.Jul 25 2022, 10:25 AM
krzysz00 requested review of this revision.Jul 25 2022, 10:25 AM
rriddle added inline comments.Jul 25 2022, 10:41 AM
mlir/lib/Dialect/Arithmetic/IR/InferIntRangeInterfaceImpls.cpp
510

What does LLVM do in this situation?

krzysz00 added inline comments.Jul 25 2022, 11:08 AM
mlir/lib/Dialect/Arithmetic/IR/InferIntRangeInterfaceImpls.cpp
510

LLVM does https://github.com/llvm/llvm-project/blob/main/llvm/lib/IR/ConstantRange.cpp#L806 , which is a bit tricky to follow because, I'm just now learning, they use values of the form [X, Y] with X >(unsigned) Y to represent [0, Y) \/ [SignedMin, X], but it seems to be roughly the same behavior

Mogball accepted this revision.Jul 30 2022, 4:28 PM

LGTM, but the code was quite tricky to follow in my head. Could you add some more comments in there explaining how these ranges are represented, interpreted, and derived? (A worked example would be nice)

This revision is now accepted and ready to land.Jul 30 2022, 4:28 PM
krzysz00 updated this revision to Diff 449047.Aug 1 2022, 9:37 AM

Added comments, fixed bug, updated tests