Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 3a76c9a

Browse files
[docs] Make WritingAnLLVMPass.rst up-to-date with current state of things
This patch updates WritingAnLLVMPass.rst to make it in line with current state of things. Specifically: * Makefile instructions replaced with CMake ones * Filenames replaced with correct ones * Example reformatted a bit to make it less confusing and more conforming to LLVM Coding Standards * opt tool output updated with what it actually prints nowdays * "gcse" (which doesn't exist anymore) replaced with "gvn" (which still does) Differential Revision: https://reviews.llvm.org/D24233 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@282482 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6be6db9 commit 3a76c9a

File tree

1 file changed

+129
-133
lines changed

1 file changed

+129
-133
lines changed

docs/WritingAnLLVMPass.rst

Lines changed: 129 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,34 @@ Setting up the build environment
5050
First, configure and build LLVM. Next, you need to create a new directory
5151
somewhere in the LLVM source base. For this example, we'll assume that you
5252
made ``lib/Transforms/Hello``. Finally, you must set up a build script
53-
(``Makefile``) that will compile the source code for the new pass. To do this,
54-
copy the following into ``Makefile``:
53+
that will compile the source code for the new pass. To do this,
54+
copy the following into ``CMakeLists.txt``:
5555

56-
.. code-block:: make
56+
.. code-block:: cmake
5757
58-
# Makefile for hello pass
58+
add_llvm_loadable_module( LLVMHello
59+
Hello.cpp
60+
61+
PLUGIN_TOOL
62+
opt
63+
)
5964
60-
# Path to top level of LLVM hierarchy
61-
LEVEL = ../../..
65+
and the following line into ``lib/Transforms/CMakeLists.txt``:
6266

63-
# Name of the library to build
64-
LIBRARYNAME = Hello
67+
.. code-block:: cmake
6568
66-
# Make the shared library become a loadable module so the tools can
67-
# dlopen/dlsym on the resulting library.
68-
LOADABLE_MODULE = 1
69+
add_subdirectory(Hello)
6970
70-
# Include the makefile implementation stuff
71-
include $(LEVEL)/Makefile.common
71+
(Note that there is already a directory named ``Hello`` with a sample "Hello"
72+
pass; you may play with it -- in which case you don't need to modify any
73+
``CMakeLists.txt`` files -- or, if you want to create everything from scratch,
74+
use another name.)
7275

73-
This makefile specifies that all of the ``.cpp`` files in the current directory
74-
are to be compiled and linked together into a shared object
75-
``$(LEVEL)/Debug+Asserts/lib/Hello.so`` that can be dynamically loaded by the
76-
:program:`opt` or :program:`bugpoint` tools via their :option:`-load` options.
77-
If your operating system uses a suffix other than ``.so`` (such as Windows or Mac
78-
OS X), the appropriate extension will be used.
79-
80-
If you are used CMake to build LLVM, see :ref:`cmake-out-of-source-pass`.
76+
This build script specifies that ``Hello.cpp`` file in the current directory
77+
is to be compiled and linked into a shared object ``$(LEVEL)/lib/LLVMHello.so`` that
78+
can be dynamically loaded by the :program:`opt` tool via its :option:`-load`
79+
option. If your operating system uses a suffix other than ``.so`` (such as
80+
Windows or Mac OS X), the appropriate extension will be used.
8181

8282
Now that we have the build scripts set up, we just need to write the code for
8383
the pass itself.
@@ -143,12 +143,12 @@ to avoid using expensive C++ runtime information.
143143

144144
.. code-block:: c++
145145

146-
bool runOnFunction(Function &F) override {
147-
errs() << "Hello: ";
148-
errs().write_escaped(F.getName()) << "\n";
149-
return false;
150-
}
151-
}; // end of struct Hello
146+
bool runOnFunction(Function &F) override {
147+
errs() << "Hello: ";
148+
errs().write_escaped(F.getName()) << '\n';
149+
return false;
150+
}
151+
}; // end of struct Hello
152152
} // end of anonymous namespace
153153

154154
We declare a :ref:`runOnFunction <writing-an-llvm-pass-runOnFunction>` method,
@@ -180,31 +180,33 @@ As a whole, the ``.cpp`` file looks like:
180180

181181
.. code-block:: c++
182182

