http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51360

             Bug #: 51360
           Summary: spurious unused-but-set-variable warning for var used
                    in OpenMP pragma
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: kmcca...@gmail.com


Hi,

gcc 4.6.2 produces a spurious unused variable warning for the following code. 
The variable 'num' is used in setting the number of threads within an OpenMP
parallel region but the compiler does not realize this.

The warning is:

% /usr/local/gcc46/bin/gcc -Wall -fopenmp omp.c
omp.c: In function ‘main’:
omp.c:12:12: warning: variable ‘num’ set but not used
[-Wunused-but-set-variable]

% cat omp.c
#include <omp.h>
#include <stdlib.h>
#include <stdio.h>

// Compile with: gcc -Wall -fopenmp omp.c
// Run with:     ./a.out <num_threads> e.g. "./a.out 4"

int main(int argc, char ** argv) {
    if (argc != 2) return EXIT_FAILURE;

    int vec[20] = { 0, };
    int i, num = atoi(argv[1]);

    #pragma omp parallel for num_threads(num)
    for (i = 0; i < 20; ++i) {
        #pragma omp critical
        {
            printf("thread %d\n", (int)omp_get_thread_num());
            fflush(stdout);
        }

        vec[i] *= 5;
    }

    return 0;
}


Running the generated binary (RHEL 4 update 6 on x86_64) with a specified value
for argv[1] indicates that the value of 'num' is being used as expected.

gcc 4.2.4 and 4.4.3 do not warn with the same compiler flags.

Thanks,
- Kevin B. McCarty

Reply via email to