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