compiler support for profiling c++ template metaprograms józsef mihalicza, norbert pataki, zoltán...
TRANSCRIPT
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
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
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
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
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;
}}
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)
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
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
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)
SPLST’11
Preprocessor overheadPreprocessor overhead
Instrumentation Instrumentation
frameworkframework
Code instrumentationCode instrumentation
Code instrumentation 2Code instrumentation 2
Warning generationWarning generation
OverheadOverhead
Deviation of Deviation of
overheadoverhead
Modified compilerModified compiler
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
Thank you for your Thank you for your
attention!attention!Questions?
József Mihalicza, Norbert Pataki, Zoltán Porkoláb