Skip to content

[SYCL][ESIMD] Fix a compilation of mix of unnamed ESIMD and non ESIMD kernels #6557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 25, 2022

Conversation

fineg74
Copy link
Contributor

@fineg74 fineg74 commented Aug 10, 2022

The purpose of this PR is to fix an issue that leads to runtime exception when unnamed ESIMD and non ESIMD kernels are present.

@fineg74
Copy link
Contributor Author

fineg74 commented Aug 10, 2022

Complementary test PR: intel/llvm-test-suite#1143

return true;
}
for (const Function *F1 : Deps.successors(F)) {
if (isESIMDFunctionInCallChain(F1, Deps)) {
Copy link
Contributor

@asudarsa asudarsa Aug 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility to break out of this loop sooner?
Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop itself probably not, as it needs to check all the functions to decide if they belong to ESIMD or SYCL kernels. The isESIMDFunctionInCallChain can technically limit its recursion depth and return faster.

@kbobrovs
Copy link
Contributor

@fineg74, could you please describe what the actual problem is - why unnamed ESIMD kernels lead to this problem?

@fineg74
Copy link
Contributor Author

fineg74 commented Aug 15, 2022

@fineg74, could you please describe what the actual problem is - why unnamed ESIMD kernels lead to this problem?

The immediate cause of the issue is that ESIMD kernel is wrapped with RoundedRangeKernel and sycl_explicit_esimd attribute is not propagated to the wrapper class. As a result ModuleSplitter in sycl-post-link creates a spurious split for the wrapper and later creates a spurious non-esimd module and pulls called functions into it. As a result non-esimd module would contain esimd functions and later fails to link. It looks like that for named ESIMD kernels the attributes are properly propagated and therefore the issue doesn't occur.
The fix proposed here fixes it by using a fact that ESIMD functions can only be called from ESIMD kernels so spurious split is eliminated and modules are properly created and linked. It is possible to implement it in a more formalized way by creating a pass that would propagate the attributes before performing module split but it would require significant code duplication between ModuleSplitter and the new pass.

@kbobrovs
Copy link
Contributor

We should try to limit ESIMD specifics penetration into general-purpose tools like post-link. My suggestion would be to find the wrapper function and add 'sycl_explicit_simd' metadata to it before doing any splits in the post-link. I believe the wrapper should have spir_kernel calling convention, which automatically makes it an entry point. The task of finding the wrapper(s) is then searching for all spir_kernels up the call chain. ESIMD splitting and lowering infra will then correctly interpret the 'sycl_explicit_simd' metadata and do what's needed.

If the wrapper does not have spir_kernel (and is a spir_function), then the user ESIMD kernel must be annotated with spir_kernel. In which case the search for the wrapper would be finding terminal function in the callgraph starting from the user kernel up the call chain. Callgraph traversal up the call chain is implemented in ESIMDUtils.cpp (traverseCallgraphUp). In this case (no spir_kernel in the wrapper), we should also force the wrapper to be an entry point by copying the "sycl_module_id" attribute from the user kernel or creating a dummy one.

@kbobrovs
Copy link
Contributor

... I would suggest to implement finding the wrapper and adding the metadata as a pass defined within the llvm/lib/SYCLLowerIR directory and invoked in sycl-post-link.cpp somewhere around (and similarly to) the const bool InvokeSimdMet = runModulePass<SYCLLowerInvokeSimdPass>(*M);

asudarsa
asudarsa previously approved these changes Aug 18, 2022
Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sycl-post-link change looks good. Thanks

},
parseASanPassOptions,
"kernel")
MODULE_PASS_WITH_PARAMS(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like most of the changes in this file are unintended reformatting. Please remove.
This command formats only modified lines (requires clang-format in the PATH):
git diff -U0 --no-color HEAD^ | clang/tools/clang-format/clang-format-diff.py -i -p1

@@ -76,6 +76,12 @@ class SYCLLowerESIMDKernelPropsPass
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
};

// Fixes ESIMD Kernel attributes for wrapper functions for ESIMD kernels
class SYCLLowerESIMDKernelAttrPass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class SYCLLowerESIMDKernelAttrPass
class SYCLFixupESIMDKernelWrapperAttrPass

