Skip to content

Commit

Permalink
SIL: a few small changes in BasicBlockData
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
eeckstein committed Jan 22, 2021
1 parent 2c083ca commit 6713c0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions include/swift/SIL/BasicBlockData.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ namespace swift {
/// long as the invalidation mechanism ensures that the analysis data is not
/// retrieved after the function's block list changed.
///
/// The Vector template argument specifies how the data is stored. By default
/// it's a SmallVector with an inline-size of 4. This avoids memory allocation
/// for about 70% of all functions.
template <typename Data, typename Vector = llvm::SmallVector<Data, 4>>
/// The template argument \p N specifies how many Data elements can be stored
/// inline in the data vector. The default value of 32 avoids malloc for about
/// 90% of all functions.
template <typename Data, unsigned N = 32>
class BasicBlockData {
SILFunction *function;
Vector data;
llvm::SmallVector<Data, N> data;

/// The data is valid if validForBlockOrder matches the function's
/// BlockListChangeIdx.
Expand Down Expand Up @@ -200,6 +200,19 @@ class BasicBlockData {
const Data &operator[] (SILBasicBlock *block) const {
return data[getIndex(block)];
}

/// If \p block is a new block, i.e. created after this BasicBlockData was
/// constructed, creates a new Data with \p init and returns it.
Data &get(SILBasicBlock *block, llvm::function_ref<Data()> init) {
if (block->index < 0) {
assert(validForBlockOrder == function->BlockListChangeIdx &&
"BasicBlockData invalid because the function's block list changed");
validForBlockOrder = ++function->BlockListChangeIdx;
block->index = data.size();
data.push_back(init());
}
return data[getIndex(block)];
}
};

} // end swift namespace
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
friend class SILSuccessor;
friend class SILFunction;
friend class SILGlobalVariable;
template <typename Data, typename Vector> friend class BasicBlockData;
template <typename, unsigned> friend class BasicBlockData;
friend class BasicBlockBitfield;

public:
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class SILFunction
friend class SILBasicBlock;
friend class SILModule;
friend class SILFunctionBuilder;
template <typename Data, typename Vector> friend class BasicBlockData;
template <typename, unsigned> friend class BasicBlockData;
friend class BasicBlockBitfield;

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

0 comments on commit 6713c0f

Please sign in to comment.