Power Open Source Development

Power Open Source Development

Explore the open source tools and capabilities for building and deploying modern applications on IBM Power platforms including AIX, IBM i, and Linux.


#Power


#Power

 View Only

Generating compiler optimization remarks in LLVM

By Archana Ravindar posted Fri August 12, 2022 11:23 AM

  
Compiler optimization reports are a good way to get insight into the optimizations done for a given application by the compiler. More importantly, they give insight into why certain optimizations could not be done by the compiler. The user can then make modifications to the code or use alternate options that can remove the obstacles to optimization. Usually, open source and proprietary compilers support options that provide such information. In this blog, we will discuss how to generate compiler optimization remarks using the LLVM compiler which is an open source and a very popular compiler and is supported on Linux on Power. Similar options are supported by the GCC compiler as well, and more information can be found here: https://gcc.gnu.org/onlinedocs/gcc-6.3.0/gccint/Dump-examples.html

The -Rpass option

The easiest way to generate optimization remarks using the -Rpass option are the Clang options -Rpass=, -Rpass-analysis=, and -Rpass-missed=.

When you use -Rpass= during compilation, clang prints the names of code transformations performed by optimization passes that match the regular expression indicated by regex.


Similarly, when you use -Rpass-analysis=, Clang prints the analysis annotations along with the code transformations in instances particularly in cases where the compiler was not able to do the optimization.


 If you need information pertaining to just the missed optimizations, you can get the same by using -Rpass-missed=.


You can also use fragments of names of optimization passes as regex in the above options. For example, clang++ -O2 -Rpass-analysis=vector bub.cc will produce the following:

bub.cc:62:68: remark: loop not vectorized: call instruction cannot be vectorized [-Rpass-analysis=loop-vectorize]
Pertaining to the source line:
62	for( uint64_t j=0; j < i; j++ ) ((double*)input)[ j ] = (double)rand();

If you want optimization reports for all the passes, you can simply pass the following:

clang++ -O2 -Rpass-analysis=[a-z]* bub.cc or 
clang++ -O2 -Rpass-analysis=. bub.cc 

Some typical examples of optimization passes regarding which you can get information using these options include:

  • licm (Loop invariant code motion)
  • loop-vectorize (Loop vectorization optimization)
  • size-info (number of IR instructions)
  • gvn (Global value numbering)
  • inline (Function inlining)
  • loop-unroll (Loop unrolling)

If the files are compiled with profile feedback options -fprofile-instr-generate and -fprofile-instr-use, using -fshow-diagnostics-hotness along with the Rpass options will print the hotness along with optimization remarks pertaining to the statements.

The -fsave-optimization-record option

LLVM also supports a more visual representation for viewing optimizations carried out by the compiler with the help of an additional compilation flag termed as -fsave-optimization- record. The LLVM framework by default comes with additional tools such opt-viewer/opt- viewer.py under the $(LLVM_SRC)/tools directory where LLVM_SRC is the root of the LLVM source repository. This tool can be used to view all the optimization remarks in line with the source file in a HTML format as shown in figure 1 below.

In the HTML file, corresponding to the source file, each source line is prefixed by the optimization pass name in the far left corner. The optimization remarks are printed in line with the source code for easy reference. The function context in which the source lines appear are shown at the far right end corner of the page. Optimizations that are carried out successfully are color coded “green” while those that were not carried out are color coded “red”.

Snapshot of annotated HTML file

Figure 1: Snapshot of annotated HTML file

In order to generate the HTML file, you need to compile the source file with the -fsave- optimization-record option as follows.

clang++ -O2 -fsave-optimization-record bub.cc

This will create a bub.opt.yaml file as output in the same directory. To create an HTML file that can be browsed, run the following command which will create the viewable HTML files in the html/ directory under the current working directory.

$(LLVM_SRC)/tools/opt-viewer/opt-viewer.py bub.opt.yaml html

Note that installing python-yaml and python-pygments is necessary for the above commands to work. After this command is run, you will see the following files generated in the HTML directory as shown in Figure 2. The bub.cc.html is the annotated HTML file corresponding to the source file bub.cc, that you saw in Figure 1.

Figure 2: HTML directory contents

Figure 2: HTML directory contents

An index file, index.html, is also generated to summarize the optimization remarks in a much terser manner as shown in Figure 3. The style.css file dictates how HTML elements are displayed on the page.

Figure 3: Contents of index.html file

Figure 3: Contents of index.html file

Recently, updates have been made to generate remarks in the bitstream format apart from YAML as it is more scalable. More details can be found here: https://llvm.org/devmtg/2019-10/slides/Mistrih-OptimizationRemarksUpdate.pdf

 

Summary

Apart from understanding what optimizations LLVM has done on Power, this option also helps in comparing optimizations carried out by LLVM on Power and other platforms such as Intel. This presentation by Adam Nemet has more details on how the optimization remarks were designed in LLVM and examples where optimization remarks were used to uncover performance opportunities: Compiler-assisted Performance Analysis . In practice we have found this option extremely helpful in uncovering some of the optimizations that tend to miss the eye.



#OpenSourceDevelopment
1 comment
368 views

Permalink

Comments

Mon November 27, 2023 12:47 AM

In the ever-evolving landscape of cybersecurity, staying ahead of emerging threats is paramount. Fortinet, a leading player in the industry, has recently unveiled its much-anticipated NSE7_EFW-7.0 update in November 2023. The Fortinet NSE7_EFW-7.0 November 2023 Release marks a significant milestone in the realm of cybersecurity. With enhanced security protocols, improved performance, and a user-friendly interface, users can expect a robust defense against emerging threats.