183-
#include "llvm/Pass.h"
184-
#include "llvm/IR/Function.h"
185-
#include "llvm/Support/raw_ostream.h"
186-
187-
using namespace llvm;
188-
189-
namespace {
190-
struct Hello : public FunctionPass {
191-
static char ID;
192-
Hello() : FunctionPass(ID) {}
193-
194-
bool runOnFunction(Function &F) override {
195-
errs() << "Hello: ";
196-
errs().write_escaped(F.getName()) << '\n';
197-
return false;
198-
}
199-
};
183+
#include "llvm/Pass.h"
184+
#include "llvm/IR/Function.h"
185+
#include "llvm/Support/raw_ostream.h"
186+
187+
using namespace llvm;
188+
189+
namespace {
190+
struct Hello : public FunctionPass {
191+
static char ID;
192+
Hello() : FunctionPass(ID) {}
193+
194+
bool runOnFunction(Function &F) override {
195+
errs() << "Hello: ";
196+
errs().write_escaped(F.getName()) << '\n';
197+
return false;
200198
}
201-
202-
char Hello::ID = 0;
203-
static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
199+
}; // end of struct Hello
200+
} // end of anonymous namespace
201+
202+
char Hello::ID = 0;
203+
static RegisterPass<Hello> X("hello", "Hello World Pass",
204+
false /* Only looks at CFG */,
205+
false /* Analysis Pass */);
204206
205207
Now that it's all together, compile the file with a simple "``gmake``" command
206208
from the top level of your build directory and you should get a new file
207-
"``Debug+Asserts/lib/Hello.so``". Note that everything in this file is
209+
"``lib/LLVMHello.so``". Note that everything in this file is
208210
contained in an anonymous namespace --- this reflects the fact that passes
209211
are self contained units that do not need external interfaces (although they
210212
can have them) to be useful.
@@ -224,7 +226,7 @@ will work):
224226

225227
.. code-block:: console
226228
227-
$ opt -load ../../Debug+Asserts/lib/Hello.so -hello < hello.bc > /dev/null
229+
$ opt -load lib/LLVMHello.so -hello < hello.bc > /dev/null
228230
Hello: __main
229231
Hello: puts
230232
Hello: main
@@ -241,20 +243,20 @@ To see what happened to the other string you registered, try running
241243

242244
.. code-block:: console
243245
244-
$ opt -load ../../Debug+Asserts/lib/Hello.so -help
245-
OVERVIEW: llvm .bc -> .bc modular optimizer
246+
$ opt -load lib/LLVMHello.so -help
247+
OVERVIEW: llvm .bc -> .bc modular optimizer and analysis printer
246248
247-
USAGE: opt [options] <input bitcode>
249+
USAGE: opt [subcommand] [options] <input bitcode file>
248250
249251
OPTIONS:
250252
Optimizations available:
251253
...
252-
-globalopt - Global Variable Optimizer
253-
-globalsmodref-aa - Simple mod/ref analysis for globals
254+
-guard-widening - Widen guards
254255
-gvn - Global Value Numbering
256+
-gvn-hoist - Early GVN Hoisting of Expressions
255257
-hello - Hello World Pass
256258
-indvars - Induction Variable Simplification
257-
-inline - Function Integration/Inlining
259+
-inferattrs - Infer set function attributes
258260
...
259261
260262
The pass name gets added as the information string for your pass, giving some
@@ -268,21 +270,20 @@ you queue up. For example:
268270

269271
.. code-block:: console
270272
271-
$ opt -load ../../Debug+Asserts/lib/Hello.so -hello -time-passes < hello.bc > /dev/null
273+
$ opt -load lib/LLVMHello.so -hello -time-passes < hello.bc > /dev/null
272274
Hello: __main
273275
Hello: puts
274276
Hello: main
275-
===============================================================================
277+
===-------------------------------------------------------------------------===
276278
... Pass execution timing report ...
277-
===============================================================================
278-
Total Execution Time: 0.02 seconds (0.0479059 wall clock)
279-
280-
---User Time--- --System Time-- --User+System-- ---Wall Time--- --- Pass Name ---
281-
0.0100 (100.0%) 0.0000 ( 0.0%) 0.0100 ( 50.0%) 0.0402 ( 84.0%) Bitcode Writer
282-
0.0000 ( 0.0%) 0.0100 (100.0%) 0.0100 ( 50.0%) 0.0031 ( 6.4%) Dominator Set Construction
283-
0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0013 ( 2.7%) Module Verifier
284-
0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0033 ( 6.9%) Hello World Pass
285-
0.0100 (100.0%) 0.0100 (100.0%) 0.0200 (100.0%) 0.0479 (100.0%) TOTAL
279+
===-------------------------------------------------------------------------===
280+
Total Execution Time: 0.0007 seconds (0.0005 wall clock)
281+
282+
---User Time--- --User+System-- ---Wall Time--- --- Name ---
283+
0.0004 ( 55.3%) 0.0004 ( 55.3%) 0.0004 ( 75.7%) Bitcode Writer
284+
0.0003 ( 44.7%) 0.0003 ( 44.7%) 0.0001 ( 13.6%) Hello World Pass
285+
0.0000 ( 0.0%) 0.0000 ( 0.0%) 0.0001 ( 10.7%) Module Verifier
286+
0.0007 (100.0%) 0.0007 (100.0%) 0.0005 (100.0%) Total
286287
287288
As you can see, our implementation above is pretty fast. The additional
288289
passes listed are automatically inserted by the :program:`opt` tool to verify
@@ -964,14 +965,14 @@ just does a few simple checks that don't require significant analysis to
964965
compute (such as: two different globals can never alias each other, etc).
965966
Passes that use the `AliasAnalysis
966967
<http://llvm.org/doxygen/classllvm_1_1AliasAnalysis.html>`_ interface (for
967-
example the `gcse <http://llvm.org/doxygen/structGCSE.html>`_ pass), do not
968+
example the `gvn <http://llvm.org/doxygen/classllvm_1_1GVN.html>`_ pass), do not
968969
care which implementation of alias analysis is actually provided, they just use
969970
the designated interface.
970971

