top | item 16049242

Clang runs in the browser and compiles C++ to WebAssembly

427 points| lifthrasiir | 8 years ago |tbfleming.github.io | reply

308 comments

order
[+] sehugg|8 years ago|reply
Clang is pretty big -- as a comparison, my Z80 IDE which uses SDCC (Simple Device C Compiler, i.e. no C++) loads about 2 MB of WASM + 2 MB of data and executes a compile/assemble/link cycle in about 0.5 sec for a small program:

http://8bitworkshop.com/v2.1.0/?platform=mw8080bw&file=gfxte...

Most of the execution time is spent in SDCC's register allocator, which was recently rewritten to generate better code but unfortunately is a lot slower than the old allocator.

[+] shalabhc|8 years ago|reply
Next we'll get to a webassembly-only VM that will replace the OS. All 'apps' will run on this VM instead of being native, and most will be cross platform. They'll talk to each other via messaging (using Javascript semantics) instead of bytes-over-pipes as they do today. An integrated globally available, namespaced data store API might replace the filesystem. Each app+version will be accessible by a distinct URL. 'My computer' will finally be fully virtual - a well defined collection of 'apps/data' URLs that materializes wherever I can open one of these webassembly VMs.

With this webassembly VM, early bound (precompiled) lower level languages may not have a strong advantage over late bound, dynamic ones, because the compiler is part of the OS layer (as it should be).

The world seems to be slowly moving, in a roundabout way, towards the OS-browser idea from Alan Kay.

[+] blub|8 years ago|reply
Add metrics and advertising obsessed companies into the mix and the only result could be a complete clusterfuck.

The major issue with web apps is that the customer (or depending on the company bag of meat that watches the ads) has no control over the app or the data. They will be abused by the company in all sorts of creative ways.

And yes, those VMs could be local-only, but why would Google, MS or Mozilla want that? The former two are heavy in the cloud+ads business, the latter was pushing FirefoxOS. Why would web developers want that? All the stuff they write is online-first.

[+] tzahola|8 years ago|reply
Replace “WebAssembly VM” with “JVM” and you will see how it’s already available, and why it won’t catch on.
[+] ttflee|8 years ago|reply
Every time I see such news, I tell myself maybe it is the time we port a browser into the Linux kernel and build METAL, like Gary Bernhardt foretold.

https://www.destroyallsoftware.com/talks/the-birth-and-death...

[+] xtrapolate|8 years ago|reply
Having some random thoughts about the comments here, some of which talk about having a "browser" drive large parts of what we consider today an "operating system". I agree that certain aspects of what most modern operating systems do today can be abstracted away behind a unified, convenient (and perhaps browser-accessible) API. The entire user-experience stack comes to mind immediately, but there are counter examples as-well. The answer to non-unified execution environments (as in different operating systems and architectures) was the rise of high-level, interpreted languages (in addition to corresponding VMs), and runtime environments which exposed a platform-independent set of APIs. We've been exploring the possibilities of implementing bytecode VMs in hardware (https://en.wikipedia.org/wiki/Java_processor), or running a managed code operating system (Microsoft's Midori) for a while now, so the concept itself isn't particularly new. What is WASM's advantage over, say, Java's bytecode? Did we really have to go through so many hoops (reinvent another instruction set and a VM that can execute it across multiple platforms)? Why wasn't an existing technology re-used here to achieve the same/similar goals?
[+] chuckdries|8 years ago|reply
Well, what would you have replace it?

Like most things that become very popular, the web is becoming an os-like platform because it solves a problem. Putting your application at a URL is hugely more accessible than asking someone to download something. Maybe not to us HN readers, but to my mom, who's skeptical to a fault of anything that says download, it's huge.

HTML+CSS+JS is everywhere and complete in a way that literally nothing else is. Yeah C is everywhere but (before nowish) you couldn't write a C application once and have it run the same pretty much everywhere.

