I posted a patch to MLIR that allows selecting the algorithm for complex number division via a pass option. The patch is here, and the discussion on the MLIR is here.
Here’s a summary of the MLIR patch:
- Both the
ComplexToLLVM
andComplexToStandard
passes have been modified to implement two transformation algorithms,smith
with NaN handling andalgebraic
, allowing for their selection. - Added a pass option
complex-range=XXXXX
to both passes.- Possible values are
improved
,basic
,none
. - By default,
ComplexToStandard
converts tosmith
, whileComplexToLLVM
converts toalgebraic
(with a future plan to unify both tosmith
). - If the option value is
improved
, it converts tosmith
. - If the value is
none
orbasic
, it converts toalgebraic
.
- Possible values are
Based on this, I want to add options to the Flang frontend to control complex number division. But I have the following questions:
- Is it necessary for Fortran to switch division algorithms per operation or within a translation unit, such as during inlining? If so, my current MLIR patch, which uses a pass option to specify the algorithm for all operations, cannot handle this.
- GCC and Clang have the option
-fcx-fortran-rules
. What are the rules for complex number division that Fortran should satisfy? I suspect it refers to range reduction and NaN handling, but I don’t know the basis for this. My MLIR patch uses thesmith
algorithm with NaN handling via-complex-range=improved
. Would this be a suitable algorithm when-fcx-fortran-rules
is specified? It seems that Clang uses thesmith
algorithm without NaN handling when-fcx-fortran-rules
is specified.
I believe Flang needs to implement -f[no-]cx-limited-range
, -f[no-]cx-fortran-rules
, and -fcomplex-arithmetic=[full|improved|basic]
.
Any feedback would be greatly appreciated.