Skip to content

Commit 17faae9

Browse files
author
Laszlo Kindrat
committed
[ADT] Introduce map_to_vector helper
The following pattern is common in the llvm codebase, as well as in downstream projects: ``` llvm::to_vector(llvm::map_range(container, lambda)) ``` This patch introduces a shortcut for this called `map_to_vector`. This template depends on both `llvm/ADT/SmallVector.h` and `llvm/ADT/STLExtras.h`, and since these are both relatively large and do not depend on each other, the `map_to_vector` helper is placed in a new header under `llvm/ADT/SmallVectorExtras.h`. Only a handful of use cases have been updated to use the new helper. Differential Revision: https://reviews.llvm.org/D145390
1 parent 7c57195 commit 17faae9

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- llvm/ADT/SmallVectorExtras.h -----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file defines less commonly used SmallVector utilities.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_ADT_SMALLVECTOREXTRAS_H
15+
#define LLVM_ADT_SMALLVECTOREXTRAS_H
16+
17+
#include "llvm/ADT/STLExtras.h"
18+
#include "llvm/ADT/SmallVector.h"
19+
20+
namespace llvm {
21+
22+
/// Map a range to a SmallVector with element types deduced from the mapping.
23+
template <class ContainerTy, class FuncTy>
24+
auto map_to_vector(ContainerTy &&C, FuncTy &&F) {
25+
return to_vector(
26+
map_range(std::forward<ContainerTy>(C), std::forward<FuncTy>(F)));
27+
}
28+
29+
} // namespace llvm
30+
31+
#endif // LLVM_ADT_SMALLVECTOREXTRAS_H

mlir/lib/Conversion/GPUCommon/GPUOpsLowering.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include "mlir/IR/Attributes.h"
1212
#include "mlir/IR/Builders.h"
1313
#include "mlir/IR/BuiltinTypes.h"
14-
#include "llvm/ADT/STLExtras.h"
14+
#include "llvm/ADT/SmallVectorExtras.h"
1515
#include "llvm/Support/FormatVariadic.h"
1616

1717
using namespace mlir;
@@ -486,8 +486,7 @@ LogicalResult impl::scalarizeVectorOp(Operation *op, ValueRange operands,
486486
return operand;
487487
return rewriter.create<LLVM::ExtractElementOp>(loc, operand, index);
488488
};
489-
auto scalarOperands =
490-
llvm::to_vector(llvm::map_range(operands, extractElement));
489+
auto scalarOperands = llvm::map_to_vector(operands, extractElement);
491490
Operation *scalarOp =
492491
rewriter.create(loc, name, scalarOperands, elementType, op->getAttrs());
493492
rewriter.create<LLVM::InsertElementOp>(loc, result, scalarOp->getResult(0),

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "mlir/Transforms/InliningUtils.h"
2121
#include "llvm/ADT/ScopeExit.h"
2222
#include "llvm/ADT/SmallBitVector.h"
23+
#include "llvm/ADT/SmallVectorExtras.h"
2324
#include "llvm/ADT/TypeSwitch.h"
2425
#include "llvm/Support/Debug.h"
2526
#include <numeric>
@@ -1360,11 +1361,11 @@ SmallVector<OpFoldResult>
13601361
mlir::affine::makeComposedFoldedMultiResultAffineApply(
13611362
OpBuilder &b, Location loc, AffineMap map,
13621363
ArrayRef<OpFoldResult> operands) {
1363-
return llvm::to_vector(llvm::map_range(
1364-
llvm::seq<unsigned>(0, map.getNumResults()), [&](unsigned i) {
1365-
return makeComposedFoldedAffineApply(b, loc, map.getSubMap({i}),
1366-
operands);
1367-
}));
1364+
return llvm::map_to_vector(llvm::seq<unsigned>(0, map.getNumResults()),
1365+
[&](unsigned i) {
1366+
return makeComposedFoldedAffineApply(
1367+
b, loc, map.getSubMap({i}), operands);
1368+
});
13681369
}
13691370

13701371
Value mlir::affine::makeComposedAffineMin(OpBuilder &b, Location loc,
@@ -4592,13 +4593,13 @@ void AffineDelinearizeIndexOp::build(OpBuilder &builder, OperationState &result,
45924593
result.addTypes(SmallVector<Type>(basis.size(), builder.getIndexType()));
45934594
result.addOperands(linearIndex);
45944595
SmallVector<Value> basisValues =
4595-
llvm::to_vector(llvm::map_range(basis, [&](OpFoldResult ofr) -> Value {
4596+
llvm::map_to_vector(basis, [&](OpFoldResult ofr) -> Value {
45964597
std::optional<int64_t> staticDim = getConstantIntValue(ofr);
45974598
if (staticDim.has_value())
45984599
return builder.create<arith::ConstantIndexOp>(result.location,
45994600
*staticDim);
46004601
return ofr.dyn_cast<Value>();
4601-
}));
4602+
});
46024603
result.addOperands(basisValues);
46034604
}
46044605

0 commit comments

Comments
 (0)