Update User's Guide, cleanup DynFlags
[ghc.git] / docs / users_guide / using.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="using-ghc">
3 <title>Using GHC</title>
4
5 <indexterm><primary>GHC, using</primary></indexterm>
6 <indexterm><primary>using GHC</primary></indexterm>
7
8 <sect1>
9 <title>Getting started: compiling programs</title>
10
11 <para>
12 In this chapter you'll find a complete reference to the GHC
13 command-line syntax, including all 400+ flags. It's a large and
14 complex system, and there are lots of details, so it can be
15 quite hard to figure out how to get started. With that in mind,
16 this introductory section provides a quick introduction to the
17 basic usage of GHC for compiling a Haskell program, before the
18 following sections dive into the full syntax.
19 </para>
20
21 <para>
22 Let's create a Hello World program, and compile and run it.
23 First, create a file <filename>hello.hs</filename> containing
24 the Haskell code:
25 </para>
26
27 <programlisting>
28 main = putStrLn "Hello, World!"
29 </programlisting>
30
31 <para>To compile the program, use GHC like this:</para>
32
33 <screen>
34 $ ghc hello.hs
35 </screen>
36
37 <para>(where <literal>$</literal> represents the prompt: don't
38 type it). GHC will compile the source
39 file <filename>hello.hs</filename>, producing
40 an <firstterm>object
41 file</firstterm> <filename>hello.o</filename> and
42 an <firstterm>interface
43 file</firstterm> <filename>hello.hi</filename>, and then it
44 will link the object file to the libraries that come with GHC
45 to produce an executable called <filename>hello</filename> on
46 Unix/Linux/Mac, or <filename>hello.exe</filename> on
47 Windows.</para>
48
49 <para>
50 By default GHC will be very quiet about what it is doing, only
51 printing error messages. If you want to see in more detail
52 what's going on behind the scenes, add <option>-v</option> to
53 the command line.
54 </para>
55
56 <para>
57 Then we can run the program like this:
58 </para>
59
60 <screen>
61 $ ./hello
62 Hello World!
63 </screen>
64
65 <para>
66 If your program contains multiple modules, then you only need to
67 tell GHC the name of the source file containing
68 the <filename>Main</filename> module, and GHC will examine
69 the <literal>import</literal> declarations to find the other
70 modules that make up the program and find their source files.
71 This means that, with the exception of
72 the <literal>Main</literal> module, every source file should be
73 named after the module name that it contains (with dots replaced
74 by directory separators). For example, the
75 module <literal>Data.Person</literal> would be in the
76 file <filename>Data/Person.hs</filename> on Unix/Linux/Mac,
77 or <filename>Data\Person.hs</filename> on Windows.
78 </para>
79 </sect1>
80
81 <sect1>
82 <title>Options overview</title>
83
84 <para>GHC's behaviour is controlled by
85 <firstterm>options</firstterm>, which for historical reasons are
86 also sometimes referred to as command-line flags or arguments.
87 Options can be specified in three ways:</para>
88
89 <sect2>
90 <title>Command-line arguments</title>
91
92 <indexterm><primary>structure, command-line</primary></indexterm>
93 <indexterm><primary>command-line</primary><secondary>arguments</secondary></indexterm>
94 <indexterm><primary>arguments</primary><secondary>command-line</secondary></indexterm>
95
96 <para>An invocation of GHC takes the following form:</para>
97
98 <screen>
99 ghc [argument...]
100 </screen>
101
102 <para>Command-line arguments are either options or file names.</para>
103
104 <para>Command-line options begin with <literal>-</literal>.
105 They may <emphasis>not</emphasis> be grouped:
106 <option>-vO</option> is different from <option>-v -O</option>.
107 Options need not precede filenames: e.g., <literal>ghc *.o -o
108 foo</literal>. All options are processed and then applied to
109 all files; you cannot, for example, invoke <literal>ghc -c -O1
110 Foo.hs -O2 Bar.hs</literal> to apply different optimisation
111 levels to the files <filename>Foo.hs</filename> and
112 <filename>Bar.hs</filename>.</para>
113 </sect2>
114
115 <sect2 id="source-file-options">
116 <title>Command line options in source files</title>
117
118 <indexterm><primary>source-file options</primary></indexterm>
119
120 <para>Sometimes it is useful to make the connection between a
121 source file and the command-line options it requires quite
122 tight. For instance, if a Haskell source file deliberately
123 uses name shadowing, it should be compiled with the
124 <option>-fno-warn-name-shadowing</option> option. Rather than maintaining
125 the list of per-file options in a <filename>Makefile</filename>,
126 it is possible to do this directly in the source file using the
127 <literal>OPTIONS_GHC</literal> pragma <indexterm><primary>OPTIONS_GHC
128 pragma</primary></indexterm>:</para>
129
130 <programlisting>
131 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
132 module X where
133 ...
134 </programlisting>
135
136 <para><literal>OPTIONS_GHC</literal> is a <emphasis>file-header pragma</emphasis>
137 (see <xref linkend="pragmas"/>).</para>
138
139 <para>Only <emphasis>dynamic</emphasis> flags can be used in an <literal>OPTIONS_GHC</literal> pragma
140 (see <xref linkend="static-dynamic-flags"/>).</para>
141
142 <para>Note that your command shell does not
143 get to the source file options, they are just included literally
144 in the array of command-line arguments the compiler
145 maintains internally, so you'll be desperately disappointed if
146 you try to glob etc. inside <literal>OPTIONS_GHC</literal>.</para>
147
148 <para>NOTE: the contents of OPTIONS_GHC are appended to the
149 command-line options, so options given in the source file
150 override those given on the command-line.</para>
151
152 <para>It is not recommended to move all the contents of your
153 Makefiles into your source files, but in some circumstances, the
154 <literal>OPTIONS_GHC</literal> pragma is the Right Thing. (If you
155 use <option>-keep-hc-file</option> and have OPTION flags in
156 your module, the OPTIONS_GHC will get put into the generated .hc
157 file).</para>
158 </sect2>
159
160 <sect2>
161 <title>Setting options in GHCi</title>
162
163 <para>Options may also be modified from within GHCi, using the
164 <literal>:set</literal> command. See <xref linkend="ghci-set"/>
165 for more details.</para>
166 </sect2>
167 </sect1>
168
169 <sect1 id="static-dynamic-flags">
170 <title>Static, Dynamic, and Mode options</title>
171 <indexterm><primary>static</primary><secondary>options</secondary>
172 </indexterm>
173 <indexterm><primary>dynamic</primary><secondary>options</secondary>
174 </indexterm>
175 <indexterm><primary>mode</primary><secondary>options</secondary>
176 </indexterm>
177
178 <para>Each of GHC's command line options is classified as
179 <firstterm>static</firstterm>, <firstterm>dynamic</firstterm> or
180 <firstterm>mode</firstterm>:</para>
181
182 <variablelist>
183 <varlistentry>
184 <term>Mode flags</term>
185 <listitem>
186 <para>For example, <option>--make</option> or <option>-E</option>.
187 There may only be a single mode flag on the command line. The
188 available modes are listed in <xref linkend="modes"/>.</para>
189 </listitem>
190 </varlistentry>
191 <varlistentry>
192 <term>Dynamic Flags</term>
193 <listitem>
194 <para>Most non-mode flags fall into this category. A dynamic flag
195 may be used on the command line, in a
196 <literal>OPTIONS_GHC</literal> pragma in a source file, or set
197 using <literal>:set</literal> in GHCi.</para>
198 </listitem>
199 </varlistentry>
200 <varlistentry>
201 <term>Static Flags</term>
202 <listitem>
203 <para>A few flags are "static", which means they can only be used on
204 the command-line, and remain in force over the entire GHC/GHCi
205 run.</para>
206 </listitem>
207 </varlistentry>
208 </variablelist>
209
210 <para>The flag reference tables (<xref
211 linkend="flag-reference"/>) lists the status of each flag.</para>
212
213 <para>There are a few flags that are static except that they can
214 also be used with GHCi's <literal>:set</literal> command; these
215 are listed as &ldquo;static/<literal>:set</literal>&rdquo; in the
216 table.</para>
217 </sect1>
218
219 <sect1 id="file-suffixes">
220 <title>Meaningful file suffixes</title>
221
222 <indexterm><primary>suffixes, file</primary></indexterm>
223 <indexterm><primary>file suffixes for GHC</primary></indexterm>
224
225 <para>File names with &ldquo;meaningful&rdquo; suffixes (e.g.,
226 <filename>.lhs</filename> or <filename>.o</filename>) cause the
227 &ldquo;right thing&rdquo; to happen to those files.</para>
228
229 <variablelist>
230
231 <varlistentry>
232 <term><filename>.hs</filename></term>
233 <listitem>
234 <para>A Haskell module.</para>
235 </listitem>
236 </varlistentry>
237
238 <varlistentry>
239 <term>
240 <filename>.lhs</filename>
241 <indexterm><primary><literal>lhs</literal> suffix</primary></indexterm>
242 </term>
243 <listitem>
244 <para>A &ldquo;literate Haskell&rdquo; module.</para>
245 </listitem>
246 </varlistentry>
247
248 <varlistentry>
249 <term><filename>.hi</filename></term>
250 <listitem>
251 <para>A Haskell interface file, probably
252 compiler-generated.</para>
253 </listitem>
254 </varlistentry>
255
256 <varlistentry>
257 <term><filename>.hc</filename></term>
258 <listitem>
259 <para>Intermediate C file produced by the Haskell
260 compiler.</para>
261 </listitem>
262 </varlistentry>
263
264 <varlistentry>
265 <term><filename>.c</filename></term>
266 <listitem>
267 <para>A C&nbsp;file not produced by the Haskell
268 compiler.</para>
269 </listitem>
270 </varlistentry>
271
272 <varlistentry>
273 <term><filename>.ll</filename></term>
274 <listitem>
275 <para>An llvm-intermediate-language source file, usually
276 produced by the compiler.</para>
277 </listitem>
278 </varlistentry>
279
280 <varlistentry>
281 <term><filename>.bc</filename></term>
282 <listitem>
283 <para>An llvm-intermediate-language bitcode file, usually
284 produced by the compiler.</para>
285 </listitem>
286 </varlistentry>
287
288 <varlistentry>
289 <term><filename>.s</filename></term>
290 <listitem>
291 <para>An assembly-language source file, usually produced by
292 the compiler.</para>
293 </listitem>
294 </varlistentry>
295
296 <varlistentry>
297 <term><filename>.o</filename></term>
298 <listitem>
299 <para>An object file, produced by an assembler.</para>
300 </listitem>
301 </varlistentry>
302 </variablelist>
303
304 <para>Files with other suffixes (or without suffixes) are passed
305 straight to the linker.</para>
306
307 </sect1>
308
309 <sect1 id="modes">
310 <title>Modes of operation</title>
311 <indexterm><primary>help options</primary></indexterm>
312
313 <para>
314 GHC's behaviour is firstly controlled by a mode flag. Only one
315 of these flags may be given, but it does not necessarily need to
316 be the first option on the command-line.
317 </para>
318
319 <para>
320 If no mode flag is present, then GHC will enter make mode
321 (<xref linkend="make-mode" />) if there are any Haskell source
322 files given on the command line, or else it will link the
323 objects named on the command line to produce an executable.
324 </para>
325
326 <para>The available mode flags are:</para>
327
328 <variablelist>
329 <varlistentry>
330 <term>
331 <cmdsynopsis><command>ghc --interactive</command>
332 </cmdsynopsis>
333 <indexterm><primary>interactive mode</primary></indexterm>
334 <indexterm><primary>ghci</primary></indexterm>
335 </term>
336 <listitem>
337 <para>Interactive mode, which is also available as
338 <command>ghci</command>. Interactive mode is described in
339 more detail in <xref linkend="ghci"/>.</para>
340 </listitem>
341 </varlistentry>
342
343 <varlistentry>
344 <term>
345 <cmdsynopsis><command>ghc --make</command>
346 </cmdsynopsis>
347 <indexterm><primary>make mode</primary></indexterm>
348 <indexterm><primary><option>--make</option></primary></indexterm>
349 </term>
350 <listitem>
351 <para>In this mode, GHC will build a multi-module Haskell
352 program automatically, figuring out dependencies for itself.
353 If you have a straightforward Haskell program, this is
354 likely to be much easier, and faster, than using
355 <command>make</command>. Make mode is described in <xref
356 linkend="make-mode"/>.</para>
357
358 <para>
359 This mode is the default if there are any Haskell
360 source files mentioned on the command line, and in this case
361 the <option>--make</option> option can be omitted.
362 </para>
363 </listitem>
364 </varlistentry>
365
366 <varlistentry>
367 <term>
368 <cmdsynopsis><command>ghc -e</command>
369 <arg choice='plain'><replaceable>expr</replaceable></arg>
370 </cmdsynopsis>
371 <indexterm><primary>eval mode</primary></indexterm>
372 </term>
373 <listitem>
374 <para>Expression-evaluation mode. This is very similar to
375 interactive mode, except that there is a single expression
376 to evaluate (<replaceable>expr</replaceable>) which is given
377 on the command line. See <xref linkend="eval-mode"/> for
378 more details.</para>
379 </listitem>
380 </varlistentry>
381
382 <varlistentry>
383 <term>
384 <cmdsynopsis>
385 <command>ghc -E</command>
386 <command>ghc -c</command>
387 <command>ghc -S</command>
388 <command>ghc -c</command>
389 </cmdsynopsis>
390 <indexterm><primary><option>-E</option></primary></indexterm>
391 <indexterm><primary><option>-C</option></primary></indexterm>
392 <indexterm><primary><option>-S</option></primary></indexterm>
393 <indexterm><primary><option>-c</option></primary></indexterm>
394 </term>
395 <listitem>
396 <para>This is the traditional batch-compiler mode, in which
397 GHC can compile source files one at a time, or link objects
398 together into an executable. This mode also applies if
399 there is no other mode flag specified on the command line,
400 in which case it means that the specified files should be
401 compiled and then linked to form a program. See <xref
402 linkend="options-order"/>.</para>
403 </listitem>
404 </varlistentry>
405
406 <varlistentry>
407 <term>
408 <cmdsynopsis>
409 <command>ghc -M</command>
410 </cmdsynopsis>
411 <indexterm><primary>dependency-generation mode</primary></indexterm>
412 </term>
413 <listitem>
414 <para>Dependency-generation mode. In this mode, GHC can be
415 used to generate dependency information suitable for use in
416 a <literal>Makefile</literal>. See <xref
417 linkend="makefile-dependencies"/>.</para>
418 </listitem>
419 </varlistentry>
420
421 <varlistentry>
422 <term>
423 <cmdsynopsis>
424 <command>ghc --mk-dll</command>
425 </cmdsynopsis>
426 <indexterm><primary>DLL-creation mode</primary></indexterm>
427 </term>
428 <listitem>
429 <para>DLL-creation mode (Windows only). See <xref
430 linkend="win32-dlls-create"/>.</para>
431 </listitem>
432 </varlistentry>
433
434 <varlistentry>
435 <term>
436 <cmdsynopsis>
437 <command>ghc --help</command> <command>ghc -?</command>
438 </cmdsynopsis>
439 <indexterm><primary><option>--help</option></primary></indexterm>
440 </term>
441 <listitem>
442 <para>Cause GHC to spew a long usage message to standard
443 output and then exit.</para>
444 </listitem>
445 </varlistentry>
446
447 <varlistentry>
448 <term>
449 <cmdsynopsis>
450 <command>ghc --show-iface <replaceable>file</replaceable></command>
451 </cmdsynopsis>
452 <indexterm><primary><option>--show-iface</option></primary></indexterm>
453 </term>
454 <listitem>
455 <para>Read the interface in
456 <replaceable>file</replaceable> and dump it as text to
457 <literal>stdout</literal>. For example <literal>ghc --show-iface M.hi</literal>.</para>
458 </listitem>
459 </varlistentry>
460
461 <varlistentry>
462 <term>
463 <cmdsynopsis>
464 <command>ghc --supported-extensions</command>
465 <command>ghc --supported-languages</command>
466 </cmdsynopsis>
467 <indexterm><primary><option>--supported-extensions</option></primary><primary><option>--supported-languages</option></primary></indexterm>
468 </term>
469 <listitem>
470 <para>Print the supported language extensions.</para>
471 </listitem>
472 </varlistentry>
473
474 <varlistentry>
475 <term>
476 <cmdsynopsis>
477 <command>ghc --show-options</command>
478 </cmdsynopsis>
479 <indexterm><primary><option>--show-options</option></primary></indexterm>
480 </term>
481 <listitem>
482 <para>Print the supported command line options. This flag can be used for autocompletion in a shell.</para>
483 </listitem>
484 </varlistentry>
485
486 <varlistentry>
487 <term>
488 <cmdsynopsis>
489 <command>ghc --info</command>
490 </cmdsynopsis>
491 <indexterm><primary><option>--info</option></primary></indexterm>
492 </term>
493 <listitem>
494 <para>Print information about the compiler.</para>
495 </listitem>
496 </varlistentry>
497
498 <varlistentry>
499 <term>
500 <cmdsynopsis>
501 <command>ghc --version</command>
502 <command>ghc -V</command>
503 </cmdsynopsis>
504 <indexterm><primary><option>-V</option></primary></indexterm>
505 <indexterm><primary><option>--version</option></primary></indexterm>
506 </term>
507 <listitem>
508 <para>Print a one-line string including GHC's version number.</para>
509 </listitem>
510 </varlistentry>
511
512 <varlistentry>
513 <term>
514 <cmdsynopsis>
515 <command>ghc --numeric-version</command>
516 </cmdsynopsis>
517 <indexterm><primary><option>--numeric-version</option></primary></indexterm>
518 </term>
519 <listitem>
520 <para>Print GHC's numeric version number only.</para>
521 </listitem>
522 </varlistentry>
523
524 <varlistentry>
525 <term>
526 <cmdsynopsis>
527 <command>ghc --print-libdir</command>
528 </cmdsynopsis>
529 <indexterm><primary><option>--print-libdir</option></primary></indexterm>
530 </term>
531 <listitem>
532 <para>Print the path to GHC's library directory. This is
533 the top of the directory tree containing GHC's libraries,
534 interfaces, and include files (usually something like
535 <literal>/usr/local/lib/ghc-5.04</literal> on Unix). This
536 is the value of
537 <literal>$libdir</literal><indexterm><primary><literal>libdir</literal></primary></indexterm>
538 in the package configuration file
539 (see <xref linkend="packages"/>).</para>
540 </listitem>
541 </varlistentry>
542
543 </variablelist>
544
545 <sect2 id="make-mode">
546 <title>Using <command>ghc</command> <option>--make</option></title>
547 <indexterm><primary><option>--make</option></primary></indexterm>
548 <indexterm><primary>separate compilation</primary></indexterm>
549
550 <para>In this mode, GHC will build a multi-module Haskell program by following
551 dependencies from one or more root modules (usually just
552 <literal>Main</literal>). For example, if your
553 <literal>Main</literal> module is in a file called
554 <filename>Main.hs</filename>, you could compile and link the
555 program like this:</para>
556
557 <screen>
558 ghc --make Main.hs
559 </screen>
560
561 <para>
562 In fact, GHC enters make mode automatically if there are any
563 Haskell source files on the command line and no other mode is
564 specified, so in this case we could just type
565 </para>
566
567 <screen>
568 ghc Main.hs
569 </screen>
570
571 <para>Any number of source file names or module names may be
572 specified; GHC will figure out all the modules in the program by
573 following the imports from these initial modules. It will then
574 attempt to compile each module which is out of date, and
575 finally, if there is a <literal>Main</literal> module, the
576 program will also be linked into an executable.</para>
577
578 <para>The main advantages to using <literal>ghc
579 --make</literal> over traditional
580 <literal>Makefile</literal>s are:</para>
581
582 <itemizedlist>
583 <listitem>
584 <para>GHC doesn't have to be restarted for each compilation,
585 which means it can cache information between compilations.
586 Compiling a multi-module program with <literal>ghc
587 --make</literal> can be up to twice as fast as
588 running <literal>ghc</literal> individually on each source
589 file.</para>
590 </listitem>
591 <listitem>
592 <para>You don't have to write a <literal>Makefile</literal>.</para>
593 <indexterm><primary><literal>Makefile</literal>s</primary><secondary>avoiding</secondary></indexterm>
594 </listitem>
595 <listitem>
596 <para>GHC re-calculates the dependencies each time it is
597 invoked, so the dependencies never get out of sync with the
598 source.</para>
599 </listitem>
600 <listitem>
601 <para>Using the <literal>-j</literal> flag, you can compile
602 modules in parallel. Specify <literal>-jN</literal> to
603 compile <replaceable>N</replaceable> jobs in parallel.</para>
604 </listitem>
605 </itemizedlist>
606
607 <para>Any of the command-line options described in the rest of
608 this chapter can be used with
609 <option>--make</option>, but note that any options
610 you give on the command line will apply to all the source files
611 compiled, so if you want any options to apply to a single source
612 file only, you'll need to use an <literal>OPTIONS_GHC</literal>
613 pragma (see <xref linkend="source-file-options"/>).</para>
614
615 <para>If the program needs to be linked with additional objects
616 (say, some auxiliary C code), then the object files can be
617 given on the command line and GHC will include them when linking
618 the executable.</para>
619
620 <para>Note that GHC can only follow dependencies if it has the
621 source file available, so if your program includes a module for
622 which there is no source file, even if you have an object and an
623 interface file for the module, then GHC will complain. The
624 exception to this rule is for package modules, which may or may
625 not have source files.</para>
626
627 <para>The source files for the program don't all need to be in
628 the same directory; the <option>-i</option> option can be used
629 to add directories to the search path (see <xref
630 linkend="search-path"/>).</para>
631 </sect2>
632
633 <sect2 id="eval-mode">
634 <title>Expression evaluation mode</title>
635
636 <para>This mode is very similar to interactive mode, except that
637 there is a single expression to evaluate which is specified on
638 the command line as an argument to the <option>-e</option>
639 option:</para>
640
641 <screen>
642 ghc -e <replaceable>expr</replaceable>
643 </screen>
644
645 <para>Haskell source files may be named on the command line, and
646 they will be loaded exactly as in interactive mode. The
647 expression is evaluated in the context of the loaded
648 modules.</para>
649
650 <para>For example, to load and run a Haskell program containing
651 a module <literal>Main</literal>, we might say</para>
652
653 <screen>
654 ghc -e Main.main Main.hs
655 </screen>
656
657 <para>or we can just use this mode to evaluate expressions in
658 the context of the <literal>Prelude</literal>:</para>
659
660 <screen>
661 $ ghc -e "interact (unlines.map reverse.lines)"
662 hello
663 olleh
664 </screen>
665 </sect2>
666
667 <sect2 id="options-order">
668 <title>Batch compiler mode</title>
669
670 <para>In <emphasis>batch mode</emphasis>, GHC will compile one or more source files
671 given on the command line.</para>
672
673 <para>The first phase to run is determined by each input-file
674 suffix, and the last phase is determined by a flag. If no
675 relevant flag is present, then go all the way through to linking.
676 This table summarises:</para>
677
678 <informaltable>
679 <tgroup cols="4">
680 <colspec align="left"/>
681 <colspec align="left"/>
682 <colspec align="left"/>
683 <colspec align="left"/>
684
685 <thead>
686 <row>
687 <entry>Phase of the compilation system</entry>
688 <entry>Suffix saying &ldquo;start here&rdquo;</entry>
689 <entry>Flag saying &ldquo;stop after&rdquo;</entry>
690 <entry>(suffix of) output file</entry>
691 </row>
692 </thead>
693 <tbody>
694 <row>
695 <entry>literate pre-processor</entry>
696 <entry><literal>.lhs</literal></entry>
697 <entry>-</entry>
698 <entry><literal>.hs</literal></entry>
699 </row>
700
701 <row>
702 <entry>C pre-processor (opt.) </entry>
703 <entry><literal>.hs</literal> (with
704 <option>-cpp</option>)</entry>
705 <entry><option>-E</option></entry>
706 <entry><literal>.hspp</literal></entry>
707 </row>
708
709 <row>
710 <entry>Haskell compiler</entry>
711 <entry><literal>.hs</literal></entry>
712 <entry><option>-C</option>, <option>-S</option></entry>
713 <entry><literal>.hc</literal>, <literal>.s</literal></entry>
714 </row>
715
716 <row>
717 <entry>C compiler (opt.)</entry>
718 <entry><literal>.hc</literal> or <literal>.c</literal></entry>
719 <entry><option>-S</option></entry>
720 <entry><literal>.s</literal></entry>
721 </row>
722
723 <row>
724 <entry>assembler</entry>
725 <entry><literal>.s</literal></entry>
726 <entry><option>-c</option></entry>
727 <entry><literal>.o</literal></entry>
728 </row>
729
730 <row>
731 <entry>linker</entry>
732 <entry><replaceable>other</replaceable></entry>
733 <entry>-</entry>
734 <entry><filename>a.out</filename></entry>
735 </row>
736 </tbody>
737 </tgroup>
738 </informaltable>
739
740 <indexterm><primary><option>-C</option></primary></indexterm>
741 <indexterm><primary><option>-E</option></primary></indexterm>
742 <indexterm><primary><option>-S</option></primary></indexterm>
743 <indexterm><primary><option>-c</option></primary></indexterm>
744
745 <para>Thus, a common invocation would be: </para>
746
747 <screen>
748 ghc -c Foo.hs
749 </screen>
750
751 <para>to compile the Haskell source file
752 <filename>Foo.hs</filename> to an object file
753 <filename>Foo.o</filename>.</para>
754
755 <para>Note: What the Haskell compiler proper produces depends on what
756 backend code generator is used. See <xref linkend="code-generators"/>
757 for more details.</para>
758
759 <para>Note: C pre-processing is optional, the
760 <option>-cpp</option><indexterm><primary><option>-cpp</option></primary></indexterm>
761 flag turns it on. See <xref linkend="c-pre-processor"/> for more
762 details.</para>
763
764 <para>Note: The option <option>-E</option><indexterm><primary>-E
765 option</primary></indexterm> runs just the pre-processing passes
766 of the compiler, dumping the result in a file.</para>
767
768 <sect3 id="overriding-suffixes">
769 <title>Overriding the default behaviour for a file</title>
770
771 <para>As described above, the way in which a file is processed by GHC
772 depends on its suffix. This behaviour can be overridden using the
773 <option>-x</option> option:</para>
774
775 <variablelist>
776 <varlistentry>
777 <term><option>-x</option> <replaceable>suffix</replaceable>
778 <indexterm><primary><option>-x</option></primary>
779 </indexterm></term>
780 <listitem>
781 <para>Causes all files following this option on the command
782 line to be processed as if they had the suffix
783 <replaceable>suffix</replaceable>. For example, to compile a
784 Haskell module in the file <literal>M.my-hs</literal>,
785 use <literal>ghc -c -x hs M.my-hs</literal>.</para>
786 </listitem>
787 </varlistentry>
788 </variablelist>
789 </sect3>
790
791 </sect2>
792 </sect1>
793
794 <sect1 id="options-help">
795 <title>Verbosity options</title>
796
797 <indexterm><primary>verbosity options</primary></indexterm>
798
799 <para>See also the <option>--help</option>, <option>--version</option>, <option>--numeric-version</option>,
800 and <option>--print-libdir</option> modes in <xref linkend="modes"/>.</para>
801 <variablelist>
802 <varlistentry>
803 <term>
804 <option>-v</option>
805 <indexterm><primary><option>-v</option></primary></indexterm>
806 </term>
807 <listitem>
808 <para>The <option>-v</option> option makes GHC
809 <emphasis>verbose</emphasis>: it reports its version number
810 and shows (on stderr) exactly how it invokes each phase of
811 the compilation system. Moreover, it passes the
812 <option>-v</option> flag to most phases; each reports its
813 version number (and possibly some other information).</para>
814
815 <para>Please, oh please, use the <option>-v</option> option
816 when reporting bugs! Knowing that you ran the right bits in
817 the right order is always the first thing we want to
818 verify.</para>
819 </listitem>
820 </varlistentry>
821
822 <varlistentry>
823 <term>
824 <option>-v</option><replaceable>n</replaceable>
825 <indexterm><primary><option>-v</option></primary></indexterm>
826 </term>
827 <listitem>
828 <para>To provide more control over the compiler's verbosity,
829 the <option>-v</option> flag takes an optional numeric
830 argument. Specifying <option>-v</option> on its own is
831 equivalent to <option>-v3</option>, and the other levels
832 have the following meanings:</para>
833
834 <variablelist>
835 <varlistentry>
836 <term><option>-v0</option></term>
837 <listitem>
838 <para>Disable all non-essential messages (this is the
839 default).</para>
840 </listitem>
841 </varlistentry>
842
843 <varlistentry>
844 <term><option>-v1</option></term>
845 <listitem>
846 <para>Minimal verbosity: print one line per
847 compilation (this is the default when
848 <option>--make</option> or
849 <option>--interactive</option> is on).</para>
850 </listitem>
851 </varlistentry>
852
853 <varlistentry>
854 <term><option>-v2</option></term>
855 <listitem>
856 <para>Print the name of each compilation phase as it
857 is executed. (equivalent to
858 <option>-dshow-passes</option>).</para>
859 </listitem>
860 </varlistentry>
861
862 <varlistentry>
863 <term><option>-v3</option></term>
864 <listitem>
865 <para>The same as <option>-v2</option>, except that in
866 addition the full command line (if appropriate) for
867 each compilation phase is also printed.</para>
868 </listitem>
869 </varlistentry>
870
871 <varlistentry>
872 <term><option>-v4</option></term>
873 <listitem>
874 <para>The same as <option>-v3</option> except that the
875 intermediate program representation after each
876 compilation phase is also printed (excluding
877 preprocessed and C/assembly files).</para>
878 </listitem>
879 </varlistentry>
880 </variablelist>
881 </listitem>
882 </varlistentry>
883
884
885 <varlistentry>
886 <term><option>--fprint-explicit-foralls, -fprint-explicit-kinds</option>
887 <indexterm><primary><option>-fprint-explicit-foralls</option></primary></indexterm>
888 <indexterm><primary><option>-fprint-explicit-kinds</option></primary></indexterm>
889 </term>
890 <listitem>
891 <para>These two flags control the way in which GHC displays types, in error messages and in GHCi.
892 Using <option>-fprint-explicit-foralls</option> makes GHC print explicit <literal>forall</literal>
893 quantification at the top level of a type; normally this is suppressed. For example, in GHCi:
894 <screen>
895 ghci> let f x = x
896 ghci> :t f
897 f :: a -> a
898 ghci> :set -fprint-explicit-foralls
899 ghci> :t f
900 f :: forall a. a -> a
901 </screen>
902 However, regardless of the flag setting, the quantifiers are printed under these circumstances:
903 <itemizedlist>
904 <listitem><para>For nested <literal>foralls</literal>, e.g.
905 <screen>
906 ghci> :t GHC.ST.runST
907 GHC.ST.runST :: (forall s. GHC.ST.ST s a) -> a
908 </screen>
909 </para></listitem>
910 <listitem><para>If any of the quantified type variables has a kind
911 that mentions a kind variable, e.g.
912 <screen>
913 ghci> :i Data.Coerce.coerce
914 coerce ::
915 forall (k :: BOX) (a :: k) (b :: k). Coercible a b => a -> b
916 -- Defined in GHC.Prim
917 </screen>
918 </para></listitem>
919 </itemizedlist>
920 </para>
921 <para>
922 Using <option>-fprint-explicit-kinds</option> makes GHC print kind arguments
923 in types, which are normally suppressed. This can be important when you are using kind polymorphism.
924 For example:
925 <screen>
926 ghci> :set -XPolyKinds
927 ghci> data T a = MkT
928 ghci> :t MkT
929 MkT :: forall (k :: BOX) (a :: k). T a
930 ghci> :set -fprint-explicit-foralls
931 ghci> :t MkT
932 MkT :: forall (k :: BOX) (a :: k). T k a
933 </screen>
934 </para>
935 </listitem>
936 </varlistentry>
937
938 <varlistentry>
939 <term><option>-ferror-spans</option>
940 <indexterm><primary><option>-ferror-spans</option></primary>
941 </indexterm>
942 </term>
943 <listitem>
944 <para>Causes GHC to emit the full source span of the
945 syntactic entity relating to an error message. Normally, GHC
946 emits the source location of the start of the syntactic
947 entity only.</para>
948
949 <para>For example:</para>
950
951 <screen>
952 test.hs:3:6: parse error on input `where'
953 </screen>
954
955 <para>becomes:</para>
956
957 <screen>
958 test296.hs:3:6-10: parse error on input `where'
959 </screen>
960
961 <para>And multi-line spans are possible too:</para>
962
963 <screen>
964 test.hs:(5,4)-(6,7):
965 Conflicting definitions for `a'
966 Bound at: test.hs:5:4
967 test.hs:6:7
968 In the binding group for: a, b, a
969 </screen>
970
971 <para>Note that line numbers start counting at one, but
972 column numbers start at zero. This choice was made to
973 follow existing convention (i.e. this is how Emacs does
974 it).</para>
975 </listitem>
976 </varlistentry>
977
978 <varlistentry>
979 <term><option>-H</option><replaceable>size</replaceable>
980 <indexterm><primary><option>-H</option></primary></indexterm>
981 </term>
982 <listitem>
983 <para>Set the minimum size of the heap to
984 <replaceable>size</replaceable>.
985 This option is equivalent to
986 <literal>+RTS&nbsp;-H<replaceable>size</replaceable></literal>,
987 see <xref linkend="rts-options-gc" />.
988 </para>
989 </listitem>
990 </varlistentry>
991
992 <varlistentry>
993 <term><option>-Rghc-timing</option>
994 <indexterm><primary><option>-Rghc-timing</option></primary></indexterm>
995 </term>
996 <listitem>
997 <para>Prints a one-line summary of timing statistics for the
998 GHC run. This option is equivalent to
999 <literal>+RTS&nbsp;-tstderr</literal>, see <xref
1000 linkend="rts-options-gc" />.
1001 </para>
1002 </listitem>
1003 </varlistentry>
1004 </variablelist>
1005 </sect1>
1006
1007 &separate;
1008
1009 <sect1 id="options-sanity">
1010 <title>Warnings and sanity-checking</title>
1011
1012 <indexterm><primary>sanity-checking options</primary></indexterm>
1013 <indexterm><primary>warnings</primary></indexterm>
1014
1015
1016 <para>GHC has a number of options that select which types of
1017 non-fatal error messages, otherwise known as warnings, can be
1018 generated during compilation. By default, you get a standard set
1019 of warnings which are generally likely to indicate bugs in your
1020 program. These are:
1021 <option>-fwarn-overlapping-patterns</option>,
1022 <option>-fwarn-warnings-deprecations</option>,
1023 <option>-fwarn-amp</option>,
1024 <option>-fwarn-deprecated-flags</option>,
1025 <option>-fwarn-unrecognised-pragmas</option>,
1026 <option>-fwarn-pointless-pragmas</option>,
1027 <option>-fwarn-duplicate-constraints</option>,
1028 <option>-fwarn-duplicate-exports</option>,
1029 <option>-fwarn-overflowed-literals</option>,
1030 <option>-fwarn-empty-enumerations</option>,
1031 <option>-fwarn-missing-fields</option>,
1032 <option>-fwarn-missing-methods</option>,
1033 <option>-fwarn-wrong-do-bind</option>,
1034 <option>-fwarn-unsupported-calling-conventions</option>,
1035 <option>-fwarn-dodgy-foreign-imports</option>,
1036 <option>-fwarn-inline-rule-shadowing</option>,
1037 <option>-fwarn-unsupported-llvm-version</option>, and
1038 <option>-fwarn-context-quantification</option>.
1039 The following flags are simple ways to select standard
1040 &ldquo;packages&rdquo; of warnings:
1041 </para>
1042
1043 <variablelist>
1044
1045 <varlistentry>
1046 <term><option>-W</option>:</term>
1047 <listitem>
1048 <indexterm><primary>-W option</primary></indexterm>
1049 <para>Provides the standard warnings plus
1050 <option>-fwarn-incomplete-patterns</option>,
1051 <option>-fwarn-dodgy-exports</option>,
1052 <option>-fwarn-dodgy-imports</option>,
1053 <option>-fwarn-unused-matches</option>,
1054 <option>-fwarn-unused-imports</option>, and
1055 <option>-fwarn-unused-binds</option>.</para>
1056 </listitem>
1057 </varlistentry>
1058
1059 <varlistentry>
1060 <term><option>-Wall</option>:</term>
1061 <listitem>
1062 <indexterm><primary><option>-Wall</option></primary></indexterm>
1063 <para>Turns on all warning options that indicate potentially
1064 suspicious code. The warnings that are
1065 <emphasis>not</emphasis> enabled by <option>-Wall</option>
1066 are
1067 <option>-fwarn-tabs</option>,
1068 <option>-fwarn-incomplete-uni-patterns</option>,
1069 <option>-fwarn-incomplete-record-updates</option>,
1070 <option>-fwarn-monomorphism-restriction</option>,
1071 <option>-fwarn-auto-orphans</option>,
1072 <option>-fwarn-implicit-prelude</option>,
1073 <option>-fwarn-missing-local-sigs</option>,
1074 <option>-fwarn-missing-import-lists</option>.</para>
1075 </listitem>
1076 </varlistentry>
1077
1078 <varlistentry>
1079 <term><option>-w</option>:</term>
1080 <listitem>
1081 <indexterm><primary><option>-w</option></primary></indexterm>
1082 <para>Turns off all warnings, including the standard ones and
1083 those that <literal>-Wall</literal> doesn't enable.</para>
1084 </listitem>
1085 </varlistentry>
1086
1087 <varlistentry>
1088 <term><option>-Werror</option>:</term>
1089 <listitem>
1090 <indexterm><primary><option>-Werror</option></primary></indexterm>
1091 <para>Makes any warning into a fatal error. Useful so that you don't
1092 miss warnings when doing batch compilation. </para>
1093 </listitem>
1094 </varlistentry>
1095
1096 <varlistentry>
1097 <term><option>-Wwarn</option>:</term>
1098 <listitem>
1099 <indexterm><primary><option>-Wwarn</option></primary></indexterm>
1100 <para>Warnings are treated only as warnings, not as errors. This is
1101 the default, but can be useful to negate a
1102 <option>-Werror</option> flag.</para>
1103 </listitem>
1104 </varlistentry>
1105
1106 </variablelist>
1107
1108 <para>The full set of warning options is described below. To turn
1109 off any warning, simply give the corresponding
1110 <option>-fno-warn-...</option> option on the command line.</para>
1111
1112 <variablelist>
1113
1114 <varlistentry>
1115 <term><option>-fwarn-typed-holes</option>:</term>
1116 <listitem>
1117 <indexterm><primary><option>-fwarn-typed-holes</option></primary>
1118 </indexterm>
1119 <indexterm><primary>warnings</primary></indexterm>
1120 <para>When the compiler encounters an unbound local
1121 variable prefixed with <literal>_</literal>, or encounters
1122 the literal <literal>_</literal> on the right-hand side of
1123 an expression, the error message for the unbound term
1124 includes the type it needs to type check. It works
1125 particularly well with <link
1126 linkend="defer-type-errors">deferred type errors</link>.
1127 See <xref linkend="typed-holes"/></para>
1128
1129 <para>This warning is on by default.</para>
1130 </listitem>
1131 </varlistentry>
1132
1133
1134 <varlistentry>
1135 <term><option>-fdefer-type-errors</option>:</term>
1136 <listitem>
1137 <indexterm><primary><option>-fdefer-type-errors</option></primary>
1138 </indexterm>
1139 <indexterm><primary>warnings</primary></indexterm>
1140 <para>Defer as many type errors as possible until runtime.
1141 At compile time you get a warning (instead of an error). At
1142 runtime, if you use a value that depends on a type error, you
1143 get a runtime error; but you can run any type-correct parts of your code
1144 just fine. See <xref linkend="defer-type-errors"/></para>
1145 </listitem>
1146 </varlistentry>
1147
1148 <varlistentry>
1149 <term><option>-fhelpful-errors</option>:</term>
1150 <listitem>
1151 <indexterm><primary><option>-fhelpful-errors</option></primary>
1152 </indexterm>
1153 <indexterm><primary>warnings</primary></indexterm>
1154 <para>When a name or package is not found in scope, make
1155 suggestions for the name or package you might have meant instead.</para>
1156 <para>This option is on by default.</para>
1157 </listitem>
1158 </varlistentry>
1159
1160 <varlistentry>
1161 <term><option>-fwarn-unrecognised-pragmas</option>:</term>
1162 <listitem>
1163 <indexterm><primary><option>-fwarn-unrecognised-pragmas</option></primary>
1164 </indexterm>
1165 <indexterm><primary>warnings</primary></indexterm>
1166 <indexterm><primary>pragmas</primary></indexterm>
1167 <para>Causes a warning to be emitted when a
1168 pragma that GHC doesn't recognise is used. As well as pragmas
1169 that GHC itself uses, GHC also recognises pragmas known to be used
1170 by other tools, e.g. <literal>OPTIONS_HUGS</literal> and
1171 <literal>DERIVE</literal>.</para>
1172
1173 <para>This option is on by default.</para>
1174 </listitem>
1175 </varlistentry>
1176
1177 <varlistentry>
1178 <term><option>-fwarn-pointless-pragmas</option>:</term>
1179 <listitem>
1180 <indexterm><primary><option>-fwarn-pointless-pragmas</option></primary>
1181 </indexterm>
1182 <indexterm><primary>warnings</primary></indexterm>
1183 <indexterm><primary>pragmas</primary></indexterm>
1184 <para>Causes a warning to be emitted when GHC detects that a
1185 module contains a pragma that has no effect.</para>
1186
1187 <para>This option is on by default.</para>
1188 </listitem>
1189 </varlistentry>
1190
1191 <varlistentry>
1192 <term><option>-fwarn-warnings-deprecations</option>:</term>
1193 <listitem>
1194 <indexterm><primary><option>-fwarn-warnings-deprecations</option></primary>
1195 </indexterm>
1196 <indexterm><primary>warnings</primary></indexterm>
1197 <indexterm><primary>deprecations</primary></indexterm>
1198 <para>Causes a warning to be emitted when a
1199 module, function or type with a WARNING or DEPRECATED pragma
1200 is used. See <xref linkend="warning-deprecated-pragma"/> for more
1201 details on the pragmas.</para>
1202
1203 <para>This option is on by default.</para>
1204 </listitem>
1205 </varlistentry>
1206
1207 <varlistentry>
1208 <term><option>-fwarn-amp</option>:</term>
1209 <listitem>
1210 <indexterm><primary><option>-fwarn-amp</option></primary>
1211 </indexterm>
1212 <indexterm><primary>amp</primary></indexterm>
1213 <indexterm><primary>applicative-monad proposal</primary></indexterm>
1214 <para>Causes a warning to be emitted when a definition
1215 is in conflict with the AMP (Applicative-Monad proosal),
1216 namely:
1217 1. Instance of Monad without Applicative;
1218 2. Instance of MonadPlus without Alternative;
1219 3. Custom definitions of join/pure/&lt;*&gt;</para>
1220
1221 <para>This option is on by default.</para>
1222 </listitem>
1223 </varlistentry>
1224
1225 <varlistentry>
1226 <term><option>-fwarn-deprecated-flags</option>:</term>
1227 <listitem>
1228 <indexterm><primary><option>-fwarn-deprecated-flags</option></primary>
1229 </indexterm>
1230 <indexterm><primary>deprecated-flags</primary></indexterm>
1231 <para>Causes a warning to be emitted when a deprecated
1232 commandline flag is used.</para>
1233
1234 <para>This option is on by default.</para>
1235 </listitem>
1236 </varlistentry>
1237
1238 <varlistentry>
1239 <term><option>-fwarn-unsupported-calling-conventions</option>:</term>
1240 <listitem>
1241 <indexterm><primary><option>-fwarn-unsupported-calling-conventions</option></primary>
1242 </indexterm>
1243 <para>Causes a warning to be emitted for foreign declarations
1244 that use unsupported calling conventions. In particular,
1245 if the <literal>stdcall</literal> calling convention is used
1246 on an architecture other than i386 then it will be treated
1247 as <literal>ccall</literal>.</para>
1248 </listitem>
1249 </varlistentry>
1250
1251 <varlistentry>
1252 <term><option>-fwarn-dodgy-foreign-imports</option>:</term>
1253 <listitem>
1254 <indexterm><primary><option>-fwarn-dodgy-foreign-imports</option></primary>
1255 </indexterm>
1256 <para>Causes a warning to be emitted for foreign imports of
1257 the following form:</para>
1258
1259 <programlisting>
1260 foreign import "f" f :: FunPtr t
1261 </programlisting>
1262
1263 <para>on the grounds that it probably should be</para>
1264
1265 <programlisting>
1266 foreign import "&amp;f" f :: FunPtr t
1267 </programlisting>
1268
1269 <para>The first form declares that `f` is a (pure) C
1270 function that takes no arguments and returns a pointer to a
1271 C function with type `t`, whereas the second form declares
1272 that `f` itself is a C function with type `t`. The first
1273 declaration is usually a mistake, and one that is hard to
1274 debug because it results in a crash, hence this
1275 warning.</para>
1276 </listitem>
1277 </varlistentry>
1278
1279 <varlistentry>
1280 <term><option>-fwarn-dodgy-exports</option>:</term>
1281 <listitem>
1282 <indexterm><primary><option>-fwarn-dodgy-exports</option></primary>
1283 </indexterm>
1284 <para>Causes a warning to be emitted when a datatype
1285 <literal>T</literal> is exported
1286 with all constructors, i.e. <literal>T(..)</literal>, but is it
1287 just a type synonym.</para>
1288 <para>Also causes a warning to be emitted when a module is
1289 re-exported, but that module exports nothing.</para>
1290 </listitem>
1291 </varlistentry>
1292
1293 <varlistentry>
1294 <term><option>-fwarn-dodgy-imports</option>:</term>
1295 <listitem>
1296 <indexterm><primary><option>-fwarn-dodgy-imports</option></primary>
1297 </indexterm>
1298 <para>Causes a warning to be emitted in the following cases:</para>
1299 <itemizedlist>
1300 <listitem>
1301 <para>When a datatype <literal>T</literal> is imported with all
1302 constructors, i.e. <literal>T(..)</literal>, but has been
1303 exported abstractly, i.e. <literal>T</literal>.
1304 </para>
1305 </listitem>
1306 <listitem>
1307 <para>When an <literal>import</literal> statement hides an
1308 entity that is not exported.</para>
1309 </listitem>
1310 </itemizedlist>
1311 </listitem>
1312 </varlistentry>
1313
1314 <varlistentry>
1315 <term><option>-fwarn-overflowed-literals</option>:</term>
1316 <listitem>
1317 <indexterm><primary><option>-fwarn-overflowed-literals</option></primary>
1318 </indexterm>
1319 <para>
1320 Causes a warning to be emitted if a literal will overflow,
1321 e.g. <literal>300 :: Word8</literal>.
1322 </para>
1323 </listitem>
1324 </varlistentry>
1325
1326 <varlistentry>
1327 <term><option>-fwarn-empty-enumerations</option>:</term>
1328 <listitem>
1329 <indexterm><primary><option>-fwarn-empty-enumerations</option></primary>
1330 </indexterm>
1331 <para>
1332 Causes a warning to be emitted if an enumeration is
1333 empty, e.g. <literal>[5 .. 3]</literal>.
1334 </para>
1335 </listitem>
1336 </varlistentry>
1337
1338 <varlistentry>
1339 <term><option>-fwarn-lazy-unlifted-bindings</option>:</term>
1340 <listitem>
1341 <indexterm><primary><option>-fwarn-lazy-unlifted-bindings</option></primary>
1342 </indexterm>
1343 <para>This flag is a no-op, and will be removed in GHC 7.10.</para>
1344 </listitem>
1345 </varlistentry>
1346
1347 <varlistentry>
1348 <term><option>-fwarn-duplicate-constraints</option>:</term>
1349 <listitem>
1350 <indexterm><primary><option>-fwarn-duplicate-constraints</option></primary></indexterm>
1351 <indexterm><primary>duplicate constraints, warning</primary></indexterm>
1352
1353 <para>Have the compiler warn about duplicate constraints in a type signature. For
1354 example
1355 <programlisting>
1356 f :: (Eq a, Show a, Eq a) => a -> a
1357 </programlisting>
1358 The warning will indicate the duplicated <literal>Eq a</literal> constraint.
1359 </para>
1360
1361 <para>This option is on by default.</para>
1362 </listitem>
1363 </varlistentry>
1364
1365 <varlistentry>
1366 <term><option>-fwarn-duplicate-exports</option>:</term>
1367 <listitem>
1368 <indexterm><primary><option>-fwarn-duplicate-exports</option></primary></indexterm>
1369 <indexterm><primary>duplicate exports, warning</primary></indexterm>
1370 <indexterm><primary>export lists, duplicates</primary></indexterm>
1371
1372 <para>Have the compiler warn about duplicate entries in
1373 export lists. This is useful information if you maintain
1374 large export lists, and want to avoid the continued export
1375 of a definition after you've deleted (one) mention of it in
1376 the export list.</para>
1377
1378 <para>This option is on by default.</para>
1379 </listitem>
1380 </varlistentry>
1381
1382 <varlistentry>
1383 <term><option>-fwarn-hi-shadowing</option>:</term>
1384 <listitem>
1385 <indexterm><primary><option>-fwarn-hi-shadowing</option></primary></indexterm>
1386 <indexterm><primary>shadowing</primary>
1387 <secondary>interface files</secondary></indexterm>
1388
1389 <para>Causes the compiler to emit a warning when a module or
1390 interface file in the current directory is shadowing one
1391 with the same module name in a library or other
1392 directory.</para>
1393 </listitem>
1394 </varlistentry>
1395
1396 <varlistentry>
1397 <term><option>-fwarn-identities</option>:</term>
1398 <listitem>
1399 <indexterm><primary><option>-fwarn-identities</option></primary></indexterm>
1400 <para>Causes the compiler to emit a warning when a Prelude numeric
1401 conversion converts a type T to the same type T; such calls
1402 are probably no-ops and can be omitted. The functions checked for
1403 are: <literal>toInteger</literal>,
1404 <literal>toRational</literal>,
1405 <literal>fromIntegral</literal>,
1406 and <literal>realToFrac</literal>.
1407 </para>
1408 </listitem>
1409 </varlistentry>
1410
1411 <varlistentry>
1412 <term><option>-fwarn-implicit-prelude</option>:</term>
1413 <listitem>
1414 <indexterm><primary><option>-fwarn-implicit-prelude</option></primary></indexterm>
1415 <indexterm><primary>implicit prelude, warning</primary></indexterm>
1416 <para>Have the compiler warn if the Prelude is implicitly
1417 imported. This happens unless either the Prelude module is
1418 explicitly imported with an <literal>import ... Prelude ...</literal>
1419 line, or this implicit import is disabled (either by
1420 <option>-XNoImplicitPrelude</option> or a
1421 <literal>LANGUAGE NoImplicitPrelude</literal> pragma).</para>
1422
1423 <para>Note that no warning is given for syntax that implicitly
1424 refers to the Prelude, even if <option>-XNoImplicitPrelude</option>
1425 would change whether it refers to the Prelude.
1426 For example, no warning is given when
1427 <literal>368</literal> means
1428 <literal>Prelude.fromInteger (368::Prelude.Integer)</literal>
1429 (where <literal>Prelude</literal> refers to the actual Prelude module,
1430 regardless of the imports of the module being compiled).</para>
1431
1432 <para>This warning is off by default.</para>
1433 </listitem>
1434 </varlistentry>
1435
1436 <varlistentry>
1437 <term><option>-fwarn-incomplete-patterns</option>,
1438 <option>-fwarn-incomplete-uni-patterns</option>:
1439 </term>
1440 <listitem>
1441 <indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
1442 <indexterm><primary><option>-fwarn-incomplete-uni-patterns</option></primary></indexterm>
1443 <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1444 <indexterm><primary>patterns, incomplete</primary></indexterm>
1445
1446 <para>The option <option>-fwarn-incomplete-patterns</option> warns
1447 about places where
1448 a pattern-match might fail at runtime.
1449 The function
1450 <function>g</function> below will fail when applied to
1451 non-empty lists, so the compiler will emit a warning about
1452 this when <option>-fwarn-incomplete-patterns</option> is
1453 enabled.
1454
1455 <programlisting>
1456 g [] = 2
1457 </programlisting>
1458
1459 This option isn't enabled by default because it can be
1460 a bit noisy, and it doesn't always indicate a bug in the
1461 program. However, it's generally considered good practice
1462 to cover all the cases in your functions, and it is switched
1463 on by <option>-W</option>.</para>
1464
1465 <para>The flag <option>-fwarn-incomplete-uni-patterns</option> is
1466 similar, except that it
1467 applies only to lambda-expressions and pattern bindings, constructs
1468 that only allow a single pattern:
1469
1470 <programlisting>
1471 h = \[] -> 2
1472 Just k = f y
1473 </programlisting>
1474
1475 </para>
1476 </listitem>
1477 </varlistentry>
1478
1479 <varlistentry>
1480 <term><option>-fwarn-incomplete-record-updates</option>:</term>
1481 <listitem>
1482 <indexterm><primary><option>-fwarn-incomplete-record-updates</option></primary></indexterm>
1483 <indexterm><primary>incomplete record updates, warning</primary></indexterm>
1484 <indexterm><primary>record updates, incomplete</primary></indexterm>
1485
1486 <para>The function
1487 <function>f</function> below will fail when applied to
1488 <literal>Bar</literal>, so the compiler will emit a warning about
1489 this when <option>-fwarn-incomplete-record-updates</option> is
1490 enabled.</para>
1491
1492 <programlisting>
1493 data Foo = Foo { x :: Int }
1494 | Bar
1495
1496 f :: Foo -> Foo
1497 f foo = foo { x = 6 }
1498 </programlisting>
1499
1500 <para>This option isn't enabled by default because it can be
1501 very noisy, and it often doesn't indicate a bug in the
1502 program.</para>
1503 </listitem>
1504 </varlistentry>
1505
1506 <varlistentry>
1507 <term>
1508 <option>-fwarn-missing-fields</option>:
1509 <indexterm><primary><option>-fwarn-missing-fields</option></primary></indexterm>
1510 <indexterm><primary>missing fields, warning</primary></indexterm>
1511 <indexterm><primary>fields, missing</primary></indexterm>
1512 </term>
1513 <listitem>
1514
1515 <para>This option is on by default, and warns you whenever
1516 the construction of a labelled field constructor isn't
1517 complete, missing initializers for one or more fields. While
1518 not an error (the missing fields are initialised with
1519 bottoms), it is often an indication of a programmer error.</para>
1520 </listitem>
1521 </varlistentry>
1522
1523 <varlistentry>
1524 <term>
1525 <option>-fwarn-missing-import-lists</option>:
1526 <indexterm><primary><option>-fwarn-import-lists</option></primary></indexterm>
1527 <indexterm><primary>missing import lists, warning</primary></indexterm>
1528 <indexterm><primary>import lists, missing</primary></indexterm>
1529 </term>
1530 <listitem>
1531
1532 <para>This flag warns if you use an unqualified
1533 <literal>import</literal> declaration
1534 that does not explicitly list the entities brought into scope. For
1535 example
1536 </para>
1537
1538 <programlisting>
1539 module M where
1540 import X( f )
1541 import Y
1542 import qualified Z
1543 p x = f x x
1544 </programlisting>
1545
1546 <para>
1547 The <option>-fwarn-import-lists</option> flag will warn about the import
1548 of <literal>Y</literal> but not <literal>X</literal>
1549 If module <literal>Y</literal> is later changed to export (say) <literal>f</literal>,
1550 then the reference to <literal>f</literal> in <literal>M</literal> will become
1551 ambiguous. No warning is produced for the import of <literal>Z</literal>
1552 because extending <literal>Z</literal>'s exports would be unlikely to produce
1553 ambiguity in <literal>M</literal>.
1554 </para>
1555 </listitem>
1556 </varlistentry>
1557
1558 <varlistentry>
1559 <term><option>-fwarn-missing-methods</option>:</term>
1560 <listitem>
1561 <indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
1562 <indexterm><primary>missing methods, warning</primary></indexterm>
1563 <indexterm><primary>methods, missing</primary></indexterm>
1564
1565 <para>This option is on by default, and warns you whenever
1566 an instance declaration is missing one or more methods, and
1567 the corresponding class declaration has no default
1568 declaration for them.</para>
1569 <para>The warning is suppressed if the method name
1570 begins with an underscore. Here's an example where this is useful:
1571 <programlisting>
1572 class C a where
1573 _simpleFn :: a -> String
1574 complexFn :: a -> a -> String
1575 complexFn x y = ... _simpleFn ...
1576 </programlisting>
1577 The idea is that: (a) users of the class will only call <literal>complexFn</literal>;
1578 never <literal>_simpleFn</literal>; and (b)
1579 instance declarations can define either <literal>complexFn</literal> or <literal>_simpleFn</literal>.
1580 </para>
1581 <para>The MINIMAL pragma can be used to change which combination of methods will be required for instances of a particular class. See <xref linkend="minimal-pragma"/>.</para>
1582 </listitem>
1583 </varlistentry>
1584
1585 <varlistentry>
1586 <term><option>-fwarn-missing-signatures</option>:</term>
1587 <listitem>
1588 <indexterm><primary><option>-fwarn-missing-signatures</option></primary></indexterm>
1589 <indexterm><primary>type signatures, missing</primary></indexterm>
1590
1591 <para>If you would like GHC to check that every top-level
1592 function/value has a type signature, use the
1593 <option>-fwarn-missing-signatures</option> option. As part of
1594 the warning GHC also reports the inferred type. The
1595 option is off by default.</para>
1596 </listitem>
1597 </varlistentry>
1598
1599 <varlistentry>
1600 <term><option>-fwarn-missing-local-sigs</option>:</term>
1601 <listitem>
1602 <indexterm><primary><option>-fwarn-missing-local-sigs</option></primary></indexterm>
1603 <indexterm><primary>type signatures, missing</primary></indexterm>
1604
1605 <para>If you use the
1606 <option>-fwarn-missing-local-sigs</option> flag GHC will warn
1607 you about any polymorphic local bindings. As part of
1608 the warning GHC also reports the inferred type. The
1609 option is off by default.</para>
1610 </listitem>
1611 </varlistentry>
1612
1613 <varlistentry>
1614 <term><option>-fwarn-name-shadowing</option>:</term>
1615 <listitem>
1616 <indexterm><primary><option>-fwarn-name-shadowing</option></primary></indexterm>
1617 <indexterm><primary>shadowing, warning</primary></indexterm>
1618
1619 <para>This option causes a warning to be emitted whenever an
1620 inner-scope value has the same name as an outer-scope value,
1621 i.e. the inner value shadows the outer one. This can catch
1622 typographical errors that turn into hard-to-find bugs, e.g.,
1623 in the inadvertent capture of what would be a recursive call in
1624 <literal>f = ... let f = id in ... f ...</literal>.</para>
1625 <para>The warning is suppressed for names beginning with an underscore. For example
1626 <programlisting>
1627 f x = do { _ignore &lt;- this; _ignore &lt;- that; return (the other) }
1628 </programlisting>
1629 </para>
1630 </listitem>
1631 </varlistentry>
1632
1633 <varlistentry>
1634 <term><option>-fwarn-orphans, -fwarn-auto-orphans</option>:</term>
1635 <listitem>
1636 <indexterm><primary><option>-fwarn-orphans</option></primary></indexterm>
1637 <indexterm><primary><option>-fwarn-auto-orphans</option></primary></indexterm>
1638 <indexterm><primary>orphan instances, warning</primary></indexterm>
1639 <indexterm><primary>orphan rules, warning</primary></indexterm>
1640
1641 <para>These flags cause a warning to be emitted whenever the
1642 module contains an "orphan" instance declaration or rewrite rule.
1643 An instance declaration is an orphan if it appears in a module in
1644 which neither the class nor the type being instanced are declared
1645 in the same module. A rule is an orphan if it is a rule for a
1646 function declared in another module. A module containing any
1647 orphans is called an orphan module.</para>
1648 <para>The trouble with orphans is that GHC must pro-actively read the interface
1649 files for all orphan modules, just in case their instances or rules
1650 play a role, whether or not the module's interface would otherwise
1651 be of any use. See <xref linkend="orphan-modules"/> for details.
1652 </para>
1653 <para>The flag <option>-fwarn-orphans</option> warns about user-written
1654 orphan rules or instances. The flag <option>-fwarn-auto-orphans</option>
1655 warns about automatically-generated orphan rules, notably as a result of
1656 specialising functions, for type classes (<literal>Specialise</literal>)
1657 or argument values (<literal>-fspec-constr</literal>).</para>
1658 </listitem>
1659 </varlistentry>
1660
1661 <varlistentry>
1662 <term>
1663 <option>-fwarn-overlapping-patterns</option>:
1664 <indexterm><primary><option>-fwarn-overlapping-patterns</option></primary></indexterm>
1665 <indexterm><primary>overlapping patterns, warning</primary></indexterm>
1666 <indexterm><primary>patterns, overlapping</primary></indexterm>
1667 </term>
1668 <listitem>
1669 <para>By default, the compiler will warn you if a set of
1670 patterns are overlapping, e.g.,</para>
1671
1672 <programlisting>
1673 f :: String -&#62; Int
1674 f [] = 0
1675 f (_:xs) = 1
1676 f "2" = 2
1677 </programlisting>
1678
1679 <para>where the last pattern match in <function>f</function>
1680 won't ever be reached, as the second pattern overlaps
1681 it. More often than not, redundant patterns is a programmer
1682 mistake/error, so this option is enabled by default.</para>
1683 </listitem>
1684 </varlistentry>
1685
1686 <varlistentry>
1687 <term><option>-fwarn-tabs</option>:</term>
1688 <listitem>
1689 <indexterm><primary><option>-fwarn-tabs</option></primary></indexterm>
1690 <indexterm><primary>tabs, warning</primary></indexterm>
1691 <para>Have the compiler warn if there are tabs in your source
1692 file.</para>
1693
1694 <para>This warning is off by default.</para>
1695 </listitem>
1696 </varlistentry>
1697
1698 <varlistentry>
1699 <term><option>-fwarn-type-defaults</option>:</term>
1700 <listitem>
1701 <indexterm><primary><option>-fwarn-type-defaults</option></primary></indexterm>
1702 <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1703 <para>Have the compiler warn/inform you where in your source
1704 the Haskell defaulting mechanism for numeric types kicks
1705 in. This is useful information when converting code from a
1706 context that assumed one default into one with another,
1707 e.g., the &lsquo;default default&rsquo; for Haskell 1.4 caused the
1708 otherwise unconstrained value <constant>1</constant> to be
1709 given the type <literal>Int</literal>, whereas Haskell 98
1710 and later
1711 defaults it to <literal>Integer</literal>. This may lead to
1712 differences in performance and behaviour, hence the
1713 usefulness of being non-silent about this.</para>
1714
1715 <para>This warning is off by default.</para>
1716 </listitem>
1717 </varlistentry>
1718
1719 <varlistentry>
1720 <term><option>-fwarn-monomorphism-restriction</option>:</term>
1721 <listitem>
1722 <indexterm><primary><option>-fwarn-monomorphism-restriction</option></primary></indexterm>
1723 <indexterm><primary>monomorphism restriction, warning</primary></indexterm>
1724 <para>Have the compiler warn/inform you where in your source
1725 the Haskell Monomorphism Restriction is applied. If applied silently
1726 the MR can give rise to unexpected behaviour, so it can be helpful
1727 to have an explicit warning that it is being applied.</para>
1728
1729 <para>This warning is off by default.</para>
1730 </listitem>
1731 </varlistentry>
1732
1733 <varlistentry>
1734 <term><option>-fwarn-unused-binds</option>:</term>
1735 <listitem>
1736 <indexterm><primary><option>-fwarn-unused-binds</option></primary></indexterm>
1737 <indexterm><primary>unused binds, warning</primary></indexterm>
1738 <indexterm><primary>binds, unused</primary></indexterm>
1739 <para>Report any function definitions (and local bindings)
1740 which are unused. More precisely:
1741
1742 <itemizedlist>
1743 <listitem><para>Warn if a binding brings into scope a variable that is not used,
1744 except if the variable's name starts with an underscore. The "starts-with-underscore"
1745 condition provides a way to selectively disable the warning.
1746 </para>
1747 <para>
1748 A variable is regarded as "used" if
1749 <itemizedlist>
1750 <listitem><para>It is exported, or</para></listitem>
1751 <listitem><para>It appears in the right hand side of a binding that binds at
1752 least one used variable that is used</para></listitem>
1753 </itemizedlist>
1754 For example
1755 <programlisting>
1756 module A (f) where
1757 f = let (p,q) = rhs1 in t p -- Warning about unused q
1758 t = rhs3 -- No warning: f is used, and hence so is t
1759 g = h x -- Warning: g unused
1760 h = rhs2 -- Warning: h is only used in the right-hand side of another unused binding
1761 _w = True -- No warning: _w starts with an underscore
1762 </programlisting>
1763 </para></listitem>
1764
1765 <listitem><para>
1766 Warn if a pattern binding binds no variables at all, unless it is a lone, possibly-banged, wild-card pattern.
1767 For example:
1768 <programlisting>
1769 Just _ = rhs3 -- Warning: unused pattern binding
1770 (_, _) = rhs4 -- Warning: unused pattern binding
1771 _ = rhs3 -- No warning: lone wild-card pattern
1772 !_ = rhs4 -- No warning: banged wild-card pattern; behaves like seq
1773 </programlisting>
1774 The motivation for allowing lone wild-card patterns is they
1775 are not very different from <literal>_v = rhs3</literal>,
1776 which elicits no warning; and they can be useful to add a type
1777 constraint, e.g. <literal>_ = x::Int</literal>. A lone
1778 banged wild-card pattern is is useful as an alternative
1779 (to <literal>seq</literal>) way to force evaluation.
1780 </para>
1781 </listitem>
1782 </itemizedlist>
1783 </para>
1784 </listitem>
1785 </varlistentry>
1786
1787 <varlistentry>
1788 <term><option>-fwarn-unused-imports</option>:</term>
1789 <listitem>
1790 <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1791 <indexterm><primary>unused imports, warning</primary></indexterm>
1792 <indexterm><primary>imports, unused</primary></indexterm>
1793
1794 <para>Report any modules that are explicitly imported but
1795 never used. However, the form <literal>import M()</literal> is
1796 never reported as an unused import, because it is a useful idiom
1797 for importing instance declarations, which are anonymous in Haskell.</para>
1798 </listitem>
1799 </varlistentry>
1800
1801 <varlistentry>
1802 <term><option>-fwarn-unused-matches</option>:</term>
1803 <listitem>
1804 <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1805 <indexterm><primary>unused matches, warning</primary></indexterm>
1806 <indexterm><primary>matches, unused</primary></indexterm>
1807
1808 <para>Report all unused variables which arise from pattern
1809 matches, including patterns consisting of a single variable.
1810 For instance <literal>f x y = []</literal> would report
1811 <varname>x</varname> and <varname>y</varname> as unused. The
1812 warning is suppressed if the variable name begins with an underscore, thus:
1813 <programlisting>
1814 f _x = True
1815 </programlisting>
1816 </para>
1817 </listitem>
1818 </varlistentry>
1819
1820 <varlistentry>
1821 <term><option>-fwarn-unused-do-bind</option>:</term>
1822 <listitem>
1823 <indexterm><primary><option>-fwarn-unused-do-bind</option></primary></indexterm>
1824 <indexterm><primary>unused do binding, warning</primary></indexterm>
1825 <indexterm><primary>do binding, unused</primary></indexterm>
1826
1827 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1828 that appear to silently throw information away.
1829 For instance <literal>do { mapM popInt xs ; return 10 }</literal> would report
1830 the first statement in the <literal>do</literal> block as suspicious,
1831 as it has the type <literal>StackM [Int]</literal> and not <literal>StackM ()</literal>, but that
1832 <literal>[Int]</literal> value is not bound to anything. The warning is suppressed by
1833 explicitly mentioning in the source code that your program is throwing something away:
1834 <programlisting>
1835 do { _ &lt;- mapM popInt xs ; return 10 }
1836 </programlisting>
1837 Of course, in this particular situation you can do even better:
1838 <programlisting>
1839 do { mapM_ popInt xs ; return 10 }
1840 </programlisting>
1841 </para>
1842 </listitem>
1843 </varlistentry>
1844
1845 <varlistentry>
1846 <term><option>-fwarn-context-quantification</option>:</term>
1847 <listitem>
1848 <indexterm><primary><option>-fwarn-context-quantification</option></primary></indexterm>
1849 <indexterm><primary>implicit context quantification, warning</primary></indexterm>
1850 <indexterm><primary>context, implicit quantification</primary></indexterm>
1851
1852 <para>Report if a variable is quantified only due to its presence
1853 in a context (see <xref linkend="universal-quantification"/>). For example,
1854 <programlisting>
1855 type T a = Monad m => a -> f a
1856 </programlisting>
1857 It is recommended to write this polymorphic type as
1858 <programlisting>
1859 type T a = forall m. Monad m => a -> f a
1860 </programlisting>
1861 instead.
1862 </para>
1863 </listitem>
1864 </varlistentry>
1865
1866 <varlistentry>
1867 <term><option>-fwarn-wrong-do-bind</option>:</term>
1868 <listitem>
1869 <indexterm><primary><option>-fwarn-wrong-do-bind</option></primary></indexterm>
1870 <indexterm><primary>apparently erroneous do binding, warning</primary></indexterm>
1871 <indexterm><primary>do binding, apparently erroneous</primary></indexterm>
1872
1873 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1874 that appear to lack a binding.
1875 For instance <literal>do { return (popInt 10) ; return 10 }</literal> would report
1876 the first statement in the <literal>do</literal> block as suspicious,
1877 as it has the type <literal>StackM (StackM Int)</literal> (which consists of two nested applications
1878 of the same monad constructor), but which is not then &quot;unpacked&quot; by binding the result.
1879 The warning is suppressed by explicitly mentioning in the source code that your program is throwing something away:
1880 <programlisting>
1881 do { _ &lt;- return (popInt 10) ; return 10 }
1882 </programlisting>
1883 For almost all sensible programs this will indicate a bug, and you probably intended to write:
1884 <programlisting>
1885 do { popInt 10 ; return 10 }
1886 </programlisting>
1887 </para>
1888 </listitem>
1889 </varlistentry>
1890
1891 <varlistentry>
1892 <term><option>-fwarn-inline-rule-shadowing</option>:</term>
1893 <listitem>
1894 <indexterm><primary><option>-fwarn-inline-rule-shadowing</option></primary></indexterm>
1895 <para>Warn if a rewrite RULE might fail to fire because the function might be
1896 inlined before the rule has a chance to fire. See <xref linkend="rules-inline"/>.
1897 </para>
1898 </listitem>
1899 </varlistentry>
1900
1901 </variablelist>
1902
1903 <para>If you're feeling really paranoid, the
1904 <option>-dcore-lint</option>
1905 option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1906 is a good choice. It turns on heavyweight intra-pass
1907 sanity-checking within GHC. (It checks GHC's sanity, not
1908 yours.)</para>
1909
1910 </sect1>
1911
1912 &packages;
1913
1914 <sect1 id="options-optimise">
1915 <title>Optimisation (code improvement)</title>
1916
1917 <indexterm><primary>optimisation</primary></indexterm>
1918 <indexterm><primary>improvement, code</primary></indexterm>
1919
1920 <para>The <option>-O*</option> options specify convenient
1921 &ldquo;packages&rdquo; of optimisation flags; the
1922 <option>-f*</option> options described later on specify
1923 <emphasis>individual</emphasis> optimisations to be turned on/off;
1924 the <option>-m*</option> options specify
1925 <emphasis>machine-specific</emphasis> optimisations to be turned
1926 on/off.</para>
1927
1928 <sect2 id="optimise-pkgs">
1929 <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
1930
1931 <para>There are <emphasis>many</emphasis> options that affect
1932 the quality of code produced by GHC. Most people only have a
1933 general goal, something like &ldquo;Compile quickly&rdquo; or
1934 &ldquo;Make my program run like greased lightning.&rdquo; The
1935 following &ldquo;packages&rdquo; of optimisations (or lack
1936 thereof) should suffice.</para>
1937
1938 <para>Note that higher optimisation levels cause more
1939 cross-module optimisation to be performed, which can have an
1940 impact on how much of your program needs to be recompiled when
1941 you change something. This is one reason to stick to
1942 no-optimisation when developing code.</para>
1943
1944 <variablelist>
1945
1946 <varlistentry>
1947 <term>
1948 No <option>-O*</option>-type option specified:
1949 <indexterm><primary>-O* not specified</primary></indexterm>
1950 </term>
1951 <listitem>
1952 <para>This is taken to mean: &ldquo;Please compile
1953 quickly; I'm not over-bothered about compiled-code
1954 quality.&rdquo; So, for example: <command>ghc -c
1955 Foo.hs</command></para>
1956 </listitem>
1957 </varlistentry>
1958
1959 <varlistentry>
1960 <term>
1961 <option>-O0</option>:
1962 <indexterm><primary><option>-O0</option></primary></indexterm>
1963 </term>
1964 <listitem>
1965 <para>Means &ldquo;turn off all optimisation&rdquo;,
1966 reverting to the same settings as if no
1967 <option>-O</option> options had been specified. Saying
1968 <option>-O0</option> can be useful if
1969 eg. <command>make</command> has inserted a
1970 <option>-O</option> on the command line already.</para>
1971 </listitem>
1972 </varlistentry>
1973
1974 <varlistentry>
1975 <term>
1976 <option>-O</option> or <option>-O1</option>:
1977 <indexterm><primary>-O option</primary></indexterm>
1978 <indexterm><primary>-O1 option</primary></indexterm>
1979 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
1980 </term>
1981 <listitem>
1982 <para>Means: &ldquo;Generate good-quality code without
1983 taking too long about it.&rdquo; Thus, for example:
1984 <command>ghc -c -O Main.lhs</command></para>
1985 </listitem>
1986 </varlistentry>
1987
1988 <varlistentry>
1989 <term>
1990 <option>-O2</option>:
1991 <indexterm><primary>-O2 option</primary></indexterm>
1992 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
1993 </term>
1994 <listitem>
1995 <para>Means: &ldquo;Apply every non-dangerous
1996 optimisation, even if it means significantly longer
1997 compile times.&rdquo;</para>
1998
1999 <para>The avoided &ldquo;dangerous&rdquo; optimisations
2000 are those that can make runtime or space
2001 <emphasis>worse</emphasis> if you're unlucky. They are
2002 normally turned on or off individually.</para>
2003
2004 <para>At the moment, <option>-O2</option> is
2005 <emphasis>unlikely</emphasis> to produce better code than
2006 <option>-O</option>.</para>
2007 </listitem>
2008 </varlistentry>
2009
2010 <varlistentry>
2011 <term>
2012 <option>-Odph</option>:
2013 <indexterm><primary>-Odph</primary></indexterm>
2014 <indexterm><primary>optimise</primary><secondary>DPH</secondary></indexterm>
2015 </term>
2016 <listitem>
2017 <para>Enables all <option>-O2</option> optimisation, sets
2018 <option>-fmax-simplifier-iterations=20</option>
2019 and <option>-fsimplifier-phases=3</option>. Designed for use with
2020 <link linkend="dph">Data Parallel Haskell (DPH)</link>.</para>
2021 </listitem>
2022 </varlistentry>
2023
2024 </variablelist>
2025
2026 <para>We don't use a <option>-O*</option> flag for day-to-day
2027 work. We use <option>-O</option> to get respectable speed;
2028 e.g., when we want to measure something. When we want to go for
2029 broke, we tend to use <option>-O2</option> (and we go for
2030 lots of coffee breaks).</para>
2031
2032 <para>The easiest way to see what <option>-O</option> (etc.)
2033 &ldquo;really mean&rdquo; is to run with <option>-v</option>,
2034 then stand back in amazement.</para>
2035 </sect2>
2036
2037 <sect2 id="options-f">
2038 <title><option>-f*</option>: platform-independent flags</title>
2039
2040 <indexterm><primary>-f* options (GHC)</primary></indexterm>
2041 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
2042
2043 <para>These flags turn on and off individual optimisations.
2044 Flags marked as <emphasis>Enabled by default</emphasis> are
2045 enabled by <option>-O</option>, and as such you shouldn't
2046 need to set any of them explicitly. A flag <option>-fwombat</option>
2047 can be negated by saying <option>-fno-wombat</option>.
2048 See <xref linkend="options-f-compact"/> for a compact list.
2049 </para>
2050
2051 <variablelist>
2052 <varlistentry>
2053 <term>
2054 <option>-fcase-merge</option>
2055 <indexterm><primary><option></option></primary></indexterm>
2056 </term>
2057 <listitem>
2058 <para><emphasis>On by default.</emphasis>
2059 Merge immediately-nested case expressions that scrutinse the same variable. Example
2060 <programlisting>
2061 case x of
2062 Red -> e1
2063 _ -> case x of
2064 Blue -> e2
2065 Green -> e3
2066 ==>
2067 case x of
2068 Red -> e1
2069 Blue -> e2
2070 Green -> e2
2071 </programlisting>
2072 </para>
2073 </listitem>
2074 </varlistentry>
2075
2076 <varlistentry>
2077 <term>
2078 <option>-fcall-arity</option>
2079 <indexterm><primary><option>-fcall-arity</option></primary></indexterm>
2080 </term>
2081 <listitem>
2082 <para><emphasis>On by default.</emphasis>.
2083 </para>
2084 </listitem>
2085 </varlistentry>
2086
2087 <varlistentry>
2088 <term>
2089 <option>-fcmm-elim-common-blocks</option>
2090 <indexterm><primary><option>-felim-common-blocks</option></primary></indexterm>
2091 </term>
2092 <listitem>
2093 <para><emphasis>On by default.</emphasis>. Enables the common block
2094 elimination optimisation in the code generator. This optimisation
2095 attempts to find identical Cmm blocks and eliminate the duplicates.
2096 </para>
2097 </listitem>
2098 </varlistentry>
2099
2100 <varlistentry>
2101 <term>
2102 <option>-fcmm-sink</option>
2103 <indexterm><primary><option>-fcmm-sink</option></primary></indexterm>
2104 </term>
2105 <listitem>
2106 <para><emphasis>On by default.</emphasis>. Enables the sinking pass
2107 in the code generator. This optimisation
2108 attempts to find identical Cmm blocks and eliminate the duplicates
2109 attempts to move variable bindings closer to their usage sites. It
2110 also inlines simple expressions like literals or registers.
2111 </para>
2112 </listitem>
2113 </varlistentry>
2114
2115 <varlistentry>
2116 <term>
2117 <option>-fcpr-off</option>
2118 <indexterm><primary><option>-fcpr-Off</option></primary></indexterm>
2119 </term>
2120 <listitem>
2121 <para>Switch off CPR analysis in the demand analyser.
2122 </para>
2123 </listitem>
2124 </varlistentry>
2125
2126 <varlistentry>
2127 <term>
2128 <option>-fcse</option>
2129 <indexterm><primary><option>-fcse</option></primary></indexterm>
2130 </term>
2131 <listitem>
2132 <para><emphasis>On by default.</emphasis>. Enables the common-sub-expression
2133 elimination optimisation.
2134 Switching this off can be useful if you have some <literal>unsafePerformIO</literal>
2135 expressions that you don't want commoned-up.</para>
2136 </listitem>
2137 </varlistentry>
2138
2139 <varlistentry>
2140 <term>
2141 <option>-fdicts-cheap</option>
2142 <indexterm><primary><option></option></primary></indexterm>
2143 </term>
2144 <listitem>
2145 <para>A very experimental flag that makes dictionary-valued
2146 expressions seem cheap to the optimiser.
2147 </para>
2148 </listitem>
2149 </varlistentry>
2150
2151 <varlistentry>
2152 <term>
2153 <option>-fdicts-strict</option>
2154 <indexterm><primary><option></option></primary></indexterm>
2155 </term>
2156 <listitem>
2157 <para>Make dictionaries strict.
2158 </para>
2159 </listitem>
2160 </varlistentry>
2161
2162 <varlistentry>
2163 <term>
2164 <option>-fdmd-tx-dict-sel</option>
2165 <indexterm><primary><option>-fdmd-tx-dict-sel</option></primary></indexterm>
2166 </term>
2167 <listitem>
2168 <para><emphasis>On by default for <option>-O0</option>, <option>-O</option>,
2169 <option>-O2</option>.</emphasis>
2170 </para>
2171 <para>Use a special demand transformer for dictionary selectors.
2172 </para>
2173 </listitem>
2174 </varlistentry>
2175
2176 <varlistentry>
2177 <term>
2178 <option>-fdo-eta-reduction</option>
2179 <indexterm><primary><option></option></primary></indexterm>
2180 </term>
2181 <listitem>
2182 <para><emphasis>On by default.</emphasis>
2183 Eta-reduce lambda expressions, if doing so gets rid of a whole
2184 group of lambdas.
2185 </para>
2186 </listitem>
2187 </varlistentry>
2188
2189 <varlistentry>
2190 <term>
2191 <option>-fdo-lambda-eta-expansion</option>
2192 <indexterm><primary><option></option></primary></indexterm>
2193 </term>
2194 <listitem>
2195 <para><emphasis>On by default.</emphasis>
2196 Eta-expand let-bindings to increase their arity.
2197 </para>
2198 </listitem>
2199 </varlistentry>
2200
2201 <varlistentry>
2202 <term>
2203 <option>-feager-blackholing</option>
2204 <indexterm><primary><option></option></primary></indexterm>
2205 </term>
2206 <listitem>
2207 <para>Usually GHC black-holes a thunk only when it switches
2208 threads. This flag makes it do so as soon as the thunk is
2209 entered. See <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/">
2210 Haskell on a shared-memory multiprocessor</ulink>.
2211 </para>
2212 </listitem>
2213 </varlistentry>
2214
2215 <varlistentry>
2216 <term>
2217 <option>-fexcess-precision</option>
2218 <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
2219 </term>
2220 <listitem>
2221 <para>When this option is given, intermediate floating
2222 point values can have a <emphasis>greater</emphasis>
2223 precision/range than the final type. Generally this is a
2224 good thing, but some programs may rely on the exact
2225 precision/range of
2226 <literal>Float</literal>/<literal>Double</literal> values
2227 and should not use this option for their compilation.</para>
2228
2229 <para>
2230 Note that the 32-bit x86 native code generator only
2231 supports excess-precision mode, so neither
2232 <option>-fexcess-precision</option> nor
2233 <option>-fno-excess-precision</option> has any effect.
2234 This is a known bug, see <xref linkend="bugs-ghc" />.
2235 </para>
2236 </listitem>
2237 </varlistentry>
2238
2239 <varlistentry>
2240 <term>
2241 <option>-fexpose-all-unfoldings</option>
2242 <indexterm><primary><option></option></primary></indexterm>
2243 </term>
2244 <listitem>
2245 <para>An experimental flag to expose all unfoldings, even for very
2246 large or recursive functions. This allows for all functions to be
2247 inlined while usually GHC would avoid inlining larger functions.
2248 </para>
2249 </listitem>
2250 </varlistentry>
2251
2252 <varlistentry>
2253 <term>
2254 <option>-ffloat-in</option>
2255 <indexterm><primary><option></option></primary></indexterm>
2256 </term>
2257 <listitem>
2258 <para><emphasis>On by default.</emphasis>
2259 Float let-bindings inwards, nearer their binding site. See
2260 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">
2261 Let-floating: moving bindings to give faster programs (ICFP'96)</ulink>.
2262 </para>
2263
2264 <para>This optimisation moves let bindings closer to their use
2265 site. The benefit here is that this may avoid unnecessary
2266 allocation if the branch the let is now on is never executed. It
2267 also enables other optimisation passes to work more effectively
2268 as they have more information locally.
2269 </para>
2270
2271 <para>This optimisation isn't always beneficial though (so GHC
2272 applies some heuristics to decide when to apply it). The details
2273 get complicated but a simple example is that it is often beneficial
2274 to move let bindings outwards so that multiple let bindings can be
2275 grouped into a larger single let binding, effectively batching
2276 their allocation and helping the garbage collector and allocator.
2277 </para>
2278 </listitem>
2279 </varlistentry>
2280
2281 <varlistentry>
2282 <term>
2283 <option>-ffull-laziness</option>
2284 <indexterm><primary><option>-ffull-laziness</option></primary></indexterm>
2285 </term>
2286 <listitem>
2287 <para><emphasis>On by default.</emphasis>
2288 Run the full laziness optimisation (also known as let-floating),
2289 which floats let-bindings outside enclosing lambdas, in the hope
2290 they will be thereby be computed less often. See
2291 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">Let-floating:
2292 moving bindings to give faster programs (ICFP'96)</ulink>.
2293 Full laziness increases sharing, which can lead to increased memory
2294 residency.
2295 </para>
2296
2297 <para>NOTE: GHC doesn't implement complete full-laziness.
2298 When optimisation in on, and <option>-fno-full-laziness</option>
2299 is not given, some transformations that increase sharing are
2300 performed, such as extracting repeated computations from a loop.
2301 These are the same transformations that a fully lazy
2302 implementation would do, the difference is that GHC doesn't
2303 consistently apply full-laziness, so don't rely on it.
2304 </para>
2305 </listitem>
2306 </varlistentry>
2307
2308 <varlistentry>
2309 <term>
2310 <option>-ffun-to-thunk</option>
2311 <indexterm><primary><option>-ffun-to-thunk</option></primary></indexterm>
2312 </term>
2313 <listitem>
2314 <para>Worker-wrapper removes unused arguments, but usually we do
2315 not remove them all, lest it turn a function closure into a thunk,
2316 thereby perhaps creating a space leak and/or disrupting inlining.
2317 This flag allows worker/wrapper to remove <emphasis>all</emphasis>
2318 value lambdas. Off by default.
2319 </para>
2320 </listitem>
2321 </varlistentry>
2322
2323 <varlistentry>
2324 <term>
2325 <option>-fignore-asserts</option>
2326 <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
2327 </term>
2328 <listitem>
2329 <para><emphasis>On by default.</emphasis>.
2330 Causes GHC to ignore uses of the function
2331 <literal>Exception.assert</literal> in source code (in
2332 other words, rewriting <literal>Exception.assert p
2333 e</literal> to <literal>e</literal> (see <xref
2334 linkend="assertions"/>).
2335 </para>
2336 </listitem>
2337 </varlistentry>
2338
2339 <varlistentry>
2340 <term>
2341 <option>-fignore-interface-pragmas</option>
2342 <indexterm><primary><option>-fignore-interface-pragmas</option></primary></indexterm>
2343 </term>
2344 <listitem>
2345 <para>Tells GHC to ignore all inessential information when reading interface files.
2346 That is, even if <filename>M.hi</filename> contains unfolding or strictness information
2347 for a function, GHC will ignore that information.</para>
2348 </listitem>
2349 </varlistentry>
2350
2351 <varlistentry>
2352 <term>
2353 <option>-flate-dmd-anal</option>
2354 <indexterm><primary><option>-flate-dmd-anal</option></primary></indexterm>
2355 </term>
2356 <listitem>
2357 <para>Run demand analysis
2358 again, at the end of the simplification pipeline. We found some opportunities
2359 for discovering strictness that were not visible earlier; and optimisations like
2360 <literal>-fspec-constr</literal> can create functions with unused arguments which
2361 are eliminated by late demand analysis. Improvements are modest, but so is the
2362 cost. See notes on the <ulink url="http://ghc.haskell.org/trac/ghc/wiki/LateDmd">Trac wiki page</ulink>.
2363 </para>
2364 </listitem>
2365 </varlistentry>
2366
2367 <varlistentry>
2368 <term>
2369 <option>-fliberate-case</option>
2370 <indexterm><primary><option>-fliberate-case</option></primary></indexterm>
2371 </term>
2372 <listitem>
2373 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2374 Turn on the liberate-case transformation. This unrolls recursive
2375 function once in its own RHS, to avoid repeated case analysis of
2376 free variables. It's a bit like the call-pattern specialiser
2377 (<option>-fspec-constr</option>) but for free variables rather than
2378 arguments.
2379 </para>
2380 </listitem>
2381 </varlistentry>
2382
2383 <varlistentry>
2384 <term>
2385 <option>-fliberate-case-threshold=<replaceable>n</replaceable></option>
2386 <indexterm><primary><option>-fliberate-case-threshold</option></primary></indexterm>
2387 </term>
2388 <listitem>
2389 <para>Set the size threshold for the liberate-case transformation. Default: 2000
2390 </para>
2391 </listitem>
2392 </varlistentry>
2393
2394 <varlistentry>
2395 <term>
2396 <option>-fllvm-pass-vectors-in-regs</option>
2397 <indexterm><primary><option>-fllvm-pass-vectors-in-regs</option></primary></indexterm>
2398 </term>
2399 <listitem>
2400 <para>On 32-bit machines it changes calling convention used with LLVM backend
2401 to pass 128-bit vectors in SIMD registers. At the moment this does not work
2402 because this is not supported by LLVM. We strongly suggest you don't use this flag.
2403 </para>
2404 </listitem>
2405 </varlistentry>
2406
2407 <varlistentry>
2408 <term>
2409 <option>-fllvm-tbaa</option>
2410 <indexterm><primary><option>-fllvm-tbaa</option></primary></indexterm>
2411 </term>
2412 <listitem>
2413 <para><emphasis>On by default for <option>-O0</option>, <option>-O</option>,
2414 <option>-O2</option>.</emphasis>
2415 </para>
2416 <para>Enables type-based alias analysis for LLVM backend.
2417 At the moment this has limited usefullness since we pass
2418 very little information to LLVM.
2419 </para>
2420 </listitem>
2421 </varlistentry>
2422
2423 <varlistentry>
2424 <term>
2425 <option>-floopification</option>
2426 <indexterm><primary><option>-floopification</option></primary></indexterm>
2427 </term>
2428 <listitem>
2429 <para><emphasis>On by default.</emphasis>
2430 </para>
2431 <para>When this optimisation is enabled the code generator will turn
2432 all self-recursive saturated tail calls into local jumps rather
2433 than function calls.
2434 </para>
2435 </listitem>
2436 </varlistentry>
2437
2438 <varlistentry>
2439 <term>
2440 <option>-fmax-inline-alloc-size=<replaceable>n</replaceable></option>
2441 <indexterm><primary><option>-fmax-inline-alloc-size</option></primary></indexterm>
2442 </term>
2443 <listitem>
2444 <para>Set the maximum size of inline array allocations to n bytes
2445 (default: 128). GHC will allocate non-pinned arrays of statically
2446 known size in the current nursery block if they're no bigger
2447 than n bytes, ignoring GC overheap. This value should be quite
2448 a bit smaller than the block size (typically: 4096).
2449 </para>
2450 </listitem>
2451 </varlistentry>
2452
2453 <varlistentry>
2454 <term>
2455 <option>-fmax-inline-memcpy-insn=<replaceable>n</replaceable></option>
2456 <indexterm><primary><option>-fmax-inline-memcpy-insn</option></primary></indexterm>
2457 </term>
2458 <listitem>
2459 <para>Inline memcpy calls if they would generate no more than n pseudo instructions (default: 32).
2460 </para>
2461 </listitem>
2462 </varlistentry>
2463
2464 <varlistentry>
2465 <term>
2466 <option>-fmax-inline-memset-insns=<replaceable>n</replaceable></option>
2467 <indexterm><primary><option>-fmax-inline-memset-insns</option></primary></indexterm>
2468 </term>
2469 <listitem>
2470 <para>Inline memset calls if they would generate no more than n pseudo instructions (default: 32).
2471 </para>
2472 </listitem>
2473 </varlistentry>
2474
2475 <varlistentry>
2476 <term>
2477 <option>-fmax-relevant-binds=<replaceable>n</replaceable></option>
2478 <indexterm><primary><option>-fmax-relevant-bindings</option></primary></indexterm>
2479 </term>
2480 <listitem>
2481 <para>The type checker sometimes displays a fragment of the type environment
2482 in error messages, but only up to some maximum number, set by this flag.
2483 The default is 6. Turning it off with <option>-fno-max-relevant-bindings</option>
2484 gives an unlimited number. Syntactically top-level bindings are also
2485 usually excluded (since they may be numerous), but
2486 <option>-fno-max-relevant-bindings</option> includes them too.
2487 </para>
2488 </listitem>
2489 </varlistentry>
2490
2491 <varlistentry>
2492 <term>
2493 <option>-fmax-simplifier-iterations=<replaceable>n</replaceable></option>
2494 <indexterm><primary><option>-fmax-simplifier-iterations</option></primary></indexterm>
2495 </term>
2496 <listitem>
2497 <para>Sets the maximal number of iterations for the simplifier. Defult: 4.
2498 </para>
2499 </listitem>
2500 </varlistentry>
2501
2502 <varlistentry>
2503 <term>
2504 <option>-fmax-worker-args=<replaceable>n</replaceable></option>
2505 <indexterm><primary><option>-fmax-worker-args</option></primary></indexterm>
2506 </term>
2507 <listitem>
2508 <para>If a worker has that many arguments, none will be unpacked anymore (default: 10)
2509 </para>
2510 </listitem>
2511 </varlistentry>
2512
2513 <varlistentry>
2514 <term>
2515 <option>-fno-opt-coercion</option>
2516 <indexterm><primary><option>-fno-opt-coercion</option></primary></indexterm>
2517 </term>
2518 <listitem>
2519 <para>Turn off the coercion optimiser.
2520 </para>
2521 </listitem>
2522 </varlistentry>
2523
2524 <varlistentry>
2525 <term>
2526 <option>-fno-pre-inlining</option>
2527 <indexterm><primary><option>-fno-pre-inlining</option></primary></indexterm>
2528 </term>
2529 <listitem>
2530 <para>Turn off pre-inlining.
2531 </para>
2532 </listitem>
2533 </varlistentry>
2534
2535 <varlistentry>
2536 <term>
2537 <option>-fno-state-hack</option>
2538 <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
2539 </term>
2540 <listitem>
2541 <para>Turn off the "state hack" whereby any lambda with a
2542 <literal>State#</literal> token as argument is considered to be
2543 single-entry, hence it is considered OK to inline things inside
2544 it. This can improve performance of IO and ST monad code, but it
2545 runs the risk of reducing sharing.
2546 </para>
2547 </listitem>
2548 </varlistentry>
2549
2550 <varlistentry>
2551 <term>
2552 <option>-fomit-interface-pragmas</option>
2553 <indexterm><primary><option>-fomit-interface-pragmas</option></primary></indexterm>
2554 </term>
2555 <listitem>
2556 <para>Tells GHC to omit all inessential information from the
2557 interface file generated for the module being compiled (say M).
2558 This means that a module importing M will see only the
2559 <emphasis>types</emphasis> of the functions that M exports, but
2560 not their unfoldings, strictness info, etc. Hence, for example,
2561 no function exported by M will be inlined into an importing module.
2562 The benefit is that modules that import M will need to be
2563 recompiled less often (only when M's exports change their type, not
2564 when they change their implementation).</para>
2565 </listitem>
2566 </varlistentry>
2567
2568 <varlistentry>
2569 <term>
2570 <option>-fomit-yields</option>
2571 <indexterm><primary><option>-fomit-yields</option></primary></indexterm>
2572 </term>
2573 <listitem>
2574 <para><emphasis>On by default.</emphasis> Tells GHC to omit
2575 heap checks when no allocation is being performed. While this improves
2576 binary sizes by about 5%, it also means that threads run in
2577 tight non-allocating loops will not get preempted in a timely
2578 fashion. If it is important to always be able to interrupt such
2579 threads, you should turn this optimization off. Consider also
2580 recompiling all libraries with this optimization turned off, if you
2581 need to guarantee interruptibility.
2582 </para>
2583 </listitem>
2584 </varlistentry>
2585
2586 <varlistentry>
2587 <term>
2588 <option>-fpedantic-bottoms</option>
2589 <indexterm><primary><option>-fpedantic-bottoms</option></primary></indexterm>
2590 </term>
2591 <listitem>
2592 <para>Make GHC be more precise about its treatment of bottom (but see also
2593 <option>-fno-state-hack</option>). In particular, stop GHC
2594 eta-expanding through a case expression, which is good for
2595 performance, but bad if you are using <literal>seq</literal> on
2596 partial applications.
2597 </para>
2598 </listitem>
2599 </varlistentry>
2600
2601 <varlistentry>
2602 <term>
2603 <option>-fregs-graph</option>
2604 <indexterm><primary><option>-fregs-graph</option></primary></indexterm>
2605 </term>
2606 <listitem>
2607 <para><emphasis>Off by default due to a performance regression bug.
2608 Only applies in combination with the native code generator.</emphasis>
2609 Use the graph colouring register allocator for register allocation
2610 in the native code generator. By default, GHC uses a simpler,
2611 faster linear register allocator. The downside being that the
2612 linear register allocator usually generates worse code.
2613 </para>
2614 </listitem>
2615 </varlistentry>
2616
2617 <varlistentry>
2618 <term>
2619 <option>-fregs-iterative</option>
2620 <indexterm><primary><option>-fregs-iterative</option></primary></indexterm>
2621 </term>
2622 <listitem>
2623 <para><emphasis>Off by default, only applies in combination with
2624 the native code generator.</emphasis>
2625 Use the iterative coalescing graph colouring register allocator for
2626 register allocation in the native code generator. This is the same
2627 register allocator as the <option>-fregs-graph</option> one but also
2628 enables iterative coalescing during register allocation.
2629 </para>
2630 </listitem>
2631 </varlistentry>
2632
2633 <varlistentry>
2634 <term>
2635 <option>-fsimplifier-phases=<replaceable>n</replaceable></option>
2636 <indexterm><primary><option>-fsimplifier-phases</option></primary></indexterm>
2637 </term>
2638 <listitem>
2639 <para>Set the number of phases for the simplifier (default 2). Ignored with -O0.
2640 </para>
2641 </listitem>
2642 </varlistentry>
2643
2644 <varlistentry>
2645 <term>
2646 <option>-fsimpl-tick-factor=<replaceable>n</replaceable></option>
2647 <indexterm><primary><option>-fsimpl-tick-factor</option></primary></indexterm>
2648 </term>
2649 <listitem>
2650 <para>GHC's optimiser can diverge if you write rewrite rules (
2651 <xref linkend="rewrite-rules"/>) that don't terminate, or (less
2652 satisfactorily) if you code up recursion through data types
2653 (<xref linkend="bugs-ghc"/>). To avoid making the compiler fall
2654 into an infinite loop, the optimiser carries a "tick count" and
2655 stops inlining and applying rewrite rules when this count is
2656 exceeded. The limit is set as a multiple of the program size, so
2657 bigger programs get more ticks. The
2658 <option>-fsimpl-tick-factor</option> flag lets you change the
2659 multiplier. The default is 100; numbers larger than 100 give more
2660 ticks, and numbers smaller than 100 give fewer.
2661 </para>
2662
2663 <para>If the tick-count expires, GHC summarises what simplifier
2664 steps it has done; you can use
2665 <option>-fddump-simpl-stats</option> to generate a much more
2666 detailed list. Usually that identifies the loop quite
2667 accurately, because some numbers are very large.
2668 </para>
2669 </listitem>
2670 </varlistentry>
2671
2672 <varlistentry>
2673 <term>
2674 <option>-fspec-constr</option>
2675 <indexterm><primary><option>-fspec-constr</option></primary></indexterm>
2676 </term>
2677 <listitem>
2678 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2679 Turn on call-pattern specialisation; see
2680 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/spec-constr/index.htm">
2681 Call-pattern specialisation for Haskell programs</ulink>.
2682 </para>
2683
2684 <para>This optimisation specializes recursive functions according to
2685 their argument "shapes". This is best explained by example so
2686 consider:
2687 <programlisting>
2688 last :: [a] -> a
2689 last [] = error "last"
2690 last (x : []) = x
2691 last (x : xs) = last xs
2692 </programlisting>
2693 In this code, once we pass the initial check for an empty list we
2694 know that in the recursive case this pattern match is redundant. As
2695 such <option>-fspec-constr</option> will transform the above code
2696 to:
2697 <programlisting>
2698 last :: [a] -> a
2699 last [] = error "last"
2700 last (x : xs) = last' x xs
2701 where
2702 last' x [] = x
2703 last' x (y : ys) = last' y ys
2704 </programlisting>
2705 </para>
2706
2707 <para>As well avoid unnecessary pattern matching it also helps avoid
2708 unnecessary allocation. This applies when a argument is strict in
2709 the recursive call to itself but not on the initial entry. As
2710 strict recursive branch of the function is created similar to the
2711 above example.
2712 </para>
2713
2714 <para>It is also possible for library writers to instruct
2715 GHC to perform call-pattern specialisation extremely
2716 aggressively. This is necessary for some highly optimized
2717 libraries, where we may want to specialize regardless of
2718 the number of specialisations, or the size of the code. As
2719 an example, consider a simplified use-case from the
2720 <literal>vector</literal> library:</para>
2721 <programlisting>
2722 import GHC.Types (SPEC(..))
2723
2724 foldl :: (a -> b -> a) -> a -> Stream b -> a
2725 {-# INLINE foldl #-}
2726 foldl f z (Stream step s _) = foldl_loop SPEC z s
2727 where
2728 foldl_loop !sPEC z s = case step s of
2729 Yield x s' -> foldl_loop sPEC (f z x) s'
2730 Skip -> foldl_loop sPEC z s'
2731 Done -> z
2732 </programlisting>
2733
2734 <para>Here, after GHC inlines the body of
2735 <literal>foldl</literal> to a call site, it will perform
2736 call-pattern specialization very aggressively on
2737 <literal>foldl_loop</literal> due to the use of
2738 <literal>SPEC</literal> in the argument of the loop
2739 body. <literal>SPEC</literal> from
2740 <literal>GHC.Types</literal> is specifically recognized by
2741 the compiler.</para>
2742
2743 <para>(NB: it is extremely important you use
2744 <literal>seq</literal> or a bang pattern on the
2745 <literal>SPEC</literal> argument!)</para>
2746
2747 <para>In particular, after inlining this will
2748 expose <literal>f</literal> to the loop body directly,
2749 allowing heavy specialisation over the recursive
2750 cases.</para>
2751 </listitem>
2752 </varlistentry>
2753
2754 <varlistentry>
2755 <term>
2756 <option>-fspec-constr-count=<replaceable>n</replaceable></option>
2757 <indexterm><primary><option>-fspec-constr-count</option></primary></indexterm>
2758 </term>
2759 <listitem>
2760 <para>Set the maximum number of specialisations
2761 that will be created for any one function by the SpecConstr
2762 transformation (default: 3).
2763 </para>
2764 </listitem>
2765 </varlistentry>
2766
2767 <varlistentry>
2768 <term>
2769 <option>-fspec-constr-threshold=<replaceable>n</replaceable></option>
2770 <indexterm><primary><option>-fspec-constr-threshold</option></primary></indexterm>
2771 </term>
2772 <listitem>
2773 <para>Set the size threshold for the SpecConstr transformation (default: 2000).
2774 </para>
2775 </listitem>
2776 </varlistentry>
2777
2778 <varlistentry>
2779 <term>
2780 <option>-fspecialise</option>
2781 <indexterm><primary><option>-fspecialise</option></primary></indexterm>
2782 </term>
2783 <listitem>
2784 <para><emphasis>On by default.</emphasis>
2785 Specialise each type-class-overloaded function defined in this
2786 module for the types at which it is called in this module. Also
2787 specialise imported functions that have an INLINABLE pragma
2788 (<xref linkend="inlinable-pragma"/>) for the types at which they
2789 are called in this module.
2790 </para>
2791 </listitem>
2792 </varlistentry>
2793
2794 <varlistentry>
2795 <term>
2796 <option>-fstatic-argument-transformation</option>
2797 <indexterm><primary><option>-fstatic-argument-transformation</option></primary></indexterm>
2798 </term>
2799 <listitem>
2800 <para>Turn on the static argument transformation, which turns a
2801 recursive function into a non-recursive one with a local
2802 recursive loop. See Chapter 7 of
2803 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/santos-thesis.ps.gz">
2804 Andre Santos's PhD thesis</ulink>
2805 </para>
2806 </listitem>
2807 </varlistentry>
2808
2809 <varlistentry>
2810 <term>
2811 <option>-fstrictness</option>
2812 <indexterm><primary><option></option></primary></indexterm>
2813 </term>
2814 <listitem>
2815 <para> <emphasis>On by default.</emphasis>.
2816 Switch on the strictness analyser. There is a very old paper about GHC's
2817 strictness analyser, <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/simple-strictnes-analyser.ps.gz">
2818 Measuring the effectiveness of a simple strictness analyser</ulink>,
2819 but the current one is quite a bit different.
2820 </para>
2821
2822 <para>The strictness analyser figures out when arguments and
2823 variables in a function can be treated 'strictly' (that is they
2824 are always evaluated in the function at some point). This allow
2825 GHC to apply certain optimisations such as unboxing that
2826 otherwise don't apply as they change the semantics of the program
2827 when applied to lazy arguments.
2828 </para>
2829 </listitem>
2830 </varlistentry>
2831
2832 <varlistentry>
2833 <term>
2834 <option>-fstrictness-before=<replaceable>n</replaceable></option>
2835 <indexterm><primary><option>-fstrictness-before</option></primary></indexterm>
2836 </term>
2837 <listitem>
2838 <para>Run an additional strictness analysis before simplifier phase n.
2839 </para>
2840 </listitem>
2841 </varlistentry>
2842
2843 <varlistentry>
2844 <term>
2845 <option>-funbox-small-strict-fields</option>:
2846 <indexterm><primary><option>-funbox-small-strict-fields</option></primary></indexterm>
2847 <indexterm><primary>strict constructor fields</primary></indexterm>
2848 <indexterm><primary>constructor fields, strict</primary></indexterm>
2849 </term>
2850 <listitem>
2851 <para><emphasis>On by default.</emphasis>. This option
2852 causes all constructor fields which are marked strict
2853 (i.e. &ldquo;!&rdquo;) and which representation is smaller
2854 or equal to the size of a pointer to be unpacked, if
2855 possible. It is equivalent to adding an
2856 <literal>UNPACK</literal> pragma (see <xref
2857 linkend="unpack-pragma"/>) to every strict constructor
2858 field that fulfils the size restriction.
2859 </para>
2860
2861 <para>For example, the constructor fields in the following
2862 data types
2863 <programlisting>
2864 data A = A !Int
2865 data B = B !A
2866 newtype C = C B
2867 data D = D !C
2868 </programlisting>
2869 would all be represented by a single
2870 <literal>Int#</literal> (see <xref linkend="primitives"/>)
2871 value with
2872 <option>-funbox-small-strict-fields</option> enabled.
2873 </para>
2874
2875 <para>This option is less of a sledgehammer than
2876 <option>-funbox-strict-fields</option>: it should rarely make things
2877 worse. If you use <option>-funbox-small-strict-fields</option>
2878 to turn on unboxing by default you can disable it for certain
2879 constructor fields using the <literal>NOUNPACK</literal> pragma (see
2880 <xref linkend="nounpack-pragma"/>).</para>
2881
2882 <para>
2883 Note that for consistency <literal>Double</literal>,
2884 <literal>Word64</literal>, and <literal>Int64</literal> constructor
2885 fields are unpacked on 32-bit platforms, even though they are
2886 technically larger than a pointer on those platforms.
2887 </para>
2888 </listitem>
2889 </varlistentry>
2890
2891 <varlistentry>
2892 <term>
2893 <option>-funbox-strict-fields</option>:
2894 <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
2895 <indexterm><primary>strict constructor fields</primary></indexterm>
2896 <indexterm><primary>constructor fields, strict</primary></indexterm>
2897 </term>
2898 <listitem>
2899 <para>This option causes all constructor fields which are marked
2900 strict (i.e. &ldquo;!&rdquo;) to be unpacked if possible. It is
2901 equivalent to adding an <literal>UNPACK</literal> pragma to every
2902 strict constructor field (see <xref linkend="unpack-pragma"/>).
2903 </para>
2904
2905 <para>This option is a bit of a sledgehammer: it might sometimes
2906 make things worse. Selectively unboxing fields by using
2907 <literal>UNPACK</literal> pragmas might be better. An alternative
2908 is to use <option>-funbox-strict-fields</option> to turn on
2909 unboxing by default but disable it for certain constructor
2910 fields using the <literal>NOUNPACK</literal> pragma (see
2911 <xref linkend="nounpack-pragma"/>).</para>
2912 </listitem>
2913 </varlistentry>
2914
2915 <varlistentry>
2916 <term>
2917 <option>-funfolding-creation-threshold=<replaceable>n</replaceable></option>:
2918 <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
2919 <indexterm><primary>inlining, controlling</primary></indexterm>
2920 <indexterm><primary>unfolding, controlling</primary></indexterm>
2921 </term>
2922 <listitem>
2923 <para>(Default: 750) Governs the maximum size that GHC will allow a
2924 function unfolding to be. (An unfolding has a &ldquo;size&rdquo;
2925 that reflects the cost in terms of &ldquo;code bloat&rdquo; of
2926 expanding (aka inlining) that unfolding at a call site. A bigger
2927 function would be assigned a bigger cost.)
2928 </para>
2929
2930 <para>Consequences: (a) nothing larger than this will be inlined
2931 (unless it has an INLINE pragma); (b) nothing larger than this
2932 will be spewed into an interface file.
2933 </para>
2934
2935 <para>Increasing this figure is more likely to result in longer
2936 compile times than faster code. The
2937 <option>-funfolding-use-threshold</option> is more useful.
2938 </para>
2939 </listitem>
2940 </varlistentry>
2941
2942 <varlistentry>
2943 <term>
2944 <option>-funfolding-dict-discount=<replaceable>n</replaceable></option>:
2945 <indexterm><primary><option>-funfolding-dict-discount</option></primary></indexterm>
2946 <indexterm><primary>inlining, controlling</primary></indexterm>
2947 <indexterm><primary>unfolding, controlling</primary></indexterm>
2948 </term>
2949 <listitem>
2950 <para>Default: 30
2951 </para>
2952 </listitem>
2953 </varlistentry>
2954
2955 <varlistentry>
2956 <term>
2957 <option>-funfolding-fun-discount=<replaceable>n</replaceable></option>:
2958 <indexterm><primary><option>-funfolding-fun-discount</option></primary></indexterm>
2959 <indexterm><primary>inlining, controlling</primary></indexterm>
2960 <indexterm><primary>unfolding, controlling</primary></indexterm>
2961 </term>
2962 <listitem>
2963 <para>Default: 60
2964 </para>
2965 </listitem>
2966 </varlistentry>
2967
2968 <varlistentry>
2969 <term>
2970 <option>-funfolding-keeness-factor=<replaceable>n</replaceable></option>:
2971 <indexterm><primary><option>-funfolding-keeness-factor</option></primary></indexterm>
2972 <indexterm><primary>inlining, controlling</primary></indexterm>
2973 <indexterm><primary>unfolding, controlling</primary></indexterm>
2974 </term>
2975 <listitem>
2976 <para>Default: 1.5
2977 </para>
2978 </listitem>
2979 </varlistentry>
2980
2981 <varlistentry>
2982 <term>
2983 <option>-funfolding-use-threshold=<replaceable>n</replaceable></option>
2984 <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
2985 <indexterm><primary>inlining, controlling</primary></indexterm>
2986 <indexterm><primary>unfolding, controlling</primary></indexterm>
2987 </term>
2988 <listitem>
2989 <para>(Default: 60) This is the magic cut-off figure for unfolding
2990 (aka inlining): below this size, a function definition will be
2991 unfolded at the call-site, any bigger and it won't. The size
2992 computed for a function depends on two things: the actual size of
2993 the expression minus any discounts that apply depending on the
2994 context into which the expression is to be inlined.
2995 </para>
2996
2997 <para>The difference between this and
2998 <option>-funfolding-creation-threshold</option> is that this one
2999 determines if a function definition will be inlined <emphasis>at
3000 a call site</emphasis>. The other option determines if a
3001 function definition will be kept around at all for potential
3002 inlining.
3003 </para>
3004 </listitem>
3005 </varlistentry>
3006
3007 <varlistentry>
3008 <term>
3009 <option>-fvectorisation-avoidance</option>
3010 <indexterm><primary><option></option></primary></indexterm>
3011 </term>
3012 <listitem>
3013 <para>Part of <link linkend="dph">Data Parallel Haskell
3014 (DPH)</link>.</para>
3015
3016 <para><emphasis>On by default.</emphasis> Enable the
3017 <emphasis>vectorisation</emphasis> avoidance optimisation. This
3018 optimisation only works when used in combination with the
3019 <option>-fvectorise</option> transformation.</para>
3020
3021 <para>While vectorisation of code using DPH is often a big win, it
3022 can also produce worse results for some kinds of code. This
3023 optimisation modifies the vectorisation transformation to try to
3024 determine if a function would be better of unvectorised and if
3025 so, do just that.</para>
3026 </listitem>
3027 </varlistentry>
3028
3029 <varlistentry>
3030 <term>
3031 <option>-fvectorise</option>
3032 <indexterm><primary><option></option></primary></indexterm>
3033 </term>
3034 <listitem>
3035 <para>Part of <link linkend="dph">Data Parallel Haskell
3036 (DPH)</link>.</para>
3037
3038 <para><emphasis>Off by default.</emphasis> Enable the
3039 <emphasis>vectorisation</emphasis> optimisation transformation. This
3040 optimisation transforms the nested data parallelism code of programs
3041 using DPH into flat data parallelism. Flat data parallel programs
3042 should have better load balancing, enable SIMD parallelism and
3043 friendlier cache behaviour.</para>
3044 </listitem>
3045 </varlistentry>
3046
3047 </variablelist>
3048
3049 </sect2>
3050
3051 </sect1>
3052
3053 &code-gens;
3054
3055 &phases;
3056
3057 &shared_libs;
3058
3059 <sect1 id="using-concurrent">
3060 <title>Using Concurrent Haskell</title>
3061 <indexterm><primary>Concurrent Haskell</primary><secondary>using</secondary></indexterm>
3062
3063 <para>GHC supports Concurrent Haskell by default, without requiring a
3064 special option or libraries compiled in a certain way. To get access to
3065 the support libraries for Concurrent Haskell, just import
3066 <ulink
3067 url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>. More information on Concurrent Haskell is provided in the documentation for that module.</para>
3068
3069 <para>
3070 Optionally, the program may be linked with
3071 the <option>-threaded</option> option (see
3072 <xref linkend="options-linker" />. This provides two benefits:
3073
3074 <itemizedlist>
3075 <listitem>
3076 <para>It enables the <option>-N</option><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm> RTS option to be
3077 used, which allows threads to run in
3078 parallel<indexterm><primary>parallelism</primary></indexterm>
3079 on a
3080 multiprocessor<indexterm><primary>multiprocessor</primary></indexterm><indexterm><primary>SMP</primary></indexterm>
3081 or
3082 multicore<indexterm><primary>multicore</primary></indexterm>
3083 machine. See <xref linkend="using-smp" />.</para>
3084 </listitem>
3085 <listitem>
3086 <para>If a thread makes a foreign call (and the call is
3087 not marked <literal>unsafe</literal>), then other
3088 Haskell threads in the program will continue to run
3089 while the foreign call is in progress.
3090 Additionally, <literal>foreign export</literal>ed
3091 Haskell functions may be called from multiple OS
3092 threads simultaneously. See
3093 <xref linkend="ffi-threads" />.</para>
3094 </listitem>
3095 </itemizedlist>
3096 </para>
3097
3098 <para>The following RTS option(s) affect the behaviour of Concurrent
3099 Haskell programs:<indexterm><primary>RTS options, concurrent</primary></indexterm></para>
3100
3101 <variablelist>
3102 <varlistentry>
3103 <term><option>-C<replaceable>s</replaceable></option></term>
3104 <listitem>
3105 <para><indexterm><primary><option>-C<replaceable>s</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3106 Sets the context switch interval to <replaceable>s</replaceable>
3107 seconds. A context switch will occur at the next heap block
3108 allocation after the timer expires (a heap block allocation occurs
3109 every 4k of allocation). With <option>-C0</option> or
3110 <option>-C</option>, context switches will occur as often as
3111 possible (at every heap block allocation). By default, context
3112 switches occur every 20ms.</para>
3113 </listitem>
3114 </varlistentry>
3115 </variablelist>
3116 </sect1>
3117
3118 <sect1 id="using-smp">
3119 <title>Using SMP parallelism</title>
3120 <indexterm><primary>parallelism</primary>
3121 </indexterm>
3122 <indexterm><primary>SMP</primary>
3123 </indexterm>
3124
3125 <para>GHC supports running Haskell programs in parallel on an SMP
3126 (symmetric multiprocessor).</para>
3127
3128 <para>There's a fine distinction between
3129 <emphasis>concurrency</emphasis> and <emphasis>parallelism</emphasis>:
3130 parallelism is all about making your program run
3131 <emphasis>faster</emphasis> by making use of multiple processors
3132 simultaneously. Concurrency, on the other hand, is a means of
3133 abstraction: it is a convenient way to structure a program that must
3134 respond to multiple asynchronous events.</para>
3135
3136 <para>However, the two terms are certainly related. By making use of
3137 multiple CPUs it is possible to run concurrent threads in parallel,
3138 and this is exactly what GHC's SMP parallelism support does. But it
3139 is also possible to obtain performance improvements with parallelism
3140 on programs that do not use concurrency. This section describes how to
3141 use GHC to compile and run parallel programs, in <xref
3142 linkend="lang-parallel" /> we describe the language features that affect
3143 parallelism.</para>
3144
3145 <sect2 id="parallel-compile-options">
3146 <title>Compile-time options for SMP parallelism</title>
3147
3148 <para>In order to make use of multiple CPUs, your program must be
3149 linked with the <option>-threaded</option> option (see <xref
3150 linkend="options-linker" />). Additionally, the following
3151 compiler options affect parallelism:</para>
3152
3153 <variablelist>
3154 <varlistentry>
3155 <term><option>-feager-blackholing</option></term>
3156 <indexterm><primary><option>-feager-blackholing</option></primary></indexterm>
3157 <listitem>
3158 <para>
3159 Blackholing is the act of marking a thunk (lazy
3160 computuation) as being under evaluation. It is useful for
3161 three reasons: firstly it lets us detect certain kinds of
3162 infinite loop (the <literal>NonTermination</literal>
3163 exception), secondly it avoids certain kinds of space
3164 leak, and thirdly it avoids repeating a computation in a
3165 parallel program, because we can tell when a computation
3166 is already in progress.</para>
3167
3168 <para>
3169 The option <option>-feager-blackholing</option> causes
3170 each thunk to be blackholed as soon as evaluation begins.
3171 The default is "lazy blackholing", whereby thunks are only
3172 marked as being under evaluation when a thread is paused
3173 for some reason. Lazy blackholing is typically more
3174 efficient (by 1-2&percnt; or so), because most thunks don't
3175 need to be blackholed. However, eager blackholing can
3176 avoid more repeated computation in a parallel program, and
3177 this often turns out to be important for parallelism.
3178 </para>
3179
3180 <para>
3181 We recommend compiling any code that is intended to be run
3182 in parallel with the <option>-feager-blackholing</option>
3183 flag.
3184 </para>
3185 </listitem>
3186 </varlistentry>
3187 </variablelist>
3188 </sect2>
3189
3190 <sect2 id="parallel-options">
3191 <title>RTS options for SMP parallelism</title>
3192
3193 <para>There are two ways to run a program on multiple
3194 processors:
3195 call <literal>Control.Concurrent.setNumCapabilities</literal> from your
3196 program, or use the RTS <option>-N</option> option.</para>
3197
3198 <variablelist>
3199 <varlistentry>
3200 <term><option>-N<optional><replaceable>x</replaceable></optional></option></term>
3201 <listitem>
3202 <para><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3203 Use <replaceable>x</replaceable> simultaneous threads when
3204 running the program. Normally <replaceable>x</replaceable>
3205 should be chosen to match the number of CPU cores on the
3206 machine<footnote><para>Whether hyperthreading cores should be counted or not is an
3207 open question; please feel free to experiment and let us know what
3208 results you find.</para></footnote>. For example,
3209 on a dual-core machine we would probably use
3210 <literal>+RTS -N2 -RTS</literal>.</para>
3211
3212 <para>Omitting <replaceable>x</replaceable>,
3213 i.e. <literal>+RTS -N -RTS</literal>, lets the runtime
3214 choose the value of <replaceable>x</replaceable> itself
3215 based on how many processors are in your machine.</para>
3216
3217 <para>Be careful when using all the processors in your
3218 machine: if some of your processors are in use by other
3219 programs, this can actually harm performance rather than
3220 improve it.</para>
3221
3222 <para>Setting <option>-N</option> also has the effect of
3223 enabling the parallel garbage collector (see
3224 <xref linkend="rts-options-gc" />).</para>
3225
3226 <para>The current value of the <option>-N</option> option
3227 is available to the Haskell program
3228 via <literal>Control.Concurrent.getNumCapabilities</literal>, and
3229 it may be changed while the program is running by
3230 calling <literal>Control.Concurrent.setNumCapabilities</literal>.</para>
3231 </listitem>
3232 </varlistentry>
3233 </variablelist>
3234
3235 <para>The following options affect the way the runtime schedules
3236 threads on CPUs:</para>
3237
3238 <variablelist>
3239 <varlistentry>
3240 <term><option>-qa</option></term>
3241 <indexterm><primary><option>-qa</option></primary><secondary>RTS
3242 option</secondary></indexterm>
3243 <listitem>
3244 <para>Use the OS's affinity facilities to try to pin OS
3245 threads to CPU cores. This is an experimental feature,
3246 and may or may not be useful. Please let us know
3247 whether it helps for you!</para>
3248 </listitem>
3249 </varlistentry>
3250 <varlistentry>
3251 <term><option>-qm</option></term>
3252 <indexterm><primary><option>-qm</option></primary><secondary>RTS
3253 option</secondary></indexterm>
3254 <listitem>
3255 <para>Disable automatic migration for load balancing.
3256 Normally the runtime will automatically try to schedule
3257 threads across the available CPUs to make use of idle
3258 CPUs; this option disables that behaviour. Note that
3259 migration only applies to threads; sparks created
3260 by <literal>par</literal> are load-balanced separately
3261 by work-stealing.</para>
3262
3263 <para>
3264 This option is probably only of use for concurrent
3265 programs that explicitly schedule threads onto CPUs
3266 with <literal>Control.Concurrent.forkOn</literal>.
3267 </para>
3268 </listitem>
3269 </varlistentry>
3270 </variablelist>
3271 </sect2>
3272
3273 <sect2>
3274 <title>Hints for using SMP parallelism</title>
3275
3276 <para>Add the <literal>-s</literal> RTS option when
3277 running the program to see timing stats, which will help to tell you
3278 whether your program got faster by using more CPUs or not. If the user
3279 time is greater than
3280 the elapsed time, then the program used more than one CPU. You should
3281 also run the program without <literal>-N</literal> for
3282 comparison.</para>
3283
3284 <para>The output of <literal>+RTS -s</literal> tells you how
3285 many &ldquo;sparks&rdquo; were created and executed during the
3286 run of the program (see <xref linkend="rts-options-gc" />), which
3287 will give you an idea how well your <literal>par</literal>
3288 annotations are working.</para>
3289
3290 <para>GHC's parallelism support has improved in 6.12.1 as a
3291 result of much experimentation and tuning in the runtime
3292 system. We'd still be interested to hear how well it works
3293 for you, and we're also interested in collecting parallel
3294 programs to add to our benchmarking suite.</para>
3295 </sect2>
3296 </sect1>
3297
3298 <sect1 id="options-platform">
3299 <title>Platform-specific Flags</title>
3300
3301 <indexterm><primary>-m* options</primary></indexterm>
3302 <indexterm><primary>platform-specific options</primary></indexterm>
3303 <indexterm><primary>machine-specific options</primary></indexterm>
3304
3305 <para>Some flags only make sense for particular target
3306 platforms.</para>
3307
3308 <variablelist>
3309
3310 <varlistentry>
3311 <term><option>-msse2</option>:</term>
3312 <listitem>
3313 <para>
3314 (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
3315 instruction set to implement floating point operations when using
3316 the <link linkend="native-code-gen">native code generator</link>.
3317 This gives a substantial performance improvement for floating
3318 point, but the resulting compiled code
3319 will only run on processors that support SSE2 (Intel Pentium 4 and
3320 later, or AMD Athlon 64 and later). The
3321 <link linkend="llvm-code-gen">LLVM backend</link> will also use SSE2
3322 if your processor supports it but detects this automatically so no
3323 flag is required.
3324 </para>
3325 <para>
3326 SSE2 is unconditionally used on x86-64 platforms.
3327 </para>
3328 </listitem>
3329 </varlistentry>
3330
3331 <varlistentry>
3332 <term><option>-msse4.2</option>:</term>
3333 <listitem>
3334 <para>
3335 (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
3336 implement some floating point and bit operations when using the
3337 <link linkend="native-code-gen">native code generator</link>. The
3338 resulting compiled code will only run on processors that
3339 support SSE4.2 (Intel Core i7 and later). The
3340 <link linkend="llvm-code-gen">LLVM backend</link> will also use
3341 SSE4.2 if your processor supports it but detects this automatically
3342 so no flag is required.
3343 </para>
3344 </listitem>
3345 </varlistentry>
3346
3347 </variablelist>
3348
3349 </sect1>
3350
3351 &runtime;
3352 &debug;
3353 &flags;
3354
3355 </chapter>
3356
3357 <!-- Emacs stuff:
3358 ;;; Local Variables: ***
3359 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
3360 ;;; End: ***
3361 -->