This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fix endian conversion of sub-byte types
ClosedPublic

Authored by uweigand on May 16 2022, 3:05 AM.

Details

Summary

As noticed in https://reviews.llvm.org/D122925, the routine DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine does not handle sub-byte types correctly.

convertEndianOfArrayRefForBEmachine may call convertEndianOfCharForBEmachine with an elementBitWidth of 1, which falls into this default case:

default: {
  size_t nBytes = elementBitWidth / CHAR_BIT;
  for (size_t i = 0; i < nBytes; i++)
    std::copy_n(inRawData + (nBytes - 1 - i), 1, outRawData + i);
  break;
}

If elementBitWidth is smaller than CHAR_BIT, that simply does nothing and leaves outRawData unmodified.

However, for sub-byte types no endian conversion is actually necessary, so this patch simply copies the input to the output.

Note that this touches only one caller of DenseIntOrFPElementsAttr::convertEndianOfArrayRefForBEmachine, but all other callers always pass a multiple of CHAR_BITS as elementBitWidth.

Diff Detail

Event Timeline

uweigand created this revision.May 16 2022, 3:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 16 2022, 3:05 AM
uweigand requested review of this revision.May 16 2022, 3:05 AM

Thanks @uweigand for fixing this.

And I also found that mlir lacks i1 dtype test of hex format. Maybe we should complete these tests to find out whether more bugs are triggered.

Ping? The mlir build bot on s390x is still red because of this problem.

Does this also show with an i4 test case? (i1 is special in a few ways, so having other sub CHAR_BIT test case would be good)

uweigand updated this revision to Diff 435933.Jun 10 2022, 8:48 AM

Add test case.

Does this also show with an i4 test case? (i1 is special in a few ways, so having other sub CHAR_BIT test case would be good)

Yes, it does. I've now added a bunch of tests, and without the fix, the i1, i4, and i8 tests fail for me . (The i16, i32, i64 tests pass either way.)

rriddle accepted this revision.Jun 11 2022, 12:49 AM
This revision is now accepted and ready to land.Jun 11 2022, 12:49 AM
This revision was automatically updated to reflect the committed changes.