This is an archive of the discontinued LLVM Phabricator instance.

ValueTracking: Fix crash on weird size bswap
AbandonedPublic

Authored by arsenm on Feb 14 2020, 8:22 AM.

Details

Summary

APInt::byteSwap doesn't handle strange cases, so just give up on them.

Diff Detail

Event Timeline

arsenm created this revision.Feb 14 2020, 8:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2020, 8:22 AM
nikic added a comment.Feb 14 2020, 8:39 AM

I think it would make more sense to add support for this to APInt. These operations are well-defined and supported in IR, so APInt should support them as well.

I think it would make more sense to add support for this to APInt. These operations are well-defined and supported in IR, so APInt should support them as well.

+1

I think it would make more sense to add support for this to APInt. These operations are well-defined and supported in IR, so APInt should support them as well.

I don't think this is really well defined for arbitrary odd cases. What does a bswap mean for non-byte multiples?

nikic added a comment.Feb 14 2020, 8:54 AM

Sure, non-byte multiplies doesn't make sense, but byte multiples generally do. However, after checking LangRef we apparently do require the bitwidth to be a multiple of 16 for this intrinsic: https://llvm.org/docs/LangRef.html#llvm-bswap-intrinsics

Sure, non-byte multiplies doesn't make sense, but byte multiples generally do. However, after checking LangRef we apparently do require the bitwidth to be a multiple of 16 for this intrinsic: https://llvm.org/docs/LangRef.html#llvm-bswap-intrinsics

Whether the intrinsic does or not, adding support to APInt is pretty trivial. Scarily we don't appear to have any unit tests at all for APInt:byteSwap

nikic added a comment.Feb 16 2020, 1:32 PM

So the APInt support is there now, but we still have to do something here. I would suggest to enforce this requirement in the IR verifier (it already has a bunch of similar checks in visitIntrinsicCall), so that individual code handling bswaps doesn't have to deal with it.

arsenm abandoned this revision.Mar 22 2020, 9:16 AM

So the APInt support is there now, but we still have to do something here. I would suggest to enforce this requirement in the IR verifier (it already has a bunch of similar checks in visitIntrinsicCall), so that individual code handling bswaps doesn't have to deal with it.

I would suggest to eliminate the restriction from the langref. The GlobalISel legalizer at least handles the case just fine

So the APInt support is there now, but we still have to do something here. I would suggest to enforce this requirement in the IR verifier (it already has a bunch of similar checks in visitIntrinsicCall), so that individual code handling bswaps doesn't have to deal with it.

Verifier check added in b76bbcc60db6cd7d42bf46f2fcdc6784250554d7