5 ms·
This article appears to have nothing to do with micro-op fusion, which is an internal optimization in some CPUs. See, for example, section 8.4 in Anger Fog man
by amluto 9y ago
This article appears to have nothing to do with micro-op fusion, which is an internal optimization in some CPUs. See, for example, section 8.4 in Anger Fog manual [1]. This article is instead about whether a particular operation is better written as a single-instruction memory RMW or as there separate instructions to read, modify, and write.
[1] http://www.agner.org/optimize/microarchitecture.pdf http://www.agner.org/optimize/microarchitecture.pdf
- chrisseaton 9y agoIntel says that: > Micro-fusion fuses multiple micro-ops from the same instruction into a single complex micro-op. And that: > Macro-fusion merges two instructions to a single micro-op. The point is that the three instruction format and the single instruction format you would think would decode both to three micro-ops (or something like that), but instead the single instruction format can use a single fused micro op. That's why it's better to write as a single instruction. And your reference backs this up - it talks about single instructions in this section on micro-op fusion. You may be confusing it with macro-fusion, which is about fusing multiple logical instructions, which is discussed in the next section in that Agner document, but isn't discussed in this blog. > This article is instead about whether a particular operation is better written as a single-instruction Yes - it's better to write as a single-instruction, because then micro-op fusion applies. Macro-op fusion isn't as strong, as doesn't apply to the three logical instructions.
- deleted 9y ago[deleted]
- pbsd 9y ago> The point is that the three instruction format and the single instruction format you would think would decode both to three micro-ops (or something like that), but instead the single instruction format can use a single fused micro op. That's why it's better to write as a single instruction. This is not exactly what happens. A read-modify-write instruction in general results in 2 fused uops, one for the read+op, the other for the address generation+write. In the specific case of INC, you still have 3 fused uops for unclear reasons---possibly because INC only partially changes the flags. Replacing the expression by ADD [memory], 1 results on the expected 2 fused ops.
- jnordwick 9y agoDo the uop counters he's printing out count fused uops as only 1 uop or do they do something like count the uops separate for uop dispatch and as one for retire?
- pbsd 9y agoAs far as I understand, with micro uop fusion the number of retired uops should be higher than the issued uops.
- jnordwick 9y ago(Wrong stuff deleted) Edit: According the this pdf, macro/micro fusion happens very early on macro decoding, but I'm still not sure how it counts retired uops: http://pc.watch.impress.co.jp/video/pcw/docs/601/161/p21.pdf http://pc.watch.impress.co.jp/video/pcw/docs/601/161/p21.pdf And does this mean micro fusion cannot occur when you use the separate load/op/store instructions?
- pbsd 9y agoAccording to Agner when the uops go back to to the reorder buffer to get retired they are still treated as fused. However, if you look at the descriptions of the events [1, 2] a retired fused uop is counted as 2 retired uops. And yes, if you do mov r, m / add r, 1 / mov m, r instead of add m, 1 no such fusion happens. This is specific to CISCy instructions that do more than one thing at the same time. [1] https://software.intel.com/sites/products/documentation/doclib/stdxe/2013SP1/amplifierxe/pmn/events/uops_retired.html https://software.intel.com/sites/products/documentation/docl... [2] https://software.intel.com/sites/products/documentation/doclib/stdxe/2013SP1/amplifierxe/pmw_sp/events/uops_issued.html https://software.intel.com/sites/products/documentation/docl...
- BeeOnRope 9y agoMost recent archs have both fused and unfused counters for retired uops: UOPS_RETIRED.RETIRE_SLOTS is fused domain, while UOPS_RETIRED.ALL/UOPS_RETIRED.ANY and friends are unfused domain.
- timmclean 9y agoThat all makes sense, but it doesn't seem to apply to the example code in the article, right? `inc` doesn't decode to a single fused uop on Ivy Bridge. AFAIK, the example code in both cases decodes to the same number of uops in the fused domain...
- jnordwick 9y agoAny macrofusion he's seeing is most likely from his loop machinery.
- chrisseaton 9y agoIntel don't provide any tool to disassemble machine code into micro-code do they? I'd love to have that in my debugger.
- BeeOnRope 9y agoIt doesn't decode to a single fused uop, but rather 2 fused pairs of uops (so 4 total unfused uops). So there is fusion going on, twice (the load and ALU op are fused, and the two store uops are fused). If you use the three-instruction sequence the load and ALU op can't fuse, which potentially makes it slower (but not in this case since the bottleneck is elsewhere).
- timmclean 9y agoI believe you're thinking about `add`. According to Agner Fog's instruction tables, the load and ALU uops are fused for `add`s, but not in the case of `inc` http://www.agner.org/optimize/instruction_tables.pdf http://www.agner.org/optimize/instruction_tables.pdf
- BeeOnRope 9y agoYes weird - it was pointed out somewhere elsewhere here and I updated some of my comments but not this one. It's quite unusual that add gets the 2-uop treatment but inc doesn't. Yes, they treat flags differently, but that's mostly been resolved though flag renaming, and the reg forms of inc don't suffer any penalty. I'll have to double check if this is true. If it is, compilers should generally be preferring add [mem], 1 then (except perhaps when optimizing for size) - the difference in the flag behavior is pretty much never relevant for compiled code.
- nkurz 9y agoHis terminology might be a tiny bit off, but I think it's actually pretty close. Intel's optimization manual (https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf https://software.intel.com/sites/default/files/managed/9e/bc...) in 2.4.2.6 says "Micro-fusion fuses multiple micro-ops from the same instruction into a single complex micro-op. The complex micro-op is dispatched in the out-of-order execution core." So while the title probably might have been better as "Micro-fusion of µops", "Micro-op fusion" isn't particularly far afield. It's called "micro-fusion", by the way, because the µops are fused together from the point of view of parts of the processor, but then "unlaminated" to be executed. The alternative is "macro-fusion", where certain instruction pairs (cmp-jcc) are fused by the decoder and treated as one after that. Using Intel's terms, RMW-type instructions are indeed always handled internally using micro-fusion.
- deleted 9y ago[deleted]
- BeeOnRope 9y agoCareful with the word "unlamination", since it is usually referring to a different thing than the usual splitting up of micro-fused ops at execution. As far as micro-fusion, there are at at least three interesting possibilities for the uops originating from the same instruction (with a memory operand): 1) No micro-fusion at all, e.g., for shrx and friends. The number of fused domain and unfused domain uops is the same. 2) Fusion in the uop cache (and probably the IDQ), but not the RAT and later stages. This is a case where the instructions are fused at decode, but are "unlaminated" before rename, and hence it usually has similar performance to no fusion at all (but it does save space in the uop cache), since RAT is more likely to be a performance limitation. If you look at the performance counters to determine fusion, this will look like case (1) (unless you check the DSB uops delivered one, I guess). 3) Full micro-fusion. This is the "normal" micro-fusion that people usually talk about: fusion persists through the RAT and get split up for execution. As far as I know, "unlamination" refers to the process that distinguishes between type (2) and type (3) - the former are unlaminated and the latter aren't. The existence of type (2) isn't even really clear from the classic sources like Agner's doc, and the behavior has changed even on recent archs (e.g., Skylake unlaminates less scnearios than Haswell). All the gory details are here: https://stackoverflow.com/q/26046634/149138 https://stackoverflow.com/q/26046634/149138 About RMW ops, it usually ends up as 4 unfused uops (store data, store AGU, load, ALU) which get fused into two micro-fused pairs (the store, and the load-ALU) op, not fully fused into one uop. The same concerns about type (2) vs type (3) likely apply also, although I haven't tested it.
- bringtheaction 9y ago> Anger Fog Probably it was due to autocorrect but I chuckled at the misspelling of Agner nonetheless.
- BeeOnRope 9y agoYeah, but the main reason the RMW is better (in a tight loop like this) is because of the available fusion for the load-op part of the RMW. So it is about micro-fusion in that way. The confusing part is that he mentions a performance different up front, so you'd expect this to be about how the micro-fused version was faster: but that difference was actually because of alignment probably causing a lack of macro-fusion in some cases (or some other alignment effect). The two versions have different alignment, but it's basically just a fluke which one happens to be faster, it could have been the other way around with some unrelated changes to other code that shifted the alignment.