X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2FCompilerDriver.html;h=761d6ee6810db1da1eaff7e5a4d9988e045278b5;hb=f9be95f867745b6754b2402b9b72f9eaeabd637f;hp=ccd23ebc7331194449d7931ab01b702c930fa8d6;hpb=23f522a5450439d22c27c24efd44c6ae722b1c54;p=oota-llvm.git diff --git a/docs/CompilerDriver.html b/docs/CompilerDriver.html index ccd23ebc733..761d6ee6810 100644 --- a/docs/CompilerDriver.html +++ b/docs/CompilerDriver.html @@ -3,46 +3,50 @@ - + Customizing LLVMC: Reference Manual

Customizing LLVMC: Reference Manual

+ -
-

Contents

+

Written by Mikhail Glushenkov

-
+

Introduction

LLVMC is a generic compiler driver, designed to be customizable and extensible. It plays the same role for LLVM as the gcc program does for GCC - LLVMC's job is essentially to transform a set of input @@ -55,11 +59,11 @@ abstract graph. The structure of this graph is completely determined by plugins, which can be either statically or dynamically linked. This makes it possible to easily adapt LLVMC for other purposes - for example, as a build tool for game resources.

-

Because LLVMC employs TableGen as its configuration language, you +

Because LLVMC employs TableGen as its configuration language, you need to be familiar with it to customize LLVMC.

-
-

Compiling with LLVMC

+
+

Compiling with LLVMC

LLVMC tries hard to be as compatible with gcc as possible, although there are some small differences. Most of the time, however, you shouldn't be able to notice them:

@@ -69,12 +73,12 @@ $ llvmc -O3 -Wall hello.cpp $ ./a.out hello -

One nice feature of LLVMC is that one doesn't have to distinguish -between different compilers for different languages (think g++ and -gcc) - the right toolchain is chosen automatically based on input -language names (which are, in turn, determined from file -extensions). If you want to force files ending with ".c" to compile as -C++, use the -x option, just like you would do it with gcc:

+

One nice feature of LLVMC is that one doesn't have to distinguish between +different compilers for different languages (think g++ vs. gcc) - the +right toolchain is chosen automatically based on input language names (which +are, in turn, determined from file extensions). If you want to force files +ending with ".c" to compile as C++, use the -x option, just like you would +do it with gcc:

 $ # hello.c is really a C++ file
 $ llvmc -x c++ hello.c
@@ -92,12 +96,11 @@ $ llvmc --linker=c++ hello.o
 $ ./a.out
 hello
 
-

By default, LLVMC uses llvm-gcc to compile the source code. It is -also possible to choose the work-in-progress clang compiler with -the -clang option.

+

By default, LLVMC uses llvm-gcc to compile the source code. It is also +possible to choose the clang compiler with the -clang option.

-
-

Predefined options

+
+

Predefined options

LLVMC has some built-in options that can't be overridden in the configuration libraries:

    @@ -107,20 +110,34 @@ until the next -x option.
  • -load PLUGIN_NAME - Load the specified plugin DLL. Example: -load $LLVM_DIR/Release/lib/LLVMCSimple.so.
  • -v - Enable verbose mode, i.e. print out all executed commands.
  • -
  • --view-graph - Show a graphical representation of the compilation -graph. Requires that you have dot and gv programs -installed. Hidden option, useful for debugging.
  • -
  • --write-graph - Write a compilation-graph.dot file in the -current directory with the compilation graph description in the -Graphviz format. Hidden option, useful for debugging.
  • -
  • --save-temps - Write temporary files to the current directory -and do not delete them on exit. Hidden option, useful for debugging.
  • +
  • --save-temps - Write temporary files to the current directory and do not +delete them on exit. This option can also take an argument: the +--save-temps=obj switch will write files into the directory specified with +the -o option. The --save-temps=cwd and --save-temps switches are +both synonyms for the default behaviour.
  • +
  • --temp-dir DIRECTORY - Store temporary files in the given directory. This +directory is deleted on exit unless --save-temps is specified. If +--save-temps=obj is also specified, --temp-dir is given the +precedence.
  • +
  • --check-graph - Check the compilation for common errors like mismatched +output/input language names, multiple default edges and cycles. Because of +plugins, these checks can't be performed at compile-time. Exit with code zero +if no errors were found, and return the number of found errors +otherwise. Hidden option, useful for debugging LLVMC plugins.
  • +
  • --view-graph - Show a graphical representation of the compilation graph +and exit. Requires that you have dot and gv programs installed. Hidden +option, useful for debugging LLVMC plugins.
  • +
  • --write-graph - Write a compilation-graph.dot file in the current +directory with the compilation graph description in Graphviz format (identical +to the file used by the --view-graph option). The -o option can be +used to set the output file name. Hidden option, useful for debugging LLVMC +plugins.
  • --help, --help-hidden, --version - These options have their standard meaning.
