JuliaHubOSS / llvm-cbe

resurrected LLVM "C Backend", with improvements
Other
826 stars 141 forks source link

Handle simple manipulation of pointers to zero-element arrays (fix #128) #129

Closed hikari-no-yume closed 3 years ago

hikari-no-yume commented 3 years ago

Handle simple manipulation of pointers to zero-element arrays (fix #128)

So far, we have translated empty types (such as zero-element arrays) to C's void type. This works fine if a pointer to such a type is just an abstract handle, but since you can't do pointer arithmetic on a void pointer, it does not work for LLVM GEPs.

This commit applies a new strategy for pointers to zero-element arrays. Instead of treating the array as void, we peel off the array and use its element type as the pointer target. In other words, we treat [Ty x 0]* as if it were Ty*. This makes simple pointer arithmetic possible.

The new test (test_empty_array_geps.ll) shows the motivating use-case.


This doesn't break any existing tests, and seems to fix the original case I was interested in (https://github.com/JuliaComputingOSS/llvm-cbe/issues/128). I haven't tested it very deeply, though. If you have any thoughts on particular problem areas that may have been missed, I'd love to know.