[LLVMdev] Mapping field names to GEP indices in clang-compiled C

Jeffrey Yasskin jyasskin at google.com
Mon May 11 10:56:25 PDT 2009


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)



More information about the llvm-dev mailing list