Say I had two functions called one after the other:

A1, A2 : vector of doubles
B1, B2 : vector of ints
f = dot product on ints
g = dot product on doubles

f(A1, A2)
g(B1, B2)

Does there exist a compiler optimization which could merge the dot product implementations for f and g such that you would get the following code(in psuedo-assembly):

mov F3 , #0
fetch from A1 into F1
fetch from A2 into F2
doublemulacum F1 F2 F3
fetch from B1 into R1
fetch from B2 into R2
integermulacum R1 R2 F3
branch if end of A1 and B1 not reached
# then branch to a cleanup implementation which completes the dot product for the longer vectors.

The advantage of this over generating two dot product calls, would be the ability to execute integer and double multiplications concurrently as well as being able to fetch concurrently, and get similar peformance to running the integer dot product on one SMT thread, and the double dot product on another SMT thread.

More generally is it possible to do something like this for two arbitrary (pure) functions, or would the cleanup logic for when one function finishes before the other be to complex?

  • 1
    $\begingroup$ It’s called loop fusing. Obviously the two calls must be unlined first. And the functions need not be pure, just independent. $\endgroup$
    – gnasher729
    Nov 28, 2019 at 15:41


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy