Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did I misuse a reference variable in a simple OpenMP for loop, or is it a clang bug?

I think I have found a clang++ bug, but would appreciate advice on whether my code is correct. Clang static analyzer thinks it is okay, and it compiles with no problems, but when compiled with clang 3.7, it gets the size off a passed reference vector completely wrong. GCC and clang 3.8 both give the correct answer. I've reduced it to this test case:

#include <vector>
#include <iostream>
// including or excluding omp makes no difference
#include <omp.h>

void doSomething(std::vector<int> &k) {
#pragma omp for
    for (int i=0; i<2; ++i) {
            std::cout << k.size() << "\n";
    }
}

int main() {
    std::vector<int> v;
    v.push_back(1);

    std::vector<int> &j = v;
    doSomething(j);
    return(0);
}

with libomp (I think), rather than libgomp:

clang-3.7++ -fopenmp clang-err.cpp
./a.out
18446708892352074976
18446708892352074976


clang-3.8++ -fopenmp clang-err.cpp
./a.out
1
1

I could not find such a bug present in clang 3.7 but fixed in 3.8. I do not know how to tell whether I am using libomp for sure, although this is how LLVM/clang was compiled. It seems like such a simple thing, so I suspect I am doing something strange rather than there being a real clang bug.

If the consensus is a clang bug, I'll report it against 3.7. Thanks.

like image 698
Jack Wasey Avatar asked Sep 14 '15 20:09

Jack Wasey


1 Answers

It does appear to be a clang bug in the current release 3.7. However, surprisingly, the bug assignee closed the bug against 3.7 because it is fixed in 3.8.

like image 80
Jack Wasey Avatar answered Oct 12 '22 09:10

Jack Wasey