LLVM Data-structures overview LLVM Data-structures Marcello Maggioni 1 Codeplay
Software Ltd.
EuroLLVM 2014
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
1 / 31
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
2 / 31
Motivation
Why having specific data-structures
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
3 / 31
Motivation
Why having specific data-structures
Why not using standard structures?
C++ Standard data-structures have performance that is platform dependent C++ Standard might not have a specific kind of data structures (like HashMaps. With C++11 this problem was solved) Specialized data-structures can be made faster than the Standard generic ones
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
4 / 31
Motivation
LLVM Resources
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
5 / 31
Motivation
LLVM Resources
Resources on LLVM Data-Structures
http://llvm.org/docs/ProgrammersManual.html http://llvm.org/docs/doxygen/html/
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
6 / 31
Motivation
LLVM Resources
Looking at Doxygen info
Look for methods in every subclass The most exposed interface does not expose all methods in documentation usually
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
7 / 31
Data-structures
Vectors
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
8 / 31
Data-structures
Vectors
Possible Choices
LLVM SmallVector std::vector
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
9 / 31
Data-structures
Vectors
SmallVector
Vector-like data structure Is optimized to contain a fixed amount of elements It is flexible if more elements are added Interface similar to std::vector
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
10 / 31
Data-structures
Vectors
SmallVector
#i n c l u d e " l l v m /ADT/ S m a l l V e c t o r . h" S m a l l V e c t o r V ; v o i d f o o ( S m a l l V e c t o r I m p l &V) { }
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
11 / 31
Data-structures
Vectors
SmallVector
S m a l l V e c t o r W o r k L i s t ; for ( . . . ) { Instruction ∗I = . . . ; W o r k L i s t . push_back ( I ) ; } ... w h i l e ( W o r k L i s t . empty ( ) ) { I n s t r u c t i o n ∗ I = W o r k L i s t . pop_back_val ( ) ; ... }
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
12 / 31
Data-structures
Vectors
SmallVector vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
13 / 31
Data-structures
Vectors
SmallVector vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
14 / 31
Data-structures
Maps
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
15 / 31
Data-structures
Maps
Possible Choices
LLVM DenseMap LLVM StringMap (only for strings) std::map std::unordered_map
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
16 / 31
Data-structures
Maps
DenseMap
DenseMap is a quadratically probed HashMap. Keeps everything in a single memory allocation Iterators potentially invalidated after insertion Matches pretty closely std::map interface insert(std::pair) find(Key&) count(Key&) begin(), end() (unordered)
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
17 / 31
Data-structures
Maps
DenseMap Keys Supports all pointers and integer types as keys Additional Key types can be specified defining a custom DenseMapInfo class struct KeyInfo { s t a t i c i n l i n e T getEmptyKey ( ) { . . . } s t a t i c i n l i n e T getTombstoneKey ( ) { . . . } s t a t i c unsigned g e t H a s h V a l u e ( const T &V a l ) { . . . } s t a t i c bool i s E q u a l ( const T &LHS , const T &RHS) {...} }; DenseMap M;
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
18 / 31
Data-structures
Maps
DenseMap vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
19 / 31
Data-structures
Maps
DenseMap vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
20 / 31
Data-structures
Maps
StringMap
Specific implementation of an HashMap only for having strings as keys Strings are copied into the map. They don’t store the pointer to the map as a key. Similar interface to DenseMap Insert is different though ... it is actually called GetOrCreateValue()
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
21 / 31
Data-structures
Maps
StringMap
const char ∗ s t r = "__some_symbol" ; StringMap Map ; Data D = { 1 0 , 5 } ; Map . G e t O r C r e a t e V a l u e ( s t r , D) ; Map [ s t r ] = D; Map . f i n d ( s t r ) ; Map . c o u n t ( s t r ) ;
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
22 / 31
Data-structures
Maps
StringMap vs World Storing a 16 character wide random string
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
23 / 31
Data-structures
Maps
StringMap vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
24 / 31
Data-structures
Sets
Outline
1
Motivation Why having specific data-structures LLVM Resources
2
Data-structures Vectors Maps Sets
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
25 / 31
Data-structures
Sets
Possible Choices
Sorted vectors LLVM SmallSet std::set std::unordered_set
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
26 / 31
Data-structures
Sets
SmallSet
Replacement for set in LLVM It is implemented as a small vector of fixed size that is not sorted Searches are linear in time When exceding the specified size switches to a quadratically probed set for some keys and std::set for others Cannot be iterated, only for querying
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
27 / 31
Data-structures
Sets
SmallSet
S m a l l S e t S int a ; S . i n s e r t (&a ) ; S . c o u n t (&a ) ;
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
28 / 31
Data-structures
Sets
SmallSet vs World
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
29 / 31
Data-structures
Sets
Summary
Covered basic data structures and their performance comparisons Other data structures are available for specific needs (BitVectors, SparseSet, ValueMap ...) Using LLVM data-structures can give performance portabilty LVM data-structures are not always faster and may require parameter tuning
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
30 / 31
Data-structures
Sets
Contacts
Marcello Maggioni
[email protected] [email protected]
Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview
EuroLLVM 2014
31 / 31