-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Encoding][NFC] Move non attribute implementation to EncodingTypes.cpp (
#20045) It follows the convention, which only put the attribute implementation and local functions to EncodingAttr.cpp. The implementation of other utilities is moved to EncodingTypes.cpp. Additionally, it spells out the types for `auto`. --------- Signed-off-by: hanhanW <[email protected]>
- Loading branch information
Showing
4 changed files
with
125 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
compiler/src/iree/compiler/Dialect/Encoding/IR/EncodingTypes.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2025 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree/compiler/Dialect/Encoding/IR/EncodingTypes.h" | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "mlir/Dialect/Affine/Utils.h" | ||
#include "mlir/Dialect/Linalg/Utils/Utils.h" | ||
#include "mlir/IR/AffineMap.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/BuiltinTypeInterfaces.h" | ||
|
||
#include <cassert> | ||
|
||
namespace mlir::iree_compiler::IREE::Encoding { | ||
|
||
SerializableEncodingAttrInterface | ||
getSerializableEncodingAttrInterface(RankedTensorType type) { | ||
return dyn_cast_or_null<SerializableEncodingAttrInterface>( | ||
type.getEncoding()); | ||
} | ||
|
||
EncodingAttr getEncodingAttr(RankedTensorType type) { | ||
return dyn_cast_or_null<EncodingAttr>(type.getEncoding()); | ||
} | ||
|
||
bool hasPackedStorageAttr(RankedTensorType type) { | ||
return dyn_cast_or_null<PackedStorageAttr>(type.getEncoding()) != nullptr; | ||
} | ||
|
||
FailureOr<linalg::ContractionDimensions> | ||
getEncodingContractionDims(EncodingAttr encoding) { | ||
ArrayAttr indexingMapsAttr = encoding.getUserIndexingMaps(); | ||
if (!indexingMapsAttr) { | ||
return failure(); | ||
} | ||
SmallVector<AffineMap> indexingMaps = llvm::map_to_vector( | ||
indexingMapsAttr.getValue(), [](Attribute m) -> AffineMap { | ||
return cast<AffineMapAttr>(m).getAffineMap(); | ||
}); | ||
return linalg::inferContractionDims(indexingMaps); | ||
} | ||
|
||
std::string stringifyOperandIndex(IntegerAttr valueAttr) { | ||
uint64_t value = valueAttr.getValue().getZExtValue(); | ||
switch (value) { | ||
case MATMUL_LHS: | ||
return "LHS"; | ||
case MATMUL_RHS: | ||
return "RHS"; | ||
case MATMUL_RESULT: | ||
return "RESULT"; | ||
default: | ||
assert(false && "invalid index"); | ||
return ""; | ||
} | ||
} | ||
|
||
MatmulNarrowDim getMatmulNarrowDim(linalg::LinalgOp linalgOp, | ||
int narrowThreshold) { | ||
linalg::ContractionDimensions cDims = | ||
linalg::inferContractionDims(linalgOp).value(); | ||
AffineMap map = linalgOp.getIndexingMapsArray().back(); | ||
auto outType = llvm::cast<ShapedType>(linalgOp.getDpsInits()[0].getType()); | ||
auto getOutputSizeAtDimPos = [=](unsigned dimPos) -> int64_t { | ||
return outType.getDimSize( | ||
map.getResultPosition(getAffineDimExpr(dimPos, linalgOp->getContext())) | ||
.value()); | ||
}; | ||
// M or N can be empty instead of having an explicit dim size of 1 for matvec | ||
// and vecmat, so set to 1 if empty. | ||
int64_t mSize = cDims.m.empty() ? 1 : getOutputSizeAtDimPos(cDims.m[0]); | ||
int64_t nSize = cDims.n.empty() ? 1 : getOutputSizeAtDimPos(cDims.n[0]); | ||
|
||
MatmulNarrowDim narrowM, narrowN; | ||
if (!ShapedType::isDynamic(mSize) && mSize < narrowThreshold) { | ||
narrowM = {/*dim=*/MatmulNarrowDim::Dim::M, /*size=*/mSize}; | ||
} | ||
if (!ShapedType::isDynamic(nSize) && nSize < narrowThreshold) { | ||
narrowN = {/*dim=*/MatmulNarrowDim::Dim::N, /*size=*/nSize}; | ||
} | ||
|
||
return (narrowM && (!narrowN || mSize <= nSize)) ? narrowM : narrowN; | ||
} | ||
|
||
MatmulNarrowDim getMatmulNarrowDim(EncodingAttr encoding) { | ||
if (encoding.getOpType().getValue() != EncodingOpType::matmul) { | ||
return {}; | ||
} | ||
ArrayRef<int64_t> roundDimsTo = encoding.getRoundDimsToArray(); | ||
if (roundDimsTo.empty()) { | ||
return {}; | ||
} | ||
int m = roundDimsTo[0]; | ||
int n = roundDimsTo[1]; | ||
if (m < n) { | ||
return {MatmulNarrowDim::Dim::M, m}; | ||
} | ||
if (n < m) { | ||
return {MatmulNarrowDim::Dim::N, n}; | ||
} | ||
return {}; | ||
} | ||
|
||
bool isNarrowNResult(EncodingAttr encoding) { | ||
if (encoding.getOperandIndex().getValue() != IREE::Encoding::MATMUL_RESULT) { | ||
return false; | ||
} | ||
|
||
return IREE::Encoding::getMatmulNarrowDim(encoding).isN(); | ||
} | ||
|
||
RankedTensorType dropEncoding(RankedTensorType type) { | ||
return RankedTensorType::get(type.getShape(), type.getElementType()); | ||
} | ||
|
||
} // namespace mlir::iree_compiler::IREE::Encoding |