Skip to content

Commit

Permalink
[CIR][Dialect][CodeGen] Add a unit attribute for OpenCL kernels (llvm…
Browse files Browse the repository at this point in the history
…#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`.)
  • Loading branch information
seven-mile authored and smeenai committed Oct 9, 2024
1 parent 908f19c commit 183567b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROpenCLAttrs.td
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,21 @@ def OpenCLVersionAttr : CIR_Attr<"OpenCLVersion", "cl.version"> {
let assemblyFormat = "`<` $major_version `,` $minor_version `>`";
}


//===----------------------------------------------------------------------===//
// OpenCLKernelAttr
//===----------------------------------------------------------------------===//

// TODO: It might be worthwhile to introduce a generic attribute applicable to
// all offloading languages.
def OpenCLKernelAttr : CIRUnitAttr<
"OpenCLKernel", "cl.kernel"> {
let summary = "OpenCL kernel";
let description = [{
Indicate the function is a OpenCL kernel.
}];

let storageType = [{ OpenCLKernelAttr }];
}

#endif // MLIR_CIR_DIALECT_CIR_OPENCL_ATTRS
3 changes: 3 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ void CIRGenModule::constructAttributeList(StringRef Name,
}

if (TargetDecl->hasAttr<OpenCLKernelAttr>()) {
auto cirKernelAttr =
mlir::cir::OpenCLKernelAttr::get(builder.getContext());
funcAttrs.set(cirKernelAttr.getMnemonic(), cirKernelAttr);
assert(!MissingFeatures::openCL());
}

Expand Down
14 changes: 14 additions & 0 deletions clang/test/CIR/CodeGen/OpenCL/kernel-unit-attr.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %clang_cc1 -fclangir -emit-cir -triple spirv64-unknown-unknown %s -o %t.cir
// RUN: FileCheck %s --input-file=%t.cir --check-prefix=CIR


// CIR: #fn_attr[[KERNEL1:[0-9]*]] = {{.+}}cl.kernel = #cir.cl.kernel
// CIR-NEXT: #fn_attr[[FUNC1:[0-9]*]] =
// CIR-NOT: cl.kernel = #cir.cl.kernel

kernel void kernel1() {}
// CIR: cir.func @kernel1{{.+}} extra(#fn_attr[[KERNEL1]])

void func1() {}

// CIR: cir.func @func1{{.+}} extra(#fn_attr[[FUNC1]])

0 comments on commit 183567b

Please sign in to comment.