llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[OpenMP] No warnings emitted when an exception is thrown in a lambda inside a target region

Open AntonRydahl opened this issue 3 years ago • 1 comments

I have found a bug that most likely has been introduced by myself. In the early summer we changed the way exceptions are handled in OpenMP offloading. Before, we would error out if the target code contained exception handling, and now we emit the code for the try statement and emit a trap instruction for the throw expression. In addition, we print warnings.

For exception handling directly embedded in target regions, and for functions declared target, we get warnings as expected. For instance, compiling the following example

void foo(double * a){
    try {
        throw 3;
    }
    catch (int e) {
        *a = e;
    }
}
#pragma omp declare target indirect to(foo)

int main(int, char**) {
  #pragma omp target
  {
    double ab = 1;
    foo(&ab);
  }
  return 0;
}

gives the warnings

warning: target 'amdgcn-amd-amdhsa' does not support exception handling; 'throw' is assumed to be never reached [-Wopenmp-target-exception]
    3 |         throw 3;
      |         ^
warning: target 'amdgcn-amd-amdhsa' does not support exception handling; 'catch' block is ignored [-Wopenmp-target-exception]
    2 |     try {

However, the following example does not produce a warning:

int main(int, char**) {
  // lambda that throws an exception
  auto lam = [](double& n){
    try {
        throw 3;
    }
    catch (int e) {
        n = e;
    }
  };

  #pragma omp target
  {
    double ab = 1;
    lam(ab);
  }
  return 0;
}

AntonRydahl avatar Nov 07 '23 18:11 AntonRydahl

@llvm/issue-subscribers-openmp

Author: Anton Rydahl (AntonRydahl)

I have found a bug that most likely has been introduced by myself. In the early summer we changed the way exceptions are handled in OpenMP offloading. Before, we would error out if the target code contained exception handling, and now we emit the code for the try statement and emit a trap instruction for the throw expression. In addition, we print warnings.

For exception handling directly embedded in target regions, and for functions declared target, we get warnings as expected. For instance, compiling the following example

void foo(double * a){
    try {
        throw 3;
    }
    catch (int e) {
        *a = e;
    }
}
#pragma omp declare target indirect to(foo)

int main(int, char**) {
  #pragma omp target
  {
    double ab = 1;
    foo(&ab);
  }
  return 0;
}

gives the warnings

warning: target 'amdgcn-amd-amdhsa' does not support exception handling; 'throw' is assumed to be never reached [-Wopenmp-target-exception]
    3 |         throw 3;
      |         ^
warning: target 'amdgcn-amd-amdhsa' does not support exception handling; 'catch' block is ignored [-Wopenmp-target-exception]
    2 |     try {

However, the following example does not produce a warning:

int main(int, char**) {
  // lambda that throws an exception
  auto lam = [](double& n){
    try {
        throw 3;
    }
    catch (int e) {
        n = e;
    }
  };

  #pragma omp target
  {
    double ab = 1;
    lam(ab);
  }
  return 0;
}

llvmbot avatar Nov 07 '23 18:11 llvmbot