forked from llvm/clangir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CIR][Dialect] Emit OpenCL kernel metadata (llvm#705)
This PR introduces a new attribute `OpenCLKernelMetadataAttr` to model the OpenCL kernel metadata structurally in CIR, with its corresponding implementations of CodeGen, Lowering and Translation. The `"TypeAttr":$vec_type_hint` part is tricky because of the absence of the signless feature of LLVM IR, while SPIR-V requires it. According to the spec, the final LLVM IR should encode signedness with an extra `i32` boolean value. In this PR, the droping logic from CIR's `TypeConverter` is still used to avoid code duplication when lowering to LLVM dialect. However, the signedness is then restored (still capsuled by a CIR attribute) and dropped again in the translation into LLVM IR.
- Loading branch information
1 parent
e2599f2
commit 6e11fd3
Showing
11 changed files
with
406 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//===- CIROpenCLAttrs.td - CIR dialect attrs for OpenCL ----*- tablegen -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file declares the CIR dialect attributes for OpenCL. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_CIR_DIALECT_CIR_OPENCL_ATTRS | ||
#define MLIR_CIR_DIALECT_CIR_OPENCL_ATTRS | ||
|
||
//===----------------------------------------------------------------------===// | ||
// OpenCLKernelMetadataAttr | ||
//===----------------------------------------------------------------------===// | ||
|
||
def OpenCLKernelMetadataAttr | ||
: CIR_Attr<"OpenCLKernelMetadata", "cl.kernel_metadata"> { | ||
|
||
let summary = "OpenCL kernel metadata"; | ||
let description = [{ | ||
Provide the required information of an OpenCL kernel for the SPIR-V backend. | ||
|
||
The `work_group_size_hint` and `reqd_work_group_size` parameter are integer | ||
arrays with 3 elements that provide hints for the work-group size and the | ||
required work-group size, respectively. | ||
|
||
The `vec_type_hint` parameter is a type attribute that provides a hint for | ||
the vectorization. It can be a CIR or LLVM type, depending on the lowering | ||
stage. | ||
|
||
The `vec_type_hint_signedness` parameter is a boolean that indicates the | ||
signedness of the vector type hint. It's useful when LLVM type is set in | ||
`vec_type_hint`, which is signless by design. It should be set if and only | ||
if the `vec_type_hint` is present. | ||
|
||
The `intel_reqd_sub_group_size` parameter is an integer that restricts the | ||
sub-group size to the specified value. | ||
|
||
Example: | ||
``` | ||
#fn_attr = #cir<extra({cl.kernel_metadata = #cir.cl.kernel_metadata< | ||
work_group_size_hint = [8 : i32, 16 : i32, 32 : i32], | ||
reqd_work_group_size = [1 : i32, 2 : i32, 4 : i32], | ||
vec_type_hint = !s32i, | ||
vec_type_hint_signedness = 1, | ||
intel_reqd_sub_group_size = 8 : i32 | ||
>})> | ||
|
||
cir.func @kernel(%arg0: !s32i) extra(#fn_attr) { | ||
cir.return | ||
} | ||
``` | ||
}]; | ||
|
||
let parameters = (ins | ||
OptionalParameter<"ArrayAttr">:$work_group_size_hint, | ||
OptionalParameter<"ArrayAttr">:$reqd_work_group_size, | ||
OptionalParameter<"TypeAttr">:$vec_type_hint, | ||
OptionalParameter<"std::optional<bool>">:$vec_type_hint_signedness, | ||
OptionalParameter<"IntegerAttr">:$intel_reqd_sub_group_size | ||
); | ||
|
||
let assemblyFormat = "`<` struct(params) `>`"; | ||
|
||
let genVerifyDecl = 1; | ||
|
||
let extraClassDeclaration = [{ | ||
/// Extract the signedness from int or int vector types. | ||
static std::optional<bool> isSignedHint(mlir::Type vecTypeHint); | ||
}]; | ||
|
||
let extraClassDefinition = [{ | ||
std::optional<bool> $cppClass::isSignedHint(mlir::Type hintQTy) { | ||
// Only types in CIR carry signedness | ||
if (!mlir::isa<mlir::cir::CIRDialect>(hintQTy.getDialect())) | ||
return std::nullopt; | ||
|
||
// See also clang::CodeGen::CodeGenFunction::EmitKernelMetadata | ||
auto hintEltQTy = mlir::dyn_cast<mlir::cir::VectorType>(hintQTy); | ||
auto isCIRSignedIntType = [](mlir::Type t) { | ||
return mlir::isa<mlir::cir::IntType>(t) && | ||
mlir::cast<mlir::cir::IntType>(t).isSigned(); | ||
}; | ||
return isCIRSignedIntType(hintQTy) || | ||
(hintEltQTy && isCIRSignedIntType(hintEltQTy.getEltType())); | ||
} | ||
}]; | ||
|
||
} | ||
|
||
#endif // MLIR_CIR_DIALECT_CIR_OPENCL_ATTRS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.