It's an interesting approach but feels a bit like over-specialization. The V8 guys on the other hand, decided to ignore the directive entirely and have been able to improve their (more "generic") JIT compiler to get it to run the same code almost as fast as Firefox's Ahead-Of-Time compiler in a lot of cases.
http://arewefastyet.com/#machine=11&view=breakdown&suite=asm...
There is no difference in approach between Firefox and Chrome there, both believe JITs can achieve excellent performance, and are always pushing that further. So both browsers "ignore" the "use asm" hint in that sense. The only difference is that Firefox, in addition, has an asm.js AOT module that detects asm.js and can optimize it very well (using the same backend as the JIT, but taking advantage of type information from the asm.js type system). But that AOT does not mean anything about the rest of the JS engine.
There are also a few runtime changes as well with asm.js code. For example, the JS engine knows that asm.js code cannot throw exceptions (asm.js code is very low-level, so JS exceptions are not possible), so it can avoid emitting some things it would normally emit in order to handle deopts due to exceptions and so forth. (Of course this does not require an AOT, a JIT could also detect the lack of need for that overhead, perhaps this is already done in some cases, but the AOT guarantees it is done for all asm.js code.)
Of course, the real solution is to fix the garbage collector, or (maybe) better yet, give user code a way to switch GC algorithms based on app-specific needs. Until then, the ability to manually manage memory is nothing to sneeze at.
>Q. Why not just keep optimizing JavaScript JIT compilers instead?
>A. No need to stop! But JIT compilers have less predictable performance based on complicated heuristics. The asm.js model provides a model closer to C/C++ by eliminating dynamic type guards, boxed values, and garbage collection.
You do realize that asm.js was not designed to be written by hand, don't you? It's a target for compilers. There are no bounds "to stay within" for hand-written JS which is still the vast majority of the code out there.
Even still someone has to write that compiler and they want to know what will be fast.
I do not see many people writing asm.js by hand beyond optimizing vital sections.
If I say "use asm" and do something that isn't optimizable by mistake, it should give me an error rather than silently falling back to slow code.
Interestingly enough, this makes the V8 approach all the more welcome since it doesn't break backwards compatibility.
Firefox will warn about asm.js validation errors, but those are just warnings in the web console. They do not affect semantics of execution at all, and are undetectable by the running code. So the code will run identically in all browsers regardless of whether it happens to fall in the asm.js subset or not.
So why Chrome should buy from Firefox? why should they follow the asm.js spec ? Maybe it i the wrong answer to a real problem, javascript speed and its memory foodprint.Why not fix the problem at the source instead of creating a subset? not meant to be written by hand? Chrome approach seems better.
I cannot pretend to know what is best in a perfect world. Yet I am willing to pretend to know what is the more practical way forward.
Native Client is and was an interesting approach, but it requires buy in. Buy in which it was not getting, and it had several years to get said buy in.
When asm.js got announced I can only saw I was shocked. asm.js is brilliant! Brilliant in the same way utf-8 is. It does not require heavy changes yet could bring 90% of the benefit. It does not require a new sandbox. It is cross-platform and cross-architecture!
PNaCl is also a subset that is not meant to be written by hand—of LLVM IR. The difference is that asm.js subsets a language that is universally supported by all Web browsers, has massive traction, and has a detailed specification.
asm.js subsets a language never meant for this, and tries to come up with something that will compile well. This approach certainly can work, but will have significantly more limitations 5 years from now than the other one.
Not to mention, LLVM, and it's IR, certainly has more traction than "asm.js".
LLVM IR was also never designed to be a portable, platform-independent, well-specified bytecode. It's designed to be a compiler IR. This shows up in numerous places: TargetData, download size, undefined behavior, and so on. Google has worked around some of these (TargetData) and has not worked around others (undefined behavior—which is really scary for a proposed Web standard).
The fact is, both PNaCl and asm.js are taking a language not designed to be used as a portable, platform-independent representation and making changes to make it work. You can argue about which is "further from the ideal", but I think that's subjective and not productive, involving predicting the future. I certainly don't think there will be any fundamental limitations 5 years from now.
> This approach certainly can work, but will have significantly more limitations 5 years from now than the other one.
I don't see this at all. As I said before, JavaScript is not some immutable thing. We can evolve it. Most of the changes needed to fill in the few remaining holes in asm.js help everyone. SIMD and 64-bit integers, for example, are something the entire Web would benefit from, asm and non-asm writers alike.
> Not to mention, LLVM, and it's IR, certainly has more traction than "asm.js".
Not on the Web, which is the important thing here.
This is completely and utterly false in every way. This is exactly what it was meant to be, and is. Maybe you mean "it does not turn non-portable languages, like C, into portable ones".
This does not change whether it is a portable, platform independent, well-specified bytecode. This is even covered in the FAQ.
To your specific examples TargetData is meant to deal with languages that have unavoidable target specific dependencies, like C. It is unnecessary in portable languages.
Download size is completely unrelated to a portable, platform-independent, well-specified bytecode, and is probably the absolutely most trivial thing to fix in the world if it was necessary. I'm not sure why one would even bring this up.
Undefined behavior is not necessarily bad. Most languages specify some amount of undefined behavior. This is done on purpose, to enable certain classes of optimizations. Is your claim that asm.js has no undefined behavior?
emscripten certainly does, even in asm.js mode.
My guess is that you meant that asm.js has a bunch of properties that are statically verifiable, and it validates conformance at compile time. That is great, but not really the same as "no undefined behavior". At a glance, asm.js has undefined behavior. For example: "As JavaScript functions, they produce the undefined value, but asm.js code is not allowed to make use of this value"
A program that attempted to make use of it would exhibit undefined behavior. It would be noncomforming to the asm.js spec, in the same way that a program in LLVM IR that uses undefined behavior is noncomforming to the language specification. Again, you may mean that you statically verify this, which is great, but that does not mean the spec does not deal have undefined behavior. It just means you try to validate conformance at compile time (and note, the asm.js spec itself explains why it can't possibly accomplish no actual undefined behavior at runtime: ' In particular, reading out of bounds from a typed array produces undefined, and calling FFI functions may produce arbitrary JavaScript values.')
"I certainly don't think there will be any fundamental limitations 5 years from now."
If you didn't believe this, i'm sure y'all wouldn't be working on asm.js. :)
However, I strongly disagree. What you are doing is not the first time someone has tried to do something like this (see, e.g., the SIMPLE IR and other IR's the McCat compiler used to use), and it won't be the last. Your claim essentially comes down to "this time for sure!". Sorry, but living in the world of compilers, i've seen and heard this too many times. The truth is there are no magic bullets in compiler technology, and asm.js isn't one either. It's a set of tradeoffs. I don't believe this chosen set of tradeoffs come without fundamental limitations.
I guess we'll see whether you are right.
"> Not to mention, LLVM, and it's IR, certainly has more traction than "asm.js".
Not on the Web, which is the important thing here."
You can always define a target audience that likes your stuff more. So let's go this way: Researchers, compiler developers, and a very large contingent of companies care very strongly about LLVM and improving it.
I certainly wouldn't disagree with "worse is better" often wins, particularly on the web.
My guess is that you meant that asm.js has a bunch of properties that are statically verifiable, and it validates conformance at compile time. That is great, but not really the same as "no undefined behavior". At a glance, asm.js has undefined behavior. For example: "As JavaScript functions, they produce the undefined value, but asm.js code is not allowed to make use of this value"
'Not allowed to make use of this value' means that the asm.js application will not get AOT compiled if it tries to use the value, because it's not a valid asm.js application. So the behavior isn't 'undefined', it doesn't work. The AOT compiler rejects it and it runs as regular JS, in which case the behavior is 'defined' in that it works the way all other regular JS does.
asm.js is specced and there's a verifier; if you pass the verifier your code should behave predictably in an AOT-compiling runtime. The runtime isn't producing code that does dynamic type checks at runtime or other sorts of nondeterministic behavior.
It's certainly possible that some undefined behavior slipped into the spec somewhere, but I definitely haven't seen any actual examples where they specced it and the Firefox implementation generates undefined behavior on purpose. That would be insane. asm.js delivers the same safety guarantees as regular JS; you can't do that with undefined behavior ala C.
Ultimately if you have concerns about the safety of asm.js or the presence of undefined behavior, you should test it and report bugs. I've tested asm.js some and played around with it and have not found any reason to be concerned.
Sorry, but no. Also, I was addressing points made about LLVM IR, and the asm.js specification, as well as it's suitability as a compiler IR. I've used asm.js, i've read and understood the current optimizing compiler implementation, as well as the published roadmaps and mailing lists.
I don't appreciate the unrelated attack, it was completely unwarranted and unnecessary. The fact that I don't agree with you does not mean "I haven't used it".
" So the behavior isn't 'undefined', it doesn't work."
You completely misunderstand undefined behavior.
The behavior is undefined, it is simply verified at compile time that the program does not perform it. That does not make it less undefined, it makes the program "statically checked" as opposed to "dynamically checked" or "not checked at all" (like most C, though it's worth pointing out it's entirely legal for a C compiler to reject code producing undefined behavior, it's only required to accept implementation defined behavior).
Exactly as I said, C programs (or others) that exhibit undefined behavior are in fact noncomforming, in exactly the same way that the asm.js program would be noncomforming. The only difference is when and whether checking and rejection of nonconforming behavior is performed.
If you are going to argue about language specifications, and make claims about them, you need to be precise.
pcwalton likely meant to make the claim that asm.js itself is safer because it has no undefined behavior at runtime, as it rejects programs that may exhibit undefined behavior at compile time (or something similar), which is at least possible to be true, though i would have a lot of trouble believing it without some formal proof (the spec offers none). It is incredibly hard to actually come up with things like this that are completely statically decidable and still useful.
Your definition of behavior that does not meet the spec as "defined" because "it works the way it all regular JS normally does" is a hilarious new definition of 'defined behavior'.
Also, would you like to refute any other point I have made about how pcwalton has completely misrepresented a lot about LLVM IR?
To respond to your comments in this post, the asm.js validator is not checking for undefined behavior. In fact, it has nothing to do with undefined behavior whatsoever. It also has nothing to do with decidability. It is merely determining which execution strategy to use. And, behaving according to a spec is actually a pretty ordinary definition of "defined behavior".
You have misunderstood how asm.js works, you have misunderstood other people's comments, and you have mischaractarized LLVM in other posts here. I know you're capable of much higher-quality discussion than this. Please take a moment to reflect.
So let's start simple:
asm.js's validator does in fact, statically try to decide asm.js spec conformance. That means it is checking for undefined behavior, since it is checking whether the program meets the language specification, and the language spec disallows various undefined behavior. I'm not sure how you could characterize it as anything else, when the spec itself says this is what it does:
"This sublanguage effectively describes a safe virtual machine for memory-unsafe languages like C or C++. A combination of static and dynamic validation allows JavaScript engines to employ an ahead-of-time (AOT) optimizing compilation strategy for valid asm.js code."
Note: it says it uses static and dynamic checking to validate spec conformance. It also claims it is safe. Such claims and static program validation of any sufficiently complex language, like asm.js, involves decidability issues and issues about undefined behavior. Period. If you disbelieve this, all I can suggest is delving into the field of program validation and static analysis. I completely understand the cases it cannot validate fall back to normal javascript engine execution, but that actually changes exactly nothing about what the validation is doing, the language specification, undefined behavior, whether it has fundamental limitations, etc.
"You have misunderstood how asm.js works, you have misunderstood other people's comments, and you have mischaractarized LLVM in other posts here. "
I disagree that I have done anything of the sort. Starting from the last, I have not mischaracterized LLVM in any way.
What I see here is a discussion with people who are trying to make very imprecise comments about very precise technical specifications and definitions, and then argue about them. I feel like i'm trying to argue physics with a bunch of chemists. It's not that they aren't smart, it's that they are abusing terminology and trying to make nonsense out of very well understood issues in the field.
In fact, i don't believe anyone working in the compiler field (even on asm.js) would disagree with anything i've said, because as far as I can tell, they understand these are all issues, and try to stay away from ridiculous claims about competitors.
pcwalton originally made some very direct claims about LLVM's IR, and asm.js. I have pointed out they are directly false. That is not mischaracterizing LLVM, or asm.js. It is in fact, a response to such mischaracterizations. Out of kindness, I did not originally point out that a very trivial reading of any paper on LLVM, it's bitcode, or the IR, would have shown that his claims are false, and instead responded directly. The very first LLVM paper ever written even covers exactly the claim he makes about portability, platform independence, and specificity. He would not have had to look very far on the website to find a refutation.
I actually have no real love for either (they are both a set of engineering tradeoffs). I just cannot stand to see one side or the other pretend that the other side is completely horrible, and that they have chosen "the one true path". I am also not going to watch one side or the other make claims that are simply false in an attempt to support that path.
In any case, i'm not going to argue about it anymore here, as it is a mild exercise in futility. You are welcome to believe what you like about asm.js and LLVM, and their relative merits.
However, it has not done so "on a regular basis" (to say otherwise is complete bullshit. I'm pretty sure the last major backwards incompatible change was version 3.0, which was 2 years ago). However, from where I sit, it's changed seriously less than asm.js in the past few years!
We'll see how long asm.js actually stays backwards compatible without having real impact.
'seriously less than asm.js in the past few years' is a blatantly fallacious claim. asm.js, as a spec, is barely a year old (going by the date of dherman's first public commit to his github repository) and the first implementation is younger than that. Mozilla turned the implementation around in a few months with a handful of engineers.
See, e.g., pcwalton's own comments:
"Most of the changes needed to fill in the few remaining holes in asm.js help everyone. SIMD and 64-bit integers, for example, are something the entire Web would benefit from, asm and non-asm writers alike."
Such changes would almost certainly be backwards incompatible with what exists now.
"'seriously less than asm.js in the past few years' is a blatantly fallacious claim."
Errr, given asm.js has changed a lot in the year it's been around, and LLVM has not changed in any backwards compatible way in that time period, it's actually what you would call "trivially true", not "blatantly false".
Again, asm.js has 'changed' but has not broken backwards compatibility, because it's just javascript and remains so.
You especially can't do this when the parent made the claim that PNaCL has exactly this issue.
Yet it never was "use asm" which made asm.js fast. It was the js engine. "use asm" carries symantical knowledge so a browser can warn if you break out of the subset. It also serves as a strong hint that should make the optimizer's easier.
This is why I think asm.js is the future. Mozila does not need buy in from Google, or Apple, or MS. Instead developers can compile to asm.js and their code will run, and in time it will run faster.
So in effect Chrome supports arm.js but they are not making a promise. I think it would be better for the internet if they made this promise.