Flang-wasm compiler

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

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

2 Likes

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

2 Likes

LFortran already does it here https://dev.lfortran.org/.

1 Like

No installation required: how WebAssembly is changing scientific computing.
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.

3 Likes

In fact, Java applets 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.

1 Like

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, but with Fortran write instead of C printf.

There is also an interesting resource here that uses flang-new-15, but the introducing example is a calculation, not a simple write.

1 Like

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

% 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!
4 Likes

A blog post about this project by its author, discussed at Hacker News, is

5 Likes

discusses the project

3 Likes