971972
From the user's perspective, commands work just like normal. Issuing the
972-
command ``opt -gcse ...`` will cause the ``basicaa`` class to be instantiated
973-
and added to the pass sequence. Issuing the command ``opt -somefancyaa -gcse
974-
...`` will cause the ``gcse`` pass to use the ``somefancyaa`` alias analysis
973+
command ``opt -gvn ...`` will cause the ``basicaa`` class to be instantiated
974+
and added to the pass sequence. Issuing the command ``opt -somefancyaa -gvn
975+
...`` will cause the ``gvn`` pass to use the ``somefancyaa`` alias analysis
975976
(which doesn't actually exist, it's just a hypothetical example) instead.
976977

977978
.. _writing-an-llvm-pass-RegisterAnalysisGroup:
@@ -1092,74 +1093,69 @@ information about all of the variants of the ``--debug-pass`` option, just type
10921093

10931094
By using the --debug-pass=Structure option, for example, we can see how our
10941095
:ref:`Hello World <writing-an-llvm-pass-basiccode>` pass interacts with other
1095-
passes. Lets try it out with the gcse and licm passes:
1096+
passes. Lets try it out with the gvn and licm passes:
10961097

10971098
.. code-block:: console
10981099
1099-
$ opt -load ../../Debug+Asserts/lib/Hello.so -gcse -licm --debug-pass=Structure < hello.bc > /dev/null
1100-
Module Pass Manager
1101-
Function Pass Manager
1102-
Dominator Set Construction
1103-
Immediate Dominators Construction
1104-
Global Common Subexpression Elimination
1105-
-- Immediate Dominators Construction
1106-
-- Global Common Subexpression Elimination
1107-
Natural Loop Construction
1108-
Loop Invariant Code Motion
1109-
-- Natural Loop Construction
1110-
-- Loop Invariant Code Motion
1100+
$ opt -load lib/LLVMHello.so -gvn -licm --debug-pass=Structure < hello.bc > /dev/null
1101+
ModulePass Manager
1102+
FunctionPass Manager
1103+
Dominator Tree Construction
1104+
Basic Alias Analysis (stateless AA impl)
1105+
Function Alias Analysis Results
1106+
Memory Dependence Analysis
1107+
Global Value Numbering
1108+
Natural Loop Information
1109+
Canonicalize natural loops
1110+
Loop-Closed SSA Form Pass
1111+
Basic Alias Analysis (stateless AA impl)
1112+
Function Alias Analysis Results
1113+
Scalar Evolution Analysis
1114+
Loop Pass Manager
1115+
Loop Invariant Code Motion
11111116
Module Verifier
1112-
-- Dominator Set Construction
1113-
-- Module Verifier
11141117
Bitcode Writer
1115-
--Bitcode Writer
11161118
1117-
This output shows us when passes are constructed and when the analysis results
1118-
are known to be dead (prefixed with "``--``"). Here we see that GCSE uses
1119-
dominator and immediate dominator information to do its job. The LICM pass
1120-
uses natural loop information, which uses dominator sets, but not immediate
1121-
dominators. Because immediate dominators are no longer useful after the GCSE
1122-
pass, it is immediately destroyed. The dominator sets are then reused to
1123-
compute natural loop information, which is then used by the LICM pass.
1119+
This output shows us when passes are constructed.
1120+
Here we see that GVN uses dominator tree information to do its job. The LICM pass
1121+
uses natural loop information, which uses dominator tree as well.
11241122

