[cfe-dev] Optimization of arithmetic expressions involving user-defined C++ objects

ardi via cfe-dev cfe-dev at lists.llvm.org
Sat Apr 18 06:49:21 PDT 2020


Thanks a lot for confirming my fears, Michael. Let me add a vote for
adding support for arithmetic expression optimization with generic
user-defined types. Granted, as you mention, there's no C++ API
available for defining operators in such a way that compilers could
have hints for optimization opportunities.

As an alternative, I just realized that some expression parsers can
optimize arithmetic expressions for speed (like ExprTk by Arash Partow
https://github.com/ArashPartow/exprtk , which, according to the
documentation, supports "optimisations: constant-folding, simple
strength reduction and dead code elimination" ).

Unfortunately, I don't know of any standard way of making the C++
preprocessor invoke an external tool for transforming the code before
compiling (of course I can write a custom preprocessor that optimizes
expressions before invoking the C++ preprocessor, but it would be much
cleaner if the expression optimizer could be run transparently by the
standard preprocessor...).

Kind regards and thanks a lot!


On Sat, Apr 18, 2020 at 5:11 AM Michael Kruse <llvm at meinersbur.de> wrote:
>
> LLVM has its predetermined set of types of instructions
> (https://llvm.org/docs/LangRef.html) and optimizations look for
> specific instructions that it can be applied to, instead of its
> properties. Hence, it is not possible to apply the same optimization
> on user-implemented types and operations.
>
> This might change with MLIR which has extendable types and operations
> and optimizations might be more guided by an operation property. For
> instance, there is mlir::OperationProperty::Commutative so
> optimization passes use this property instead of just knowing all the
> instructions that are commutative.
>
> Sadly, commutativity is (currently) the only semantic property of
> mlir::OperationPropery.
>
> Another item to consider is, once you implemented a custom type and
> operators in C++, how does the compiler know about its properties.
> There is no C++ API to make the compiler know.
>
> Michael
>


More information about the cfe-dev mailing list