vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.24k stars 145 forks source link

Assertion error in 'get_uptodate_def_insn' relating to division by `sizeof(char)` #393

Closed andy-hanson closed 1 month ago

andy-hanson commented 3 months ago
#include <stdio.h>

struct string { char* begin; char* end; };

unsigned long size(struct string a) {
    return (a.end - a.begin) / sizeof(char);
}

struct string2 {
    char* begin;
    unsigned long size;
};

struct string2 convert(struct string a) {
    return (struct string2) {a.begin, size(a)};
}

void main(void) {
    char c[3] = "foo";
    struct string s = {c, c + 3};
    printf("%lu\n", size(s));
}

Running c2m a.c -eg, I get:

c2m: mir-gen.c:4600: get_uptodate_def_insn: Assertion `!gen_ctx->selection_ctx->hreg_refs_addr[hr].del_p' failed.

I've done my best to reduce this error, but it seems to depend on a pretty specific scenario.

andy-hanson commented 3 months ago

Update: It's not the sizeof that's the problem, but the type of the divisor. It breaks the same if you replace sizeof(char) with 1uLL.

vnmakarov commented 1 month ago

Thank you for reporting this issue which is actually a bug in code selection pass. I've fixed it on master by b79c0ee2e919bea54ac0f877b5aba5a6cf68b69e.

andy-hanson commented 1 month ago

Thanks, I confirmed it's working now.