Skip to content

Commit 88cdb8d

Browse files
authored
[CIR][CodeGen] Add nothrow for functions in OpenCL languages (#903)
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.
1 parent 561a78a commit 88cdb8d

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenCall.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,16 @@ static void getTrivialDefaultFunctionAttributes(
15911591
auto convgt = mlir::cir::ConvergentAttr::get(CGM.getBuilder().getContext());
15921592
funcAttrs.set(convgt.getMnemonic(), convgt);
15931593
}
1594+
1595+
// TODO: NoThrow attribute should be added for other GPU modes CUDA, SYCL,
1596+
// HIP, OpenMP offload.
1597+
// AFAIK, neither of them support exceptions in device code.
1598+
if ((langOpts.CUDA && langOpts.CUDAIsDevice) || langOpts.SYCLIsDevice)
1599+
llvm_unreachable("NYI");
1600+
if (langOpts.OpenCL) {
1601+
auto noThrow = mlir::cir::NoThrowAttr::get(CGM.getBuilder().getContext());
1602+
funcAttrs.set(noThrow.getMnemonic(), noThrow);
1603+
}
15941604
}
15951605

15961606
void CIRGenModule::getTrivialDefaultFunctionAttributes(

clang/test/CIR/CodeGen/OpenCL/convergent.cl

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

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

1312
__attribute__((noinline))
1413
void non_convfun(void) {
@@ -37,7 +36,7 @@ void test_merge_if(int a) {
3736
g();
3837
}
3938
}
40-
// CIR: cir.func @test_merge_if{{.*}} extra(#fn_attr[[CONV_NOTHROW_ATTR]])
39+
// CIR: cir.func @test_merge_if{{.*}} extra(#fn_attr[[CONV_DECL_ATTR]])
4140

4241
// The LLVM IR below is equivalent to:
4342
// if (a) {
@@ -81,7 +80,7 @@ void test_no_merge_if(int a) {
8180
g();
8281
}
8382
}
84-
// CIR: cir.func @test_no_merge_if{{.*}} extra(#fn_attr[[CONV_NOTHROW_ATTR]])
83+
// CIR: cir.func @test_no_merge_if{{.*}} extra(#fn_attr[[CONV_DECL_ATTR]])
8584

8685
// LLVM-LABEL: define{{.*}} spir_func void @test_no_merge_if
8786
// LLVM: %[[tobool:.+]] = icmp eq i32 %[[ARG:.+]], 0
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -fclangir -triple=spirv64-unknown-unknown -emit-cir -o %t.cir %s
2+
// RUN: FileCheck %s -input-file=%t.cir -check-prefixes CIR
3+
// RUN: %clang_cc1 -fclangir -triple=spirv64-unknown-unknown -emit-llvm -o %t.ll %s
4+
// RUN: FileCheck %s -input-file=%t.ll -check-prefixes LLVM
5+
6+
// CIR-LABEL: #fn_attr =
7+
// CIR: cl.kernel = #cir.cl.kernel
8+
// CIR: nothrow = #cir.nothrow
9+
10+
// CIR-LABEL: #fn_attr1 =
11+
// CIR-NOT: cl.kernel = #cir.cl.kernel
12+
// CIR: nothrow = #cir.nothrow
13+
14+
kernel void ker() {};
15+
// CIR: cir.func @ker{{.*}} extra(#fn_attr) {
16+
// LLVM: define{{.*}}@ker(){{.*}} #0
17+
18+
void foo() {};
19+
// CIR: cir.func @foo{{.*}} extra(#fn_attr1) {
20+
// LLVM: define{{.*}}@foo(){{.*}} #1
21+
22+
// LLVM-LABEL: attributes #0
23+
// LLVM: nounwind
24+
25+
// LLVM-LABEL: attributes #1
26+
// LLVM: nounwind

0 commit comments

Comments
 (0)