LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps...

Post on 29-Jul-2020

27 views 1 download

Transcript of LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps...

LLVM Data-structures overviewLLVM Data-structures

Marcello Maggioni

1Codeplay Software Ltd.

EuroLLVM 2014

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 1 / 31

Outline

1 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 2 / 31

Motivation Why having specific data-structures

Outline

1 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

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 platformdependentC++ Standard might not have a specific kind of data structures (likeHashMaps. With C++11 this problem was solved)Specialized data-structures can be made faster than the Standardgeneric ones

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 4 / 31

Motivation LLVM Resources

Outline

1 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

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.htmlhttp://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 subclassThe most exposed interface does not expose all methods indocumentation usually

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 7 / 31

Data-structures Vectors

Outline

1 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 8 / 31

Data-structures Vectors

Possible Choices

LLVM SmallVectorstd::vector

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 9 / 31

Data-structures Vectors

SmallVector

Vector-like data structureIs optimized to contain a fixed amount of elementsIt is flexible if more elements are addedInterface similar to std::vector

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 10 / 31

Data-structures Vectors

SmallVector

#inc lude " l l vm /ADT/ Sma l lVec to r . h"

Smal lVector<type , N> V;

void f oo ( Sma l lVec to r Imp l<type> &V) {}

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 11 / 31

Data-structures Vectors

SmallVector

Smal lVector<I n s t r u c t i o n ∗ , 10> WorkL is t ;

fo r ( . . . ) {I n s t r u c t i o n ∗ I = . . . ;WorkL is t . push_back ( I ) ;

}. . .while ( WorkL is t . empty ( ) ) {

I n s t r u c t i o n ∗ I = WorkL is 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 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 15 / 31

Data-structures Maps

Possible Choices

LLVM DenseMapLLVM StringMap (only for strings)std::mapstd::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 allocationIterators potentially invalidated after insertionMatches 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 keysAdditional Key types can be specified defining a customDenseMapInfo<> class

s t ruct Key In fo {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 getHashValue ( const T &Val ) { . . . }s t a t i c bool i s E q u a l ( const T &LHS , const T &RHS)

{ . . . }} ;

DenseMap<Key , Value , KeyIn fo> 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 keysStrings are copied into the map. They don’t store the pointer to themap as a key.Similar interface to DenseMapInsert 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" ;

Str ingMap<Data> Map ;Data D = { 10 , 5 } ;

Map . GetOrCreateVa lue ( s t r , D) ;Map [ s t r ] = D;Map . f i n d ( s t r ) ;Map . count ( 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 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 25 / 31

Data-structures Sets

Possible Choices

Sorted vectorsLLVM SmallSetstd::setstd::unordered_set

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 26 / 31

Data-structures Sets

SmallSet

Replacement for set in LLVMIt is implemented as a small vector of fixed size that is not sortedSearches are linear in timeWhen exceding the specified size switches to a quadratically probedset for some keys and std::set for othersCannot be iterated, only for querying

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 27 / 31

Data-structures Sets

SmallSet

Smal lSet<i n t ∗ , 10> Si n t a ;S . i n s e r t (&a ) ;S . count (&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 comparisonsOther data structures are available for specific needs (BitVectors,SparseSet, ValueMap ...)Using LLVM data-structures can give performance portabiltyLVM data-structures are not always faster and may require parametertuning

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 30 / 31

Data-structures Sets

Contacts

Marcello Maggionimarcello.maggioni@gmail.commarcello@codeplay.com

Marcello Maggioni (Codeplay Software Ltd.) LLVM Data-structures overview EuroLLVM 2014 31 / 31