# Flang-wasm compiler

**URL:** <https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589>\
**Category:** Uncategorized\
**Created:** [March 11, 2024, 1:07pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589 "2024-03-11T13:07:48Z")\
**Posts on this page:** 10\
**Page:** 1

<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 11, 2024, 1:07pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/1 "2024-03-11T13:07:48Z")

</div>

R for WebAssembly is an organization developing projects and packages related to running the statistical language R with WebAssembly. They have a 4-month-old project

> **[GitHub - r-wasm/flang-wasm: Tools to build a version of LLVM's flang-new...](https://github.com/r-wasm/flang-wasm)**
>
> Tools to build a version of LLVM's flang-new compiler that can be used to compile Fortran sources for WebAssembly. - r-wasm/flang-wasm

I know little about WebAssembly, but if there were a mature Fortran compiler for it, could users run Fortran programs in their browsers? A GitHub project with 143 stars but which has not been updated in 2 years is [GitHub - StarGate01/Full-Stack-Fortran: Fortran to WebAssembly](https://github.com/StarGate01/Full-Stack-Fortran)

---

<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 11, 2024, 1:34pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/2 "2024-03-11T13:34:17Z")

</div>

LFortran has also a WebAssembly (WASM) back-end: [https://lfortran.org/](https://lfortran.org/)

---

<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 11, 2024, 7:29pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/3 "2024-03-11T19:29:23Z")

</div>

> [@Beliavsky](#):
>
> could users run Fortran programs in their browsers?

> **[WebAssembly | MDN](https://developer.mozilla.org/en-US/docs/WebAssembly)**
>
> WebAssembly is a type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++, C# and Rust with a compilation...

---

<div class="post-metadata">

**Author:** ![Ubaid-Shaikh](https://avatars.discourse-cdn.com/v4/letter/u/43a26b/32.png) [@Ubaid-Shaikh](https://fortran-lang.discourse.group/u/Ubaid-Shaikh)\
**Post date:** [March 12, 2024, 12:27pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/4 "2024-03-12T12:27:34Z")

</div>

> [@Beliavsky](#):
>
> could users run Fortran programs in their browsers?

LFortran already does it here [https://dev.lfortran.org/](https://dev.lfortran.org/).

---

<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 12, 2024, 12:28pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/5 "2024-03-12T12:28:35Z")

</div>

[No installation required: how WebAssembly is changing scientific computing.](https://www.nature.com/articles/d41586-024-00725-1)  
Enabling code execution in the web browser, the multilanguage tool is powerful but complicated.  
By Jeffrey M. Perkel  
Nature, 11 March 2024

> …
> 
> Code written in any of a few dozen languages, including C, C++ or Rust, can be compiled into the WebAssembly (or Wasm) instruction format, allowing it to run in a software-based environment inside a browser. No external servers are required. All modern browsers support WebAssembly, so code that works on one computer should produce the same result on any other. Best of all, no installation is needed, so scientists who are not authorized to install software — or lack the know-how or desire to do so — can use it.
> 
> WebAssembly allows developers to recycle their finely tuned code, so they don’t have to rewrite it in the language of the web: JavaScript.
> 
> …
> 
> “You often have to modify the original code to get around things that WebAssembly doesn’t support.”
> 
> For instance, modern operating systems can handle 64-bit numbers. WebAssembly, however, is limited to 32 bits, and can access only 232 bytes (4 gigabytes) of memory. Furthermore, it cannot directly access a computer’s file system or its open network connections. And it’s not multithreaded; many algorithms depend on this form of parallelization, which allows different parts of a computation to be performed simultaneously. “A lot of older code won’t compile into WebAssembly, because it assumes that it can do things that can’t be done,” Stagg says.

By setting the real KIND in one place, Fortran makes it easier to shift between single and double precision.

---

<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 12, 2024, 8:01pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/6 "2024-03-12T20:01:18Z")

</div>

In fact, [Java applets](https://en.wikipedia.org/wiki/Java_applet) are just reborn… In the mid-90’s, it was a revolution: you could execute the bytecode of those applets in any browser of any OS, while in those early days (a new dawn!) the Web 1.0 was mainly static HTML code. It made Java popular.

---

<div class="post-metadata">

**Author:** ![nja](https://avatars.discourse-cdn.com/v4/letter/n/ed8c4c/32.png) [@nja](https://fortran-lang.discourse.group/u/nja)\
**Post date:** [March 20, 2024, 2:58pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/7 "2024-03-20T14:58:50Z")

</div>

I would be glad to have a “hello world” example here, it’s not obvious to me how to build a `.wasm` from a `.f90` file.

Something along the line of [Compiler un nouveau module C/C++ en WebAssembly - WebAssembly | MDN](https://developer.mozilla.org/fr/docs/WebAssembly/C_to_Wasm), but with Fortran `write` instead of C `printf`.

There is also an interesting resource [here](https://niconiconi.neocities.org/tech-notes/fortran-in-webassembly-and-field-solver/) that uses `flang-new-15`, but the introducing example is a calculation, not a simple `write`.

---

<div class="post-metadata">

**Author:** ![Ubaid-Shaikh](https://avatars.discourse-cdn.com/v4/letter/u/43a26b/32.png) [@Ubaid-Shaikh](https://fortran-lang.discourse.group/u/Ubaid-Shaikh)\
**Post date:** [March 21, 2024, 1:08pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/8 "2024-03-21T13:08:38Z")

</div>

> I would be glad to have a “hello world” example here, it’s not obvious to me how to build a `.wasm` from a `.f90` file.

You can do

```console
% cat examples/expr2.f90                                        
program expr2
implicit none

print *, "hello world!"

end program
% lfortran examples/expr2.f90 --backend wasm -o hello_world.wasm
% wasmtime hello_world.wasm                                  
hello world!
% node --experimental-wasi-unstable-preview1 hello_world.wasm.js
(node:69670) ExperimentalWarning: WASI is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
hello world!

```

---

<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:** [April 5, 2024, 4:56pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/9 "2024-04-05T16:56:36Z")

</div>

A blog post about this project by its author, [discussed](https://news.ycombinator.com/item?id=39944275) at Hacker News, is

> **[Dr George W Stagg - Fortran on WebAssembly](https://gws.phd/posts/fortran_wasm/)**
>
> Patching LLVM Flang to output WebAssembly objects.

---

<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:** [May 7, 2024, 4:36pm UTC](https://fortran-lang.discourse.group/t/flang-wasm-compiler/7589/10 "2024-05-07T16:36:54Z")

</div>

> **[Using Fortran on Cloudflare Workers](https://blog.cloudflare.com/using-fortran-on-cloudflare-workers)**
>
> Work on LLVM has enabled Fortran to compile to WebAssembly. So, today, we’re writing about running Fortran code on Cloudflare Workers

discusses the project

> **[GitHub - cloudflare/fortiche](https://github.com/cloudflare/fortiche)**
>
> Contribute to cloudflare/fortiche development by creating an account on GitHub.
