[PATCH] [complex] Teach Clang to preserve different-type operands to arithmeticoperators where one type is a C complex type, and to emit both the efficientand correct implementation for complex arithmetic according to C11 AnnexG using this extra...

Chandler Carruth chandlerc at gmail.com
Thu Oct 9 03:33:41 PDT 2014


A couple of other points on this patch.

- We could get a lot more out of these kinds of simplification, and
generally improve the sanity of Clang's model here, by adding support for
the C11 Imaginary type and using that to build up things. But this is more
work than I can sign up for right now, and I'm not the best person to do it.

- You might wonder why not add a complex type to the IR so that the
properties here are exposed at that level. There are some compelling
reasons not to do this IMO.
  - Would need to "lower" this at some point which would be ugly. No chip
(that LLVM supports) has complex registers and arithmetic operations....
  - Incredibly hard to change the IR in general, so bad cost/benefit
tradeoff.

- While this patch handles the simplifications to complex arithmetic we can
do when the typesystem itself shows one operand to be a real, it doesn't
handle large other categories such as when inlining or other optimizations
propagate away the imaginary part for example.
  - All of these should be easily caught by a target library optimization
  - I'll work on just such a target library pass to help polish off the
last few missed optimzations.

- This has uncovered some nasty behavior with SROA. Need to fix that.....

On Thu, Oct 9, 2014 at 3:16 AM, Chandler Carruth <chandlerc at gmail.com>
wrote:

> Hi hfinkel, scanon, resistor,
>
> ...information.
>
> For both multiply and divide the old code was writing a long-hand
> reduced version of the math without any of the special handling of inf
> and NaN recommended by the standard here. Instead of putting more
> complexity here, this change does what GCC does which is to emit
> a libcall for the fully general case.
>
> However, the old code also failed to do the proper minimization of the
> set of operations when there was a mixed complex and real operation. In
> those cases, C provides a spec for much more minimal operations that are
> valid. Clang now emits the exact suggested operations. This change isn't
> *just* about performance though, without minimizing these operations, we
> again lose the correct handling of infinities and NaNs. It is critical
> that this happen in the frontend based on assymetric type operands to
> complex math operations.
>
> The performance implications of this change aren't trivial either. I've
> run a set of benchmarks in Eigen, an open source mathematics library
> that makes heavy use of complex. While a few have slowed down due to the
> libcall being introduce, most sped up and some by a huge amount.
>
> TODO: In order to make all of this work, also match the algorithm in the
> constant evaluator to the one in the runtime library. Currently it is a
> broken
> port of the simplifications from C's Annex G to the long-hand formulation
> of
> the algorithm.
>
> Splitting this patch up is very hard because none of this works without
> the AST change to preserve non-complex operands. Sorry for the enormous
> change.
>
> http://reviews.llvm.org/D5698
>
> Files:
>   lib/AST/ExprConstant.cpp
>   lib/CodeGen/CGExprComplex.cpp
>   lib/Sema/SemaExpr.cpp
>   test/CodeGen/complex-math.c
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141009/b60263b7/attachment.html>


More information about the cfe-commits mailing list