4 ms·
Intel says that: > Micro-fusion fuses multiple micro-ops from the same instruction into a single complex micro-op. And that: > Macro-fusion merges two instru
by chrisseaton 9y ago
Intel 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.
- BeeOnRope 9y agoAt least on Skylake the "retired uops" counters are counted in the "unfused" domain (i.e., an instruction that turns into 2 uops that get micro-fused counts as 2 not 1). I believe earlier archs had a similar counter with the opposite behavior (i.e., they counted in the unfused domain). Micro-fusion can happen only for stores, or when instructions have at least one memory source argument. On x86 this means either load-op instructions, which have a memory _source_ argument like: add eax, [rdx] ... or the so-called RMW instructions, which use memory as source and destination (sometimes with another non-memory source as in the first example) like: add [rdx], 1 ; memory source/dest, immediate 2nd source dec [rdx] ; memory source/dest only In these cases fusion happens because the memory access uop(s) can be grouped together with the ALU op, because the memory source is "anonymous" (doesn't get put into a visible register, so no-one can later use it - meaning it doesn't need a named register, hence renaming resources). The decomposition of the first add example into separate instructions looks like: mov ecx, [rdx] add eax, ecx This doesn't fit the pattern above so can't be micro-fused. in particular, the use of ecx as a temporary register here means you couldn't really fuse it: the value in ecx is live after this code segment, so it needs a named physical register. The third case of fusion is stores: mov [rdx], eax Internally this uses two uops (one address-generation and one store-data), but they are generally fused so it executes as one uop in the fused domain. This isn't super interesting since there aren't really alternatives for stores, they always have the above form, so micro-fusion is kind of less interesting (unlike the load-op ALU stuff, where you could use memory source, or not). Finally we get to RMW. This is logically a load-op combined with a store. So you have 4 unfused uops in total! Here we potentially get both types of fusion: the load-op fusion, and the store fusion, reducing the number of fused uops to 2. So to actually answer your question: if you implement a RMW has a separate load, ALU op, store like: ; instead of dec DWORD [rdx] mov eax, [rdx] dec eax mov [rdx], eax You would lose _half_ of the available fusion: the load-op part is now two instructions, so you don't get the fusion there, but you still get the store fusion. So you have 3 fused-domain uops and 4 unfused, versus 2 and 4 for the RMW case. On the other hand, for cases where you aren't writing the result back immediately to the same memory location, RMW doesn't apply, and you can get maximum fusion by using the "load-op" for of the instruction.
- deleted 9y ago[deleted]
- 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.