Optimization of complex number division

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 and ComplexToStandard passes have been modified to implement two transformation algorithms, smith with NaN handling and algebraic, allowing for their selection.
  • Added a pass option complex-range=XXXXX to both passes.
    • Possible values are improved, basic, none.
    • By default, ComplexToStandard converts to smith , while ComplexToLLVM converts to algebraic (with a future plan to unify both to smith ).
    • If the option value is improved, it converts to smith.
    • If the value is none or basic, it converts to algebraic.

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 the smith 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 the smith 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.