-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[Flang] Canonicalize divdc3 calls into arithmetic-based complex division #146017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[Flang] Canonicalize divdc3 calls into arithmetic-based complex division #146017
Conversation
Co-Authored-By: ict-ql <168183727+ict-ql@users.noreply.github.com> Co-Authored-By: Chyaka <52224511+liliumshade@users.noreply.github.com>
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
SPEC2017 benchmark results have been shared in comments below:
|
Thank you for working on this! Actually, we've been discussing a similar feature on Discourse. You can refer to it here if you're interested: (https://discourse.llvm.org/t/optimization-of-complex-number-division/83468) I'm currently working on a feature based on the discussion at the end of that thread, and I plan to post the patch next week. In that patch, specifying the newly added driver option |
Thank you for the heads-up, and it’s great to hear that we’re thinking along the same lines—seems like a case of great minds thinking alike! It’s truly sweet to see that others have been independently thinking along similar lines—clearly, this is an area worth optimizing. I found the discussion quite illuminating and it actually informed some aspects of the design in my implementation. I’ve recently finished implementing and submitted a patch that addresses this very optimization. It supports expanding complex division algebraically without relying on the runtime function, and it could potentially serve the same goal as the one you’re working on. Of course, I’d be happy to hear your thoughts or suggestions—collaboration always leads to better outcomes. But perhaps this patch could save you some effort and serve as a starting point or even a complete solution. Thanks again for the discussion link, I’d be very glad to receive any suggestions or feedback. I really appreciate the collaborative spirit in this community! Have a wonderful day :) |
Hi @jeanPerier, this PR implements a new MLIR-based frontend pass in Flang to optimize complex division. As an external contributor, I am unable to assign reviewers directly. Would you be willing to review this change when you have the chance? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using the complex dialect directly? There has been discussion on the discourse about that.
Hi @Hanwen-ece, thanks for the patch! I do have a preference using and improving mlir |
This patch introduces a new MLIR-based frontend pass in Flang to optimize complex division by rewriting calls to __divdc3 into arithmetic-based FIR operations. The pass uses the complex division formula
(a + bi)/(c + di) = ((ac + bd)/(c² + d²)) + ((bc - ad)/(c² + d²))i
, eliminating external library calls and enabling further optimization in the LLVM pipeline.Motivation
The
__divdc3
helper function is typically used to perform double-precision complex division. However, such calls can inhibit optimization and inline opportunities. By rewriting these calls into raw floating-point operations (arith
dialect), we enable better mid-level IR analysis, improve code generation, and reduce runtime dependencies.Implementation
A new
OpRewritePattern<fir::CallOp>
match is added to detect calls to__divdc3
with four floating-point operands representing the real and imaginary parts of the numerator and denominator. The transformation applies the standard complex division formula:(a + bi)/(c + di) = ((ac + bd)/(c² + d²)) + ((bc - ad)/(c² + d²))i
This is implemented using the
arith.{MulFOp, AddFOp, SubFOp, DivFOp}
ops, followed by construction of the complex result viafir.insert_value
.Key details
-mllvm --flang-complex-div-converter
is enabled (disabled by default)fir.undef
+fir.insert_value
to build the resultTests
__divdc3
are replaced by explicit arithmetic in the IRFuture Work
__divsc3
,__divxc3
, and other complex intrinsics calls__muldc3
,__adddc3
, etc.Authors
The XSCC compiler team developed this implementation.