[Mlir-commits] [mlir] [mlir][Vector] Add a rewrite pattern for better low-precision bitcast… (PR #66387)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Sep 15 08:10:12 PDT 2023


================
@@ -155,6 +164,221 @@ struct ConvertVectorTransferRead final
 };
 } // end anonymous namespace
 
+//===----------------------------------------------------------------------===//
+// RewriteBitCastOfTruncI
+//===----------------------------------------------------------------------===//
+
+namespace {
+
+/// Helper struct to keep track of the provenance of a contiguous set of bits
+/// in a source vector.
+struct SourceElementRange {
+  int64_t sourceElement;
+  int64_t sourceBitBegin;
+  int64_t sourceBitEnd;
+};
+
+struct SourceElementRangeList : public SmallVector<SourceElementRange> {
+  /// Given the index of a SourceElementRange in the SourceElementRangeList,
+  /// compute the amount of bits that need to be shifted to the left to get the
+  /// bits in their final location. This shift amount is simply the sum of the
+  /// bits *before* `shuffleIdx` (i.e. the bits of `shuffleIdx = 0` are always
+  /// the LSBs, the bits of `shuffleIdx = ` come next, etc).
+  int64_t computeLeftShiftAmount(int64_t shuffleIdx) const {
+    int64_t res = 0;
+    for (int64_t i = 0; i < shuffleIdx; ++i)
+      res += (*this)[i].sourceBitEnd - (*this)[i].sourceBitBegin;
+    return res;
+  }
+};
+
+/// Helper struct to enumerate the source elements and bit ranges that are
+/// involved in a bitcast operation.
+/// This allows rewriting a vector.bitcast into shuffles and bitwise ops for
----------------
qcolombet wrote:

Additional comments would be useful:
```
This creates and holds a mapping of the form:
[dstVectorElementJ] == [{srcVectorElementX, bitRange}, {srcVectorElementY, bitRange}, ...]
```
Then we can also give examples:
```
E.g., for <3 x i8> = bitcast <1 x i24>
[0] = {0, [0-8)}
[1] = {0, [8-16)}
[2] = {0, [16-24)}

And for <2 x i15> = bitcast <3 x i10>
[0] = {0, [0, 10)}, {1, [0, 5)}
[1] = {1, [5, 10)}, {2, [0, 10)}
```

https://github.com/llvm/llvm-project/pull/66387


More information about the Mlir-commits mailing list