Jeffrey Yasskin
2009-May-11 17:56 UTC
[LLVMdev] Mapping field names to GEP indices in clang-compiled C
I'm using clang to compile functions and types written in C into LLVM IR so that I can inline calls and avoid hand-writing the StructType definitions. The types clang generates are packed structs instead of ordinary structs. So for struct Foo { char x; int* y; }; clang produces the type <{i8, i8, i8, i8, i32*}> instead of {i8, i32*}. To extract the 'y' field from the unpacked struct, I'd use "GEP ..., 1", and it'd be right on all architectures. To extract 'y' from the packed struct that clang produces, I have to use "GEP ..., 4" on x86-32, "GEP ..., 8" on x86-64, and who knows what on other architectures. Is there a way to get clang to emit a C++-readable mapping from field names to GEP offsets? Or some other way to avoid special-casing these offsets for each architecture? If not, what would be the easiest way to add such an ability? Thanks, Jeffrey (and the Unladen Swallow team)
Eli Friedman
2009-May-11 18:56 UTC
[LLVMdev] Mapping field names to GEP indices in clang-compiled C
On Mon, May 11, 2009 at 10:56 AM, Jeffrey Yasskin <jyasskin at google.com> wrote:> Is there a way to get clang to emit a C++-readable mapping from field > names to GEP offsets? Or some other way to avoid special-casing these > offsets for each architecture? If not, what would be the easiest way > to add such an ability?First-off, this sort of question is more appropriate for cfe-dev; please direct any follow-up questions there. clang's current behavior here is a bug, but it's a low priority to fix because the generated IR isn't incorrect, just somewhat difficult to read. The only completely reliable solution I can think of is generating something like "int Foo_offsets[] = { offsetof(struct Foo, x), offsetof(struct Foo, y)};", then use some bitcasting to do the arithmetic. -Eli