620 likes | 634 Vues
Explore optimizations in XSLT programming language design and implementation, including techniques like inlining and loop transformations for performance enhancement. Learn about architecture-specific and independent optimizations.
E N D
Optimizations in XSLT http://www-sato.cc.u-tokyo.ac.jp/schuko/XSLT-opt.ppt 24/June/04 Programming Language Design and Implementation
Evaluation of Stylesheets • 1. Find the template which matches ‘/’2. Evaluate the template Evaluate each child in order if (child is LiteralResultElement) { Make the same node, and Evaluate Children as its children(Depends on Recursive Structure of TREEs) } else Programming Language Design and Implementation
Need for Optimization • Example: Matrix Multiplication(Itanium2 1.5GHz 6MB 3rd Cache: Intel Fortran Ver. 8.0) Programming Language Design and Implementation
List of Optimizations • -O2 • Architecture Specific Optimizations such as Global code scheduling, software pipelining, predication, speculation. • Inlining of intrinsics. • Architecture Independent Optimizations such as Programming Language Design and Implementation
List of Optimizations (2) • Higher Level Optimization • Constant propagation, copy propagation, dead-code elimination, global register allocation, global instruction scheduling, control speculation, loop unrolling, code selection, partial redundancy elimination, strength reduction, induction variable simplification, variable renaming, exception optimization, tail recursion elimination, peephole optimization, structure assignment lowering, dead store elimination Programming Language Design and Implementation
List of Optimizations (3) • -O3 • Prefetching, scalar replacement • Loop transformations Source of Performance Gain in most Technical Computing. Programming Language Design and Implementation
Points of Optimizations • They are NEVER magic or ad-hoc technologies. • Program Analysis • Dataflow Equation based Global Analysis • Symbolic Evaluation/Partial Evaluation Semantics Based Optimizations. Programming Language Design and Implementation
Points of Optimizations(2) • Architecture Specific Optimizations • New Features of Architectures • SuperScalar/VLIW • Vector Processing • Speculation • Prefetching • … Programming Language Design and Implementation
Points of Optimizations(3) • Source-to-source conversion • Accelerator + meta instruction • Algorithm transformation Programming Language Design and Implementation
do I do J do K c(I,j) = c(I,j) + a(I,k)*b(k,j) end do end do end do do J do K do I c(I,j)=c(I,j)+ a(I,k)*b(k,j) end do end do end do Source-to-Source Conversion Programming Language Design and Implementation
do I do J do K c(I,j) = c(I,j) + a(I,k)*b(k,j) end do end do end do !$omp parallel do do I do J do K c(I,j) = c(I,j) + a(I,k)*b(k,j) end do end do end do Accelerator + meta instruction Programming Language Design and Implementation
Call Bubblesort(a) Call quicksort(a) Needs to Ensure the transformation Preserves program semantics Algorithm Transformation Programming Language Design and Implementation
Optimization and Tuning • Tune – Adjust an engine to run smoothly • Performance tuning • Human side Job • Optimize – make the most effective use of • Performance or other metrics • Complicated in general – Computer side Job • A Kind of (automatic) Program transformation • They are Very Similar. • Profiling is Critical for Tuning Programming Language Design and Implementation
Tuning • Find HOT SPOT • Most resource consuming part • Profiling Tools profiling by sampling • cc –p gcc –pg • prof gprof Programming Language Design and Implementation
Tuning(2) • Profiling with hardware support • Most modern processors • instruction counts • CPU time • Cache miss rate/cache hit rate • Hardware utilization (vector unit etc.) Programming Language Design and Implementation
HOT SPOTS in XSLT Evaluation • Tuning of XSLT Engine + Stylesheet Optimization Performance Improvement • HOT SPOTS in XSLT Evaluation = • Evaluation of XPATH Expression • Template Instantiation(IN GENERAL, WHERE LOOP EXISTS) Programming Language Design and Implementation
Template Instantiation • Evaluation of <xsl:apply-templates/> For each (target node) 1. select matching template (query required) 2. make frame 3. call template Programming Language Design and Implementation
Procedure Call Optimization • Interprocedural Optimization • Dataflow across Calls • Inlining (Inline Expansion, Procedure Integration) • Save Call Overhead • Tail Call Elimination • Save Frame Overhead Programming Language Design and Implementation
void a() { b(2); } void b(int x) { printf(“%d\n”, x+1); } Void a() { printf(“%d\n”, 2+1); } Void b(int x) { printf(“%d\n”, x+1); } Inlining Programming Language Design and Implementation
Effect of Inlining • Call Overhead Reduction • Execution of Call: • Arguments Stack • Return address Stack • Address of subroutine Program Counter • Make frame • Save registers • Execute • Destroy frame (stack unreel) • Return address Program Counter • Destroy arguments Heavy Programming Language Design and Implementation
Effect of Inlining(2) • Very Effective for small functions • Methods • Inline prefix in C++ • Similar to macros Programming Language Design and Implementation
Effect of Inlining(3) • Further Optimization across Calls • Most optimizations are done within a procedure. • Code Size Increase (Drawback) • Harder Program Analysis (Drawback) Programming Language Design and Implementation
Effect of Inlining(4) • Alias problem in Fortran • Fortran does not assume aliases among arguments (exists in reality, though) • If inlined, Compiler must check if there is not any alias among arguments (often fails) • Then, poor code may be generated. Programming Language Design and Implementation
Note on Inlining • Note that You must not do manual inlining.Be sure to write a program for inlining. Programming Language Design and Implementation
… <xsl:call-template name=“a”/> <xsl:with-param name=“prefix” select=“..”/> … <xsl:template name=“a”> <xsl:param name=“prefix”/> <path> <xsl:value-of select=“$prefix”/> / <xsl:value-of select=“.”/> </path> </xsl:template> … <path> <xsl:value-of select=“..”/> / <xsl:value-of select=“.”/> </path> … Inlining in XSLT Programming Language Design and Implementation
Tail Call Elimination • Observation:<xsl:template name=“x”> <xsl:param name=“n”/> <xsl:choose> <xsl:when test=“…”> <xsl:call-template name=“a”/> <xsl:with-param name=“$n”/> </xsl:when> <xsl:otherwise> <xsl:call-template name=“x”/> <xsl:with-param name=“$n – 1”/> </xsl:otherwise> </xsl:choose> </xsl:template> Programming Language Design and Implementation
Tail Call Elimination(2) • Return from template a immediate return • Tail Call • Return from template x immediate return • Tail Recursion • Used as LOOP. • In this case, caller’s frame can be destroyed at calls of a or x. • However, Call is done, and a new frame is allocated for a and x. Programming Language Design and Implementation
Tail Call Elimination(3) • Ordinary…… destroy frame of x call of a jump to a make frame of a make frame of a execute execute destroy frame of a destroy frame of a return to x return from a destroy frame of x = return from x. return from x. Programming Language Design and Implementation
Tail Call Elimination(4) Before Elimination After Elimination frame call Ret address frame frame jump Ret address Ret address Programming Language Design and Implementation
Tail Call Elimination(5) • This Optimization Requires a Jump to a procedure • Low Level Stack Manipulation such as • Frame Destruction • Return Address Identification • Are Required. Programming Language Design and Implementation
Tail Recursion Elimination • Tail Recursion ⊆Tail Call Tail Call to Self. • Most Programming Languages have goto construct Can do Source-to-source conversion • Frame creation/destruction =rewrite local variables Programming Language Design and Implementation
Tail Recursion Elimination(2) • Observation: int f(int n) int ff(int n) { {top: if (n==0) return 0; if (n==0) return 0; else f(n-1); else {n=n-1; } goto top; } } Jump Frame destruction/ creation Programming Language Design and Implementation
Tail Recursion Elimination(3) Before Optimization After Optimization n=… call Ret address n=n-1 n=… call Ret address Ret address Programming Language Design and Implementation
Effect of Tail Call Elimination • Save Frame Creation/Destruction Cost • Save Space for Frame Creation • Significant when LOOP is implemented as Tail Recursion (XSLT, most Functional Languages). Programming Language Design and Implementation
Tail Call Elimination in XSLT • No GOTO Construct • ×Source-to-source conversion • Optimization of XSLT Engine • Recognition of Tail Call/Recursion. • Frame Adjustment, and Jump. Programming Language Design and Implementation
Tail Recursion Elimination in General int fact(int n) { if (n == 0) return 1; else return n * fact(n-1); } Not Tail Recursion Programming Language Design and Implementation
Tail Recursion Elimination in General(2) int fact2(int n, int res) { if (n==0) return res; else return fact2(n-1, n * res); } Tail Recursion Programming Language Design and Implementation
int fact2(int n, int res) { if (n==0) return res; else return fact2(n-1, n*res); } int fact(int n) { return fact2(n, 1);} int fact2(int n, int res) {top: if (n==0) return res; else { n = n-1; res = n * res; goto top; } int fact(int n) { return fact2(n,1);} Tail Recursion Elimination in General(3) Programming Language Design and Implementation
Tail Recursion Elimination in General(4) • How to Rewrite non tail-recursion to tail-recursion. • Commutative, associative operations • Some Linearity in Call • Introduction of Intermediate Variables. Programming Language Design and Implementation
Accumulator type fun f(n) fun f(n) … top: call f(n-1); … some-instructions(n); r *=some-instruction(n) return; n n-1; goto top; Programming Language Design and Implementation
Accumulator type(2) • If call graph is of the typef f f f … (linear),then, we can use r as Accumulator: r insn(n); r r*insn(n-1);r r*insn(n-2); … Programming Language Design and Implementation
XPATH Expression Optimization • Loop is a major source for improving performance. • In XSLT, We have Loop in Recursion and Xpath Expressions. Programming Language Design and Implementation
XML Tree Structure • Not the same as Unix file system. x a a b b b Programming Language Design and Implementation
Simple Evaluator • Evaluate(current, a/b/c):S φ; for-each x (child-node of current) { if (name(x) == a) { S S ∪ Evaluate (x, b/c); } } Loop Programming Language Design and Implementation
Menu of Optimizations • Partial Evaluation/Symbolic Evaluation • Statically Obtain Result before Evaluation. • Dataflow Equation Based Optimization • Solve Equation for Optimality in Dataflow • Redundancy Elimination Programming Language Design and Implementation
Menu of Optimizations(2) • Loop Optimization • Memory Hierarchy Optimization • Hardware Resource Utilization • Semantics Based Optimization Programming Language Design and Implementation
Partial Evaluation/Symbolic Evaluation • Definition: Specialize Code by Replacing a Part of Code by Statically Evaluated Code. • Static Evaluation and Specialization are Essential. Programming Language Design and Implementation
f(n, t) { if (t == 0) return g(n); else return h(n); } p(n) { return f(n, 0); } p(n) { return f0(n); } f0(n) { h(n); } Example of Specialization Programming Language Design and Implementation
Partial Evaluation in General • Strictly, Partial Evaluation is a Specialization. • However, together with Symbolic Evaluation, constant propagation and constant folding are also classified as Partial Evaluation. Programming Language Design and Implementation
a = 1; if (a+1 == 1) return 1; else return 2; a=1; if (1+1==1) return 1; else return 2; return 2; Constant Propagation/Folding Programming Language Design and Implementation