CIRCT 23.0.0git
Loading...
Searching...
No Matches
Utils.h
Go to the documentation of this file.
1//===- Utils.h - Miscellaneous utilities ----------------------------------===//
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// Miscellaneous utilities for CIRCT that do not fit in with other files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_SUPPORT_UTILS_H
14#define CIRCT_SUPPORT_UTILS_H
15
16#include "circt/Support/LLVM.h"
17#include "mlir/IR/Operation.h"
18#include "mlir/IR/Value.h"
19#include "llvm/Support/Parallel.h"
20
21namespace circt {
22
23/// Return true if a Value is created "underneath" an operation. This is
24/// frequently useful when negated as that indicates that a Value is defined
25/// "outside" the region of an Operation and that the Operation is thereby
26/// "capturing" the value.
27inline bool isAncestorOfValueOwner(Operation *op, Value value) {
28 return op->isAncestor(value.getParentBlock()->getParentOp());
29}
30
31/// Remove elements from the input array corresponding to set bits in
32/// `indicesToDrop`, returning the elements not mentioned.
33template <typename T>
34static SmallVector<T>
35removeElementsAtIndices(ArrayRef<T> input,
36 const llvm::BitVector &indicesToDrop) {
37#ifndef NDEBUG
38 if (!input.empty()) {
39 int lastIndex = indicesToDrop.find_last();
40 if (lastIndex >= 0)
41 assert((size_t)lastIndex < input.size() && "index out of range");
42 }
43#endif
44
45 // If the input is empty (which is an optimization we do for certain array
46 // attributes), simply return an empty vector.
47 if (input.empty())
48 return {};
49
50 // Copy over the live chunks.
51 size_t lastCopied = 0;
52 SmallVector<T> result;
53 result.reserve(input.size() - indicesToDrop.count());
54
55 for (unsigned indexToDrop : indicesToDrop.set_bits()) {
56 // If we skipped over some valid elements, copy them over.
57 if (indexToDrop > lastCopied) {
58 result.append(input.begin() + lastCopied, input.begin() + indexToDrop);
59 lastCopied = indexToDrop;
60 }
61 // Ignore this value so we don't copy it in the next iteration.
62 ++lastCopied;
63 }
64
65 // If there are live elements at the end, copy them over.
66 if (lastCopied < input.size())
67 result.append(input.begin() + lastCopied, input.end());
68
69 return result;
70}
71
72//===----------------------------------------------------------------------===//
73// Parallel utilities
74//===----------------------------------------------------------------------===//
75
76/// Wrapper for llvm::parallelTransformReduce that performs the transform_reduce
77/// serially when MLIR multi-threading is disabled.
78/// Does not add a ParallelDiagnosticHandler like mlir::parallelFor.
79template <class IterTy, class ResultTy, class ReduceFuncTy,
80 class TransformFuncTy>
81static ResultTy transformReduce(MLIRContext *context, IterTy begin, IterTy end,
82 ResultTy init, ReduceFuncTy reduce,
83 TransformFuncTy transform) {
84 // Parallel when enabled
85 if (context->isMultithreadingEnabled())
86 return llvm::parallelTransformReduce(begin, end, init, reduce, transform);
87
88 // Serial fallback (from llvm::parallelTransformReduce)
89 for (IterTy i = begin; i != end; ++i)
90 init = reduce(std::move(init), transform(*i));
91 return std::move(init);
92}
93
94/// Range wrapper
95template <class RangeTy, class ResultTy, class ReduceFuncTy,
96 class TransformFuncTy>
97static ResultTy transformReduce(MLIRContext *context, RangeTy &&r,
98 ResultTy init, ReduceFuncTy reduce,
99 TransformFuncTy transform) {
100 return transformReduce(context, std::begin(r), std::end(r), init, reduce,
101 transform);
102}
103
104} // namespace circt
105
106#endif // CIRCT_SUPPORT_UTILS_H
assert(baseType &&"element must be base type")
static std::unique_ptr< Context > context
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
static ResultTy transformReduce(MLIRContext *context, IterTy begin, IterTy end, ResultTy init, ReduceFuncTy reduce, TransformFuncTy transform)
Wrapper for llvm::parallelTransformReduce that performs the transform_reduce serially when MLIR multi...
Definition Utils.h:81
bool isAncestorOfValueOwner(Operation *op, Value value)
Return true if a Value is created "underneath" an operation.
Definition Utils.h:27
static SmallVector< T > removeElementsAtIndices(ArrayRef< T > input, const llvm::BitVector &indicesToDrop)
Remove elements from the input array corresponding to set bits in indicesToDrop, returning the elemen...
Definition Utils.h:35