[mlir][Vector] Fix vector.extract lowering to llvm for 0-d vectors (#117731)

The current implementation of lowering to llvm for vector.extract
incorrectly assumes that if the number of indices is zero, the operation
can be folded away. This PR removes this condition and relies on the
folder to do it instead.

This PR also unifies the logic for scalar extracts and slice extracts,
which as a side effect also enables vector.extract lowering for n-d
vector.extract with dynamic inner most dimension. (This was only
prevented by a conservative check in the old implementation)
This commit is contained in:
Kunwar Grover
2024-12-04 17:26:53 +00:00
committed by GitHub
parent bb9bb68674
commit a8f927161b
2 changed files with 92 additions and 38 deletions

View File

@@ -1096,43 +1096,55 @@ public:
SmallVector<OpFoldResult> positionVec = getMixedValues(
adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);
// Extract entire vector. Should be handled by folder, but just to be safe.
ArrayRef<OpFoldResult> position(positionVec);
if (position.empty()) {
rewriter.replaceOp(extractOp, adaptor.getVector());
return success();
// The Vector -> LLVM lowering models N-D vectors as nested aggregates of
// 1-d vectors. This nesting is modeled using arrays. We do this conversion
// from a N-d vector extract to a nested aggregate vector extract in two
// steps:
// - Extract a member from the nested aggregate. The result can be
// a lower rank nested aggregate or a vector (1-D). This is done using
// `llvm.extractvalue`.
// - Extract a scalar out of the vector if needed. This is done using
// `llvm.extractelement`.
// Determine if we need to extract a member out of the aggregate. We
// always need to extract a member if the input rank >= 2.
bool extractsAggregate = extractOp.getSourceVectorType().getRank() >= 2;
// Determine if we need to extract a scalar as the result. We extract
// a scalar if the extract is full rank, i.e., the number of indices is
// equal to source vector rank.
bool extractsScalar = static_cast<int64_t>(positionVec.size()) ==
extractOp.getSourceVectorType().getRank();
// Since the LLVM type converter converts 0-d vectors to 1-d vectors, we
// need to add a position for this change.
if (extractOp.getSourceVectorType().getRank() == 0) {
Type idxType = typeConverter->convertType(rewriter.getIndexType());
positionVec.push_back(rewriter.getZeroAttr(idxType));
}
// One-shot extraction of vector from array (only requires extractvalue).
// Except for extracting 1-element vectors.
if (isa<VectorType>(resultType) &&
position.size() !=
static_cast<size_t>(extractOp.getSourceVectorType().getRank())) {
if (extractOp.hasDynamicPosition())
return failure();
Value extracted = rewriter.create<LLVM::ExtractValueOp>(
loc, adaptor.getVector(), getAsIntegers(position));
rewriter.replaceOp(extractOp, extracted);
return success();
}
// Potential extraction of 1-D vector from array.
Value extracted = adaptor.getVector();
if (position.size() > 1) {
if (extractOp.hasDynamicPosition())
if (extractsAggregate) {
ArrayRef<OpFoldResult> position(positionVec);
if (extractsScalar) {
// If we are extracting a scalar from the extracted member, we drop
// the last index, which will be used to extract the scalar out of the
// vector.
position = position.drop_back();
}
// llvm.extractvalue does not support dynamic dimensions.
if (!llvm::all_of(position, llvm::IsaPred<Attribute>)) {
return failure();
SmallVector<int64_t> nMinusOnePosition =
getAsIntegers(position.drop_back());
extracted = rewriter.create<LLVM::ExtractValueOp>(loc, extracted,
nMinusOnePosition);
}
extracted = rewriter.create<LLVM::ExtractValueOp>(
loc, extracted, getAsIntegers(position));
}
Value lastPosition = getAsLLVMValue(rewriter, loc, position.back());
// Remaining extraction of element from 1-D LLVM vector.
rewriter.replaceOpWithNewOp<LLVM::ExtractElementOp>(extractOp, extracted,
lastPosition);
if (extractsScalar) {
extracted = rewriter.create<LLVM::ExtractElementOp>(
loc, extracted, getAsLLVMValue(rewriter, loc, positionVec.back()));
}
rewriter.replaceOp(extractOp, extracted);
return success();
}
};