@@ -76,6 +76,12 @@ class SYCLLowerESIMDKernelPropsPass
PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
};

// Fixes ESIMD Kernel attributes for wrapper functions for ESIMD kernels
class SYCLLowerESIMDKernelAttrPass
: public PassInfoMixin<SYCLLowerESIMDKernelPropsPass> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
: public PassInfoMixin<SYCLLowerESIMDKernelPropsPass> {
: public PassInfoMixin<SYCLFixupESIMDKernelWrapperAttrPass> {

@@ -16,6 +16,7 @@ namespace llvm {
namespace esimd {

constexpr char ATTR_DOUBLE_GRF[] = "esimd-double-grf";
constexpr char ATTR_ESIMD_KERNEL[] = "sycl_explicit_simd";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an attribute, actually, but metadata. I suggest to name the same way as in
llvm/lib/SYCLLowerIR/LowerInvokeSimd.cpp:constexpr char ESIMD_MARKER_MD[] = "sycl_explicit_simd";

Suggested change
constexpr char ATTR_ESIMD_KERNEL[] = "sycl_explicit_simd";
constexpr char ESIMD_MARKER_MD[] = "sycl_explicit_simd";

@@ -1,4 +1,4 @@
if(${CMAKE_VERSION} VERSION_LESS 3.14)
if (${CMAKE_VERSION} VERSION_LESS 3.14)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please drop unneeded changes in this file, only one line change is expected, AFAIU

bool Modified = false;
for (Function &F : M) {
if (llvm::esimd::isESIMD(F)) {
llvm::esimd::traverseCallgraphUp(&F, [&](Function *GraphNode) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, is the wrapper function a spir_kernel or spir_function? Is the user function a spir_kernel or spir_function in presence of wrapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both user function and the wrapper are marked as spir_kernel .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record: as we talked, the user function is spir_func, which is called by two kernels with spir_kernel CC - the normal and the one with the rounded range.

MODULE_PASS("inliner-ml-advisor-release",
ModuleInlinerWrapperPass(getInlineParams(), true, {},
InliningAdvisorMode::Release, 0))
MODULE_PASS("inliner-ml-advisor-release", ModuleInlinerWrapperPass(getInlineParams(), true, {}, InliningAdvisorMode::Release, 0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still lots of unrelated changes in the file, please verify there are only intended before pushing update

@@ -77,8 +77,8 @@ class SYCLLowerESIMDKernelPropsPass
};

// Fixes ESIMD Kernel attributes for wrapper functions for ESIMD kernels
class SYCLLowerESIMDKernelAttrPass
: public PassInfoMixin<SYCLLowerESIMDKernelPropsPass> {
class SYCLFixupESIMDKernelWrapperAttrPass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good name either, my bad. As I noted, sycl_explicit_simd is a metadata. So please rename once again - SYCLFixupESIMDKernelWrapperMDPass. We need another iteration anyways.

ShouldNotRunFunctionPassesAnalysis())
FUNCTION_ANALYSIS("should-run-extra-vector-passes",
ShouldRunExtraVectorPasses())
FUNCTION_ANALYSIS("should-not-run-function-passes", ShouldNotRunFunctionPassesAnalysis())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated changes

@kbobrovs kbobrovs requested a review from asudarsa August 20, 2022 22:41
Copy link
Contributor

@asudarsa asudarsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. Thanks.

@kbobrovs kbobrovs merged commit 3d54d7e into intel:sycl Aug 25, 2022
@fineg74 fineg74 deleted the KernelBuild branch August 25, 2022 17:22
kbobrovs added a commit to kbobrovs/llvm that referenced this pull request Sep 28, 2022
intel#6557 rolled back necessary code,
return it back with this patch. Problem somehow reproduced only in
internal debug Windows build - the
SYCLLowerIR/ESIMD/vec_arg_call_conv.ll test crashed opt.

Signed-off-by: Konstantin S Bobrovsky <[email protected]>
pvchupin pushed a commit that referenced this pull request Sep 29, 2022
…on. (#6908)

#6557 rolled back necessary code,
return it back with this patch. Problem somehow reproduced only in
internal debug Windows build - the
SYCLLowerIR/ESIMD/vec_arg_call_conv.ll test crashed opt.

Signed-off-by: Konstantin S Bobrovsky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants