# What Ofast means for Fortran developers

**URL:** <https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741>\
**Category:** Uncategorized\
**Created:** [November 15, 2022, 11:36am UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741 "2022-11-15T11:36:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiranchandramohan](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kiranchandramohan](https://fortran-lang.discourse.group/u/kiranchandramohan)\
**Post date:** [November 15, 2022, 11:36am UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/1 "2022-11-15T11:36:29Z")

</div>

We are having a discussion in llvm discourse ([RFC: The meaning of -Ofast - Flang - LLVM Discussion Forums](https://discourse.llvm.org/t/rfc-the-meaning-of-ofast/66554)) about what `Ofast` means for `llvm/flang`. One way to model this would be to do the same as what Clang (the LLVM C/C++ compiler) does, i.e enable all optimisations at `O3` and `-ffast-math` where the latter allows transformations that might break strict IEEE conformance for floats and other transformations.

There are also some suggestions to model it as what Fortran developers would naturally expect from using this flag.

It will be great if we can get some input on this topic in the llvm discourse thread or if that is not possible then in this thread. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [November 15, 2022, 2:51pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/2 "2022-11-15T14:51:20Z")

</div>

Just a thought: you could try a small program like:

```auto
program ofast
    implicit none
    write(*,*) compiler_options()
end program ofast

```

and run that through various compilers with various options. The intrinsic function prints the actual compiler options that were used to build the program.

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [November 15, 2022, 3:29pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/3 "2022-11-15T15:29:15Z")

</div>

In general `-Ofast` should not be used because it sets flags to truncate subnormal numbers to 0 which can break linked programs that were relying on IEEE complaint behavior.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [November 15, 2022, 4:08pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/4 "2022-11-15T16:08:56Z")

</div>

The use of SSE/AVX/GPU hardware also breaks IEEE compliant behavior, so if a programmer is aware of this and still wants to take advantage of the increased performance, there should be some way to tell the compiler to do so.

So some extent, options like -Ofast depend on the underlying hardware. Yes, there may be some high-level transformations that are common to all hardware, but the real impact is more likely using some low-level hardware feature, such as a GPU or some vector hardware. Another possibility is the option to link different math libraries, one for speed where some documented compromises have been made regarding accuracy, and another for accuracy where documented compromises have been made regarding performance. A single option like -Ofast would be expected to make some overall selection of these various high- and low-level choices.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 15, 2022, 5:42pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/5 "2022-11-15T17:42:18Z")

</div>

I think there is no general agreement in the Fortran community on these optimization flags.

I personally recommend writing numerical codes in such a way so that you can use `-ffast-math`, but some others recommend not to use this flag.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [November 15, 2022, 5:48pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/6 "2022-11-15T17:48:48Z")

</div>

The problem with “-Ofast” is that it is suggesting there is no trade-off. There is always a trade-off.

---

<div class="post-metadata">

**Author:** ![oscardssmith](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@oscardssmith](https://fortran-lang.discourse.group/u/oscardssmith)\
**Post date:** [November 15, 2022, 5:51pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/7 "2022-11-15T17:51:21Z")

</div>

The difference between `-Ofast` and instructions like AVX is that using an AVX instruction won’t break other programs that aren’t using them. If you ever link any program using `-Ofast` to anything else, it will disable IEEE semantics for all of the programs even if they don’t use `-Ofast`.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [November 15, 2022, 5:54pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/8 "2022-11-15T17:54:40Z")

</div>

Yes, `-ffast-math` is problematic for a library. I had to disable it by default, because it messes up end applications. But for an end application that you control and want the best performance, I recommend using the flag.

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [November 15, 2022, 6:16pm UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/9 "2022-11-15T18:16:59Z")

</div>

In general, I expect `-Ofast` to enable at least `-O3` and `-ffast-math`, but depending on the compiler it might enable more than that. As far I know, there is no standard for what `-Ofast` does, so I guess the best way to implement `-Ofast` is the same as in Clang. At least in this case we will avoid the mess (same flag does one thing in Clang but another thing in Flang or LFortran.)

I have seen codes that do compile and run with the more usual `-O2` while I get segmentation faults with `-Ofast` - or vice-versa (usually this may happen when the code uses C interoperability.) I also know developers of quality software that won’t go with anything above `-O1` and would rather parallelize the code manually instead.  
personally, I prefer to avoid the generic `-Ofast` and give the compiler specific flags instead. As @certik already pointed out, `-ffast-math` is the most common one, but I also use `-funroll-loops`, `-ftree-vectorize` etc. It all depends on the code. Whenever I use libraries, especially the ones I didn’t wrote and can’t control myself, I would avoid those too.

---

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [November 16, 2022, 2:38am UTC](https://fortran-lang.discourse.group/t/what-ofast-means-for-fortran-developers/4741/10 "2022-11-16T02:38:57Z")

</div>

> [@Is -Ofast in gfortran or -fast flag in Intel Fortran safe to use?](https://fortran-lang.discourse.group/t/is-ofast-in-gfortran-or-fast-flag-in-intel-fortran-safe-to-use/2755):
>
> Dear all, I just wonder, has anyone use -Ofast in gfortran or -fast flag in Intel Fortran? I sometimes find that some results with -fast are a little different from -O3. Is -Ofast or -fast safe to use? Thank you very much in advance!
