Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Interprocedural optimization

From Wikipedia, the free encyclopedia
Computer program optimization method

Interprocedural optimization (IPO) is a collection ofcompiler techniques used incomputer programming to improve performance in programs containing many frequently usedfunctions of small or medium length. IPO differs from othercompiler optimizations by analyzing the entire program as opposed to a single function or block of code.

IPO seeks to reduce or eliminate duplicate calculations and inefficient use of memory and to simplify iterative sequences such as loops. If a call to another routine occurs within a loop, IPO analysis may determine that it is best toinline that routine. Additionally, IPO may re-order the routines for better memory layout andlocality.

IPO may also include typical compiler optimizations applied on a whole-program level, for example,dead code elimination (DCE), which removes code that is never executed. IPO also tries to ensure better use of constants. Modern compilers offer IPO as an option at compile-time. The actual IPO process may occur at any step between the human-readable source code and producing a finished executable binary program.

For languages that compile on a file-by-file basis, effective IPO acrosstranslation units (module files) requires knowledge of the "entry points" of the program so that awhole program optimization (WPO) can be run. In many cases, this is implemented as alink-time optimization (LTO) pass, because the whole program is visible to the linker.

Analysis

[edit]

The objective of any optimization for speed is to have the program run as swiftly as possible; the problem is that it is not possible for a compiler to correctly analyze a program and determine what itwill do, much less what the programmerintended for it to do. By contrast, human programmers start at the other end with a purpose and attempt to produce a program that will achieve it, preferably without expending a lot of thought in the process.

For various reasons, including readability, programs are frequently broken up into a number of procedures that handle a few general cases. However, the generality of each procedure may result in wasted effort in specific usages. Interprocedural optimization represents an attempt at reducing this waste.

Suppose there is a procedure that evaluatesf(x), and thatf is apure function, and the code requests the result off(6) and then later,f(6) again. This second evaluation is almost certainly unnecessary: the result could have instead been saved and referred to later. This simple optimization is foiled the moment that the implementation off(x) becomes impure; that is, its execution involves references to parameters other than the explicit argument6 that has been changed between the invocations, or side effects such as printing some message to a log, counting the number of evaluations, accumulating theCPU time consumed, preparing internal tables so that subsequent invocations for related parameters will be facilitated, and so forth. Losing these side effects via non-evaluation a second time may be acceptable, or they may not.

More generally, aside from optimization, the second reason to use procedures is to avoid duplication of code that would produce the same results, or almost the same results, each time the procedure is performed. A general approach to optimization would therefore be to reverse this: some or all invocations of a certain procedure are replaced by the respective code, with the parameters appropriately substituted. The compiler will then try to optimize the result.

WPO and LTO

[edit]

Whole program optimization (WPO) is the compiler optimization of a program using information about all themodules in the program. Normally, optimizations are performed on aper module, "compiland", basis; but this approach, while easier to write and test and less demanding of resources during the compilation itself, does not allow certainty about the safety of a number of optimizations such as aggressiveinlining and thus cannot perform them even if they would actually turn out to be efficiency gains that do not change thesemantics of the emitted object code.

