Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restrict qualifier in C vs noalias attribute in LLVM IR

Tags:

c++

c

llvm-ir

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?

like image 953
Kiko Fernandez Avatar asked Oct 24 '16 16:10

Kiko Fernandez


1 Answers

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.

like image 137
Kiko Fernandez Avatar answered Nov 15 '22 18:11

Kiko Fernandez