Closed Bug 502776 Opened 17 years ago Closed 17 years ago

nanojit: lots of indirect branch mispredicts in the instruction-writing pipeline

Categories

(Core :: JavaScript Engine, defect)

x86
Linux
defect
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: n.nethercote, Assigned: n.nethercote)

Details

Attachments

(2 files)

Attached file Cachegrind results —
Nanojit uses classes that inherit from the LirWriter class to implement a pipeline of writing stages: eg. CseFilter, FuncFilter, ExprFilter... ending at LirBufWriter which actually allocates and writes the instructions in a LirBuffer. This is clever in a way... a more typical pipeline has several stages each of which contains a big switch on the instruction opcode, and those switches lead to lots of indirect branch mispredictions. Having insLoad(), insStorei(), ins2(), etc. avoids much of that switching. However, there is a different source of indirect branch mispredictions. That comes from the fact that each ins*() call is done through a LirWriter base class pointer, and so involves a virtual dispatch (or whatever the correct term is). This leads to lots of indirect branch mispredictions, according to Cachegrind -- about 10% of the total for SunSpider. You can see in the attached file Cachegrind's output. The column on the left ("Bim") is the number of indirect branch mispredictions. The virtual ins*() functions within class LirWriter exhibit lots of misses. The ones that are overridden more by the child classes have fewer misses (eg. ins2() is overridden by all of them, and has no misses). This is a shame, because the full virtual dispatch isn't really necessary. Although the compiler must be safe and assume that any of the calls to ins*() could involve any class that inherits from LirWriter, we know, for example, that when CseFilter::ins2() calls 'out->ins2()' that 'out' is always a FuncFilter (or whatever is next in the pipeline). So the full polymorphic power is overkill. A similar situation appears with the read pipeline, but Cachegrind doesn't report significant numbers of indirect branch mispredictions there. I don't know why. I tried to work out a way to structure the pipeline so that the virtual dispatches aren't required, but failed; even the failed ideas I had (lots of function pointers) would have been a lot uglier than the current design, even if they had worked. Any ideas on this front would be welcome. Because I couldn't get any alternatives working, it's unclear whether restructuring to avoid the virtual dispatches would save any time... my gut feeling says yes, but I don't have any proof beyond the Cachegrind results.
I actually wrote a patch to do this, but didn't see any speedups from it. Not sure whether its in bugzilla. I replaced the virtual method dispatch with template-based mixins. template <class T> class Filter { T* out; ... out->ins2(); .. } and then chain the pipelines together as template specializations when they are instantiated. Was about 30 minutes of shifting code around. If we can't find the patch I don't mind doing it again. Its pretty manual labor.
Oh, you beat me to it!
(In reply to comment #1) > I actually wrote a patch to do this, but didn't see any speedups from it. Not > sure whether its in bugzilla. Bug 466135. /be
(In reply to comment #1) > I actually wrote a patch to do this, but didn't see any speedups from it. Not > sure whether its in bugzilla. I replaced the virtual method dispatch with > template-based mixins. > > template <class T> > class Filter { > T* out; > ... > out->ins2(); > .. > } > > and then chain the pipelines together as template specializations when they are > instantiated. Was about 30 minutes of shifting code around. If we can't find > the patch I don't mind doing it again. Its pretty manual labor. Did you eliminate the virtual methods? In the snippet above, |out->ins2()| might still be a virtual call if ins2 is declared as virtual. Examination of the assembly or measurement of the branch mispredict count should indicate. I would recommend trying instead something like: template<typename T> class Filter { T& out; void ins2(...) { out.ins2(...); Also, it might help to make many of these methods inline. I think GCC currently can only do that if the method definition is visible at the point of use. This could be done by putting it in the header file, or maybe by concatenating the nanojit C++ files, as WebKit used to (or maybe still does).
Actually, I think my above example would still call methods indirectly if they are declared virtual. But that could, and probably should, be fixed in that design simply by making the methods not virtual and the classes not inheriting from each other. Alternatively, template<typename T> class Filter { T out; void ins2(...) { out.ins2(...); This requires careful design of the constructors but it should be possible to get it to work.
(In reply to comment #3) > (In reply to comment #1) > > I actually wrote a patch to do this, but didn't see any speedups from it. Not > > sure whether its in bugzilla. > > Bug 466135. Looks like you only did the read pipeline, and for whatever reason, cachegrind says mispredicts are only a problem for the write pipeline. I'll try that patch's technique (with dmandelin's suggested mods) for the write pipeline.
Assignee: general → nnethercote
Status: NEW → ASSIGNED
I've got it working for the write pipeline, I'm seeing a 5ms speedup on my Mac (960 -> 955ms). The current patch is a total mess, I'll clean it up and post it tomorrow, and also make sure that the generated code looks like I expect. It's a fair bit more complicated than the old version, though.
Attached patch patch — — Splinter Review
Hmm. The patch is attached. Pros: - Cachegrind number of simulated indirect branch mispredictions is down by about 7%, as expected. Cons: - The speedup of 5ms on SunSpider from last night has diminished today to the range -1..3ms. - The templates are a lot more complicated. Compiler error messages if you get them wrong are difficult to understand. - Lots of the write pipeline classes had to be moved into header files because GCC doesn't support 'export'. - Pipelines are a lot less flexible -- if you add another stage you have to change a few types. And having optional stages (controlled by compile-time or run-time flags) is much more difficult -- for compile-time flags it can be done with conditional compilation, for run-time flags (eg. soft float) I don't see how to do it other than having dummy stages that just pass everything through. (The patch doesn't deal with these, they're just commented out.) - Code size is greater: 1324216 vs. 1297216 bytes for the JS shell. Overall it's not exactly compelling. I'm not very experienced with C++ templates, perhaps someone can improve my patch further.
Given the underwhelming results and the extra complexity/inflexibility imposed by the change, I will resolve this as WONTFIX.
Status: ASSIGNED → RESOLVED
Closed: 17 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: