Skip to content

Commit

Permalink
part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lialan committed Jan 15, 2025
1 parent 6cc7e03 commit 3d083eb
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions compiler/src/iree/compiler/Dialect/Encoding/IR/EncodingAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"

static constexpr int64_t kNumBitsInByte = 8;

namespace mlir::iree_compiler::IREE::Encoding {

EncodingAttr EncodingAttr::get(MLIRContext *ctx, int64_t operandIndex,
Expand Down Expand Up @@ -162,7 +164,7 @@ Value PackedStorageAttr::calculateStorageSizeInBytes(
bool isPackedStorage = IREE::Encoding::hasPackedStorageAttr(type);
int64_t staticCount = 1;
if (!isPackedStorage) {
staticCount *= elementBits * 8;
staticCount *= elementBits * kNumBitsInByte;
}

for (unsigned i = 0; i < type.getRank(); ++i) {
Expand All @@ -175,18 +177,10 @@ Value PackedStorageAttr::calculateStorageSizeInBytes(
for (auto dim : dynamicDims) {
value = builder.createOrFold<arith::MulIOp>(loc, value, dim);
}
// Sub-byte packing requires putting multiple elements in the same byte.

if (isPackedStorage) {
assert(8 % elementBits == 0);
unsigned byteElements = 8 / elementBits;
// TODO(antiagainst): We may want to emit runtime check to make sure this is
// divisible.
auto divisor = builder.create<arith::ConstantIndexOp>(loc, byteElements);
if (!isPackedStorage && dynamicDims.empty() &&
(staticCount * elementBits) % 8 != 0) {
return nullptr;
}
value = builder.createOrFold<arith::CeilDivUIOp>(loc, value, divisor);
auto divisor = builder.create<arith::ConstantIndexOp>(loc, kNumBitsInByte);
value = builder.createOrFold<arith::CeilDivSIOp>(loc, value, divisor);
}
return value;
}
Expand Down Expand Up @@ -227,7 +221,6 @@ Value EncodingAttr::calculateStorageSizeInBytes(Location loc,
pad(k, roundDimsTo[2]);
}

constexpr int64_t kNumBitsInByte = 8;
unsigned elementBits = getTypeBitWidth(type.getElementType());
// Deal with unpacked storage of i1.
if (elementBits == 1 && !IREE::Encoding::hasPackedStorageAttr(type)) {
Expand Down

0 comments on commit 3d083eb

Please sign in to comment.