clangir icon indicating copy to clipboard operation
clangir copied to clipboard

The 'deref' attribute isn't always set for derefences in the source

Open andykaylor opened this issue 8 months ago • 0 comments

If I dereference a pointer with an explicit cast, the deref attribute isn't set on the cir.load for the pointer being dereferenced.

Example:

void foo(int *p) {
  unsigned u = *(unsigned *)p;
}

Produces

  cir.func @_Z3fooPi(%arg0: !cir.ptr<!s32i> {
    %0 = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["p", init] {alignment = 8 : i64}
    %1 = cir.alloca !u32i, !cir.ptr<!u32i>, ["u", init] {alignment = 4 : i64}
    cir.store %arg0, %0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
    %2 = cir.load %0 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
    %3 = cir.cast(bitcast, %2 : !cir.ptr<!s32i>), !cir.ptr<!u32i>
    %4 = cir.load %3 : !cir.ptr<!u32i>, !u32i
    cir.store %4, %1 : !u32i, !cir.ptr<!u32i>
    cir.return
  }

https://godbolt.org/z/qKfG7rsE8

The attribute is also missed if I am derefencing a pointer that is the result of pointer arithmetic. For example:

void foo(int *p) {
  int n = *(++p);
}

Produces:

  cir.func @_Z3fooPi(%arg0: !cir.ptr<!s32i> {
    %0 = cir.alloca !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>, ["p", init] {alignment = 8 : i64}
    %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["n", init] {alignment = 4 : i64}
    cir.store %arg0, %0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
    %2 = cir.load %0 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
    %3 = cir.const #cir.int<1> : !s32i
    %4 = cir.ptr_stride(%2 : !cir.ptr<!s32i>, %3 : !s32i), !cir.ptr<!s32i>
    cir.store %4, %0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
    %5 = cir.load %4 : !cir.ptr<!s32i>, !s32i
    cir.store %5, %1 : !s32i, !cir.ptr<!s32i>
    cir.return
  }

andykaylor avatar Apr 04 '25 18:04 andykaylor