symforce.codegen.backends.cpp.cpp_config module#

class CppConfig(doc_comment_line_prefix=' * ', line_length=100, use_eigen_types=True, render_template_config=<factory>, cse_optimizations=None, zero_epsilon_behavior=<factory>, normalize_results=True, support_complex=False, force_no_inline=False, zero_initialization_sparsity_threshold=0.5, explicit_template_instantiation_types=None, override_methods=None, extra_imports=None, databuffer_type=None)[source]#

Bases: CodegenConfig

Code generation config for the C++ backend.

Parameters:
  • doc_comment_line_prefix (str) – Prefix applied to each line in a docstring

  • line_length (int) – Maximum allowed line length in docstrings; used for formatting docstrings.

  • use_eigen_types (bool) – Use eigen_lcm types for vectors instead of lists

  • autoformat – Run a code formatter on the generated code

  • custom_preamble – An optional string to be prepended on the front of the rendered template

  • cse_optimizations (Literal['basic'] | ~typing.Sequence[~typing.Tuple[~typing.Callable, ~typing.Callable]] | None) – Optimizations argument to pass to sf.cse

  • zero_epsilon_behavior (ZeroEpsilonBehavior) – What should codegen do if a default epsilon is not set?

  • normalize_results (bool) – Should function outputs be explicitly projected onto the manifold before returning?

  • support_complex (bool) – Generate code that can work with std::complex or with regular float types

  • force_no_inline (bool) – Mark generated functions as __attribute__((noinline))

  • zero_initialization_sparsity_threshold (float) – Threshold between 0 and 1 for the sparsity below which we’ll initialize an output matrix to 0, so we don’t have to generate a line to set each zero element to 0 individually

  • explicit_template_instantiation_types (Sequence[str] | None) – Explicity instantiates templated functions in a .cc file for each given type. This allows the generated function to be compiled in its own translation unit. Useful for large functions which take a long time to compile

  • override_methods (Dict[Function, str] | None) – Add special function overrides in dictionary with symforce function keys (e.g. sympy.sin) and a string for the new method (e.g. "fast_math::sin_lut"), note that this bypasses the default namespace (so std:: won’t be added in front automatically). Note that the keys here need to be sympy keys, not symengine (e.g. sympy.sin NOT sf.sin with the symengine backend). SymEngine to SymPy conversion does not work for Function types. Note that this function works in the code printer, and should only be used for replacing functions that compute the same thing but in a different way, e.g. replacing sin with my_lib::sin. It should not be used for substituting a function with a different function, which will break derivatives and certain simplifications, e.g. you should not use this to replace sin with cos or sin with my_lib::cos.

  • extra_imports (List[str] | None) – Add extra imports to the file if you use custom overrides for some functions (e.g. add fast_math.h). Note that these are only added on a call to generate_function, i.e. you can’t define custom functions in e.g. the geo package using this

  • databuffer_type (str | None) – Changes the type of any DataBuffers to the given type instead of using the default Scalar type. Useful for cases where DataBuffers have a different type than other arguments to the generated function.

  • render_template_config (RenderTemplateConfig) –

doc_comment_line_prefix: str = ' * '#
line_length: int = 100#
use_eigen_types: bool = True#
support_complex: bool = False#
force_no_inline: bool = False#
zero_initialization_sparsity_threshold: float = 0.5#
explicit_template_instantiation_types: Sequence[str] | None = None#
override_methods: Dict[Function, str] | None = None#
extra_imports: List[str] | None = None#
databuffer_type: str | None = None#
classmethod backend_name()[source]#

String name for the backend. This should match the directory name in codegen/backends and will be used to namespace by backend in generated code.

Return type:

str

classmethod template_dir()[source]#

Directory for jinja templates.

Return type:

Path

templates_to_render(generated_file_name)[source]#

Given a single symbolic function’s filename, provide one or more Jinja templates to render and the relative output paths where they should go.

Parameters:

generated_file_name (str) –

Return type:

List[Tuple[str, str]]

printer()[source]#

Return an instance of the code printer to use for this language.

Return type:

CodePrinter

static format_data_accessor(prefix, index)[source]#

Format data for accessing a data array in code.

Parameters:
  • prefix (str) –

  • index (int) –

Return type:

str

format_matrix_accessor(key, i, j, *, shape)[source]#

Format accessor for 2D matrices.

Raises an index exception if either of the following is false:

0 <= i < shape[0]
0 <= j < shape[1]
Parameters:
Return type:

str

static format_eigen_lcm_accessor(key, i)[source]#

Format accessor for eigen_lcm types.

Parameters:
Return type:

str