Wednesday, January 12, 2011
Beta 9 now available
After working more with the nanojit, I have decided not to make it the default, but it is there, and you can use it. Go into about:config, under javascript.options.tracejit.*, and set either or both of chrome and/or content to true. Restart the browser. You'll find many things are faster, but some things are still slower, and there also were occasional crashes when quitting the browser: more of a nuisance than a serious problem, since you were quitting anyway, but still not very smooth. If you have good knowledge of PowerPC, I'd love your insight into improving it. The nanojit is in the changesets, but I have now submitted it to Mozilla and you can also read the diff on Bugzilla if you like. Feel free to comment.
Other things in beta 9 include a less flickery title bar (hopefully), which was a very annoying thing that cropped up in beta 8. Although it was an infrequent irritation on my G5, some users got really walloped by it. Mozilla fixed some part of this for Leopard, but that fix doesn't help Tiger, so 10.4Fx b9 has a separate rendering path for 10.4 that uses a naughty kludge. Because of this, the title bar does look ever so slightly different in 10.4 than 10.5, but only if you're looking for it. The title bar still does not always match the window gradient, but this is part Mozilla's bug and part ours. If you are really anal about that, install a Persona; they always work.
The UI was also adjusted to bring the app to the front when a window is clicked on. I am dithering over forcing the window up, or just forcing the app up, so that you can type but keep your window ordering -- I'm leaning towards the second, but will take arguments for the first. You can always click the Dock icon to bring the whole application and all windows forward, and this fixes the bug in beta 8 where sometimes it didn't bring anything forward when you clicked on a background window. It uses Carbon, don't tell Steve! Oh, and find Sluggo! We miss Power Computing! Classilla users will have seen an earlier version of this easter egg before. No fair reading the changesets to find the magic words.
Note that Flashblock as of this writing is not yet upgraded for beta 9, which should be a oneline change for them. If you really need it (and most of us do prefer it), you might want to wait until it is, or use an equivalent.
Thursday, January 6, 2011
How the bench was run, and what it got us*
First some laundry list stuff. I've heard rumours about a Fink version of 10.4Fx, which sounds great. I see fangism is following this blog and I think this is his work, so ping me in the comments and maybe we can get your info into the project wiki.
Second, beta 9. Mozilla is apparently shrinking the scope for it, which is a good sign in that they are probably approaching release candidate state finally. At this rate, we should have Firefox 4 just in time for the USA presidential election. ;) This coincides nicely with the release of the nanojit, so there will be a beta 9 since it is convenient to do so. Mozilla's time frame is shooting for a code freeze, like, tomorrow, though historically their estimates have been at times wildly optimistic. That said, however, if their forecasted schedule is in any way accurate our beta 9 may lag it slightly instead of being ahead as usual. I'll explain, but first, the nanojit.
I implied in my last post that the nanojit is unfortunately not the dramatic leap I was hoping for (notice that I did not say how the bench[mark] was won), and those of you who know me at 68KMLA have probably read my post on that subject. To understand why this is a mixed bag, I should explain what the nanojit actually does, which is also useful to Mozilla hackers at large since there is precious little MDN documentation on it.
When the tracer thinks it has hot code, it turns on a trace recorder and slurps up the JavaScript opcodes as they come down the "pipeline." These opcodes are not executed as is (as the interpreter would), but are turned into an intermediate language called LIR. LIR is also the intermediate language for Flash ActionScript, as they are both part of Tamarin, though SpiderMonkey (Mozilla's JavaScript implementation) does not use other parts of the ActionScript interpreter.
After an initial spew of LIR opcodes and/or optimization, the opcodes are then handed to the nanojit assembler (in the source tree, this is in js/src/nanojit/Assembler.cpp). This section is more or less architecture-agnostic, which is important later on. It goes through each of the opcodes and as needed to generate code calls specific portions of the architecture-specific nanojit (in our case, NativePPC.cpp) which then write instructions to memory, starting with a standard function prologue. This is not done using things like __asm__(), but using a collection of macros and an opcode table to generate 32-bit instructions directly. If there is some concern about the result of an operation, a guard may be inserted as an internal consistency check, and side exits are made available to abort to the interpreter if funny business is detected. Finally, at least the first time through, an epilogue is emitted. As more code is written and more labels are fixed in memory, the assembler will go through and patch the branches in much the way a two-pass assembler would, eventually terminating in the epilogue that was originally emitted.
When it comes time to execute, the tracer makes certain statistical decisions about how useful the trace will be relative to simply falling back on the interpreter and runs one or the other. If a trace is deemed to be detrimental or pathological for performance, it is blacklisted. Otherwise, once it is compiled, it is used as long as it remains valid or until garbage collection destroys it, the rules of which are still not yet determined for Firefox 4.
As originally written, the PPC nanojit worked fine for Tamarin, but was not sufficient for Firefox. It did not have an implementation of nFragExit, which is needed to manage linkages with other code fragments and the epilogue, nor could it deal with the situation of a branch with a 14-bit or 24-bit displacement needing to be patched to a larger displacement (i.e., turning simple branch instructions into things such as mtctr b(c)ctr), and it did not implement the overflow arithmetic instructions, which use guards to catch math overflow and are heavily relied upon by Mozilla.
My first pass, then, was simply to add those features (I am grateful to Edwin Smith's help here, the original author, who gave me the critical pointers to get started). This was enough to get the browser up and running, but it didn't seem much faster, and SunSpider numbers were disastrous: on my quad G5 in Reduced power, the benchmark took around 7000ms on the stock interpreter in beta 8, but on "TraceMonkey PPC" it took nearly 10000ms -- almost half again as long! Dromaeo was less discouraging, and it did extremely well on the microbenchmarks, but its aggregate score was seriously hurt by the SunSpider dependency causing it to still be slightly slower in the final summation. (Please note that no PowerPC browser with the possible exception of Safari 5 on Leopard can run Kraken without having UI stalls, so I didn't bother.)
The suckage was, fortunately, proportional. There was no specific penalty paid other than that of the tracer and assembler themselves, and using Apple's built-in MakeDataExecutable() function -- which is still part of Carbon, but is only available on PPC -- to flush the instruction cache was actually faster than a hand-rolled routine. Less complex routines ran in proportionately less time, so the problem was the code it was generating.
It is instructive to look at the example of a simple loop:
var i, j = 0; for(i=0;i<2999999;i++) { j += 2; } print(j);
This can be written extremely fast in PowerPC assembly, especially being a single loop that can easily take advantage of the CTR branch instructions to do multiple operations simultaneously. Indeed, as a ceiling test I handwrote this into assembly and fed it to gcc, and the result was too fast to time accurately. However, when compiled by the nanojit, it took around 5000ms (!). Here's what it generates, edited for space and clarity. You can see this yourself by building a debug version of TenFourFox and starting it up with the environment TMFLAGS set to native (there are loads of other options, set it to help to see them):
/src/mozilla-central/obj-ff-dbg/dist/TenFourFoxDebug.app/Contents/MacOS/% ./js -b -d -j -p ~/empty.js
[ ... ]
0x3565ed0 [prologue]
0x3565ed0 mflr r0
0x3565ed4 stw r0,8(sp)
0x3565ed8 stw r31,4(sp)
0x3565edc mr r31,sp
0x3565ee0 stwu sp,-144(sp)
; single target patch 03565fd4 -> 03565f30 [ I'll explain this in a moment ]
0x3565ee4 stw r13,-72(r31) <= spill r13
0x3565ee8 stw r14,-68(r31) <= spill r14
[ .. rest of the prologue .. ]
0x3565f28 stw r30,-4(r31) <= spill r30
0x3565f2c stw r3,-76(r31) <= spill state
0x3565f30 [label1] ; the actual meat starts here
0x3565f30 lwz r28,8(r3)
0x3565f34 lwz r29,12(r3)
0x3565f34 ; pc now 03565f34
0x3565f38 ; swapped @ 03565f38
0x3565f38 lis r30,801 (0x3210000)
0x3565f3c ori r30,r30,57368 (0xe018)
0x3565f40 lwz r30,0(r30)
0x3565f44 cmpwi cr7,r30,0 (0x0)
0x3565f48 beq+ cr7,0x3565f58
0x3565f4c b 0x3575dec ; this goes to an exit block
0x3565f50 nop
0x3565f54 nop
0x3565f58 lwz r30,1560(r29)
0x3565f5c stw r30,0(r28)
0x3565f5c ; pc now 03565f5c
0x3565f60 ; swapped @ 03565f60
0x3565f60 li r27,2 (0x2)
0x3565f64 stw r27,8(r28)
0x3565f68 addo r30,r30,r27
0x3565f6c mcrxr cr7
0x3565f70 ble+ cr7,0x3565f84
0x3565f74 lis r0,855 (0x3570000)
0x3565f78 ori r0,r0,24140 (0x5e4c)
0x3565f7c mtctr r0
0x3565f80 bctr
0x3565f84 stw r30,1560(r29)
0x3565f88 lwz r30,1552(r29)
0x3565f88 ; pc now 03565f88
0x3565f8c ; swapped @ 03565f8c
0x3565f8c li r27,1 (0x1)
0x3565f90 addo r30,r30,r27
0x3565f94 mcrxr cr7
0x3565f98 ble+ cr7,0x3565fac
0x3565f9c lis r0,855 (0x3570000)
0x3565fa0 ori r0,r0,24236 (0x5eac)
0x3565fa4 mtctr r0
0x3565fa8 bctr ; this also goes to an exit block
0x3565fac stw r30,1552(r29)
0x3565fb0 stw r30,0(r28)
0x3565fb0 ; pc now 03565fb0
0x3565fb4 ; swapped @ 03565fb4
0x3565fb4 lis r29,45 (0x2d0000)
0x3565fb8 ori r29,r29,50879 (0xc6bf)
0x3565fbc stw r29,8(r28)
0x3565fbc ; pc now 03565fbc
0x3565fc0 ; swapped @ 03565fc0
0x3565fc0 cmpw cr7,r30,r29
0x3565fc4 blt+ cr7,0x3565fd4
0x3565fc8 b 0x3575f0c ; and this also goes to an exit block
0x3565fcc nop
0x3565fd0 nop
0x3565fd4 lis r2,0 (0x0) ; this is patched by the single target patch above
0x3565fd8 ori r2,r2,0 (0x0)
0x3565fdc mtctr r2
0x3565fe0 bctr
0x3565fe4 b 0x3575f6c
[ .. skip memory to epilogue .. ]
0x3575f6c lwz r3,-76(r31) <= restore state
0x3575f70 lwz r13,-72(r31) <= restore r13
[ ... ]
0x3575fb0 lwz r29,-8(r31) <= restore r29
0x3575fb4 lwz r30,-4(r31) <= restore r30
0x3575fb8 lis r12,1152 (0x4800000)
0x3575fbc ori r12,r12,31964 (0x7cdc)
0x3575fc0 lis r2,855 (0x3570000)
0x3575fc4 ori r2,r2,24528 (0x5fd0)
0x3575fc8 mtctr r2
0x3575fcc bctr
0x3575fd0 mr r3,r12
0x3575fd4 mr sp,r31
0x3575fd8 lwz r31,4(sp)
0x3575fdc lwz r0,8(sp)
0x3575fe0 mtlr r0
0x3575fe4 blr
Ugh!
For the sake of fairness, I have left the guards out as they have a very minimal impact on runtime, and the prologue and epilogue are fairly standard ABI stuff. But this is a lot of code for a small loop!
As originally written, the branches were not hinted, and my first attempt was to hint the branches with likely bits and allow it to optimize a patch down to a smaller set of instructions where possible. Notice the code near the end that loads CTR with 0x0 and tries to jump to it -- that is recognized by another nanojit function called nPatchBranch that (supplies!) patches branches as a long jump and the correct target is inserted into the lis-ori by the patch routine. If later in runtime the branch had to be changed, if it could be turned into a 14-bit branch with fewer instructions, it would be and the rest simply nopped out. Likewise, the nops in the code are there for room if a branch has to be "demoted" to a slower far call. This didn't help much, so I loaded the code into Shark.
Shark showed several things of note. First, as you scan the code, you will notice lots of potential pipeline stalls (ignore the swapping comments, I'll get to that). While the G5 is fairly aggressive with instruction reordering, it is not perfect, and older CPUs are less effective. Second, the branches are really hot. Third, there are a lot of store instructions inside the loop body proper aggravating the stall situation. Fourth, it's decrementing the loop the hard way.
To help with instruction ordering, I rewrote the macro that emits instructions to see if it could dynamically reorder them with the preceding instruction (call it "peephole optimization" if you like). You can see where the swapper worked in the instruction stream (just swap in your mind the instructions immediately around the swapper's log note). If there were no register dependencies and the code did not need to be emitted in strict serialized order, the instructions were swapped, which helped to turn things like
li r0,...
ori r0,...
li r1,...
ori r1,...
into a more typical and efficient
li r0,...
li r1,...
ori r0,...
ori r1,...
and cool off the registers. Although the G5 could do some of this in hardware, it helped with the G4s. A special attempt was made with comparison instructions to turn st cmp b into cmp st b so that the comparison would be more likely to be computed by the time the branch came along, and a store instruction could be safely switched with it since it would not affect the comparison. Finally, expensive instructions such as integer multiply were strength-reduced into repeated smaller operations (in mulli's case, into repeated strategic adds). These improvements yielded about 5-10% additional performance.
Still, the really ugly instructions such as branching could not be totally eliminated, and the memory stores could not be moved out of the loop because they stored stuff in the runtime stack that the side exits (if they were taken) might require. This was really painful for a processor like the PPC that is strongly oriented to load-store (whereas x86 is much better at memory operations) and had pipelining consequences. There is no way right now for the PPC nanojit to get a bird's eye view of what it's compiling so that it can more efficiently order larger functional blocks, such as moving the stack store operations into a side exit or the epilogue, or rewriting loops with the special PPC loop instructions if that sort of situation can be recognized. Perhaps that is more the domain of the methodjit, but it would be nice to have such meta-analysis available for the nanojit as well.
It could hurt less if the tracer were statistically able to predict accurately for the PowerPC which operations would be more painful for runtime, but tuning these parameters yielded small margins to date.
So am I throwing in the towel with the nanojit? No. For one thing, it still is faster at certain operations, and it can cache code it compiles (which is beneficial to L2-fat processors). While code would have to execute a large number of times to be worth it, things like the browser chrome do indeed execute a large number of times, as do many AJAXy applications. For another thing, I want to get more eyes on it to see if we as a group can make it better. While trying to beat a powerful optimizing compiler with a kludgey code generator is generally a losing proposition, there are probably other optimizations I haven't thought of.
The best and final solution would be to change some aspects of the nanojit to allow the assembler to see more of the LIR at once and make bigger level decisions (or hint the processor-specific portions about them), or to expand the LIR opcode set to embrace more specific purpose-oriented opcodes that could hint a code generator like this one about the intended purpose of a section of code. Certainly for loops there is a better way of implementing it in assembly than this. I don't know how likely that is to happen, however.
The plan for the nanojit then is this: it will come out as part of beta 9, turned on by default (you can turn it off in about:config). It has been tested mostly on G5 and G4/7450, though I also ran it on a G4/7400 and it seemed okay. I don't have a G3 running Tiger to test this with, so you guys will be on your own. See if it's faster for your workload. See if it's something you can improve upon. It's a start.
Wednesday, December 29, 2010
Got it == flaunt it

Mozilla sent out their Firefox 4 Beta Team T-shirts today, with a thank-you card that says, "thanks for all your hard work! You rock!" Now, maybe that was for OverbiteFF, or Classilla, or being a pest on Bugzilla, and not your favourite and my favourite POWER ISA legacy Mac browser, but hey! free T-shirt! :-D
The nanojit has been up and running for a few days on my personal dev build and is pretty stable, but the results are currently mixed, and I'm going to do a full post on that when I have some hard numbers to present. The long and short of it is, for many tasks the nanojit is a screamer, but for certain tasks it is actually slower (because remember, the trace and emit step has overhead of its own, and any savings the JIT makes seem to be outweighed by that overhead under certain conditions). The argument is which benchmark to believe, but I'm still working on optimizations to the nanojit, it still seems to be an overall win, and even modulo my author bias I wouldn't go back to TenFourFox without it. Still, it's instructive to note how much optimization we take for granted out of our compilers and how much work we have to do to hand-optimize assembly, and hand-optimizing PowerPC assembly language is in fact exactly what I'm doing. Once I get to a stopping point, we'll observe what Mozilla does with beta 9 while I push Classilla 9.2.2 out the door since we're already tardy on its deadline.
Tuesday, December 21, 2010
Enter the nanojit
Some terminology: the pure C (and now C++) JavaScript interpreter is properly called SpiderMonkey and descends directly from Brendan Eich's work when he was at Netscape. All Mozilla derivatives use it; Classilla uses the last C version which was in Firefox 3.0.19, and so does Camino, while SeaMonkey and Firefox use the C++ version which was introduced in 3.5 with TraceMonkey. TraceMonkey's key addition to SpiderMonkey is a clever piece of code called the nanojit, which watches execution through code paths and remembers the "trace" (hence the name). That trace is in fact generated as direct machine code, and run as such, so it is very fast. However, the nanojit is unable to effectively optimize certain types of code, notably code with evals in it and code with many type combinations, and it must run the program to a certain extent to determine what code is "hot" (so very branchy code causes the tracing JIT to give up, because it would use too much memory and CPU time to figure out where the hotness lays). To handle those cases, Mozilla added JägerMonkey to Firefox 4.0, which is a true method JIT, borrowing the assembler that powers it (Nitro) from
So that's our year in review; now for the diplomatic cables. If you look in the nanojit, you see tons of files for lots of architectures, including x86 (of course), ARM, SPARC, even MIPS, and of course PPC. Great, you say, the nanojit in Firefox seems to generate PowerPC code. Then you look at the configuration scripts and your heart sinks:
PowerPC nanojit doesn't get compiled or included in Firefox 3.5 or 3.6.
But wait, I hear you cry! The code is there, right? What's NativePPC.cpp for? Well, it's truly part of the nanojit, but it is incomplete. While it works as part of Tamarin, which is Adobe's implementation of ActionScript (and is indeed part of PPC Flash Player), it does not support enough instructions to work as part of Firefox or in fact any Gecko browser, and this is why your red-hot quad 2.5GHz G5 still benches slower on SunSpider than a measly Pentium 4: the P4 gets native code, we don't.
Well, that's about to change. With big thanks to Edwin Smith at Adobe who put up with my constant queries, I'm proud to announce that we're making progress on doing the obvious: expanding the PowerPC nanojit so it can be used as part of TenFourFox. Ed helped me out with some of the fiddly aspects, and we're now full speed ahead on implementing the LIR instructions needed to fully support the length and breadth of JavaScript that the Mozilla framework requires. Already it is passing most of the LIR assembler tests, and when it passes all of the supported ones, I'll start work on integrating it with the main browser. At the end of this, we'll pass our work back to Mozilla so it can be a part of Firefox, if they want it.
When will you get to play with the new hotness, and how much faster will it get? Conservatively, you can expect up to 2-3x improvement on many JavaScript benchmarks, possibly more. This is still not enough to reach x86 parity because we don't implement the method JIT (yet: more on that later when that work actually commences, if it's possible -- TraceMonkey depends heavily on Nitro, so we'll have to see how feasible such a port would actually be). However, it is still dramatically faster and wrings that much more performance out of our beloved machines. More to the point, it will work on any supported TenFourFox processor architecture.
However, when you'll get it is another matter. While this might be ready for beta 9 or 10, such a massive sea change requires heavy testing and may have subtle bugs, so it may sit 4.0 out and appear in 4.1, or it may appear in a later beta. I'll post more about this later when I have enough testing data to decide, but I've got enough working code already that I couldn't resist a little tease for y'all. Great things are afoot!
Friday, December 17, 2010
Changesets available
I am currently reviewing the beta 9 blockers to see if we will release a beta 9, or wait for beta 10 -- each release is about 64MB off our Google Code quota, so I'd rather not blow archival space on a beta release we might not get much benefit from. I'll post a decision here when I've committed one way or the other.
Wednesday, December 15, 2010
Beta 8 now available
Mozilla has made some interesting changes to beta 8, the most obvious being the redesigned tabs (with a nice rounded edge) and the new tab-modal dialogue boxes. In beta 7 (and any prior version of Mozilla), that link would give you a drop-down dialogue; in beta 8 ... well, just try it and see. The UI improvement is obvious in that a JavaScript application can't lock up your session by throwing lots of dialogue boxes at you, but there are still some questions about performance and user interaction, so you can expect this to continue to evolve. There are also some under-the-hood changes to JavaScript that should improve stability and improvements to fonts.
Specific to 10.4Fx, this version now offers four separate builds: G3, G4/7400 and G4/7450, and G5. The G5 build is the most unusual since the source is very unstable built for 64-bit PowerPC, so simply -mcpu=G5 or -fast will not work. Instead, this build actually builds for G4/7450, and simply turns on the extra G5 instructions, while including -mtune=G5 which does work (effectively making it 32-bit PowerPC with the PPC 970 instructions bolted on, tuned for the G5's instruction dispatch). Together, these tweaks squeeze out about 15% better performance on a G5 than the old unified 7450 version and is steady as a rock. There have also been performance improvements to the G4/7450 version itself, which is now offered specifically for G4e systems.
10.4Fx also adds support for font tables, enabling many of Firefox 4's marquee font capabilities; enables update checking (downloads are still manual, but you now get background notifications when new versions are ready); makes some UI adjustments and fixes all the known 10.4Fx-specific crash bugs. Note that there are still some Mozilla-generic crash bugs out there and this version is still susceptible to them. That said, I've been using beta 8 as my sole browser on my quad G5 and my iBook G4/1.33 for the last couple weeks and it hasn't crashed a single time on either computer.
Have at it!
Thursday, December 2, 2010
Beta 8 is go
So far in Firefox beta 8 performance continues to get better, there are some interface improvements, and a number of the glitches in the renderer are gone. On our side of the bug fence, update notification appears to work and all of the known crash bugs are stamped out. We're also planning to deploy a G4/7400 version at the request of those users, and I have a few ideas for a G5-specific version. A G5-specific release was always planned, but the Mozilla source is terribly unstable even with -mcpu=G5, let alone -fast, and I couldn't sort this out in time for beta 7. However, I may be able to use ISA-independent tuning parameters to squeeze more oomph out of it, which is useful to me personally typing here on this quad G5. No promises yet but I am planning to do some tests this weekend to see if such a build will be stable.
One noticeable problem which comes late in the game is that unaccelerated graphics performance, which 10.4Fx is intentionally limited to, is slower in Firefox 4 due to a performance regression in the Cairo backend. We really need this, and Mozilla has marked it blocking final as it affects all platforms. The problem is most acute with animated images; you can track this bug yourself in Bugzilla. Hopefully it gets fixed sooner than later. We're monitoring it.
10.4Fx beta 8 also introduces our own infrastructure and livery, just like Classilla. You'll like it.
Watch this space!