compiler support for profiling c++ template metaprograms

17
Compiler Support for Compiler Support for Profiling Profiling C++ C++ Template Template Metaprograms Metaprograms József Mihalicza, Norbert Pataki, Zoltán Porkoláb Eötvös Loránd University Faculty of Informatics Dept. Of Programming Languages and Compilers

Upload: tayten

Post on 16-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

Compiler Support for Profiling C++ Template Metaprograms. József Mihalicza , Norbert Pataki , Zoltán Porkoláb Eötvös Loránd University Faculty of Informatics Dept. Of Programming Languages and Compilers. Outline. Templates in C++ Template metaprograms - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Compiler Support for Profiling  C++  Template Metaprograms

Compiler Support for Compiler Support for

Profiling Profiling

C++ C++ Template Template

MetaprogramsMetaprogramsJózsef Mihalicza, Norbert Pataki, Zoltán Porkoláb

Eötvös Loránd University

Faculty of Informatics

Dept. Of Programming Languages and Compilers

Page 2: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

OutlineOutline

Templates in C++Templates in C++

Template metaprogramsTemplate metaprograms

EfficiencEfficiencyy problems with C++ problems with C++

templates templates

Earlier efforts / Related worksEarlier efforts / Related works

Our solutionOur solution

ExamplesExamples

Page 3: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

C++ templatesC++ templates

Parametric polymorphismParametric polymorphism

Fundamental tools for generic Fundamental tools for generic

programmingprogramming

UnconstrainedUnconstrained

Implemented by instantiationImplemented by instantiation

Typical solution: multiply header filesTypical solution: multiply header files

Examples: standard library, inlc. STLExamples: standard library, inlc. STL

Page 4: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Template metaprogramsTemplate metaprograms

Unruh 1994: prime numbersUnruh 1994: prime numbers

Has been proved to be Turing-Has been proved to be Turing-

completecomplete

Functional programming paradigmFunctional programming paradigm Recursion + pattern matchingRecursion + pattern matching

Referential transparencyReferential transparency

No assignmentNo assignment

(Almost) no i/o(Almost) no i/o

Page 5: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Template metaprograms Template metaprograms

22template <int N>template <int N>

struct Factorialstruct Factorial

{{

enum { value = Factorial<N-1>::value * N }; enum { value = Factorial<N-1>::value * N };

};};

template <>template <>

struct Factorial<0>struct Factorial<0>

{{

enum { value = 1 };enum { value = 1 };

};};

int main()int main()

{{

int fact5 = Factorial<5>::value;int fact5 = Factorial<5>::value;

}}

Page 6: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Template metaprogram Template metaprogram

usageusage Expression templates (blitz++, …)Expression templates (blitz++, …)

Parser generators (boost::spirit, …)Parser generators (boost::spirit, …)

Static interface checking (boost::concept, Static interface checking (boost::concept,

…)…)

Configuration management (boost::math, Configuration management (boost::math,

…)…)

Active libraries (…)Active libraries (…)

DSL integration (Porkolab-DSL integration (Porkolab-

Sinkovics,GPCE2010)Sinkovics,GPCE2010)

Page 7: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Efficiency issuesEfficiency issues

Recursive header inclusionsRecursive header inclusions

Data structures (recursive templates)Data structures (recursive templates)

Imitating data manipulationsImitating data manipulations

Complex, unclear syntaxComplex, unclear syntax

Compilers was not optimized for TMPsCompilers was not optimized for TMPs

Hard to predict compilation time and Hard to predict compilation time and

memory usagememory usage

Page 8: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Earlier effortsEarlier efforts Veldhuizen (1996- )Veldhuizen (1996- )

Ad-hoc measurement, non-standard tools Ad-hoc measurement, non-standard tools

Gurtovoy – Abrahams (2004)Gurtovoy – Abrahams (2004) Measuring full programs to identify idiomsMeasuring full programs to identify idioms

Porkolab-MihaliczaPorkolab-Mihalicza Debugging (GPCE 2006) + Profiling (Splst 2007)Debugging (GPCE 2006) + Profiling (Splst 2007)

Steven Watanabe (2008)Steven Watanabe (2008) Boost library extensionBoost library extension

Compilers was not optimized for TMPsCompilers was not optimized for TMPs

Hard to predict compilation time and memory Hard to predict compilation time and memory

usageusage

Page 9: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Measuring compilation Measuring compilation

unitsunits AdvantagesAdvantages

Easy, platform and compiler independentEasy, platform and compiler independent

Get the big picture, understand behaviourGet the big picture, understand behaviour

Statistically correctStatistically correct

DrawbacksDrawbacks Artificial problemsArtificial problems

Not revealing details in complex programsNot revealing details in complex programs

Overheads (code generation, preprocessor)Overheads (code generation, preprocessor)

Page 10: Compiler Support for Profiling  C++  Template Metaprograms

SPLST’11

Preprocessor overheadPreprocessor overhead

Page 11: Compiler Support for Profiling  C++  Template Metaprograms

Instrumentation Instrumentation

frameworkframework

Page 12: Compiler Support for Profiling  C++  Template Metaprograms

Code instrumentationCode instrumentation

Page 13: Compiler Support for Profiling  C++  Template Metaprograms

Code instrumentation 2Code instrumentation 2

Page 14: Compiler Support for Profiling  C++  Template Metaprograms

Warning generationWarning generation

OverheadOverhead

Deviation of Deviation of

overheadoverhead

Page 15: Compiler Support for Profiling  C++  Template Metaprograms

Modified compilerModified compiler

Page 16: Compiler Support for Profiling  C++  Template Metaprograms

SummarySummary

We have to measure template metaprogramsWe have to measure template metaprograms(and perhaps all large projects with many (and perhaps all large projects with many templates)templates)

Measuring compilation units are not feasible in Measuring compilation units are not feasible in real-world industrial projectsreal-world industrial projects

Preprocessing step has a significant overheadPreprocessing step has a significant overhead Instrumentation has a linear distortionInstrumentation has a linear distortion Modification of compiler can eliminate warning Modification of compiler can eliminate warning

overheadoverhead Template metaprogram profiling is still learnedTemplate metaprogram profiling is still learned

Page 17: Compiler Support for Profiling  C++  Template Metaprograms

Thank you for your Thank you for your

attention!attention!Questions?

József Mihalicza, Norbert Pataki, Zoltán Porkoláb

[email protected], [email protected], [email protected]