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

31
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

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

Page 1: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 2: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Outline

1 MotivationWhy having specific data-structuresLLVM Resources

2 Data-structuresVectorsMapsSets

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

Page 3: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 4: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 5: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 6: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 7: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 8: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 9: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Vectors

Possible Choices

LLVM SmallVectorstd::vector

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

Page 10: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 11: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 12: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 13: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Vectors

SmallVector vs World

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

Page 14: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Vectors

SmallVector vs World

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

Page 15: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 16: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 17: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 18: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 19: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Maps

DenseMap vs World

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

Page 20: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Maps

DenseMap vs World

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

Page 21: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 22: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 23: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 24: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Maps

StringMap vs World

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

Page 25: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 26: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Sets

Possible Choices

Sorted vectorsLLVM SmallSetstd::setstd::unordered_set

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

Page 27: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 28: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 29: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Sets

SmallSet vs World

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

Page 30: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

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

Page 31: LLVM Data-structures overviewllvm.org/devmtg/2014-04/PDFs/LightningTalks/data...Data-structures Maps Outline 1 Motivation Whyhavingspecificdata-structures LLVMResources 2 Data-structures

Data-structures Sets

Contacts

Marcello [email protected]@codeplay.com

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