# What is the exact difference between LLVM Flang and LFortran?

**URL:** <https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901>\
**Category:** Help\
**Created:** [March 24, 2021, 10:02am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901 "2021-03-24T10:02:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashutosh-pandey](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ashutosh-pandey/32/403_2.png) [@ashutosh-pandey](https://fortran-lang.discourse.group/u/ashutosh-pandey)\
**Post date:** [March 24, 2021, 10:02am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/1 "2021-03-24T10:02:08Z")

</div>

I am sorry if this is a naive question. Both are based on LLVM, so what are the use cases where a person would use one over the other and vice versa?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 24, 2021, 10:12am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/2 "2021-03-24T10:12:27Z")

</div>

As the LFortran page [says](https://lfortran.org/), it “can execute user’s code interactively to allow exploratory work”, unlike a typical compiler. It will be nice when it supports arrays, which are on the [roadmap](https://gitlab.com/lfortran/lfortran/-/issues/272).

---

<div class="post-metadata">

**Author:** ![ashutosh-pandey](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ashutosh-pandey/32/403_2.png) [@ashutosh-pandey](https://fortran-lang.discourse.group/u/ashutosh-pandey)\
**Post date:** [March 24, 2021, 10:14am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/3 "2021-03-24T10:14:13Z")

</div>

I see. So like Jupyter notebooks. Thanks!

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 24, 2021, 11:11am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/4 "2021-03-24T11:11:59Z")

</div>

LLVM is an infrastructure to build compilers:

> **[LLVM](https://en.wikipedia.org/wiki/LLVM)**
>
> The LLVM compiler infrastructure project is a set of compiler and toolchain technologies, which can be used to develop a front end for any programming language and a back end for any instruction set architecture. LLVM is designed around a language-independent intermediate representation (IR) that serves as a portable, high-level assembly language that can be optimized with a variety of transformations over multiple passes.
> LLVM is written in C++ and is designed for compile-time, link-time, run-t...

In the page [https://fortran-lang.org/compilers/](https://fortran-lang.org/compilers/), one can read that the new Intel `ifx` compiler and the ARM Fortran compiler also use LLVM.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [March 24, 2021, 12:25pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/5 "2021-03-24T12:25:19Z")

</div>

Both are using LLVM to generate the object code, but their front ends - parsing the code, generating an abstract syntax tree (AST) and converting that to LLVM’s intermediate representation (IR) - are being developed by completely separate teams. Thus they are likely to have different sets of bugs and missing features. The more options we have in compilers the more likely something will be able to compile your code, and then you can submit bug reports to the others saying “they can do it, why can’t you?”

Also, as mention above, LFortran has the goal of also having an interactive capability (a REPL) and being the Fortran kernel for Jupyter notebooks (which I think they’ve already achieved).

---

<div class="post-metadata">

**Author:** ![ashutosh-pandey](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ashutosh-pandey/32/403_2.png) [@ashutosh-pandey](https://fortran-lang.discourse.group/u/ashutosh-pandey)\
**Post date:** [March 24, 2021, 4:03pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/7 "2021-03-24T16:03:59Z")

</div>

I’ve recently started working in a team that works on F18 Flang (the new one). One of the resources they pointed me towards was this: [FOSDEM 2020 - Flang : The Fortran frontend of LLVM](https://archive.fosdem.org/2020/schedule/event/llvm_flang/) . They didn’t mention LFortran though, that’s why I asked.

Anyway, I appreciate the responses. For some reason it didn’t strike me that adding interactive functionality requires a whole different compiler, but then I remembered iPython and Jupyter.

---

<div class="post-metadata">

**Author:** ![billlong](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billlong](https://fortran-lang.discourse.group/u/billlong)\
**Post date:** [March 25, 2021, 12:25pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/8 "2021-03-25T12:25:22Z")

</div>

There are also several C compilers that use LLVM, but only one, CLANG, is an integral part of the LLVM project. As pmk pointed out, there is also an LLVM Fortran compiler under development, based on what started as F18. There are other Fortran compilers that use parts of LLVM, but only the F18-baased one is an official part of the LLVM project, and is now called FLANG. (I’ve heard that the previous “pretenders” that fancied themselves as “Flang” are now informally referred to as “Flung”. )

---

<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:** [March 25, 2021, 1:25pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/9 "2021-03-25T13:25:57Z")

</div>

🙂 That is a very interesting take.

The classic Flang frontend ([GitHub - flang-compiler/flang: Flang is a Fortran language front-end designed for integration with LLVM.](https://github.com/flang-compiler/flang)) continues to be developed and is the base of at least the Arm, AMD and Huawei Fortran compilers. The plan is to eventually move to using the F18 (or llvm/flang) frontend when it becomes ready. Classic Flang frontend community members are also part of the F18 community and make contributions to F18.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 25, 2021, 2:03pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/10 "2021-03-25T14:03:54Z")

</div>

I’m so old that I remember when g95 was a better compiler than gfortran 🙂 . Which open source effort is in the lead can change over time.

I cannot use Flang because it has trouble with I/O of derived types. Maybe someone in the GSoC can work on this, after checking with their mentor. This program causes an ICE for

```auto
clang version 7.0.1 
Target: x86_64-pc-linux-gnu
Thread model: posix

```

```
program main
! 12/19/2020 07:55 PM -- crash the flang compiler, reported at https://github.com/flang-compiler/flang/issues/961
implicit none
type :: foo
   integer :: bar
end type foo
type(foo) :: x(1)
x%bar = 0
print*,x([1])
end program main
```

---

<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:** [March 25, 2021, 2:26pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/11 "2021-03-25T14:26:41Z")

</div>

Apologies for the issue you are facing with Flang.

Is this part of an application that is commonly used?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [March 25, 2021, 2:39pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/12 "2021-03-25T14:39:26Z")

</div>

No, it is from a program for personal use. I am grateful for all open source efforts, and no apology is needed.

---

<div class="post-metadata">

**Author:** ![jr\_reuter](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jr_reuter/32/352_2.png) [@jr\_reuter](https://fortran-lang.discourse.group/u/jr_reuter)\
**Post date:** [March 30, 2021, 9:58pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/13 "2021-03-30T21:58:06Z")

</div>

That’s an interesting topic. At some time there was a flang project, probably the first one, which still needed a proper Fortran compiler at the end to actually produce assembler code, so it only transferred the Fortran code into a parse tree. Now, as I understand the new flang/f18 already compiles code into a binary program? And that comes automatically with LLVM?

---

<div class="post-metadata">

**Author:** ![h-vetinari](https://avatars.discourse-cdn.com/v4/letter/h/ebca7d/32.png) [@h-vetinari](https://fortran-lang.discourse.group/u/h-vetinari)\
**Post date:** [July 14, 2021, 8:52am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/15 "2021-07-14T08:52:32Z")

</div>

I had the same question, and it took me a fair amount of searching to find this discussion.

I’ve been following the development of flang for a while, and the opening question arises almost immediately when hearing that someone is developing an LLVM-based fortran compiler - might still be a bit until they have native code generation, but that’s flang’s main focus right now. Their current development tracker & integration branch is [GitHub - flang-compiler/f18-llvm-project at fir-dev](https://github.com/flang-compiler/f18-llvm-project/tree/fir-dev)

The [goals](https://lfortran.org/blog/2019/04/why-we-created-lfortran/) of lfortran sound awesome, and IMO are largely overlapping with what the flang-in-LLVM is doing (perhaps with the addition of the interactive REPL). It’s possible that the projects are not as compatible as it appears of course.

---

<div class="post-metadata">

**Author:** ![R\_cubed](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/r_cubed/32/263_2.png) [@R\_cubed](https://fortran-lang.discourse.group/u/R_cubed)\
**Post date:** [July 14, 2021, 6:52pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/16 "2021-07-14T18:52:37Z")

</div>

Can anyone elaborate on why LFortran and the FLang projects can’t collaborate on a common parser? It seems having one would assist in a number of downstream projects while minimizing redundant work.

At the very least, there should be a common test case reference set. I was able to find [one for F77 published by NIST](https://sourceryinstitute.github.io/RefactorF4Acc-test-docs/tests/NIST_F78_test_suite/fcvs21_f95/doc/index.html), but haven’t found any others for more recent versions of Fortran.

---

<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:** [July 15, 2021, 12:11am UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/17 "2021-07-15T00:11:40Z")

</div>

I somehow missed this thread. I am the original author of LFortran. It turns our LFortran and the new Flang started almost the same month a few years ago. For a while we did not know about each other, but even after I met many Flang developers in person, it is unclear how we can collaborate more tightly because the projects have different goals and priorities:

- LFortran is interactive, so it can parse more than the standard Fortran, it can also parse loose expressions and statements. Flang cannot (currently) do that as far as I know.

- One of the requirements for LFortran is a very fast (as fast as possible) compilation speed, which guides the internal data structures to be as efficient as possible.

- LFortran has multiple backends. That has been the design from the beginning to not be tied to LLVM. It can use it of course (it is the default backend currently), but we also have a C++ translation backend, direct x86 machine code generation backend (very fast compilation speed, but no optimizations) and we plan many other backends (Python wrappers, Julia translation, MLIR backend, etc.). LFortran is not tied to, nor part of LLVM for this reason.

- The previous point guides a design decision to transform AST (Abstract Semantic Tree) to ASR (Abstract Semantic Representation), which is a standalone representation, but it does not lose any Fortran semantics, everything is figured out and it is the starting point of our backends, makes it very easy to maintain a backend. ASR also allows to write transformations ASR-\>ASR, which we plan to use for implementing optimizations at this level, before lowering to LLVM or other tools (such as MLIR): [Optimizations list (#427) · Issues · lfortran / lfortran · GitLab](https://gitlab.com/lfortran/lfortran/-/issues/427)

- Our main focus is native code generation. You can follow our progress on compiling this proxy app (Fortran 95): [MVP: Roadmap to compile the SNAP project (#313) · Issues · lfortran / lfortran · GitLab](https://gitlab.com/lfortran/lfortran/-/issues/313)

- We also updated our front page with the latest status: [https://lfortran.org/](https://lfortran.org/)

- One of our goals is to build a large team of developers and contributors and make it easy for LFortran to be included in other libraries and tools — for that reason we want to make it as easy as possible to contribute to the compiler source code. LFortran currently fully compiles in about 20s on my laptop (it assumes LLVM is already built). We are thinking of (also) making it an fpm package once fpm can compile C++ code. That would make it very easy to install and develop for Fortran users.

The parser, while tedious to initially develop, is the least interesting part of a compiler. Both Flang and LFortran have now a Fortran 2018 parser. Maintaining it is not a big problem. The rest of the compilers are completely different as explained above, so the way I see it, the best way to collaborate are things like:

- Prototyping and designing new Fortran features for the standard committee

- Collaborating on the LLVM or MLIR backend that both compilers could use

- Collaborating / brainstorming how to implement some optimizations

- Collaborating on extending (if needed) the Fortran language to run well on GPUs and other platforms. Collaborating on how to compile well for those.

- Collaborating on creating and maintaining a Fortran standard conformance test suite

- Collaborating on benchmarks

There is nothing wrong with having more than one compiler. On the contrary! We could have just taken the GFortran parser and port it on top of LLVM. But neither Flang nor LFortran chose to do that for various technical and license reasons. Yet GFortran is the most mature and best open source Fortran compiler.

Speaking as a Fortran user, it is very exciting to be able to use multiple compilers that are independent of each other, because that ensures the Fortran code that I write is truly multiplatform — it is not dependent on any one platform or a compiler, and thus “lives on its own”. Finally, competition is extremely good for progress. We are now seeing some new innovative ideas. I believe this is not a zero sum game and all these efforts will benefit the wide Fortran community.

---

<div class="post-metadata">

**Author:** ![R\_cubed](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/r_cubed/32/263_2.png) [@R\_cubed](https://fortran-lang.discourse.group/u/R_cubed)\
**Post date:** [July 15, 2021, 2:39pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/18 "2021-07-15T14:39:49Z")

</div>

Thanks for that detailed reply. It is interesting 2 independent teams decided to write or release a Fortran compiler at roughly the same time.

Although I mirror the sources for a number of compiler projects, I have not studied LFortran source code in much depth. While it is likely a moot point now, I was thinking the goals of both teams could have been accomplished by looking at how Common Lisp compilers were implemented. They are simultaneously compiled (for efficient code generation) but also interactive. Julia uses many ideas developed in Common Lisp compilers.

I’m particularly fond of the POP-11 incremental compiler, that included Prolog, a pre-ANSI version of Common Lisp, as well as ML.

> **[Incremental compiler](https://en.wikipedia.org/wiki/Incremental_compiler)**
>
> An incremental compiler is a kind of incremental computation applied to the field of compilation. Quite naturally, whereas ordinary compilers make so called clean build, that is, (re)build all program modules, incremental compiler recompiles only those portions of a program that have been modified.
> In imperative programming and software development, an incremental compiler is one that when invoked, takes only the changes of a known set of source files and updates any corresponding output files (...

Can you provide a bit of guidance on the situation for validating the compiler/interpreter? Is the goal to have LFortran be an interpreter for Standard Fortran, or is it more Fortran inspired?

---

<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:** [July 15, 2021, 3:12pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/19 "2021-07-15T15:12:09Z")

</div>

Yes, LFortran works just like Julia does: if used interactively, it compiles a given cell to machine code via LLVM, loads into memory and executes it. It keeps the old cells’s variables alive in memory and allows you to refernce them.

> [@R\_cubed](#):
>
> Can you provide a bit of guidance on the situation for validating the compiler/interpreter? Is the goal to have LFortran be an interpreter for Standard Fortran, or is it more Fortran inspired?

The goal of LFortran is to be eventually Fortran 2018 conforming. When used interactively, Fortran is extended to allow expressions and statements at the global scope, but that’s it. We would like to compile any existing Fortran code, both free form and fixed form. When used in the ahead of time mode, it behaves just like any other Fortran compiler.

---

<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:** [August 1, 2021, 8:54pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/20 "2021-08-01T20:54:53Z")

</div>

Where are optimizations planned in the LFortran compiler? One possibility could be to share the MLIR based high-level Fortran IR (FIR). MLIR also has the OpenMP and OpenACC dialects which LFortran can use. If there are no plans for high-level IRs then the OpenMP IRBuilder project in LLVM can be used for OpenMP support in LFortran just like Flang does.

It would have been great to share the array descriptor and module format. But I think it is too late. I hope we can at least add compatibility layers to understand each other’s formats.

---

<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:** [August 1, 2021, 9:40pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/21 "2021-08-01T21:40:46Z")

</div>

> [@kiranchandramohan](#):
>
> Where are optimizations planned in the LFortran compiler?

The optimizations are planned at the LFortran’s ASR level (ASR-\>ASR transformations).

We plan to have an MLIR backend also, once it stabilizes. There are many disadvantages of MLIR, such as speed of compilation, but for optimizations it might work very well.

@kiranchandramohan if you are interested in collaboration, please let me know.

---

<div class="post-metadata">

**Author:** ![Ashok](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Ashok](https://fortran-lang.discourse.group/u/Ashok)\
**Post date:** [August 3, 2021, 12:22pm UTC](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901/22 "2021-08-03T12:22:18Z")

</div>

Sorry for this question, because I don’t know any thing about compilers, I am just trying to understand.  
If LFortran compiler is written in C++, I what way it will be faster than C++? I understand that compiler is a program that converts source code into binary. If that is written in C++, then it is another C++ program. Then Fortran becomes a higher level C++. How then it becomes another language ? Are there any specific rules of memory allocation in Fortran compiler ? Or what makes it a Fortran language ? Semantics ?  
If I write  
print\*, “hello world”  
If this is processed by a C++ program, then I have written a higher level C++ that’s all ! Or am I missing anything here ?

[Next page](https://fortran-lang.discourse.group/t/what-is-the-exact-difference-between-llvm-flang-and-lfortran/901.md?page=2)
