Skip to content

Commit

Permalink
[CIR][CodeGen] Add nothrow for functions in OpenCL languages (llvm#903
Browse files Browse the repository at this point in the history
)

Heterogeneous languages do not support exceptions, which corresponds to
`nothrow` in ClangIR and `nounwind` in LLVM IR.

This PR adds nothrow attributes for all functions for OpenCL languages
in CIRGen. The Lowering for it is already supported previously.
  • Loading branch information
seven-mile authored and lanza committed Oct 2, 2024
1 parent ccdc2ff commit d9c1133
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,16 @@ static void getTrivialDefaultFunctionAttributes(
auto convgt = mlir::cir::ConvergentAttr::get(CGM.getBuilder().getContext());
funcAttrs.set(convgt.getMnemonic(), convgt);
}

// TODO: NoThrow attribute should be added for other GPU modes CUDA, SYCL,
// HIP, OpenMP offload.
// AFAIK, neither of them support exceptions in device code.
if ((langOpts.CUDA && langOpts.CUDAIsDevice) || langOpts.SYCLIsDevice)
llvm_unreachable("NYI");
if (langOpts.OpenCL) {
auto noThrow = mlir::cir::NoThrowAttr::get(CGM.getBuilder().getContext());
funcAttrs.set(noThrow.getMnemonic(), noThrow);
}
}

void CIRGenModule::getTrivialDefaultFunctionAttributes(
Expand Down
5 changes: 2 additions & 3 deletions clang/test/CIR/CodeGen/OpenCL/convergent.cl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

// CIR: #fn_attr[[CONV_NOINLINE_ATTR:[0-9]*]] = #cir<extra({convergent = #cir.convergent, inline = #cir.inline<no>
// CIR-NEXT: #fn_attr[[CONV_DECL_ATTR:[0-9]*]] = #cir<extra({convergent = #cir.convergent
// CIR-NEXT: #fn_attr[[CONV_NOTHROW_ATTR:[0-9]*]] = #cir<extra({convergent = #cir.convergent, nothrow = #cir.nothrow

__attribute__((noinline))
void non_convfun(void) {
Expand Down Expand Up @@ -37,7 +36,7 @@ void test_merge_if(int a) {
g();
}
}
// CIR: cir.func @test_merge_if{{.*}} extra(#fn_attr[[CONV_NOTHROW_ATTR]])
// CIR: cir.func @test_merge_if{{.*}} extra(#fn_attr[[CONV_DECL_ATTR]])

// The LLVM IR below is equivalent to:
// if (a) {
Expand Down Expand Up @@ -81,7 +80,7 @@ void test_no_merge_if(int a) {
g();
}
}
// CIR: cir.func @test_no_merge_if{{.*}} extra(#fn_attr[[CONV_NOTHROW_ATTR]])
// CIR: cir.func @test_no_merge_if{{.*}} extra(#fn_attr[[CONV_DECL_ATTR]])

// LLVM-LABEL: define{{.*}} spir_func void @test_no_merge_if
// LLVM: %[[tobool:.+]] = icmp eq i32 %[[ARG:.+]], 0
Expand Down
26 changes: 26 additions & 0 deletions clang/test/CIR/CodeGen/OpenCL/nothrow.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %clang_cc1 -fclangir -triple=spirv64-unknown-unknown -emit-cir -o %t.cir %s
// RUN: FileCheck %s -input-file=%t.cir -check-prefixes CIR
// RUN: %clang_cc1 -fclangir -triple=spirv64-unknown-unknown -emit-llvm -o %t.ll %s
// RUN: FileCheck %s -input-file=%t.ll -check-prefixes LLVM

// CIR-LABEL: #fn_attr =
// CIR: cl.kernel = #cir.cl.kernel
// CIR: nothrow = #cir.nothrow

// CIR-LABEL: #fn_attr1 =
// CIR-NOT: cl.kernel = #cir.cl.kernel
// CIR: nothrow = #cir.nothrow

kernel void ker() {};
// CIR: cir.func @ker{{.*}} extra(#fn_attr) {
// LLVM: define{{.*}}@ker(){{.*}} #0

void foo() {};
// CIR: cir.func @foo{{.*}} extra(#fn_attr1) {
// LLVM: define{{.*}}@foo(){{.*}} #1

// LLVM-LABEL: attributes #0
// LLVM: nounwind

// LLVM-LABEL: attributes #1
// LLVM: nounwind

0 comments on commit d9c1133

Please sign in to comment.