-
-

Compiling LLVMC plugins

+
+

Compiling LLVMC plugins

It's easiest to start working on your own LLVMC plugin by copying the skeleton project which lives under $LLVMC_DIR/plugins/Simple:

@@ -135,7 +152,7 @@ counting the build script). Simpl
 description of the compilation graph; its format is documented in the
 following sections. PluginMain.cpp is just a helper file used to
 compile the auto-generated C++ code produced from TableGen source. It
-can also contain hook definitions (see below).

+can also contain hook definitions (see below).

The first thing that you should do is to change the LLVMC_PLUGIN variable in the Makefile to avoid conflicts (since this variable is used to name the resulting library):

@@ -147,33 +164,58 @@ generic:

 $ mv Simple.td MyPlugin.td
 
-

Note that the plugin source directory must be placed under -$LLVMC_DIR/plugins to make use of the existing build -infrastructure. To build a version of the LLVMC executable called -mydriver with your plugin compiled in, use the following command:

-
-$ cd $LLVMC_DIR
-$ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
-

To build your plugin as a dynamic library, just cd to its source directory and run make. The resulting file will be called -LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION) (in our case, -LLVMCMyPlugin.so). This library can be then loaded in with the +plugin_llvmc_$(LLVMC_PLUGIN).$(DLL_EXTENSION) (in our case, +plugin_llvmc_MyPlugin.so). This library can be then loaded in with the -load option. Example:

 $ cd $LLVMC_DIR/plugins/Simple
 $ make
-$ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
+$ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so
 
+
+
+

Compiling standalone LLVMC-based drivers

+

By default, the llvmc executable consists of a driver core plus several +statically linked plugins (Base and Clang at the moment). You can +produce a standalone LLVMC-based driver executable by linking the core with your +own plugins. The recommended way to do this is by starting with the provided +Skeleton example ($LLVMC_DIR/example/Skeleton):

+
+$ cd $LLVMC_DIR/example/
+$ cp -r Skeleton mydriver
+$ cd mydriver
+$ vim Makefile
+[...]
+$ make
+
+

If you're compiling LLVM with different source and object directories, then you +must perform the following additional steps before running make:

+
+# LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
+# LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
+$ cp $LLVMC_SRC_DIR/example/mydriver/Makefile \
+  $LLVMC_OBJ_DIR/example/mydriver/
+$ cd $LLVMC_OBJ_DIR/example/mydriver
+$ make
+
+

Another way to do the same thing is by using the following command:

+
+$ cd $LLVMC_DIR
+$ make LLVMC_BUILTIN_PLUGINS=MyPlugin LLVMC_BASED_DRIVER_NAME=mydriver
+
+

This works with both srcdir == objdir and srcdir != objdir, but assumes that the +plugin source directory was placed under $LLVMC_DIR/plugins.

Sometimes, you will want a 'bare-bones' version of LLVMC that has no built-in plugins. It can be compiled with the following command:

 $ cd $LLVMC_DIR
-$ make BUILTIN_PLUGINS=""
+$ make LLVMC_BUILTIN_PLUGINS=""
 
-
-

Customizing LLVMC: the compilation graph

+
+

Customizing LLVMC: the compilation graph

Each TableGen configuration file should include the common definitions:

@@ -240,8 +282,8 @@ plugin priority feature described above.

debugging), run llvmc --view-graph. You will need dot and gsview installed for this to work properly.

-
-

Describing options

+
+

Describing options

Command-line options that the plugin supports are defined by using an OptionList:

@@ -260,42 +302,66 @@ separate option groups syntactically.

  • Possible option types:

      -
    • switch_option - a simple boolean switch, for example -time.
    • -
    • parameter_option - option that takes an argument, for example --std=c99;
    • -
    • parameter_list_option - same as the above, but more than one -occurence of the option is allowed.
    • -
    • prefix_option - same as the parameter_option, but the option name -and parameter value are not separated.
    • -
    • prefix_list_option - same as the above, but more than one -occurence of the option is allowed; example: -lm -lpthread.
    • -
    • alias_option - a special option type for creating -aliases. Unlike other option types, aliases are not allowed to -have any properties besides the aliased option name. Usage -example: (alias_option "preprocess", "E")
    • +
    • switch_option - a simple boolean switch without arguments, for example +-O2 or -time. At most one occurrence is allowed.
    • +
    • parameter_option - option that takes one argument, for example +-std=c99. It is also allowed to use spaces instead of the equality +sign: -std c99. At most one occurrence is allowed.
    • +
    • parameter_list_option - same as the above, but more than one option +occurence is allowed.
    • +
    • prefix_option - same as the parameter_option, but the option name and +argument do not have to be separated. Example: -ofile. This can be also +specified as -o file; however, -o=file will be parsed incorrectly +(=file will be interpreted as option value). At most one occurrence is +allowed.
    • +
    • prefix_list_option - same as the above, but more than one occurence of +the option is allowed; example: -lm -lpthread.
    • +
    • alias_option - a special option type for creating aliases. Unlike other +option types, aliases are not allowed to have any properties besides the +aliased option name. Usage example: (alias_option "preprocess", "E")
  • Possible option properties:

      -
    • help - help string associated with this option. Used for ---help output.
    • -
    • required - this option is obligatory.
    • -
    • hidden - this option should not appear in the --help -output (but should appear in the --help-hidden output).
    • -
    • really_hidden - the option should not appear in any help +
    • help - help string associated with this option. Used for --help +output.
    • +
    • required - this option must be specified exactly once (or, in case of +the list options without the multi_val property, at least +once). Incompatible with zero_or_one and one_or_more.
    • +
    • one_or_more - the option must be specified at least one time. Useful +only for list options in conjunction with multi_val; for ordinary lists +it is synonymous with required. Incompatible with required and +zero_or_one.
    • +
    • zero_or_one - the option can be specified zero or one times. Useful +only for list options in conjunction with multi_val. Incompatible with +required and one_or_more.
    • +
    • hidden - the description of this option will not appear in +the --help output (but will appear in the --help-hidden +output).
    • +
    • really_hidden - the option will not be mentioned in any help output.
    • +
    • multi_val n - this option takes n arguments (can be useful in some +special cases). Usage example: (parameter_list_option "foo", (multi_val +3)); the command-line syntax is '-foo a b c'. Only list options can have +this attribute; you can, however, use the one_or_more, zero_or_one +and required properties.
    • +
    • init - this option has a default value, either a string (if it is a +parameter), or a boolean (if it is a switch; boolean constants are called +true and false). List options can't have this attribute. Usage +examples: (switch_option "foo", (init true)); (prefix_option "bar", +(init "baz")).
    • extern - this option is defined in some other plugin, see below.
  • -
    -

    External options

    +
    +

    External options

    Sometimes, when linking several plugins together, one plugin needs to access options defined in some other plugin. Because of the way -options are implemented, such options should be marked as +options are implemented, such options must be marked as extern. This is what the extern option property is for. Example:

    @@ -303,11 +369,12 @@ for. Example:

    (switch_option "E", (extern)) ...
    -

    See also the section on plugin priorities.

    +

    If an external option has additional attributes besides 'extern', they are +ignored. See also the section on plugin priorities.

    -
    -

    Conditional evaluation

    +
    +

    Conditional evaluation

    The 'case' construct is the main means by which programmability is achieved in LLVMC. It can be used to calculate edge weights, program actions and modify the shell commands to be executed. The 'case' @@ -352,8 +419,15 @@ readability. It is usually better to split tool descriptions and/or use TableGen inheritance instead.

    • Possible tests are:
        -
      • switch_on - Returns true if a given command-line switch is -provided by the user. Example: (switch_on "opt").
      • +
      • switch_on - Returns true if a given command-line switch is provided by +the user. Can be given a list as argument, in that case (switch_on ["foo", +"bar", "baz"]) is equivalent to (and (switch_on "foo"), (switch_on +"bar"), (switch_on "baz")). +Example: (switch_on "opt").
      • +
      • any_switch_on - Given a list of switch options, returns true if any of +the switches is turned on. +Example: (any_switch_on ["foo", "bar", "baz"]) is equivalent to (or +(switch_on "foo"), (switch_on "bar"), (switch_on "baz")).
      • parameter_equals - Returns true if a command-line parameter equals a given value. Example: (parameter_equals "W", "all").
      • @@ -363,29 +437,45 @@ Example: (parameter_in_listinput_languages_contain - Returns true if a given language belongs to the current input language set. Example: (input_languages_contain "c++"). -
      • in_language - Evaluates to true if the input file language -equals to the argument. At the moment works only with cmd_line -and actions (on non-join nodes). +
      • in_language - Evaluates to true if the input file language is equal to +the argument. At the moment works only with cmd_line and actions (on +non-join nodes). Example: (in_language "c++").
      • -
      • not_empty - Returns true if a given option (which should be -either a parameter or a parameter list) is set by the -user. +
      • not_empty - Returns true if a given option (which should be either a +parameter or a parameter list) is set by the user. Like switch_on, can +be also given a list as argument. Example: (not_empty "o").
      • +
      • any_not_empty - Returns true if not_empty returns true for any of +the options in the list. +Example: (any_not_empty ["foo", "bar", "baz"]) is equivalent to (or +(not_empty "foo"), (not_empty "bar"), (not_empty "baz")).
      • +
      • empty - The opposite of not_empty. Equivalent to (not (not_empty +X)). Provided for convenience. Can be given a list as argument.
      • +
      • any_not_empty - Returns true if not_empty returns true for any of +the options in the list. +Example: (any_empty ["foo", "bar", "baz"]) is equivalent to (not (and +(not_empty "foo"), (not_empty "bar"), (not_empty "baz"))).
      • +
      • single_input_file - Returns true if there was only one input file +provided on the command-line. Used without arguments: +(single_input_file).
      • +
      • multiple_input_files - Equivalent to (not (single_input_file)) (the +case of zero input files is considered an error).
      • default - Always evaluates to true. Should always be the last test in the case expression.
      • -
      • and - A standard logical combinator that returns true iff all -of its arguments return true. Used like this: (and (test1), -(test2), ... (testN)). Nesting of and and or is allowed, -but not encouraged.
      • -
      • or - Another logical combinator that returns true only if any -one of its arguments returns true. Example: (or (test1), -(test2), ... (testN)).
      • +
      • and - A standard binary logical combinator that returns true iff all of +its arguments return true. Used like this: (and (test1), (test2), +... (testN)). Nesting of and and or is allowed, but not +encouraged.
      • +
      • or - A binary logical combinator that returns true iff any of its +arguments returns true. Example: (or (test1), (test2), ... (testN)).
      • +
      • not - Standard unary logical combinator that negates its +argument. Example: (not (or (test1), (test2), ... (testN))).
    -
    -

    Writing a tool description

    +
    +

    Writing a tool description

    As was said earlier, nodes in the compilation graph represent tools, which are described separately. A tool definition looks like this (taken from the include/llvm/CompilerDriver/Tools.td file):

    @@ -408,8 +498,8 @@ options that aren't mentioned in the option list.

  • Possible tool properties:
    • in_language - input language name. Can be either a string or a list, in case the tool supports multiple input languages.
    • -
    • out_language - output language name. Tools are not allowed to -have multiple output languages.
    • +
    • out_language - output language name. Multiple output languages are not +allowed.
    • output_suffix - output file suffix. Can also be changed dynamically, see documentation on actions.
    • cmd_line - the actual command used to run the tool. You can @@ -426,8 +516,8 @@ below).
  • -
    -

    Actions

    +
    +

    Actions

    A tool often needs to react to command-line options, and this is precisely what the actions property is for. The next example illustrates this feature:

    @@ -462,12 +552,16 @@ like a linker.

    • append_cmd - append a string to the tool invocation command. -Example: (case (switch_on "pthread"), (append_cmd "-lpthread"))
    • -
    • forward - forward an option unchanged. -Example: (forward "Wall").
    • +Example: (case (switch_on "pthread"), (append_cmd +"-lpthread")) +
    • error - exit with error. +Example: (error "Mixing -c and -S is not allowed!").
    • +
    • warning - print a warning. +Example: (warning "Specifying both -O1 and -O2 is meaningless!").
    • +
    • forward - forward an option unchanged. Example: (forward "Wall").
    • forward_as - Change the name of an option, but forward the argument unchanged. -Example: (forward_as "O0" "--disable-optimization").
    • +Example: (forward_as "O0", "--disable-optimization").
    • output_suffix - modify the output suffix of this tool. Example: (output_suffix "i").
    • @@ -484,8 +578,8 @@ Example: (unpack_values
    -
    -

    Language map

    +
    +

    Language map

    If you are adding support for a new language to LLVMC, you'll need to modify the language map, which defines mappings from file extensions to language names. It is used to choose the proper toolchain(s) for a @@ -507,27 +601,59 @@ linked with the root node. Since tools are not allowed to have multiple output languages, for nodes "inside" the graph the input and output languages should match. This is enforced at compile-time.

    -
    -

    More advanced topics

    -
    -

    Hooks and environment variables

    +
    +

    Option preprocessor

    +

    It is sometimes useful to run error-checking code before processing the +compilation graph. For example, if optimization options "-O1" and "-O2" are +implemented as switches, we might want to output a warning if the user invokes +the driver with both of these options enabled.

    +

    The OptionPreprocessor feature is reserved specially for these +occasions. Example (adapted from the built-in Base plugin):

    +
    +def Preprocess : OptionPreprocessor<
    +(case (and (switch_on "O3"), (any_switch_on ["O0", "O1", "O2"])),
    +           [(unset_option ["O0", "O1", "O2"]),
    +            (warning "Multiple -O options specified, defaulted to -O3.")],
    +      (and (switch_on "O2"), (any_switch_on ["O0", "O1"])),
    +           (unset_option ["O0", "O1"]),
    +      (and (switch_on "O1"), (switch_on "O0")),
    +           (unset_option "O0"))
    +>;
    +
    +

    Here, OptionPreprocessor is used to unset all spurious optimization options +(so that they are not forwarded to the compiler).

    +

    OptionPreprocessor is basically a single big case expression, which is +evaluated only once right after the plugin is loaded. The only allowed actions +in OptionPreprocessor are error, warning and a special action +unset_option, which, as the name suggests, unsets a given option. For +convenience, unset_option also works on lists.

    +
    +
    +

    More advanced topics

    +
    +

    Hooks and environment variables

    Normally, LLVMC executes programs from the system PATH. Sometimes, -this is not sufficient: for example, we may want to specify tool names -in the configuration file. This can be achieved via the mechanism of -hooks - to write your own hooks, just add their definitions to the -PluginMain.cpp or drop a .cpp file into the -$LLVMC_DIR/driver directory. Hooks should live in the hooks -namespace and have the signature std::string hooks::MyHookName -(void). They can be used from the cmd_line tool property:

    +this is not sufficient: for example, we may want to specify tool paths +or names in the configuration file. This can be easily achieved via +the hooks mechanism. To write your own hooks, just add their +definitions to the PluginMain.cpp or drop a .cpp file into the +your plugin directory. Hooks should live in the hooks namespace +and have the signature std::string hooks::MyHookName ([const char* +Arg0 [ const char* Arg2 [, ...]]]). They can be used from the +cmd_line tool property:

     (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
     
    +

    To pass arguments to hooks, use the following syntax:

    +
    +(cmd_line "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2")
    +

    It is also possible to use environment variables in the same manner:

     (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
     

    To change the command line string based on user-provided options use -the case expression (documented above):

    +the case expression (documented above):

     (cmd_line
       (case
    @@ -537,8 +663,8 @@ the case expression (
            "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
     
    -
    -

    How plugins are loaded

    +
    +

    How plugins are loaded

    It is possible for LLVMC plugins to depend on each other. For example, one can create edges between nodes defined in some other plugin. To make this work, however, that plugin should be loaded first. To @@ -553,14 +679,42 @@ def Priority : PluginPriority<$PRIORITY_VALUE>; with 0. Therefore, the plugin with the highest priority value will be loaded last.

    -
    -

    Debugging

    +
    +

    Debugging

    When writing LLVMC plugins, it can be useful to get a visual view of the resulting compilation graph. This can be achieved via the command -line option --view-graph. This command assumes that Graphviz and -Ghostview are installed. There is also a --dump-graph option that +line option --view-graph. This command assumes that Graphviz and +Ghostview are installed. There is also a --write-graph option that creates a Graphviz source file (compilation-graph.dot) in the current directory.

    +

    Another useful llvmc option is --check-graph. It checks the +compilation graph for common errors like mismatched output/input +language names, multiple default edges and cycles. These checks can't +be performed at compile-time because the plugins can load code +dynamically. When invoked with --check-graph, llvmc doesn't +perform any compilation tasks and returns the number of encountered +errors as its status code.

    +
    +
    +

    Conditioning on the executable name

    +

    For now, the executable name (the value passed to the driver in argv[0]) is +accessible only in the C++ code (i.e. hooks). Use the following code:

    +
    +namespace llvmc {
    +extern const char* ProgramName;
    +}
    +
    +std::string MyHook() {
    +//...
    +if (strcmp(ProgramName, "mydriver") == 0) {
    +   //...
    +
    +}
    +
    +

    In general, you're encouraged not to make the behaviour dependent on the +executable file name, and use command-line switches instead. See for example how +the Base plugin behaves when it needs to choose the correct linker options +(think g++ vs. gcc).


    @@ -573,7 +727,7 @@ current directory.

    Mikhail Glushenkov
    LLVM Compiler Infrastructure
    -Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $ +Last modified: $Date$