Commit Graph

13 Commits

Author SHA1 Message Date
Darius Rueckert 9049688c60 Default Initialize ExpressionRef to Zero
The default constructor of ExpressionRef now creates a compile
time constant zero assignment. This patch is required, because
a reason change in Jet assumes default 0 initialization.

Change-Id: I8185cef587c17ab828896bce1e768170f8229d4e
2020-03-10 09:30:34 +01:00
Darius Rueckert 13c7a22cee Codegen Optimizer API
This patch adds the classes Optimizer and OptimizationPass, which
define the core API of the codegen optimization.

A single (trivial) optimization is added that removes NOP
expressions from a graph.

Change-Id: I0430d6ffc61a474f997475ff787a81a9c69cfe23
2020-02-18 10:50:10 +01:00
Darius Rueckert d82de91b88 Add ExpressionGraph::Erase(ExpressionId)
Add the function ExpressionGraph::Erase and a test-case for it.
Erase removes the given expression from the graph by shifting
all later expressions to the front. Indices and references
are updated accordingly.

Change-Id: Ic0449ccf28b369600fd2959a7e2a919d47f4cbe3
2020-02-10 16:19:55 +01:00
Darius Rueckert c8e35e19fd Add namespaces to generated functions and constants
The generated function names now include the containing namespace.
For example:
	std::abs(...)
	std::sin(...)
	ceres::Ternary(...)

This patch also fixes the generation of inf/nan compile time constants,
using std::numeric_limits.

Change-Id: I4a36b09c68dd2adabed49fd4f7f37c8229ab7377
2020-02-07 14:51:18 +01:00
Darius Rueckert 8def196166 Remove ExpressionRef Move Constructor
The move constructor and move =operator are not required. They make
the code more complex and prone to bugs. The few saved assignments
are all trivial and are optimized away by the compiler or our
optimizer.

In fact, there is a bug in the current move-constructor implementation
that occurs, for example, when moving Eigen matrices around.

Change-Id: I013796495bb39f3f27677111bd0aaf49e2454e20
2020-01-14 17:00:31 +01:00
Darius Rueckert fdf9cfd320 Add functions to find the matching ELSE, ENDIF expressions
ExpressionId FindMatchingEndif(ExpressionId id)
   - Finds the closing ENDIF expression for a given IF expression.

ExpressionId FindMatchingElse(ExpressionId id)
   - Finds the ELSE expression for a given IF expression.
   - Returns -1, if this IF doesn't have an else

This patch is a preparation to the code analyzing required
for various optimization passes.

Change-Id: I893102a757c6a0bcacbcc1190c2b8ef08314eb97
2020-01-07 09:35:09 +01:00
Darius Rueckert a60136b7aa Add COMMENT ExpressionType
The comment expression allows the user to insert single-line
comments into the generated code. These will not be moved or
optimized away.

Comment expressions should make it easier to understand the
generated autodiff code.

Change-Id: Ie87d0698aa6af2aac8a437a13b25e0fef33dbfdc
2019-12-18 09:44:33 +01:00
Darius Rueckert edd54b83e1 Update Jet.h and rotation.h to use the new IF/ELSE macros
Also use branchless implementation for isfinite(Jet),
isinf(Jet), isnan(Jet), and isnormal(Jet).

Change-Id: Ia881df03ba873e0560d67e976ab1e99e199eb523
2019-12-09 18:30:30 +01:00
Darius Rueckert 848c1f90c0 Update return type in code generator and add tests for logical functions
Change-Id: I128f8ccc21c2c3a2c79674ac28dd9a5b26b58772
2019-12-09 17:57:10 +01:00
Darius Rueckert 5010421bb7 Add the expression return type as a member to Expression
Before this patch the return type was implicitly defined by the
ExpressionType. This patch separates this connection and allows
each Expression to have one of the predefined types (scalar,
boolean, void).

This patch is required to add support for the functions isfinite,
isinf, isnan, and isnormal. These are function taking a double and
returning a bool.

This also moves some complexity of the code generator to the
Expression, because the generator can direclty get the c++ type.

Change-Id: I8b32bab1bfab2f668875e506d6f3b789a5d1f3fd
2019-12-09 17:13:05 +01:00
Darius Rueckert f4dc670ee8 Improve testing of the codegen system
- Move codegen tests to a sub directory
- Add tests for all functions of Expression, ExpressionRef, and ExpressionGraph
- Respect dependencies during tests: The ExpressionGraph test doesn't
  use ExpressionRef anymore.

The new tests revealed a few bugs so the following changes were made:

- Expression::MakeNop now resets the current expression with the default
  constructed NOP expression
- ExpressionGraph::Insert now updates the lhs_id the same way as
  InsertBack()

Change-Id: I6a18925c1e4d972c29ec1219f2073b4eaf2df737
2019-12-09 15:13:07 +01:00
Darius Rueckert 572ec4a5a5 Rework Expression creation and insertion
Objects of 'Expression' can now be freely created and copied around.
This creation does NOT add them to the active ExpressionGraph any
more. The insertion into the graph is now done by ExpressionRef,
which explicitly call graph.add(...) in each operation.

This change brings the following advantages:

1. 'Expression' is now stand-alone and side-effect free
   - Remove the dependency Expression->ExpressionGraph
   - Expressions can be created by the optimizer

2. Explicit graph insertion
  - Previously CreateCompileTimeConstant not only created an
    expression, but also inserted it into the active graph. Now
    this insertion is done explicitly by ExpressionRef

3. It is now easier to insert new types and members
  - Should be straight forward now, because we got rid of the
    3-way dependency
  - This is a preparation patch for the new return-type member in
    https://ceres-solver-review.googlesource.com/c/ceres-solver/+/16224

Change-Id: Icef2fe529a4db001a10d1fb6816c9dc681b14ff2
2019-12-06 13:35:21 +01:00
Darius Rueckert d1703db45a Moved AutoDiffCodeGen macros to a separate (public) header
User defined cost functors have to include this new header
instead of internal/expression_ref.h. This hides some
complexity of ExpressionRef and reduces compile time outside
of code generation mode.

This patch also removes the dependency ExpressionRef->Jet.

Change-Id: Ie3f93648775e14881dc5cfab213bbc983c6cfeee
2019-12-02 16:17:19 +01:00