Skip to content

Commit 6713c0f

Browse files
committed
SIL: a few small changes in BasicBlockData
* Instead of passing the vector type as template argument, use a SmallVector and just pass the inline size * Increase the inline size to 32. Found by experiment, this fits 90% of all functions. * add an API for getting data for newly created blocks.
1 parent 2c083ca commit 6713c0f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

include/swift/SIL/BasicBlockData.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ namespace swift {
4141
/// long as the invalidation mechanism ensures that the analysis data is not
4242
/// retrieved after the function's block list changed.
4343
///
44-
/// The Vector template argument specifies how the data is stored. By default
45-
/// it's a SmallVector with an inline-size of 4. This avoids memory allocation
46-
/// for about 70% of all functions.
47-
template <typename Data, typename Vector = llvm::SmallVector<Data, 4>>
44+
/// The template argument \p N specifies how many Data elements can be stored
45+
/// inline in the data vector. The default value of 32 avoids malloc for about
46+
/// 90% of all functions.
47+
template <typename Data, unsigned N = 32>
4848
class BasicBlockData {
4949
SILFunction *function;
50-
Vector data;
50+
llvm::SmallVector<Data, N> data;
5151

5252
/// The data is valid if validForBlockOrder matches the function's
5353
/// BlockListChangeIdx.
@@ -200,6 +200,19 @@ class BasicBlockData {
200200
const Data &operator[] (SILBasicBlock *block) const {
201201
return data[getIndex(block)];
202202
}
203+
204+
/// If \p block is a new block, i.e. created after this BasicBlockData was
205+
/// constructed, creates a new Data with \p init and returns it.
206+
Data &get(SILBasicBlock *block, llvm::function_ref<Data()> init) {
207+
if (block->index < 0) {
208+
assert(validForBlockOrder == function->BlockListChangeIdx &&
209+
"BasicBlockData invalid because the function's block list changed");
210+
validForBlockOrder = ++function->BlockListChangeIdx;
211+
block->index = data.size();
212+
data.push_back(init());
213+
}
214+
return data[getIndex(block)];
215+
}
203216
};
204217

205218
} // end swift namespace

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
3535
friend class SILSuccessor;
3636
friend class SILFunction;
3737
friend class SILGlobalVariable;
38-
template <typename Data, typename Vector> friend class BasicBlockData;
38+
template <typename, unsigned> friend class BasicBlockData;
3939
friend class BasicBlockBitfield;
4040

4141
public:

include/swift/SIL/SILFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class SILFunction
148148
friend class SILBasicBlock;
149149
friend class SILModule;
150150
friend class SILFunctionBuilder;
151-
template <typename Data, typename Vector> friend class BasicBlockData;
151+
template <typename, unsigned> friend class BasicBlockData;
152152
friend class BasicBlockBitfield;
153153

154154
/// Module - The SIL module that the function belongs to.

0 commit comments

Comments
 (0)