But chuck, you say, HTML has notoriously bad compatibility and working with it is frustrating. I propose to you that HTML and CSS have so many compatibility problems because of how pervasive it is and has been for so long, and how many different implementations of it exist. Anything is bound to develop compatibility issues when it gets as large as the web.

But you're right, it's suboptimal and other tech has tried to be everywhere. Java and Flash did try, but have you ever used a java applet? They're fine I guess, but they're stuck in a square on the page. You can make a java or flash object take up the whole page and completely control the website but then you run into accessibility issues and complexity on your part.

Javascript has grown organically over the past decades for a reason, and it's being used in this fucked up way because, all things considered, it's the best platform for the job right now

[+] vbezhenar|8 years ago|reply
I think it just happens that browser is the most widely installed VM independent of a single major vendor, pretty cross-platform and with enough features to build applications of any complexity. If you would design that from scratch, I think that something much simpler could be designed, much simpler than JVM, for example. But how would you install it to every computer out there? Nobody would use it if it's not installed everywhere.
[+] imtringued|8 years ago|reply
>What is WASM's advantage over, say, Java's bytecode?

Precise control over memory layout and allocation which can translate into massive performance gains of several orders of magnitude.

LLVM IR isn't stable accross LLVM versions.

Webassembly is the solution to a problem that stayed unsolved for decades.

There is no alternative to webassembly. It is in it's own category.

Probably the closest competitor you could find is LuaJIT with it's FFI module but LuaJIT wasn't designed as a compilation target. It'd suffer from the same problems that asm.js did.

JVM bytecode is stable but high level.

LLVM IR is unstable and low level.

WebAssembly is stable and low level.

[+] pjmlp|8 years ago|reply
As usual, politics.
[+] stevemk14ebr|8 years ago|reply
People might not like it but i really think native apps are going to die once everyone figures out how to optimize this crazy web stack.
[+] meritt|8 years ago|reply
It's definitely possible but we went through all of this a decade ago with Adobe Air, Microsoft Silverlight, and JavaFX. Nearly a decade before that, in the early 2000s, we went through this with Macromedia Flash, Java Applets, and Microsoft ActiveX.

Trying to replace native applications with runs-in-the-browser RIAs is nothing new, but we're at least finally agreeing on the language/implementation, so maybe this time will truly be different.

[+] snarfy|8 years ago|reply
I don't think so, not when my native game needs 50GB of install space. There is no way I'm waiting for that progress bar to download in a webassembly version game. This is one area where native will always have an advantage. The network is no match for the local bus.
[+] ateesdalejr|8 years ago|reply
I agree, once browser get a good grip on CPU parallelism things will probably be all downhill from there.
[+] knowThySelfx|8 years ago|reply
The browser is gonna become like a front for a distributed OS.
[+] collinmanderson|8 years ago|reply
Back in 2010 I considered native to be a fad and that we'd eventually go back to web (just like native desktop apps have gone to web.)

However, 8 years later, things really don't seem to be moving in that direction. Native is always going to be one step ahead in terms of speed and ability.

We would need to see native features start to stall so the web can catch up in ability, and websites need to do a lot of work to catch up in speed.

[+] mr_toad|8 years ago|reply
For every step towards browser-based apps there seems to be a step back again - e.g. React Native, Electron and Android Play on ChromeOS.

Back when the first iPhones debuted without an app store, Apple claimed that everyone could just use web apps. That didn't last long.

[+] mark-r|8 years ago|reply
So Clang itself was compiled to WebAssembly? That's slick, even if it does take forever to load.
[+] jokoon|8 years ago|reply
Finally, wasm short-circuited the will of os manufacturers to make their platform exclusive and incompatible with others.

Now let's see if wasm performs well enough on Android phones, and I guess we are finally arriving into the software deployment heaven.

[+] flavio81|8 years ago|reply
This is just fantastic. The "happy 2018!" news i was waiting for.

As i've said before, little by little we're witnessing the end of Javascript dominance.

