Article View: comp.arch
Article #1041Re: hardware reassociation (was: reassociation and undefined behaviour)
From: Anton Ertl
Date: Thu, 03 Dec 2026 07:40
Date: Thu, 03 Dec 2026 07:40
83 lines
4990 bytes
4990 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes: >anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > >> MitchAlsup <user5857@newsgrouper.org.invalid> writes: >> >anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: >> >> In any case, the best association depends on the latencies of how the >> >> operands are computed. If I don't know that, I would go for >> >> (a+b)+(c+d), where the latencies are the same for all operands. >> > >> >This is an argument that HW (not SW) should be the one to reassociate. >> >> Yes. But how to do it? > >Micro-fault > >> One problem with register machines is that you usually don't know if a >> register actually lives after the register has been read. So if the >> hardware sees >> >> t1=a+b >> t2=c+d >> t3=t1+t2 >> >> ... it does not know that t1 and t2 are dead and the whole thing can >> be reassociated. But if we limit the reassociation to sequences of >> >> t = a+b >> t += c >> t += d > >Do them like this: and if there is no Overflow: its golden. If there >is OF then backup and do them the slow way. HW can do this, SW cannot. My thinking was about addition with modulo arithmetic, and without overflow flags, i.e., like RISC-V, in an OoO machine. So: how do you implement hardware reassociation? If the ISA has overflow flags, like AMD64 and ARM A64, or also in the case of my suggested overflow-in-GPR variant, the hardware can compute (for the example above) a+b+c with arbitrary association (because any overflow is overwritten by the final addition), and then perform the t+=d. If we also want to reassociate the t+=d with the others, I see two options: 1) Look further ahead and see if there is an overflow-overwriting instruction X before the next branch or potentially-trapping instruction. If so, delay any exception handling until after X, and you can reassociate the t+=d with the rest. 2) Compute a+b+c+d (freely associated) for the result with minimum latency, and also compute (a+b+c)+d to compute the overflow flag (where the latency is probably not as critical), With overflow-in-GPRs, that's not very practical. Alternatively, you can attack this at the ISA level: use an "add without setting carry nor overflow (and maybe not the others)" instruction for the t += reg parts. That's probably the right solution for my overflow-in-GPR idea. But you are apparently thinking of add instructions that trap on overflow, like MIPS add and Alpha addv. The overflow conditions are not associative. You have to compute each addition as specified to perform exactly the architectural overflows. If you reassociate, and there is no overflow, you might have missed an overflow that occurs with the original association. However, you can do something: Compute a+b+c+d to get the register result, and compute a+b, t1+c, t2+d, with more latency to produce the architectural overflow result (latency is not that important here). And when there is an actual overflow, use the results of the latter computations. no microtrapping necessary. But given architectural trends, trapping on overflow is something of the past, so I would not invest any hardware in making it fast. These days, if you think that overflow is possible, you use Bigints, not traps. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID:
<2026Dec3.084005@mips.complang.tuwien.ac.at>Path:
mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <11emvbp$1sc68$3@dont-email.me> <2026Dec2.084013@mips.complang.tuwien.ac.at> <1796233819-5857@newsgrouper.org> <2026Dec2.190958@mips.complang.tuwien.ac.at> <1796263840-5857@newsgrouper.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu.MISMATCH!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: anton@mips.complang.tuwien.ac.at (Anton Ertl) Newsgroups: comp.arch Subject: Re: hardware reassociation (was: reassociation and undefined behaviour) Date: Thu, 03 Dec 2026 07:40:05 GMT Organization: Institut fuer Computersprachen, Technische Universitaet Wien Lines: 83 Message-ID: <2026Dec3.084005@mips.complang.tuwien.ac.at> References: <11cpqih$acf5$1@dont-email.me> <86o6b6kaqf.fsf@linuxsc.com> <11ejr5e$nk8$1@reader2.panix.com> <11ejtoe$eoj7$1@dont-email.me> <11el2c1$q43$1@reader2.panix.com> <11emcgn$1k681$1@dont-email.me> <11emvbp$1sc68$3@dont-email.me> <2026Dec2.084013@mips.complang.tuwien.ac.at> <1796233819-5857@newsgrouper.org> <2026Dec2.190958@mips.complang.tuwien.ac.at> <1796263840-5857@newsgrouper.org> Injection-Date: Thu, 03 Dec 2026 08:23:23 +0000 (UTC) Injection-Info: dont-email.me; logging-data="101203"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/h57vlElO3MoIh//pYO0ln"; posting-host="b318e488349409aaa87abf1f338dcb0c" Cancel-Lock: sha1:tgmQwpJ+Rite7YZpoU4sl25CXdg= sha256:+iQeDMHtCR5issUhjzxf2R0N+/fbLVFCvhfrKOVcB+c= sha1:vbxUl5D57xYiRKHCjWt9UUyXR8g= sha256:Ne4U1jtd69Fisj0Sm9L7ZSh4J65ItLTaVN+wJnrWah8= X-newsreader: xrn 10.11