Prevent sizeof being replaced by a constant in IR


Using the following command:

clang -O0 -S -emit-llvm size.c

Will generate an LLVM IR file where uses of ‘sizeof’ have been replaced by constants. Is there any way to prevent this, and perhaps leave the 'sizeof’s replaced by GEPs as detailed here:


"sizeof" _is_ a constant.

The technique from the note only works for aggregates that end with an array (or arrays themselves).


No, there is no such clang option to control this.

Even if there were, the size would bleed through in lots of other places. For example, C++ features like templates and constexpr will require baking in these sizes.

I also doubt LLVM IR supports array dimensions that aren’t constant ints, so the following code would have to use a constant:

struct foo { … };
foo a1[NUMBER];
foo a2[sizeof(a1) / sizeof(foo)];