8

my question is related to the different semantics of the restrict qualifier in C and the noalias attribute in LLVM when they are used as function parameters.

According to the LLVM documentation for noalias:

This indicates that objects accessed via pointer values based on the argument or return value are not also accessed, during the execution of the function, via pointer values not based on the argument or return value.

In case of the restrict qualifier, the draft of the C11 (Example 3, page124, sect. 6.7.3.1) puts an example where there is aliasing between two restrict arguments, which is fine as long as they only read data:

void h(int n, int * restrict p, int * restrict q, int * restrict r) {
    int i;
    for (i = 0; i < n; i++)
        p[i] = q[i] + r[i];
}

To me, it seems as if the example given above would not satisfy the semantics of noalias. Is this the case?

Kiko Fernandez
  • 857
  • 10
  • 26
  • 2
    It says explicitly that it is intended to be very similar. In particular if you dig into the links, you see that `noalias` also works the same if two pointers are only read. The real difference is that `noalias` has also significance for return values, which C can't even express. Qualifications of return types are dropped. – Jens Gustedt Oct 24 '16 at 20:15

1 Answers1

1

As suggested by Jens Gustedt, digging into the links brought me to the AliasAnalysis page that states:

The most obvious example is when the two pointers point to non-overlapping memory ranges. Another is when the two pointers are only ever used for reading memory. Another is when the memory is freed and reallocated between accesses through one pointer and accesses through the other — in this case, there is a dependence, but it’s mediated by the free and reallocation.

This solves the question: the noalias attribute is equivalent to the C restrict qualifier in function parameters.

Kiko Fernandez
  • 857
  • 10
  • 26