blob: 3198a417caaac3fbb986f142e488013dc6136a44 [file] [log] [blame]
Tobias Grosser719f8432016-02-04 07:45:32 +00001======================
2Using Polly with Clang
3======================
4
5This documentation discusses how Polly can be used in Clang to automatically
6optimize C/C++ code during compilation.
7
8
9.. warning::
10
11 Warning: clang/LLVM/Polly need to be in sync (compiled from the same SVN
12 revision).
13
14Make Polly available from Clang
15===============================
16
Tobias Grosserc4a80162016-04-29 12:35:46 +000017Polly is available through clang, opt, and bugpoint, if Polly was checked out
18into tools/polly before compilation. No further configuration is needed.
Tobias Grosser719f8432016-02-04 07:45:32 +000019
20Optimizing with Polly
21=====================
22
23Optimizing with Polly is as easy as adding -O3 -mllvm -polly to your compiler
24flags (Polly is only available at -O3).
25
26.. code-block:: console
27
Tobias Grosserc4a80162016-04-29 12:35:46 +000028 clang -O3 -mllvm -polly file.c
Tobias Grosser719f8432016-02-04 07:45:32 +000029
30Automatic OpenMP code generation
31================================
32
33To automatically detect parallel loops and generate OpenMP code for them you
34also need to add -mllvm -polly-parallel -lgomp to your CFLAGS.
35
36.. code-block:: console
37
Tobias Grosserc4a80162016-04-29 12:35:46 +000038 clang -O3 -mllvm -polly -mllvm -polly-parallel -lgomp file.c
Tobias Grosser719f8432016-02-04 07:45:32 +000039
40Automatic Vector code generation
41================================
42
43Automatic vector code generation can be enabled by adding -mllvm
44-polly-vectorizer=stripmine to your CFLAGS.
45
46.. code-block:: console
47
Tobias Grosserc4a80162016-04-29 12:35:46 +000048 clang -O3 -mllvm -polly -mllvm -polly-vectorizer=stripmine file.c
Tobias Grosser719f8432016-02-04 07:45:32 +000049
Michael Kruse9b67e562018-04-06 19:24:18 +000050Isolate the Polly passes
51========================
Tobias Grosser719f8432016-02-04 07:45:32 +000052
Michael Kruse9b67e562018-04-06 19:24:18 +000053Polly's analysis and transformation passes are run with many other
54passes of the pass manager's pipeline. Some of passes that run before
55Polly are essential for its working, for instance the canonicalization
56of loop. Therefore Polly is unable to optimize code straight out of
57clang's -O0 output.
58
59To get the LLVM-IR that Polly sees in the optimization pipeline, use the
60command:
Tobias Grosser719f8432016-02-04 07:45:32 +000061
62.. code-block:: console
63
Michael Kruse9b67e562018-04-06 19:24:18 +000064 clang file.c -c -O3 -mllvm -polly -mllvm -polly-dump-before-file=before-polly.ll
65
66This writes a file 'before-polly.ll' containing the LLVM-IR as passed to
67polly, after SSA transformation, loop canonicalization, inlining and
68other passes.
69
70Thereafter, any Polly pass can be run over 'before-polly.ll' using the
71'opt' tool. To found out which Polly passes are active in the standard
72pipeline, see the output of
73
74.. code-block:: console
75
76 clang file.c -c -O3 -mllvm -polly -mllvm -debug-pass=Arguments
77
78The Polly's passes are those between '-polly-detect' and
79'-polly-codegen'. Analysis passes can be omitted. At the time of this
80writing, the default Polly pass pipeline is:
81
82.. code-block:: console
83
84 opt before-polly.ll -polly-simplify -polly-optree -polly-delicm -polly-simplify -polly-prune-unprofitable -polly-opt-isl -polly-codegen
85
86Note that this uses LLVM's old/legacy pass manager.
87
88For completeness, here are some other methods that generates IR
89suitable for processing with Polly from C/C++/Objective C source code.
90The previous method is the recommended one.
91
92The following generates unoptimized LLVM-IR ('-O0', which is the
93default) and runs the canonicalizing passes on it
94('-polly-canonicalize'). This does /not/ include all the passes that run
95before Polly in the default pass pipeline. The '-disable-O0-optnone'
96option is required because otherwise clang adds an 'optnone' attribute
97to all functions such that it is skipped by most optimization passes.
98This is meant to stop LTO builds to optimize these functions in the
99linking phase anyway.
100
101.. code-block:: console
102
103 clang file.c -c -O0 -Xclang -disable-O0-optnone -emit-llvm -S -o - | opt -polly-canonicalize -S
104
105The option '-disable-llvm-passes' disables all LLVM passes, even those
106that run at -O0. Passing -O1 (or any optimization level other than -O0)
107avoids that the 'optnone' attribute is added.
108
109.. code-block:: console
110
111 clang file.c -c -O1 -Xclang -disable-llvm-passes -emit-llvm -S -o - | opt -polly-canonicalize -S
112
113As another alternative, Polly can be pushed in front of the pass
114pipeline, and then its output dumped. This implicitly runs the
115'-polly-canonicalize' passes.
116
117.. code-block:: console
118
119 clang file.c -c -O3 -mllvm -polly -mllvm -polly-position=early -mllvm -polly-dump-before-file=before-polly.ll
Tobias Grosser719f8432016-02-04 07:45:32 +0000120
121Further options
122===============
123Polly supports further options that are mainly useful for the development or the
124analysis of Polly. The relevant options can be added to clang by appending
125-mllvm -option-name to the CFLAGS or the clang command line.
126
127Limit Polly to a single function
128--------------------------------
129
130To limit the execution of Polly to a single function, use the option
131-polly-only-func=functionname.
132
133Disable LLVM-IR generation
134--------------------------
135
136Polly normally regenerates LLVM-IR from the Polyhedral representation. To only
137see the effects of the preparing transformation, but to disable Polly code
138generation add the option polly-no-codegen.
139
140Graphical view of the SCoPs
141---------------------------
142Polly can use graphviz to show the SCoPs it detects in a program. The relevant
143options are -polly-show, -polly-show-only, -polly-dot and -polly-dot-only. The
144'show' options automatically run dotty or another graphviz viewer to show the
145scops graphically. The 'dot' options store for each function a dot file that
146highlights the detected SCoPs. If 'only' is appended at the end of the option,
147the basic blocks are shown without the statements the contain.
148
149Change/Disable the Optimizer
150----------------------------
151
152Polly uses by default the isl scheduling optimizer. The isl optimizer optimizes
Tobias Grosser0a828aa2016-05-17 19:44:16 +0000153for data-locality and parallelism using the Pluto algorithm.
154To disable the optimizer entirely use the option -polly-optimizer=none.
Tobias Grosser719f8432016-02-04 07:45:32 +0000155
156Disable tiling in the optimizer
157-------------------------------
158
159By default both optimizers perform tiling, if possible. In case this is not
160wanted the option -polly-tiling=false can be used to disable it. (This option
161disables tiling for both optimizers).
162
Tobias Grosser719f8432016-02-04 07:45:32 +0000163Import / Export
164---------------
165
166The flags -polly-import and -polly-export allow the export and reimport of the
167polyhedral representation. By exporting, modifying and reimporting the
168polyhedral representation externally calculated transformations can be
169applied. This enables external optimizers or the manual optimization of
Tobias Grosser5c88f002017-07-19 01:16:55 +0000170specific SCoPs.
171
172Viewing Polly Diagnostics with opt-viewer
173-----------------------------------------
174
175The flag -fsave-optimization-record will generate .opt.yaml files when compiling
176your program. These yaml files contain information about each emitted remark.
177Ensure that you have Python 2.7 with PyYaml and Pygments Python Packages.
178To run opt-viewer:
179
180.. code-block:: console
181
182 llvm/tools/opt-viewer/opt-viewer.py -source-dir /path/to/program/src/ \
183 /path/to/program/src/foo.opt.yaml \
184 /path/to/program/src/bar.opt.yaml \
185 -o ./output
186
Eli Friedman84c73fd2017-07-19 18:18:37 +0000187Include all yaml files (use \*.opt.yaml when specifying which yaml files to view)
Tobias Grosser5c88f002017-07-19 01:16:55 +0000188to view all diagnostics from your program in opt-viewer. Compile with `PGO
Eli Friedman84c73fd2017-07-19 18:18:37 +0000189<https://clang.llvm.org/docs/UsersManual.html#profiling-with-instrumentation>`_ to view
Tobias Grosser5c88f002017-07-19 01:16:55 +0000190Hotness information in opt-viewer. Resulting html files can be viewed in an internet browser.