cgccopenmp

OpenMP pragma translation to runtime calls


I wrote a short program in C with OpenMP pragma, and I need to know to which libGOMP function a pragma is translated by GCC. Here is my marvelous code:

#include <stdio.h>
#include "omp.h"

int main(int argc, char** argv)
{
  int k = 0;
  #pragma omp parallel private(k) num_threads(4)
  {
    k = omp_get_thread_num();
    printf("Hello World from %d !\n", k);
  }
  return 0;
}

In order to generate intermediate language from GCC v8.2.0, I compiled this program with the following command:

gcc -fopenmp -o hello.exe hello.c -fdump-tree-ompexp

And the result is given by:

;; Function main (main, funcdef_no=0, decl_uid=2694, cgraph_uid=0, symbol_order=0)


OMP region tree

bb 2: gimple_omp_parallel
bb 3: GIMPLE_OMP_RETURN  

Added new low gimple function main._omp_fn.0 to callgraph
Introduced new external node (omp_get_thread_num/2).     
Introduced new external node (printf/3).                 

;; Function main._omp_fn.0 (main._omp_fn.0, funcdef_no=1, decl_uid=2700, cgraph_uid=1, symbol_order=1)

main._omp_fn.0 (void * .omp_data_i)
{                                  
  int k;                           

  <bb 6> :

  <bb 3> :
  k = omp_get_thread_num ();
  printf ("Hello World from %d !\n", k);
  return;                               

}



;; Function main (main, funcdef_no=0, decl_uid=2694, cgraph_uid=0, symbol_order=0)

Merging blocks 2 and 7
Merging blocks 2 and 4
main (int argc, char * * argv)
{
  int k;
  int D.2698;

  <bb 2> :
  k = 0;
  __builtin_GOMP_parallel (main._omp_fn.0, 0B, 4, 0);
  D.2698 = 0;

  <bb 3> :
<L0>:
  return D.2698;

}

The function call to "__builtin_GOMP_parallel" is what it interest me. So, I looked at the source code of the libGOMP from GCC. However, the only function calls I found was (from parallel.c file):

GOMP_parallel_start (void (*fn) (void *), void *data, unsigned num_threads)
GOMP_parallel_end (void)

So, I can imiagine that, in a certain manner, the call to "__builtin_GOMP_parallel" is transformed to GOMP_parallel_start and GOMP_parallel_end. How can I be sure of this assumption ? How can I found the translation from the builtin function to the two other ones I found in the source code?


Solution

  • You almost got it. __builtin_GOMP_parallel is just a compiler alias to GOMP_parallel (defined in omp-builtins.def) which is translated very late in compilation, you can see the actual call in the assembly with gcc -S.

    GOMP_parallel is similar to

    GOMP_parallel_start(...);
    fn(...);
    GOMP_parallel_end();