Flang & Web Assembly

Are the plans to support the WebAssembly backend in flang? I downloaded and compiled the latest code, and tried to compile a simple Fortran application. I don’t know much about how LLVM works, but since LLVM already supports WebAssembly, shouldn’t flang be able to use it as a target?

$ ~/Downloads/llvm-project/build/bin/flang-new --target=wasm32-wasi fibonacci.f90  -v
flang-new version 15.0.0 (https://github.com/llvm/llvm-project.git 4fe2e42bbc18cb9e412b18dfcfe50e9b1f45f631)
Target: wasm32-unknown-wasi
Thread model: posix
InstalledDir: /home/rjzak/Downloads/llvm-project/build/bin
 "/home/rjzak/Downloads/llvm-project/build/bin/flang-new" -fc1 -triple wasm32-unknown-wasi -emit-obj -o /tmp/fibonacci-53e55a.o fibonacci.f90
LLVM ERROR: target not implemented
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/rjzak/Downloads/llvm-project/build/bin/flang-new -fc1 -triple wasm32-unknown-wasi -emit-obj -o /tmp/fibonacci-53e55a.o fibonacci.f90
 #0 0x000055ba62d95944 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055ba62d931e4 SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f800acf93c0 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x143c0)
 #3 0x00007f800a79103b raise /build/glibc-sMfBJT/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #4 0x00007f800a770859 abort /build/glibc-sMfBJT/glibc-2.31/stdlib/abort.c:81:7
 #5 0x000055ba62cdcec7 llvm::report_fatal_error(llvm::Twine const&, bool) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x45c8ec7)
 #6 0x000055ba62cdd02e (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x45c902e)
 #7 0x000055ba63fe0271 fir::CodeGenSpecifics::get(mlir::MLIRContext*, llvm::Triple&&, fir::KindMapping&&) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x58cc271)
 #8 0x000055ba63fe6a42 (anonymous namespace)::TargetRewrite::runOnOperation() TargetRewrite.cpp:0:0
 #9 0x000055ba6542eeda mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x6d1aeda)
#10 0x000055ba6542f418 mlir::detail::OpToOpPassAdaptor::runPipeline(llvm::iterator_range<llvm::pointee_iterator<std::unique_ptr<mlir::Pass, std::default_delete<mlir::Pass> >*, mlir::Pass> >, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x6d1b418)
#11 0x000055ba6542ffb7 mlir::PassManager::run(mlir::Operation*) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x6d1bfb7)
#12 0x000055ba62fcb08c Fortran::frontend::CodeGenAction::GenerateLLVMIR() (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x48b708c)
#13 0x000055ba63074665 Fortran::frontend::BackendAction::ExecuteAction() (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x4960665)
#14 0x000055ba62db1bb1 Fortran::frontend::FrontendAction::Execute() (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x469dbb1)
#15 0x000055ba62da5e3e Fortran::frontend::CompilerInstance::ExecuteAction(Fortran::frontend::FrontendAction&) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x4691e3e)
#16 0x000055ba62db703b Fortran::frontend::ExecuteCompilerInvocation(Fortran::frontend::CompilerInstance*) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x46a303b)
#17 0x000055ba61a0a8f8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x32f68f8)
#18 0x000055ba61955b6b main (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x3241b6b)
#19 0x00007f800a7720b3 __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:342:3
#20 0x000055ba61a099ae _start (/home/rjzak/Downloads/llvm-project/build/bin/flang-new+0x32f59ae)
flang-new: error: unable to execute command: Aborted (core dumped)
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 15.0.0 (https://github.com/llvm/llvm-project.git 4fe2e42bbc18cb9e412b18dfcfe50e9b1f45f631)
Target: wasm32-unknown-wasi
Thread model: posix
InstalledDir: /home/rjzak/Downloads/llvm-project/build/bin
flang-new: note: diagnostic msg: 
********************

At the moment the codegen support for Flang is a Work In Progress. I guess supporting Web Assembly and other backends can be thought of once the codegen support is available by default.

2 Likes

@rjzak You are basically hitting this: llvm-project/Target.cpp at 3d0e0e1027203fe5e89104ad81ee7bb53e525f95 · llvm/llvm-project · GitHub. If you’d like to experiment with WebAssembly, you could just edit that switch statement and that should be sufficient for flang-new to generate some code.

Longer term, there are no specific plans for WebAssembly. Like you said, there’s a WebAssembly backend in LLVM and Flang can, in principle, leverage that. But we don’t have the manpower to verify whether we need anything extra in the frontend itself :frowning: And there are no Flang buildbots targetting WebAssembly.

HTH,
-Andrzej

1 Like