Skip to content

Commit 4ae5f1c

Browse files
seven-milelanza
authored andcommitted
[CIR][Dialect][CodeGen] Add a unit attribute for OpenCL kernels (#877)
We need a target-independent way to distinguish OpenCL kernels in ClangIR. This PR adds a unit attribute `OpenCLKernelAttr` similar to the one in Clang AST. This attribute is attached to the extra attribute dictionary of `cir.func` operations only. (Not for `cir.call`.)
1 parent 4736544 commit 4ae5f1c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td

+17
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,21 @@ def OpenCLVersionAttr : CIR_Attr<"OpenCLVersion", "cl.version"> {
168168
let assemblyFormat = "`<` $major_version `,` $minor_version `>`";
169169
}
170170

171+
172+
//===----------------------------------------------------------------------===//
173+
// OpenCLKernelAttr
174+
//===----------------------------------------------------------------------===//
175+
176+
// TODO: It might be worthwhile to introduce a generic attribute applicable to
177+
// all offloading languages.
178+
def OpenCLKernelAttr : CIRUnitAttr<
179+
"OpenCLKernel", "cl.kernel"> {
180+
let summary = "OpenCL kernel";
181+
let description = [{
182+
Indicate the function is a OpenCL kernel.
183+
}];
184+
185+
let storageType = [{ OpenCLKernelAttr }];
186+
}
187+
171188
#endif // MLIR_CIR_DIALECT_CIR_OPENCL_ATTRS

clang/lib/CIR/CodeGen/CIRGenCall.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ void CIRGenModule::constructAttributeList(StringRef Name,
430430
}
431431

432432
if (TargetDecl->hasAttr<OpenCLKernelAttr>()) {
433+
auto cirKernelAttr =
434+
mlir::cir::OpenCLKernelAttr::get(builder.getContext());
435+
funcAttrs.set(cirKernelAttr.getMnemonic(), cirKernelAttr);
433436
assert(!MissingFeatures::openCL());
434437
}
435438

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fclangir -emit-cir -triple spirv64-unknown-unknown %s -o %t.cir
2+
// RUN: FileCheck %s --input-file=%t.cir --check-prefix=CIR
3+
4+
5+
// CIR: #fn_attr[[KERNEL1:[0-9]*]] = {{.+}}cl.kernel = #cir.cl.kernel
6+
// CIR-NEXT: #fn_attr[[FUNC1:[0-9]*]] =
7+
// CIR-NOT: cl.kernel = #cir.cl.kernel
8+
9+
kernel void kernel1() {}
10+
// CIR: cir.func @kernel1{{.+}} extra(#fn_attr[[KERNEL1]])
11+
12+
void func1() {}
13+
14+
// CIR: cir.func @func1{{.+}} extra(#fn_attr[[FUNC1]])

0 commit comments

Comments
 (0)