Skip to content

Use nullptr to represent fallback kernels #11484

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

Merged
merged 1 commit into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions runtime/kernel/operator_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ constexpr uint32_t kMaxRegisteredKernels = kMaxOperators * kMaxKernelsPerOp;
// require constructing them at init time. Since we don't care about the values
// until we add each entry to the table, allocate static zeroed memory instead
// and point the table at it.
struct alignas(Kernel) KernelBuffer {
uint8_t data[sizeof(Kernel)];
};

// @lint-ignore CLANGTIDY facebook-hte-CArray
alignas(sizeof(Kernel)) uint8_t
registered_kernels_data[kMaxRegisteredKernels * sizeof(Kernel)];
KernelBuffer registered_kernels_data[kMaxRegisteredKernels];

/// Global table of registered kernels.
Kernel* registered_kernels = reinterpret_cast<Kernel*>(registered_kernels_data);
Expand Down
11 changes: 5 additions & 6 deletions runtime/kernel/operator_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ struct KernelKey {
* for all input tensor dtypes and dim orders if the specialized kernel is not
* registered.
*/
KernelKey() : is_fallback_(true) {}
KernelKey() = default;

/**
* Creates a specialized (non-fallback) kernel key that matches a specific
* set of input tensor dtypes and dim orders. See the class comment for the
* expected format of `kernel_key_data`.
*/
/* implicit */ KernelKey(const char* kernel_key_data)
: kernel_key_data_(kernel_key_data), is_fallback_(false) {}
: kernel_key_data_(kernel_key_data) {}

bool operator==(const KernelKey& other) const {
return this->equals(other);
Expand All @@ -142,17 +142,17 @@ struct KernelKey {
}

bool equals(const KernelKey& other) const {
if (is_fallback_ != other.is_fallback_) {
if (is_fallback() != other.is_fallback()) {
return false;
}
if (is_fallback_) {
if (is_fallback()) {
return true;
}
return strcmp(kernel_key_data_, other.kernel_key_data_) == 0;
}

bool is_fallback() const {
return is_fallback_;
return kernel_key_data_ == nullptr;
}

const char* data() const {
Expand All @@ -168,7 +168,6 @@ struct KernelKey {

private:
const char* kernel_key_data_ = nullptr;
bool is_fallback_;
};

/**
Expand Down
Loading