[Openmp-commits] [PATCH] D69927: [libomptarget] Export __kmp_internal_end_fini to fix [Thin]LTO build

Andrey Churbanov via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Wed Nov 13 11:00:29 PST 2019


AndreyChurbanov added a comment.

In D69927#1743197 <https://reviews.llvm.org/D69927#1743197>, @aaronpuchert wrote:

> I will try removing `__kmp_internal_end_fini` with a test that `__kmp_internal_end_atexit` is still called.


I don't know a reliable way to test that dtor was called. But was able to write a linux test which fails 99% if dtor was not called:

  #include <dlfcn.h>
  #include <unistd.h>
  #include <stdio.h>
  #include <stdlib.h>
  #define LIBNAME "libomp.so"
  int volatile flag = 0;
  int(*get_max_threads)(void) = NULL; // omp_get_max_threads
  void *foo(void* arg)
  {
    printf("enter foreign thread\n");
    fflush(0);
    if (get_max_threads)
    {
      int nt = get_max_threads();
      flag = 1;
      printf("sleeping foreign thread\n");
      fflush(0);
      usleep(100000);
      printf("exiting foreign thread\n");
      fflush(0);
    } else {
      printf("error loading runtime library\n");
      exit(1);
    }
    pthread_exit(0);
  }
  int main(int argc, char** argv )
  {
    pthread_t t;
    int rc;
    char *error;
    void *h = dlopen(LIBNAME, RTLD_LAZY);
    if (h == NULL) {
      printf("dlopen failed with error %s\n", dlerror());
      exit(1);
    }
    get_max_threads = (int(*)(void))dlsym(h, "omp_get_max_threads");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(2);
    }
    rc = get_max_threads();
    printf("initial thread registered for %d threads\n", rc);
    fflush(0);
    rc = pthread_create(&t, NULL, foo, NULL);
    if (rc) {
      printf("Could not create thread!\n");
      exit(-1);
    }
    // let foreign thread register itself with the runtime library
    // (to prevent conflict of initialization and shutdown)
    while(flag == 0) {
      usleep(1);
    }
    // try to cleanup
    dlclose( h );
    // foreign thread should crash if library closed w/o shutdown
    pthread_join(t, NULL);
    printf("passed\n");
    fflush(0);
    return 0;
  }

Tried the test with regular library and with one with empty destructors:

$ clang -pthread test.c -ldl

The library with fake (empty) destructors:
$ ./a.out
initial thread registered for 88 threads
enter foreign thread
sleeping foreign thread
exiting foreign thread
Segmentation fault (core dumped)

The library with real destructor(s):
$ ./a.out
initial thread registered for 88 threads
enter foreign thread
sleeping foreign thread
exiting foreign thread
passed

Not sure though how to properly run such test in LIT (all library tests so far used -fopenmp, while this one needs to avoid it).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69927/new/

https://reviews.llvm.org/D69927





More information about the Openmp-commits mailing list