[all-commits] [llvm/llvm-project] 0cfc2d: [OpenMP] Allow exceptions in target regions when o...

Anton Rydahl via All-commits all-commits at lists.llvm.org
Mon Aug 28 22:36:54 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 0cfc2dba93b172802b580713a492ea14148a0218
      https://github.com/llvm/llvm-project/commit/0cfc2dba93b172802b580713a492ea14148a0218
  Author: Anton Rydahl <rydahl2610 at gmail.com>
  Date:   2023-08-28 (Mon, 28 Aug 2023)

  Changed paths:
    M clang/include/clang/Basic/DiagnosticCommonKinds.td
    M clang/include/clang/Basic/DiagnosticGroups.td
    M clang/lib/CodeGen/CGException.cpp
    M clang/lib/Sema/SemaExprCXX.cpp
    M clang/lib/Sema/SemaStmt.cpp
    A clang/test/OpenMP/amdgpu_exceptions.cpp
    A clang/test/OpenMP/amdgpu_throw.cpp
    A clang/test/OpenMP/amdgpu_throw_trap.cpp
    A clang/test/OpenMP/amdgpu_try_catch.cpp
    A clang/test/OpenMP/nvptx_exceptions.cpp
    M clang/test/OpenMP/nvptx_target_exceptions_messages.cpp
    A clang/test/OpenMP/nvptx_throw.cpp
    A clang/test/OpenMP/nvptx_throw_trap.cpp
    A clang/test/OpenMP/nvptx_try_catch.cpp
    A clang/test/OpenMP/x86_target_exceptions.cpp
    A clang/test/OpenMP/x86_target_throw.cpp
    A clang/test/OpenMP/x86_target_try_catch.cpp

  Log Message:
  -----------
  [OpenMP] Allow exceptions in target regions when offloading to GPUs

The motivation for this patch is that many code bases use exception handling. As GPUs are not expected to support exception handling in the near future, we can experiment with compiling the code for GPU targets anyway. This will
allow us to run the code, as long as no exception is thrown.

The overall idea is very simple:
- If a throw expression is compiled to AMDGCN or NVPTX, it is replaced with a trap during code generation.
- If a try/catch statement is compiled to AMDGCN or AMDHSA, we ganerate code for the try statement as if it were a basic block.

With this patch, the compilation of the following example
```{C++}
int gaussian_sum(int a,int b){
	if ((a + b) % 2 == 0) {throw -1;};
	return (a+b) * ((a+b)/2);
}

int main(void) {
	int gauss = 0;
	#pragma omp target map(from:gauss)
	{
		try {
			gauss = gaussian_sum(1,100);
		}
		catch (int e){
			gauss = e;
		}
	}
	std::cout << "GaussianSum(1,100)="<<gauss<<std::endl;
        #pragma omp target map(from:gauss)
        {
                try {
                     	gauss = gaussian_sum(1,101);
                }
                catch (int e){
                        gauss = e;
                }
        }
	std::cout << "GaussianSum(1,101)="<<gauss<<std::endl;
	return (gauss > 1) ? 0 : 1;
}
```
with offloading to `gfx906` results in
```{bash}
./bin/target_try_minimal_fail
GaussianSum(1,100)=5050
AMDGPU fatal error 1: Received error in queue 0x155555506000: HSA_STATUS_ERROR_EXCEPTION: An HSAIL operation resulted in a hardware exception.
zsh: abort (core dumped)
```

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D153924




More information about the All-commits mailing list