Defining Safe Behavior in Clad-Generated Functions
Introduction
This project focuses on improving the safety and reliability of functions generated by Clad, an automatic differentiation tool built on top of LLVM and Clang. In certain cases, these generated functions may produce undefined behavior when given inputs outside their valid domains. The goal is to introduce a mechanism that ensures predictable and well-defined behavior in such situations.
Mentor: Vassil Vassilev
Overview
Clad generates derivative functions automatically from user-written C++ code. While this provides powerful capabilities for scientific computing and optimization, it also introduces potential issues when mathematical functions are evaluated outside their valid domains.
For example, operations such as taking the logarithm of a non-positive number or dividing by zero can lead to undefined behavior, which may result in incorrect outputs or program crashes. These issues are often difficult to detect and debug.
This project introduces an optional safety mechanism using a compile-time flag (CLAD_SAFE_MATH) that enables boundary checks in generated functions. When enabled, the system ensures that invalid inputs produce well-defined results such as NaN instead of causing unpredictable behavior. This allows developers to debug issues more easily while preserving performance when safety checks are disabled.
Goals
The goal of this project is to improve the robustness of Clad-generated functions while maintaining flexibility for performance-critical applications. The main steps include:
- Identify mathematical operations and generated code paths that can lead to undefined behavior.
- Implement boundary checks for functions such as logarithmic, root, and division operations.
- Introduce an optional safe math mode using
CLAD_SAFE_MATHand conditional compilation. - Add debugging support to help detect when invalid inputs occur.
- Evaluate the performance impact of safety checks and ensure minimal overhead when disabled.