1 ===================================
2 Customizing LLVMC: Reference Manual
3 ===================================
5 This file was automatically generated by rst2html.
6 Please do not edit directly!
7 The ReST source lives in the directory 'tools/llvmc/doc'.
13 <div class="doc_author">
14 <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
20 LLVMC is a generic compiler driver, designed to be customizable and
21 extensible. It plays the same role for LLVM as the ``gcc`` program
22 does for GCC - LLVMC's job is essentially to transform a set of input
23 files into a set of targets depending on configuration rules and user
24 options. What makes LLVMC different is that these transformation rules
25 are completely customizable - in fact, LLVMC knows nothing about the
26 specifics of transformation (even the command-line options are mostly
27 not hard-coded) and regards the transformation structure as an
28 abstract graph. The structure of this graph is completely determined
29 by plugins, which can be either statically or dynamically linked. This
30 makes it possible to easily adapt LLVMC for other purposes - for
31 example, as a build tool for game resources.
33 Because LLVMC employs TableGen_ as its configuration language, you
34 need to be familiar with it to customize LLVMC.
36 .. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
42 LLVMC tries hard to be as compatible with ``gcc`` as possible,
43 although there are some small differences. Most of the time, however,
44 you shouldn't be able to notice them::
46 $ # This works as expected:
47 $ llvmc -O3 -Wall hello.cpp
51 One nice feature of LLVMC is that one doesn't have to distinguish
52 between different compilers for different languages (think ``g++`` and
53 ``gcc``) - the right toolchain is chosen automatically based on input
54 language names (which are, in turn, determined from file
55 extensions). If you want to force files ending with ".c" to compile as
56 C++, use the ``-x`` option, just like you would do it with ``gcc``::
58 $ # hello.c is really a C++ file
59 $ llvmc -x c++ hello.c
63 On the other hand, when using LLVMC as a linker to combine several C++
64 object files you should provide the ``--linker`` option since it's
65 impossible for LLVMC to choose the right linker in that case::
69 [A lot of link-time errors skipped]
70 $ llvmc --linker=c++ hello.o
74 By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is
75 also possible to choose the work-in-progress ``clang`` compiler with
76 the ``-clang`` option.
82 LLVMC has some built-in options that can't be overridden in the
83 configuration libraries:
85 * ``-o FILE`` - Output file name.
87 * ``-x LANGUAGE`` - Specify the language of the following input files
88 until the next -x option.
90 * ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
91 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
93 * ``-v`` - Enable verbose mode, i.e. print out all executed commands.
95 * ``--view-graph`` - Show a graphical representation of the compilation
96 graph. Requires that you have ``dot`` and ``gv`` programs
97 installed. Hidden option, useful for debugging.
99 * ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
100 current directory with the compilation graph description in the
101 Graphviz format. Hidden option, useful for debugging.
103 * ``--save-temps`` - Write temporary files to the current directory
104 and do not delete them on exit. Hidden option, useful for debugging.
106 * ``--help``, ``--help-hidden``, ``--version`` - These options have
107 their standard meaning.
110 Compiling LLVMC plugins
111 =======================
113 It's easiest to start working on your own LLVMC plugin by copying the
114 skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
116 $ cd $LLVMC_DIR/plugins
117 $ cp -r Simple MyPlugin
120 Makefile PluginMain.cpp Simple.td
122 As you can see, our basic plugin consists of only two files (not
123 counting the build script). ``Simple.td`` contains TableGen
124 description of the compilation graph; its format is documented in the
125 following sections. ``PluginMain.cpp`` is just a helper file used to
126 compile the auto-generated C++ code produced from TableGen source. It
127 can also contain hook definitions (see `below`__).
131 The first thing that you should do is to change the ``LLVMC_PLUGIN``
132 variable in the ``Makefile`` to avoid conflicts (since this variable
133 is used to name the resulting library)::
135 LLVMC_PLUGIN=MyPlugin
137 It is also a good idea to rename ``Simple.td`` to something less
140 $ mv Simple.td MyPlugin.td
142 Note that the plugin source directory must be placed under
143 ``$LLVMC_DIR/plugins`` to make use of the existing build
144 infrastructure. To build a version of the LLVMC executable called
145 ``mydriver`` with your plugin compiled in, use the following command::
148 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
150 To build your plugin as a dynamic library, just ``cd`` to its source
151 directory and run ``make``. The resulting file will be called
152 ``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
153 ``LLVMCMyPlugin.so``). This library can be then loaded in with the
154 ``-load`` option. Example::
156 $ cd $LLVMC_DIR/plugins/Simple
158 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
160 Sometimes, you will want a 'bare-bones' version of LLVMC that has no
161 built-in plugins. It can be compiled with the following command::
164 $ make BUILTIN_PLUGINS=""
167 Customizing LLVMC: the compilation graph
168 ========================================
170 Each TableGen configuration file should include the common
173 include "llvm/CompilerDriver/Common.td"
175 Internally, LLVMC stores information about possible source
176 transformations in form of a graph. Nodes in this graph represent
177 tools, and edges between two nodes represent a transformation path. A
178 special "root" node is used to mark entry points for the
179 transformations. LLVMC also assigns a weight to each edge (more on
180 this later) to choose between several alternative edges.
182 The definition of the compilation graph (see file
183 ``plugins/Base/Base.td`` for an example) is just a list of edges::
185 def CompilationGraph : CompilationGraph<[
186 Edge<"root", "llvm_gcc_c">,
187 Edge<"root", "llvm_gcc_assembler">,
190 Edge<"llvm_gcc_c", "llc">,
191 Edge<"llvm_gcc_cpp", "llc">,
194 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
196 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
200 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
201 (case (input_languages_contain "c++"), (inc_weight),
202 (or (parameter_equals "linker", "g++"),
203 (parameter_equals "linker", "c++")), (inc_weight))>,
208 As you can see, the edges can be either default or optional, where
209 optional edges are differentiated by an additional ``case`` expression
210 used to calculate the weight of this edge. Notice also that we refer
211 to tools via their names (as strings). This makes it possible to add
212 edges to an existing compilation graph in plugins without having to
213 know about all tool definitions used in the graph.
215 The default edges are assigned a weight of 1, and optional edges get a
216 weight of 0 + 2*N where N is the number of tests that evaluated to
217 true in the ``case`` expression. It is also possible to provide an
218 integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
219 the weight is increased (or decreased) by the provided value instead
220 of the default 2. It is also possible to change the default weight of
221 an optional edge by using the ``default`` clause of the ``case``
224 When passing an input file through the graph, LLVMC picks the edge
225 with the maximum weight. To avoid ambiguity, there should be only one
226 default edge between two nodes (with the exception of the root node,
227 which gets a special treatment - there you are allowed to specify one
228 default edge *per language*).
230 When multiple plugins are loaded, their compilation graphs are merged
231 together. Since multiple edges that have the same end nodes are not
232 allowed (i.e. the graph is not a multigraph), an edge defined in
233 several plugins will be replaced by the definition from the plugin
234 that was loaded last. Plugin load order can be controlled by using the
235 plugin priority feature described above.
237 To get a visual representation of the compilation graph (useful for
238 debugging), run ``llvmc --view-graph``. You will need ``dot`` and
239 ``gsview`` installed for this to work properly.
244 Command-line options that the plugin supports are defined by using an
247 def Options : OptionList<[
248 (switch_option "E", (help "Help string")),
249 (alias_option "quiet", "q")
253 As you can see, the option list is just a list of DAGs, where each DAG
254 is an option description consisting of the option name and some
255 properties. A plugin can define more than one option list (they are
256 all merged together in the end), which can be handy if one wants to
257 separate option groups syntactically.
259 * Possible option types:
261 - ``switch_option`` - a simple boolean switch, for example ``-time``.
263 - ``parameter_option`` - option that takes an argument, for example
266 - ``parameter_list_option`` - same as the above, but more than one
267 occurence of the option is allowed.
269 - ``prefix_option`` - same as the parameter_option, but the option name
270 and parameter value are not separated.
272 - ``prefix_list_option`` - same as the above, but more than one
273 occurence of the option is allowed; example: ``-lm -lpthread``.
275 - ``alias_option`` - a special option type for creating
276 aliases. Unlike other option types, aliases are not allowed to
277 have any properties besides the aliased option name. Usage
278 example: ``(alias_option "preprocess", "E")``
281 * Possible option properties:
283 - ``help`` - help string associated with this option. Used for
286 - ``required`` - this option is obligatory.
288 - ``hidden`` - this option should not appear in the ``--help``
289 output (but should appear in the ``--help-hidden`` output).
291 - ``really_hidden`` - the option should not appear in any help
294 - ``extern`` - this option is defined in some other plugin, see below.
299 Sometimes, when linking several plugins together, one plugin needs to
300 access options defined in some other plugin. Because of the way
301 options are implemented, such options should be marked as
302 ``extern``. This is what the ``extern`` option property is
306 (switch_option "E", (extern))
309 See also the section on plugin `priorities`__.
315 Conditional evaluation
316 ======================
318 The 'case' construct is the main means by which programmability is
319 achieved in LLVMC. It can be used to calculate edge weights, program
320 actions and modify the shell commands to be executed. The 'case'
321 expression is designed after the similarly-named construct in
322 functional languages and takes the form ``(case (test_1), statement_1,
323 (test_2), statement_2, ... (test_N), statement_N)``. The statements
324 are evaluated only if the corresponding tests evaluate to true.
328 // Edge weight calculation
330 // Increases edge weight by 5 if "-A" is provided on the
331 // command-line, and by 5 more if "-B" is also provided.
333 (switch_on "A"), (inc_weight 5),
334 (switch_on "B"), (inc_weight 5))
337 // Tool command line specification
339 // Evaluates to "cmdline1" if the option "-A" is provided on the
340 // command line; to "cmdline2" if "-B" is provided;
341 // otherwise to "cmdline3".
344 (switch_on "A"), "cmdline1",
345 (switch_on "B"), "cmdline2",
346 (default), "cmdline3")
348 Note the slight difference in 'case' expression handling in contexts
349 of edge weights and command line specification - in the second example
350 the value of the ``"B"`` switch is never checked when switch ``"A"`` is
351 enabled, and the whole expression always evaluates to ``"cmdline1"`` in
354 Case expressions can also be nested, i.e. the following is legal::
356 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
359 You should, however, try to avoid doing that because it hurts
360 readability. It is usually better to split tool descriptions and/or
361 use TableGen inheritance instead.
363 * Possible tests are:
365 - ``switch_on`` - Returns true if a given command-line switch is
366 provided by the user. Example: ``(switch_on "opt")``.
368 - ``parameter_equals`` - Returns true if a command-line parameter equals
370 Example: ``(parameter_equals "W", "all")``.
372 - ``element_in_list`` - Returns true if a command-line parameter
373 list contains a given value.
374 Example: ``(parameter_in_list "l", "pthread")``.
376 - ``input_languages_contain`` - Returns true if a given language
377 belongs to the current input language set.
378 Example: ``(input_languages_contain "c++")``.
380 - ``in_language`` - Evaluates to true if the input file language
381 equals to the argument. At the moment works only with ``cmd_line``
382 and ``actions`` (on non-join nodes).
383 Example: ``(in_language "c++")``.
385 - ``not_empty`` - Returns true if a given option (which should be
386 either a parameter or a parameter list) is set by the
388 Example: ``(not_empty "o")``.
390 - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty
391 X))``. Provided for convenience.
393 - ``default`` - Always evaluates to true. Should always be the last
394 test in the ``case`` expression.
396 - ``and`` - A standard logical combinator that returns true iff all
397 of its arguments return true. Used like this: ``(and (test1),
398 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
401 - ``or`` - Another logical combinator that returns true only if any
402 one of its arguments returns true. Example: ``(or (test1),
403 (test2), ... (testN))``.
406 Writing a tool description
407 ==========================
409 As was said earlier, nodes in the compilation graph represent tools,
410 which are described separately. A tool definition looks like this
411 (taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
413 def llvm_gcc_cpp : Tool<[
415 (out_language "llvm-assembler"),
416 (output_suffix "bc"),
417 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
421 This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
422 ``llvm-g++``. As you can see, a tool definition is just a list of
423 properties; most of them should be self-explanatory. The ``sink``
424 property means that this tool should be passed all command-line
425 options that aren't mentioned in the option list.
427 The complete list of all currently implemented tool properties follows.
429 * Possible tool properties:
431 - ``in_language`` - input language name. Can be either a string or a
432 list, in case the tool supports multiple input languages.
434 - ``out_language`` - output language name. Tools are not allowed to
435 have multiple output languages.
437 - ``output_suffix`` - output file suffix. Can also be changed
438 dynamically, see documentation on actions.
440 - ``cmd_line`` - the actual command used to run the tool. You can
441 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
442 with ``>``, hook invocations (``$CALL``), environment variables
443 (via ``$ENV``) and the ``case`` construct.
445 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
446 list of input files and joins them together. Used for linkers.
448 - ``sink`` - all command-line options that are not handled by other
449 tools are passed to this tool.
451 - ``actions`` - A single big ``case`` expression that specifies how
452 this tool reacts on command-line options (described in more detail
458 A tool often needs to react to command-line options, and this is
459 precisely what the ``actions`` property is for. The next example
460 illustrates this feature::
462 def llvm_gcc_linker : Tool<[
463 (in_language "object-code"),
464 (out_language "executable"),
465 (output_suffix "out"),
466 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
468 (actions (case (not_empty "L"), (forward "L"),
469 (not_empty "l"), (forward "l"),
471 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
474 The ``actions`` tool property is implemented on top of the omnipresent
475 ``case`` expression. It associates one or more different *actions*
476 with given conditions - in the example, the actions are ``forward``,
477 which forwards a given option unchanged, and ``append_cmd``, which
478 appends a given string to the tool execution command. Multiple actions
479 can be associated with a single condition by using a list of actions
480 (used in the example to append some dummy options). The same ``case``
481 construct can also be used in the ``cmd_line`` property to modify the
484 The "join" property used in the example means that this tool behaves
487 The list of all possible actions follows.
491 - ``append_cmd`` - append a string to the tool invocation
493 Example: ``(case (switch_on "pthread"), (append_cmd
496 - ``error` - exit with error.
497 Example: ``(error "Mixing -c and -S is not allowed!")``.
499 - ``forward`` - forward an option unchanged.
500 Example: ``(forward "Wall")``.
502 - ``forward_as`` - Change the name of an option, but forward the
504 Example: ``(forward_as "O0" "--disable-optimization")``.
506 - ``output_suffix`` - modify the output suffix of this
508 Example: ``(output_suffix "i")``.
510 - ``stop_compilation`` - stop compilation after this tool processes
511 its input. Used without arguments.
513 - ``unpack_values`` - used for for splitting and forwarding
514 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
515 converted to ``-foo=bar -baz`` and appended to the tool invocation
517 Example: ``(unpack_values "Wa,")``.
522 If you are adding support for a new language to LLVMC, you'll need to
523 modify the language map, which defines mappings from file extensions
524 to language names. It is used to choose the proper toolchain(s) for a
525 given input file set. Language map definition looks like this::
527 def LanguageMap : LanguageMap<
528 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
529 LangToSuffixes<"c", ["c"]>,
533 For example, without those definitions the following command wouldn't work::
536 llvmc: Unknown suffix: cpp
538 The language map entries should be added only for tools that are
539 linked with the root node. Since tools are not allowed to have
540 multiple output languages, for nodes "inside" the graph the input and
541 output languages should match. This is enforced at compile-time.
549 Hooks and environment variables
550 -------------------------------
552 Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
553 this is not sufficient: for example, we may want to specify tool names
554 in the configuration file. This can be achieved via the mechanism of
555 hooks - to write your own hooks, just add their definitions to the
556 ``PluginMain.cpp`` or drop a ``.cpp`` file into the
557 ``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
558 namespace and have the signature ``std::string hooks::MyHookName
559 (void)``. They can be used from the ``cmd_line`` tool property::
561 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
563 It is also possible to use environment variables in the same manner::
565 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
567 To change the command line string based on user-provided options use
568 the ``case`` expression (documented `above`__)::
573 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
575 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
581 How plugins are loaded
582 ----------------------
584 It is possible for LLVMC plugins to depend on each other. For example,
585 one can create edges between nodes defined in some other plugin. To
586 make this work, however, that plugin should be loaded first. To
587 achieve this, the concept of plugin priority was introduced. By
588 default, every plugin has priority zero; to specify the priority
589 explicitly, put the following line in your plugin's TableGen file::
591 def Priority : PluginPriority<$PRIORITY_VALUE>;
592 # Where PRIORITY_VALUE is some integer > 0
594 Plugins are loaded in order of their (increasing) priority, starting
595 with 0. Therefore, the plugin with the highest priority value will be
601 When writing LLVMC plugins, it can be useful to get a visual view of
602 the resulting compilation graph. This can be achieved via the command
603 line option ``--view-graph``. This command assumes that Graphviz_ and
604 Ghostview_ are installed. There is also a ``--dump-graph`` option that
605 creates a Graphviz source file (``compilation-graph.dot``) in the
608 .. _Graphviz: http://www.graphviz.org/
609 .. _Ghostview: http://pages.cs.wisc.edu/~ghost/
615 <a href="http://jigsaw.w3.org/css-validator/check/referer">
616 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
617 alt="Valid CSS" /></a>
618 <a href="http://validator.w3.org/check?uri=referer">
619 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
620 alt="Valid XHTML 1.0 Transitional"/></a>
622 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
623 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
625 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $