-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[mlir] add tensor_static.extract/insert to take only static indices. #110550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cathyzhyi
wants to merge
1
commit into
llvm:main
Choose a base branch
from
cathyzhyi:piper_export_cl_680597887
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -39,6 +39,59 @@ using llvm::divideCeilSigned; | |
using llvm::divideFloorSigned; | ||
using llvm::mod; | ||
|
||
namespace { | ||
template <typename ExtractOpTy> | ||
OpFoldResult foldExtractFromElementsHelper(ExtractOpTy op, | ||
FromElementsOp fromElementsOp, | ||
ArrayRef<uint64_t> indices) { | ||
// Fold extract(from_elements(...)). | ||
auto tensorType = llvm::cast<RankedTensorType>(fromElementsOp.getType()); | ||
auto rank = tensorType.getRank(); | ||
assert(static_cast<int64_t>(indices.size()) == tensorType.getRank() && | ||
"rank mismatch"); | ||
int flatIndex = 0; | ||
int stride = 1; | ||
for (int i = rank - 1; i >= 0; --i) { | ||
flatIndex += indices[i] * stride; | ||
stride *= tensorType.getDimSize(i); | ||
} | ||
// Prevent out of bounds accesses. This can happen in invalid code that | ||
// will never execute. | ||
if (static_cast<int>(fromElementsOp.getElements().size()) <= flatIndex || | ||
flatIndex < 0) | ||
return {}; | ||
return fromElementsOp.getElements()[flatIndex]; | ||
} | ||
|
||
LogicalResult verifyStaticIndicesInBound(RankedTensorType type, | ||
ArrayRef<int64_t> indices) { | ||
ArrayRef<int64_t> shape = type.getShape(); | ||
for (auto [dim, index] : llvm::zip(shape, indices)) { | ||
if (index < 0) | ||
return failure(); | ||
if (ShapedType::isDynamic(dim)) | ||
continue; | ||
if (index >= dim) | ||
return failure(); | ||
} | ||
return success(); | ||
} | ||
|
||
template <typename InsertOpTy, typename AdapterTy> | ||
OpFoldResult insertOpFoldHelper(InsertOpTy insert, AdapterTy adaptor) { | ||
Attribute scalar = adaptor.getScalar(); | ||
Attribute dest = adaptor.getDest(); | ||
if (scalar && dest) { | ||
if (auto splatDest = llvm::dyn_cast<SplatElementsAttr>(dest)) { | ||
if (scalar == splatDest.getSplatValue<Attribute>()) | ||
return dest; | ||
} | ||
} | ||
return {}; | ||
} | ||
|
||
} // namespace | ||
|
||
/// Materialize a single constant operation from a given attribute value with | ||
/// the desired resultant type. | ||
Operation *TensorDialect::materializeConstant(OpBuilder &builder, | ||
|
@@ -1097,18 +1150,28 @@ namespace { | |
/// to | ||
/// | ||
/// %extracted_element = tensor.extract %source[%c0] : tensor<?xi32> | ||
struct ExtractFromTensorCast : public OpRewritePattern<tensor::ExtractOp> { | ||
using OpRewritePattern<tensor::ExtractOp>::OpRewritePattern; | ||
template <typename ExtractOpTy> | ||
struct ExtractFromTensorCast : public OpRewritePattern<ExtractOpTy> { | ||
using OpRewritePattern<ExtractOpTy>::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(tensor::ExtractOp extract, | ||
LogicalResult matchAndRewrite(ExtractOpTy extract, | ||
PatternRewriter &rewriter) const final { | ||
auto tensorCast = extract.getTensor().getDefiningOp<tensor::CastOp>(); | ||
auto tensorCast = | ||
extract.getTensor().template getDefiningOp<tensor::CastOp>(); | ||
if (!tensorCast) | ||
return failure(); | ||
if (!llvm::isa<RankedTensorType>(tensorCast.getSource().getType())) | ||
return failure(); | ||
rewriter.replaceOpWithNewOp<tensor::ExtractOp>( | ||
extract, tensorCast.getSource(), extract.getIndices()); | ||
Operation *op = extract; | ||
if (auto extractOp = llvm::dyn_cast<tensor::ExtractOp>(op)) { | ||
rewriter.replaceOpWithNewOp<tensor::ExtractOp>( | ||
extractOp, tensorCast.getSource(), extractOp.getIndices()); | ||
} else if (auto extractStaticOp = | ||
llvm::dyn_cast<tensor::ExtractStaticOp>(op)) { | ||
rewriter.replaceOpWithNewOp<tensor::ExtractStaticOp>( | ||
extractStaticOp, tensorCast.getSource(), | ||
extractStaticOp.getStaticIndices()); | ||
} | ||
return success(); | ||
} | ||
}; | ||
|
@@ -1145,22 +1208,8 @@ OpFoldResult ExtractOp::fold(FoldAdaptor adaptor) { | |
|
||
// Fold extract(from_elements(...)). | ||
if (auto fromElementsOp = getTensor().getDefiningOp<FromElementsOp>()) { | ||
auto tensorType = llvm::cast<RankedTensorType>(fromElementsOp.getType()); | ||
auto rank = tensorType.getRank(); | ||
assert(static_cast<int64_t>(indices.size()) == tensorType.getRank() && | ||
"rank mismatch"); | ||
int flatIndex = 0; | ||
int stride = 1; | ||
for (int i = rank - 1; i >= 0; --i) { | ||
flatIndex += indices[i] * stride; | ||
stride *= tensorType.getDimSize(i); | ||
} | ||
// Prevent out of bounds accesses. This can happen in invalid code that | ||
// will never execute. | ||
if (static_cast<int>(fromElementsOp.getElements().size()) <= flatIndex || | ||
flatIndex < 0) | ||
return {}; | ||
return fromElementsOp.getElements()[flatIndex]; | ||
return foldExtractFromElementsHelper<ExtractOp>(*this, fromElementsOp, | ||
indices); | ||
} | ||
|
||
// If this is an elements attribute, query the value at the given indices. | ||
|
@@ -1175,7 +1224,56 @@ OpFoldResult ExtractOp::fold(FoldAdaptor adaptor) { | |
|
||
void ExtractOp::getCanonicalizationPatterns(RewritePatternSet &results, | ||
MLIRContext *context) { | ||
results.add<ExtractFromTensorCast>(context); | ||
results.add<ExtractFromTensorCast<tensor::ExtractOp>>(context); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// ExtractStaticOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
void ExtractStaticOp::getAsmResultNames( | ||
function_ref<void(Value, StringRef)> setNameFn) { | ||
setNameFn(getResult(), "extracted"); | ||
} | ||
|
||
LogicalResult ExtractStaticOp::verify() { | ||
// Verify the # indices match if we have a ranked type. | ||
auto tensorType = llvm::cast<RankedTensorType>(getTensor().getType()); | ||
if (tensorType.getRank() != static_cast<int64_t>(getStaticIndices().size())) | ||
return emitOpError("incorrect number of indices for extract_static"); | ||
if (failed(verifyStaticIndicesInBound(tensorType, getStaticIndices()))) | ||
return emitOpError("static index out of bound for extract_static"); | ||
return success(); | ||
} | ||
|
||
OpFoldResult ExtractStaticOp::fold(FoldAdaptor adaptor) { | ||
// If this is a splat elements attribute, simply return the value. All of | ||
// the elements of a splat attribute are the same. | ||
if (Attribute tensor = adaptor.getTensor()) { | ||
if (auto splatTensor = llvm::dyn_cast<SplatElementsAttr>(tensor)) | ||
return splatTensor.getSplatValue<Attribute>(); | ||
} | ||
|
||
SmallVector<uint64_t, 8> indices(getStaticIndices()); | ||
// Fold extract(from_elements(...)). | ||
if (auto fromElementsOp = getTensor().getDefiningOp<FromElementsOp>()) { | ||
return foldExtractFromElementsHelper<ExtractStaticOp>(*this, fromElementsOp, | ||
indices); | ||
} | ||
|
||
// If this is an elements attribute, query the value at the given indices. | ||
if (Attribute tensor = adaptor.getTensor()) { | ||
auto elementsAttr = llvm::dyn_cast<ElementsAttr>(tensor); | ||
if (elementsAttr && elementsAttr.isValidIndex(indices)) | ||
return elementsAttr.getValues<Attribute>()[indices]; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
void ExtractStaticOp::getCanonicalizationPatterns(RewritePatternSet &results, | ||
MLIRContext *context) { | ||
results.add<ExtractFromTensorCast<tensor::ExtractStaticOp>>(context); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
|
@@ -1368,13 +1466,34 @@ LogicalResult InsertOp::verify() { | |
} | ||
|
||
OpFoldResult InsertOp::fold(FoldAdaptor adaptor) { | ||
Attribute scalar = adaptor.getScalar(); | ||
Attribute dest = adaptor.getDest(); | ||
if (scalar && dest) | ||
if (auto splatDest = llvm::dyn_cast<SplatElementsAttr>(dest)) | ||
if (scalar == splatDest.getSplatValue<Attribute>()) | ||
return dest; | ||
return {}; | ||
return insertOpFoldHelper<InsertOp, | ||
InsertOpGenericAdaptor<ArrayRef<Attribute>>>( | ||
*this, adaptor); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// InsertStaticOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
void InsertStaticOp::getAsmResultNames( | ||
function_ref<void(Value, StringRef)> setNameFn) { | ||
setNameFn(getResult(), "inserted"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use "insert_static"? |
||
} | ||
|
||
LogicalResult InsertStaticOp::verify() { | ||
// Verify the # indices match if we have a ranked type. | ||
auto destType = llvm::cast<RankedTensorType>(getDest().getType()); | ||
if (destType.getRank() != static_cast<int64_t>(getStaticIndices().size())) | ||
return emitOpError("incorrect number of indices for insert_static"); | ||
if (failed(verifyStaticIndicesInBound(destType, getStaticIndices()))) | ||
return emitOpError("static index out of bound for insert_static"); | ||
return success(); | ||
} | ||
|
||
OpFoldResult InsertStaticOp::fold(FoldAdaptor adaptor) { | ||
return insertOpFoldHelper<InsertStaticOp, | ||
InsertStaticOpGenericAdaptor<ArrayRef<Attribute>>>( | ||
*this, adaptor); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be marked as a static function.