[+] Sreyanth|8 years ago|reply
This is awesome. I was planning to write one myself after I couldn't find any such Clang runner for browsers. I guess I could just strip off a few overheads so I can run some pretty small programs to teach algorithms.
[+] seibelj|8 years ago|reply
Idea - make a website that lets you compile and test any open source program. For example, I should be able to clone, compile, and run gedit (a stagnating text editor included with many linux distros). This would let me quickly experiment with and edit a variety of programs and tools within the web browser without needing to get a development system up and running. I bet open source projects can get a ton of new hackers to continue development on their programs if everything could be worked on quickly within a web browser.
[+] exikyut|8 years ago|reply
Main issue is memory usage. Running KDE+Kate - http://vps2.etotheipiplusone.com:30176/redmine/emscripten-qt... - uses 1793MB (htop VIRT), 416MB (Chrome task manager). (Oh, and the renderer process wasn't hosting any other tabs.)

Second main issue is speed. qsterix isn't as memory-heavy but runs all Tetris game code inside KDE JavascriptCore, which as part of the KDE/Emscripten build is transpiled to JS. In other words, http://vps2.etotheipiplusone.com:30176/redmine/emscripten-qt... is JavaScript inside JavaScript, and on my reasonable-but-not-last-week-new i3 box, I experience a small pause that I can't explain when I hit the spacebar to drop a block.

Half of me wants to warn everyone from opening these on their phones or tablets, the other half wants to know how badly everything crashes. :P

Note: I tried opening the Kate demo a week or so ago on a marginally older version of Chrome, and the download phase hung. It worked this time though.

[+] krapp|8 years ago|reply
Something like that needs to be built into Github or a similar site. Along with fork and download, 'compile to web' should be an option.

Keeping code alive would also make long-term archiving of obsolete software viable, more so than just putting the code online, anyway. This is important because so much of our culture has been expressed by and tied up in software.

[+] ychen306|8 years ago|reply
It's hard to just apply this to an arbitrary project, which may use nontrivial linker features or native code.
[+] mailslot|8 years ago|reply
Cool! Now I just need the JVM as a clang target, and then I'll never have to use anything other than C++ ever again!! :D Webassembly has a tad more promise than ActiveX.
[+] imtringued|8 years ago|reply
Perhaps you could use node.js to compile your C++ code into a native WASM module?
[+] deepGem|8 years ago|reply
This is really slick. Clang took about 20 secs to load on my quad core MBPro. How does this change web development ? Do we need an Ecmascript equivalent for C++ ?
[+] bawana|8 years ago|reply
so does the concept of the 'browser is the OS' , improve security? Is it analogous to a hypervisor running on a server somewhere, then when you log into the server, it spins up a vm os just for you. Now any hacking done on that os is isolated and disappears when that instance is closed. in the same way, when the browser is the os, none of the files,apps or stuff on your machine is available to it?
[+] paulhilbert|8 years ago|reply
So we could finally port windows93(.net) to C++?
[+] netheril96|8 years ago|reply
There is actually an immediate real world use for this (not just being a toy): online judge (like leetcode). Of course, the final evaluation has to be done on the server to avoid cheating on the test set, but the user can do his/her own test in the browser without the roundtrip to and from the server then.
[+] kgraves|8 years ago|reply
This is neat and shows great potential, I hope in future that Adobe products would be integrated with WebAssembly or even old products converted to WebAssembly and running in the browser. We shouldn't be that far off.
[+] sp332|8 years ago|reply
Are you thinking like Photoshop, or a sandbox for Flash?
[+] muthdra|8 years ago|reply

  enlarged memory arrays from 536870912 to 1073741824, took 28094 ms
[+] MobiusHorizons|8 years ago|reply
Now we just need to compile a browser to webassembly so we can run a browser in a browser :) then the browser will officially be a complete operating system.

On a serious note though, this is an awesome example!!

[+] AndrewGaspar|8 years ago|reply
This is very cool.

It seems like there's some sort of memory leak, though. If you press the Run button over and over, you'll get a little message every 2*N runs about expanding memory arrays by double.