Skip to content

Commit a8f9271

Browse files
authored
[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)
1 parent bb9bb68 commit a8f9271

File tree

2 files changed

+91
-37
lines changed

2 files changed

+91
-37
lines changed

mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,43 +1096,55 @@ class VectorExtractOpConversion
10961096
SmallVector<OpFoldResult> positionVec = getMixedValues(
10971097
adaptor.getStaticPosition(), adaptor.getDynamicPosition(), rewriter);
10981098

1099-
// Extract entire vector. Should be handled by folder, but just to be safe.
1100-
ArrayRef<OpFoldResult> position(positionVec);
1101-
if (position.empty()) {
1102-
rewriter.replaceOp(extractOp, adaptor.getVector());
1103-
return success();
1104-
}
1105-
1106-
// One-shot extraction of vector from array (only requires extractvalue).
1107-
// Except for extracting 1-element vectors.
1108-
if (isa<VectorType>(resultType) &&
1109-
position.size() !=
1110-
static_cast<size_t>(extractOp.getSourceVectorType().getRank())) {
1111-
if (extractOp.hasDynamicPosition())
1112-
return failure();
1113-
1114-
Value extracted = rewriter.create<LLVM::ExtractValueOp>(
1115-
loc, adaptor.getVector(), getAsIntegers(position));
1116-
rewriter.replaceOp(extractOp, extracted);
1117-
return success();
1099+
// The Vector -> LLVM lowering models N-D vectors as nested aggregates of
1100+
// 1-d vectors. This nesting is modeled using arrays. We do this conversion
1101+
// from a N-d vector extract to a nested aggregate vector extract in two
1102+
// steps:
1103+
// - Extract a member from the nested aggregate. The result can be
1104+
// a lower rank nested aggregate or a vector (1-D). This is done using
1105+
// `llvm.extractvalue`.
1106+
// - Extract a scalar out of the vector if needed. This is done using
1107+
// `llvm.extractelement`.
1108+
1109+
// Determine if we need to extract a member out of the aggregate. We
1110+
// always need to extract a member if the input rank >= 2.
1111+
bool extractsAggregate = extractOp.getSourceVectorType().getRank() >= 2;
1112+
// Determine if we need to extract a scalar as the result. We extract
1113+
// a scalar if the extract is full rank, i.e., the number of indices is
1114+
// equal to source vector rank.
1115+
bool extractsScalar = static_cast<int64_t>(positionVec.size()) ==
1116+
extractOp.getSourceVectorType().getRank();
1117+
1118+
// Since the LLVM type converter converts 0-d vectors to 1-d vectors, we
1119+
// need to add a position for this change.
1120+
if (extractOp.getSourceVectorType().getRank() == 0) {
1121+
Type idxType = typeConverter->convertType(rewriter.getIndexType());
1122+
positionVec.push_back(rewriter.getZeroAttr(idxType));
11181123
}
11191124

1120-
// Potential extraction of 1-D vector from array.
11211125
Value extracted = adaptor.getVector();
1122-
if (position.size() > 1) {
1123-
if (extractOp.hasDynamicPosition())
1126+
if (extractsAggregate) {
1127+
ArrayRef<OpFoldResult> position(positionVec);
1128+
if (extractsScalar) {
1129+
// If we are extracting a scalar from the extracted member, we drop
1130+
// the last index, which will be used to extract the scalar out of the
1131+
// vector.
1132+
position = position.drop_back();
1133+
}
1134+
// llvm.extractvalue does not support dynamic dimensions.
1135+
if (!llvm::all_of(position, llvm::IsaPred<Attribute>)) {
11241136
return failure();
1137+
}
1138+
extracted = rewriter.create<LLVM::ExtractValueOp>(
1139+
loc, extracted, getAsIntegers(position));
1140+
}
11251141

1126-
SmallVector<int64_t> nMinusOnePosition =
1127-
getAsIntegers(position.drop_back());
1128-
extracted = rewriter.create<LLVM::ExtractValueOp>(loc, extracted,
1129-
nMinusOnePosition);
1142+
if (extractsScalar) {
1143+
extracted = rewriter.create<LLVM::ExtractElementOp>(
1144+
loc, extracted, getAsLLVMValue(rewriter, loc, positionVec.back()));
11301145
}
11311146

1132-
Value lastPosition = getAsLLVMValue(rewriter, loc, position.back());
1133-
// Remaining extraction of element from 1-D LLVM vector.
1134-
rewriter.replaceOpWithNewOp<LLVM::ExtractElementOp>(extractOp, extracted,
1135-
lastPosition);
1147+
rewriter.replaceOp(extractOp, extracted);
11361148
return success();
11371149
}
11381150
};

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,26 +1290,68 @@ func.func @extract_scalar_from_vec_1d_f32_dynamic_idx_scalable(%arg0: vector<[16
12901290

12911291
// -----
12921292

1293-
func.func @extract_scalar_from_vec_2d_f32_dynamic_idx(%arg0: vector<1x16xf32>, %arg1: index) -> f32 {
1293+
func.func @extract_scalar_from_vec_2d_f32_inner_dynamic_idx(%arg0: vector<1x16xf32>, %arg1: index) -> f32 {
12941294
%0 = vector.extract %arg0[0, %arg1]: f32 from vector<1x16xf32>
12951295
return %0 : f32
12961296
}
12971297

1298-
// Multi-dim vectors are not supported but this test shouldn't crash.
1298+
// Lowering supports extracting from multi-dim vectors with dynamic indices
1299+
// provided that only the trailing index is dynamic.
12991300

1300-
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_dynamic_idx(
1301-
// CHECK: vector.extract
1301+
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_inner_dynamic_idx(
1302+
// CHECK: llvm.extractvalue
1303+
// CHECK: llvm.extractelement
13021304

1303-
func.func @extract_scalar_from_vec_2d_f32_dynamic_idx_scalable(%arg0: vector<1x[16]xf32>, %arg1: index) -> f32 {
1305+
func.func @extract_scalar_from_vec_2d_f32_inner_dynamic_idx_scalable(%arg0: vector<1x[16]xf32>, %arg1: index) -> f32 {
13041306
%0 = vector.extract %arg0[0, %arg1]: f32 from vector<1x[16]xf32>
13051307
return %0 : f32
13061308
}
13071309

1308-
// Multi-dim vectors are not supported but this test shouldn't crash.
1310+
// Lowering supports extracting from multi-dim vectors with dynamic indices
1311+
// provided that only the trailing index is dynamic.
1312+
1313+
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_inner_dynamic_idx_scalable(
1314+
// CHECK: llvm.extractvalue
1315+
// CHECK: llvm.extractelement
1316+
1317+
// -----
13091318

1310-
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_dynamic_idx_scalable(
1319+
func.func @extract_scalar_from_vec_2d_f32_outer_dynamic_idx(%arg0: vector<1x16xf32>, %arg1: index) -> f32 {
1320+
%0 = vector.extract %arg0[%arg1, 0]: f32 from vector<1x16xf32>
1321+
return %0 : f32
1322+
}
1323+
1324+
// Lowering supports extracting from multi-dim vectors with dynamic indices
1325+
// provided that only the trailing index is dynamic.
1326+
1327+
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_outer_dynamic_idx(
13111328
// CHECK: vector.extract
13121329

1330+
func.func @extract_scalar_from_vec_2d_f32_outer_dynamic_idx_scalable(%arg0: vector<1x[16]xf32>, %arg1: index) -> f32 {
1331+
%0 = vector.extract %arg0[%arg1, 0]: f32 from vector<1x[16]xf32>
1332+
return %0 : f32
1333+
}
1334+
1335+
// Lowering does not support extracting from multi-dim vectors with non trailing
1336+
// dynamic index, but it shouldn't crash.
1337+
1338+
// CHECK-LABEL: @extract_scalar_from_vec_2d_f32_outer_dynamic_idx_scalable(
1339+
// CHECK: vector.extract
1340+
1341+
// -----
1342+
1343+
func.func @extract_scalar_from_vec_0d_index(%arg0: vector<index>) -> index {
1344+
%0 = vector.extract %arg0[]: index from vector<index>
1345+
return %0 : index
1346+
}
1347+
// CHECK-LABEL: @extract_scalar_from_vec_0d_index(
1348+
// CHECK-SAME: %[[A:.*]]: vector<index>)
1349+
// CHECK: %[[T0:.*]] = builtin.unrealized_conversion_cast %[[A]] : vector<index> to vector<1xi64>
1350+
// CHECK: %[[T1:.*]] = llvm.mlir.constant(0 : i64) : i64
1351+
// CHECK: %[[T2:.*]] = llvm.extractelement %[[T0]][%[[T1]] : i64] : vector<1xi64>
1352+
// CHECK: %[[T3:.*]] = builtin.unrealized_conversion_cast %[[T2]] : i64 to index
1353+
// CHECK: return %[[T3]] : index
1354+
13131355
// -----
13141356

13151357
func.func @insertelement_into_vec_0d_f32(%arg0: f32, %arg1: vector<f32>) -> vector<f32> {

0 commit comments

Comments
 (0)