Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UB in hannk FillWithRandom operation. #6645

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions apps/hannk/util/buffer_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <iostream>
#include <random>
#include <type_traits>

#include "HalideBuffer.h"
#include "HalideRuntime.h"
Expand Down Expand Up @@ -191,11 +192,13 @@ struct FillWithRandom {

private:
inline static void fill_with_random_impl(HalideBuffer<T> &b, std::mt19937 &rng) {
std::uniform_int_distribution<T> dis(std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
b.for_each_value([&rng, &dis](T &value) {
value = dis(rng);
});
// std::uniform_int_distribution<T> is undefined behaviour when T is not
// one of [short, int, long, long long], or their respective unsigned variants.
// (https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution)
typedef typename std::conditional<sizeof(T) >= sizeof(int16_t), T, std::int16_t>::type rand_type;
std::uniform_int_distribution<rand_type> dis(std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
b.for_each_value([&rng, &dis](T &value) { value = dis(rng); });
}
};

Expand Down