11251123
After the LICM pass, the module verifier runs (which is automatically added by
1126-
the :program:`opt` tool), which uses the dominator set to check that the
1127-
resultant LLVM code is well formed. After it finishes, the dominator set
1128-
information is destroyed, after being computed once, and shared by three
1129-
passes.
1124+
the :program:`opt` tool), which uses the dominator tree to check that the
1125+
resultant LLVM code is well formed. Note that the dominator tree is computed
1126+
once, and shared by three passes.
11301127

11311128
Lets see how this changes when we run the :ref:`Hello World
11321129
<writing-an-llvm-pass-basiccode>` pass in between the two passes:
11331130

11341131
.. code-block:: console
11351132
1136-
$ opt -load ../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1137-
Module Pass Manager
1138-
Function Pass Manager
1139-
Dominator Set Construction
1140-
Immediate Dominators Construction
1141-
Global Common Subexpression Elimination
1142-
-- Dominator Set Construction
1143-
-- Immediate Dominators Construction
1144-
-- Global Common Subexpression Elimination
1133+
$ opt -load lib/LLVMHello.so -gvn -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1134+
ModulePass Manager
1135+
FunctionPass Manager
1136+
Dominator Tree Construction
1137+
Basic Alias Analysis (stateless AA impl)
1138+
Function Alias Analysis Results
1139+
Memory Dependence Analysis
1140+
Global Value Numbering
11451141
Hello World Pass
1146-
-- Hello World Pass
1147-
Dominator Set Construction
1148-
Natural Loop Construction
1149-
Loop Invariant Code Motion
1150-
-- Natural Loop Construction
1151-
-- Loop Invariant Code Motion
1142+
Dominator Tree Construction
1143+
Natural Loop Information
1144+
Canonicalize natural loops
1145+
Loop-Closed SSA Form Pass
1146+
Basic Alias Analysis (stateless AA impl)
1147+
Function Alias Analysis Results
1148+
Scalar Evolution Analysis
1149+
Loop Pass Manager
1150+
Loop Invariant Code Motion
11521151
Module Verifier
1153-
-- Dominator Set Construction
1154-
-- Module Verifier
11551152
Bitcode Writer
1156-
--Bitcode Writer
11571153
Hello: __main
11581154
Hello: puts
11591155
Hello: main
11601156
11611157
Here we see that the :ref:`Hello World <writing-an-llvm-pass-basiccode>` pass
1162-
has killed the Dominator Set pass, even though it doesn't modify the code at
1158+
has killed the Dominator Tree pass, even though it doesn't modify the code at
11631159
all! To fix this, we need to add the following :ref:`getAnalysisUsage
11641160
<writing-an-llvm-pass-getAnalysisUsage>` method to our pass:
11651161

@@ -1174,26 +1170,26 @@ Now when we run our pass, we get this output:
11741170

11751171
.. code-block:: console
11761172
1177-
$ opt -load ../../Debug+Asserts/lib/Hello.so -gcse -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1178-
Pass Arguments: -gcse -hello -licm
1179-
Module Pass Manager
1180-
Function Pass Manager
1181-
Dominator Set Construction
1182-
Immediate Dominators Construction
1183-
Global Common Subexpression Elimination
1184-
-- Immediate Dominators Construction
1185-
-- Global Common Subexpression Elimination
1173+
$ opt -load lib/LLVMHello.so -gvn -hello -licm --debug-pass=Structure < hello.bc > /dev/null
1174+
Pass Arguments: -gvn -hello -licm
1175+
ModulePass Manager
1176+
FunctionPass Manager
1177+
Dominator Tree Construction
1178+
Basic Alias Analysis (stateless AA impl)
1179+
Function Alias Analysis Results
1180+
Memory Dependence Analysis
1181+
Global Value Numbering
11861182
Hello World Pass
1187-
-- Hello World Pass
1188-
Natural Loop Construction
1189-
Loop Invariant Code Motion
1190-
-- Loop Invariant Code Motion
1191-
-- Natural Loop Construction
1183+
Natural Loop Information
1184+
Canonicalize natural loops
1185+
Loop-Closed SSA Form Pass
1186+
Basic Alias Analysis (stateless AA impl)
1187+
Function Alias Analysis Results
1188+
Scalar Evolution Analysis
1189+
Loop Pass Manager
1190+
Loop Invariant Code Motion
11921191
Module Verifier
1193-
-- Dominator Set Construction
1194-
-- Module Verifier
11951192
Bitcode Writer
1196-
--Bitcode Writer
11971193
Hello: __main
11981194
Hello: puts
11991195
Hello: main

0 commit comments

Comments
 (0)