This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Fix DenseElementsAttr::mapValues(i1, splat).
ClosedPublic

Authored by wecing on Aug 26 2022, 3:37 PM.

Details

Summary

Splat of bool is encoded as a byte with all-ones in it [1]. Without this
change, this piece of code:

auto xs = builder.getI32TensorAttr({42, 42, 42, 42});
auto xs2 = xs.mapValues(builder.getI1Type(), [](const llvm::APInt &x) {
  return x.isZero() ? llvm::APInt::getZero(1) : llvm::APInt::getAllOnes(1);
});
xs2.dump();

Prints:

dense<[true, false, false, false]> : tensor<4xi1>

Because only the first bit is set. This applies to both
DenseIntElementsAttr::mapValues() and DenseFPElementsAttr::mapValues().

[1]: https://github.com/llvm/llvm-project/blob/e877b42e2c70813352c1963ea33e992f481d5cba/mlir/lib/IR/BuiltinAttributes.cpp#L984

Diff Detail

Event Timeline

wecing created this revision.Aug 26 2022, 3:37 PM
Herald added a project: Restricted Project. · View Herald Transcript
wecing requested review of this revision.Aug 26 2022, 3:37 PM
rriddle requested changes to this revision.Aug 28 2022, 10:36 PM

Is there a test that you could add? Maybe in mlir/unittests/AttributeTest.cpp?

mlir/lib/IR/BuiltinAttributes.cpp
1524–1525

This could likely be applied within the if (attr.isSplat()) { branch below, after the call to processElt; i.e. you could call and then check the result, fixing if necessary.

This revision now requires changes to proceed.Aug 28 2022, 10:36 PM
wecing updated this revision to Diff 456482.Aug 29 2022, 3:45 PM

Add tests and move special handling code

rriddle accepted this revision.Aug 30 2022, 1:02 PM
This revision is now accepted and ready to land.Aug 30 2022, 1:02 PM
wecing updated this revision to Diff 456786.Aug 30 2022, 1:41 PM

Include all local changes

Thank you! Since I don't have write access, could you submit the patch for me?

This revision was automatically updated to reflect the committed changes.