__restirct ignored when including headers like <cmath>

Hi,

I am observing a strange behaviour in which Clang ignores __restirct when I include some standard headers.

For example, this code:

void vec_add(int* __restrict a,
int* __restrict b,
int n) {
#pragma unroll 4
for(int i=0; i<n; ++i) {
a[i] += b[i];
}
}

results in:

; Function Attrs: nofree norecurse nounwind
define dso_local void @_Z7vec_addPiS_i(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32 %n) local_unnam
ed_addr #0 {
entry:

.
.

(note the noaliass before function arguments).

But this code:

#include

void vec_add(int* __restrict a,
int* __restrict b,
int n) {
#pragma unroll 4
for(int i=0; i<n; ++i) {
a[i] += b[i];
}
}

results in:

; Function Attrs: nofree norecurse nounwind
define dso_local void @_Z7vec_addPiS_i(i32* nocapture %a, i32* nocapture readonly %b, i32 %n) local_unnamed_addr #0 {
entry:
%cmp6 = icmp sgt i32 %n, 0
br i1 %cmp6, label %for.body.preheader, label %for.cond.cleanup
.
.

I’m running this with LLVM RISC-V backend using RISC-V GCC compiled Newlib as the C/C++ library.

Is it not okay to use GCC libraries with LLVM? Or could this be a specific issue with Newlib’s header files misinterpreted by Clang?

Thank you,
Bandhav

Hi,

Later found out this is because some Newlib headers defined __restrict as empty. restrict seems to work…

What’s the supposed difference between __restrict and restrict?

Thanks,
Bandhav

From wikipedia ()

C++ does not have standard support for restrict, but many compilers have equivalents that usually work in both C++ and C, such as the GCC’s and Clang’s __restrict__, and Visual C++'s __declspec(restrict). In addition, __restrict is supported by those three compilers. The exact interpretation of these alternative keywords vary by the compiler:

Hope this helps,

~ Johannes