<commit_before>#include "runtime_internal.h"
#include "HalideRuntime.h"

namespace {
struct old_dev_wrapper {
    uint64_t device;
    const halide_device_interface_t *interface;
};
}

extern "C" {

WEAK int halide_upgrade_buffer_t(void *user_context, const char *name,
                                 const buffer_t *old_buf, halide_buffer_t *new_buf) {
    new_buf->host = old_buf->host;
    if (old_buf->dev) {
        old_dev_wrapper *wrapper = (old_dev_wrapper *)(old_buf->dev);
        new_buf->device = wrapper->device;
        new_buf->device_interface = wrapper->interface;
    }
    for (int i = 0; i < new_buf->dimensions; i++) {
        new_buf->dim[i].min = old_buf->min[i];
        new_buf->dim[i].extent = old_buf->extent[i];
        new_buf->dim[i].stride = old_buf->stride[i];
    }
    new_buf->flags = 0;
    new_buf->device = 0;
    new_buf->device_interface = NULL;
    new_buf->set_host_dirty(old_buf->host_dirty);
    new_buf->set_device_dirty(old_buf->dev_dirty);
    return 0;
}

WEAK int halide_downgrade_buffer_t(void *user_context, const char *name,<commit_msg>Check elem_size on old buffer being upgraded<commit_after>#include "runtime_internal.h"
#include "HalideRuntime.h"
#include "printer.h"

namespace {
struct old_dev_wrapper {
    uint64_t device;
    const halide_device_interface_t *interface;
};
}

extern "C" {

WEAK int halide_upgrade_buffer_t(void *user_context, const char *name,
                                 const buffer_t *old_buf, halide_buffer_t *new_buf) {
    if ((old_buf->host || old_buf->dev) &&
        (old_buf->elem_size != new_buf->type.bytes())) {
        // Unless we're doing a bounds query, we expect the elem_size to match the type.
        char err[256];
        stringstream sstr(user_context);
        sstr << "buffer has incorrect elem_size (" << old_buf->elem_size << ") "
             << "for expected type (" << new_buf->type << ")";
        return halide_error_failed_to_upgrade_buffer_t(user_context, name, sstr.str());
    }
    new_buf->host = old_buf->host;
    if (old_buf->dev) {
        old_dev_wrapper *wrapper = (old_dev_wrapper *)(old_buf->dev);
        new_buf->device = wrapper->device;
        new_buf->device_interface = wrapper->interface;
    }
    for (int i = 0; i < new_buf->dimensions; i++) {
        new_buf->dim[i].min = old_buf->min[i];
        new_buf->dim[i].extent = old_buf->extent[i];
        new_buf->dim[i].stride = old_buf->stride[i];
    }
    new_buf->flags = 0;
    new_buf->device = 0;
    new_buf->device_interface = NULL;
    new_buf->set_host_dirty(old_buf->host_dirty);
    new_buf->set_device_dirty(old_buf->dev_dirty);
    return 0;
}

WEAK int halide_downgrade_buffer_t(void *user_context, const char *name,
{'fraction_non_alphanumeric': 0.1340433345574734, 'fraction_numerical': 0.005508630187293426, 'mean_word_length': 3.1779141104294477, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4305476', 'n_tokens_mistral': 967, 'n_tokens_neox': 947, 'n_words': 180}