Is there any way to retain the GEPs which lead to the first field of a structure

llvm optimizes out (Removes) the getelementptr instructions which access the first field of a structure, since this boils down to a zero offset, retaining this would help in understand structure accesses. The following explains this in brief,

struct { int a, int b} NODE;

void func(NODE *xyz)
{
printf(“%ld”, xyz.a);
}

current IR looks like,

func(ptr xyz)
{
%1 = load xyz;
printf(ptr noundef nonnull dereferenceable(1) @.str.16, i32 %1)
}

rather than

func(ptr xyz)
{
%1 = getelementptr inbounds (%struct.NODE, ptr xyz, i64 0, i32 0)
%2 = load xyz;
printf(ptr noundef nonnull dereferenceable(1) @.str.16, i32 %2)
}

Please do let me know if there is any option or mechanism which would help to retain these getelementptr instructions.

No, there’s no such functionality. If you could add a description of your use-case to [RFC] Replacing getelementptr with ptradd , that might help guide future work.

If this is semantically relevant for you, the way to handle it is to use intrinsics instead of GEPs, see e.g. the llvm.preserve.struct.access.index intrinsic used by the eBPF target.

GEPs themselves are semantically considered to be pure offset arithmetic, and as such any type information they carry is only preserved incidentally, not intentionally. The only circumstances under which we retain zero index GEPs is if they carry an inrange (as opposed to inbounds) attribute or perform an implicit splat.

Thanks for a response on this, Sure with add description about use-case in the RFC

Do you mean that there is an option where by compiler front end emits structure access intrinsics instead of GEPs?