Skip to content

Commit

Permalink
Don't crash when constant folding an integer division by zero.
Browse files Browse the repository at this point in the history
Signed-off-by: Tiago Trevisan Jost <tiago.trevisanjost@amd.com>
Co-authored-by: Matthias Gehre <matthias.gehre@amd.com>
  • Loading branch information
mgehre-amd authored and ttjost committed Jul 3, 2024
1 parent 2cec11f commit a971fda
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Dialect/ONNX/Transforms/ConstProp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,16 @@ struct ElementWiseBinaryOpImpl<ONNXMulOp, T, EnableNotBool<T>> {

template <typename T>
struct ElementWiseBinaryOpImpl<ONNXDivOp, T, EnableNotBool<T>> {
static T eval(T lhs, T rhs) { return lhs / rhs; }
static T eval(T lhs, T rhs) {
if constexpr (std::is_integral_v<T>) {
if (rhs == 0) {
// Undefined behavior. We can return any value.
// Performing the divison would crash.
return lhs;
}
}
return lhs / rhs;
}
};

template <typename T>
Expand Down

0 comments on commit a971fda

Please sign in to comment.