[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:16:09 PDT 2014


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5698.14644.patch
Type: text/x-patch
Size: 33644 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141009/4ee56d9c/attachment.bin>


More information about the cfe-commits mailing list