Link-time optimization (LTO) is a type of program optimization performed by a compiler to a program atlink time. Link time optimization is relevant in programming languages that compile programs on a file-by-file basis, and then link those files together (such asC andFortran), rather than all at once (such asJava'sjust-in-time compilation (JIT)).

Once all files have been compiled separately intoobject files, traditionally, a compiler links (merges) the object files into a single file, theexecutable. However, in LTO as implemented by theGNU Compiler Collection (GCC) andLLVM, the compiler is able to dump itsintermediate representation (IR), i.e.GIMPLE bytecode or LLVM bitcode, respectively, so that all the different compilation units that will go to make up a single executable can be optimized as a single module when the link finally happens. This expands the scope of interprocedural optimizations to encompass the whole program (or, rather, everything that is visible at link time). With link-time optimization, the compiler can apply various forms of interprocedural optimization to the whole program, allowing for deeper analysis, more optimization, and ultimately better program performance.

In practice, LTO does not always optimize the entire program—library functions, especiallydynamically linked shared objects, are intentionally kept out to avoid excessive duplication and to allow for updating.Static linking does naturally lend to the concept of LTO, but it only works with library archives that contain IR objects as opposed to machine-code only object files.[1] Due to performance concerns, not even the entire unit is always directly used—a program could be partitioned in a divide-and-conquer style LTO such as GCC's WHOPR.[2] And of course, when the program being built is itself a library, the optimization would keep every externally-available (exported) symbol, without trying too hard at removing them as a part of DCE.[1]

A much more limited form of WPO is still possible without LTO, as exemplified by GCC's-fwhole-program switch. This mode makes GCC assume that the module being compiled contains theentry point of the entire program, so that every other function in it is not externally used and can be safely optimized away. Since it only applies to a single module, it cannot truly encompass the whole program. It can be combined with LTO in the one-big-module sense, which is useful when the linker is not communicating back to GCC about what entry points or symbols are being used externally.[1]

History

[edit]

Forprocedural languages likeALGOL, interprocedural analysis and optimization appear to have entered commercial practice in the early 1970s. IBM'sPL/I Optimizing Compiler performed interprocedural analysis to understand the side effects of both procedure calls and exceptions (cast, in PL/I terms as "on conditions")[3] and in papers byFran Allen.[4][5] Work on compilation of theAPL programming language was necessarily interprocedural.[6][7]

The techniques of interprocedural analysis and optimization were the subject of academic research in the 1980s and 1990s. They re-emerged into the commercial compiler world in the early 1990s with compilers from bothConvex Computer Corporation (the "Application Compiler" for the Convex C4) and from Ardent (the compiler for theArdent Titan). These compilers demonstrated that the technologies could be made sufficiently fast to be acceptable in a commercial compiler; subsequently interprocedural techniques have appeared in a number of commercial and non-commercial systems.

Flags and implementation

[edit]

Unix-like

[edit]

TheGNU Compiler Collection has function inlining at all optimization levels. At-O1 this only applies to those only called once (-finline-functions-once), at-O2 this constraint is relaxed (-finline-functions). By default this is a single-file-only behavior, but with link-time optimization-flto it becomes whole program.[1]Clang's command-line interface is similar to that of GCC, with the exception that there is no-fwhole-program option.[8]

Object files produced by LTO contain a compiler-specificintermediate representation (IR) that is interpreted at link-time. To make sure this plays well withstatic libraries, newerGNU linkers have a "linker plugin" interface that allows the compiler to convert the object files into a machine code form when needed. This plugin also helps drive the LTO process in general. Alternatively, a "fat LTO" object can be produced to contain both machine code and the IR, but this takes more space.[1]

Since both GCC and LLVM (clang) are able produce an IR from a variety of programming languages, link-time IPO can happen even across language boundaries. This is most commonly demonstrated with C and C++,[9] but LLVM makes it possible forRust and all other LLVM-based compilers too.[10]

Non-LTO options

[edit]

GCC and Clang perform IPO by default at optimization level 2. However, the degree of optimization is limited when LTO is disabled, as IPO can only happen within an object file and non-static functions can never be eliminated. The latter problem has a non-LTO solution: the-fwhole-program switch can be used to assume that onlymain() is non-static, i.e. visible from the outside.[11]

Another non-LTO technique is "function sections" (-ffunction-sections in GCC and Clang). By placing each function into its own section in the object file, the linker can perform dead code removal without an IR by removing unreferenced sections (using the linker option--gc-sections).[12] A similar option is available for variables, but it causes much worse code to be produced.

Other

[edit]

TheIntel C/C++ compilers allow whole-program IPO. The flag to enable interprocedural optimizations for a single file is-ip, the flag to enable interprocedural optimization across all files in the program is-ipo.[13][14]

TheMSVC compiler, integrated into Visual Studio, also supports interprocedural optimization on the whole program.[15]

A compiler-independent interface for enabling whole-program interprocedural optimizations is via theINTERPROCEDURAL_OPTIMIZATION property inCMake.[16]

See also

[edit]

References

[edit]
  1. ^abcde"Optimize Options".Using the GNU Compiler Collection (GCC).Link-time optimizations do not require the presence of the whole program to operate. If the program does not require any symbols to be exported, it is possible to combine -flto and -fwhole-program to allow the interprocedural optimizers to use more aggressive assumptions which may lead to improved optimization opportunities. Use of -fwhole-program is not needed when linker plugin is active (see -fuse-linker-plugin).
  2. ^"LTO Overview".GNU Compiler Collection (GCC) Internals.
  3. ^Thomas C. Spillman, "Exposing side effects in a PL/I optimizing compiler", inProceedings of IFIPS 1971, North-Holland Publishing Company, pages 376-381.
  4. ^Frances E. Allen, "Interprocedural Data Flow Analysis", IFIPS Proceedings, 1974.
  5. ^Frances E. Allen, and Jack Schwartz, "Determining the Data Flow Relationships in a Collection of Procedures", IBM Research Report RC 4989, Aug. 1974.
  6. ^Philip Abrams, "An APL Machine", Stanford University Computer Science Department, Report STAN-CS-70-158, February, 1970.
  7. ^Terrence C. Miller, "Tentative Compilation: A Design for an APL Compiler", Ph.D. Thesis, Yale University, 1978.
  8. ^"Clang command line argument reference".Clang 11 documentation.
  9. ^Reinhart, Jonathan."Can LTO for gcc or clang optimize across C and C++ methods".Stack Overflow.
  10. ^Woerister, Michael (19 September 2019)."Closing the gap: cross-language LTO between Rust and C/C++".LLVM Dev Blog.
  11. ^"Optimize Options".Using the GNU Compiler Collection (GCC).
  12. ^"Function sections".elinux.org.
  13. ^"Intel compiler 8 documentation". Archived fromthe original on 2006-09-21. Retrieved2007-02-13.
  14. ^Intel Visual Fortran Compiler 9.1, Standard and Professional Editions, for Windows* - Intel Software Network
  15. ^"/GL (Whole Program Optimization)".Microsoft Docs. 2019-03-12. Retrieved2020-01-26.
  16. ^"INTERPROCEDURAL_OPTIMIZATION".CMake 3.17.2 Documentation.

External links

[edit]
Basic block
Loop
Data-flow
analysis
SSA-based
Code generation
Functional
Global
Other
Static analysis
Retrieved from "https://en.wikipedia.org/w/index.php?title=Interprocedural_optimization&oldid=1318902879"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp