Fix FXC compile errors on divide by zero

FXC fails to compile when it determines that the rhs of an integral
division is zero with "error X4010: Unsigned integer divide by zero".

bclayton's fix (https://dawn-review.googlesource.com/c/tint/+/60500)
addressed cases for division by an integer constant 0. This CL adds the
missing support for division by integral vectors with 0 components.

FXC also fails on division by integral expressions that it can fold to
0. To handle these cases, we now emit a runtime check for 0 and replace
by 1. In the cases I've tested, FXC seems able to optimize these checks
away.

Bug: tint:1083
Change-Id: I02f08e9077882f03c1e42b62dacb742a48fa48ba
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/73580
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: David Neto <dneto@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
Antonio Maiorano
2021-12-21 14:48:26 +00:00
committed by Tint LUCI CQ
parent 5965c6ed1f
commit 821f9bb525
187 changed files with 2691 additions and 40 deletions

View File

@@ -14,6 +14,7 @@
#include "src/sem/constant.h"
#include <functional>
#include <utility>
#include "src/debug.h"
@@ -61,5 +62,23 @@ Constant::~Constant() = default;
Constant& Constant::operator=(const Constant& rhs) = default;
bool Constant::AnyZero() const {
for (size_t i = 0; i < Elements().size(); ++i) {
if (WithScalarAt(i, [&](auto&& s) {
// Use std::equal_to to work around -Wfloat-equal warnings
auto equals_to =
std::equal_to<std::remove_reference_t<decltype(s)>>{};
if (equals_to(s, 0)) {
return true;
}
return false;
})) {
return true;
}
}
return false;
}
} // namespace sem
} // namespace tint

View File

@@ -97,6 +97,9 @@ class Constant {
/// @returns the constant's scalar elements
const Scalars& Elements() const { return elems_; }
/// @returns true if any scalar element is zero
bool AnyZero() const;
/// Calls `func(s)` with s being the current scalar value at `index`.
/// `func` is typically a lambda of the form '[](auto&& s)'.
/// @param index the index of the scalar value