-
Notifications
You must be signed in to change notification settings - Fork 790
[SYCL][Fusion] Enable fusion of kernels with different ND-ranges #8209
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
steffenlarsen
merged 7 commits into
intel:sycl
from
victor-eds:kernel-fusion/different-nd-ranges
Feb 14, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eff2d2f
[SYCL][Fusion] Enable fusion of kernels with different ND-ranges
victor-eds ebe4bcb
Empty commit
victor-eds a7f93b8
Fix compilation error in old compilers
victor-eds cfeadce
Address comments
victor-eds 69bcf3e
Make new library private
victor-eds c8259a1
Remove common/lib from include directories
victor-eds b8fb581
Include array header in Kernel.h
victor-eds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,23 @@ | ||
add_llvm_library(sycl-fusion-common | ||
lib/NDRangesHelper.cpp | ||
) | ||
|
||
target_include_directories(sycl-fusion-common | ||
PUBLIC | ||
${CMAKE_CURRENT_SOURCE_DIR}/include | ||
${CMAKE_CURRENT_SOURCE_DIR}/lib | ||
) | ||
|
||
if (BUILD_SHARED_LIBS) | ||
if(NOT MSVC AND NOT APPLE) | ||
# Manage symbol visibility through the linker to make sure no LLVM symbols | ||
# are exported and confuse the drivers. | ||
set(linker_script "${CMAKE_CURRENT_SOURCE_DIR}/ld-version-script.txt") | ||
target_link_libraries( | ||
sycl-fusion-common PRIVATE "-Wl,--version-script=${linker_script}") | ||
set_target_properties(sycl-fusion-common | ||
PROPERTIES | ||
LINK_DEPENDS | ||
${linker_script}) | ||
endif() | ||
endif() |
This file contains hidden or 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 hidden or 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,8 @@ | ||
{ | ||
global: | ||
/* Export everything from jit_compiler namespace */ | ||
_ZN12jit_compiler*; | ||
|
||
local: | ||
*; | ||
}; |
File renamed without changes.
This file contains hidden or 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,99 @@ | ||
//==-------------------------- NDRangesHelper.cpp --------------------------==// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "NDRangesHelper.h" | ||
|
||
#include <map> | ||
|
||
using namespace jit_compiler; | ||
using namespace llvm; | ||
|
||
/// | ||
/// Return the maximal global size using the following order: | ||
/// 1. A range is greater than another range if it contains more elements (see | ||
/// linearize()); | ||
/// 2. Else if it appears more times in the input list of ranges; | ||
/// 3. Else if it is greater in lexicographical order. | ||
static Indices getMaximalGlobalSize(ArrayRef<NDRange> NDRanges) { | ||
size_t NumElements{0}; | ||
std::map<Indices, size_t> FreqMap; | ||
for (const auto &ND : NDRanges) { | ||
const auto &GS = ND.getGlobalSize(); | ||
const auto N = NDRange::linearize(GS); | ||
if (N < NumElements) { | ||
continue; | ||
} | ||
if (N > NumElements) { | ||
NumElements = N; | ||
FreqMap.clear(); | ||
} | ||
++FreqMap[GS]; | ||
} | ||
return std::max_element(FreqMap.begin(), FreqMap.end(), | ||
[](const auto &LHS, const auto &RHS) { | ||
const auto LHSN = LHS.second; | ||
const auto RHSN = RHS.second; | ||
if (LHSN < RHSN) { | ||
return true; | ||
} | ||
if (LHSN > RHSN) { | ||
return false; | ||
} | ||
return LHS.first < RHS.first; | ||
}) | ||
->first; | ||
} | ||
|
||
static bool compatibleRanges(const NDRange &LHS, const NDRange &RHS) { | ||
const auto Dimensions = std::max(LHS.getDimensions(), RHS.getDimensions()); | ||
const auto EqualIndices = [Dimensions](const Indices &LHS, | ||
const Indices &RHS) { | ||
return std::equal(LHS.begin(), LHS.begin() + Dimensions, RHS.begin()); | ||
}; | ||
return (!LHS.hasSpecificLocalSize() || !RHS.hasSpecificLocalSize() || | ||
EqualIndices(LHS.getLocalSize(), RHS.getLocalSize())) && | ||
EqualIndices(LHS.getOffset(), RHS.getOffset()); | ||
} | ||
|
||
NDRange jit_compiler::combineNDRanges(ArrayRef<NDRange> NDRanges) { | ||
assert(isValidCombination(NDRanges) && "Invalid ND-ranges combination"); | ||
const auto Dimensions = | ||
std::max_element(NDRanges.begin(), NDRanges.end(), | ||
[](const auto &LHS, const auto &RHS) { | ||
return LHS.getDimensions() < RHS.getDimensions(); | ||
}) | ||
->getDimensions(); | ||
const auto GlobalSize = getMaximalGlobalSize(NDRanges); | ||
const auto *End = NDRanges.end(); | ||
const auto *LocalSizeIter = findSpecifiedLocalSize(NDRanges); | ||
const auto &LocalSize = | ||
LocalSizeIter == End ? NDRange::AllZeros : LocalSizeIter->getLocalSize(); | ||
const auto &Front = NDRanges.front(); | ||
return {Dimensions, GlobalSize, LocalSize, Front.getOffset()}; | ||
} | ||
|
||
bool jit_compiler::isHeterogeneousList(ArrayRef<NDRange> NDRanges) { | ||
const auto *FirstSpecifiedLocalSize = findSpecifiedLocalSize(NDRanges); | ||
const auto &ND = FirstSpecifiedLocalSize == NDRanges.end() | ||
? NDRanges.front() | ||
: *FirstSpecifiedLocalSize; | ||
return any_of(NDRanges, [&ND](const auto &Other) { return ND != Other; }); | ||
} | ||
|
||
bool jit_compiler::isValidCombination(llvm::ArrayRef<NDRange> NDRanges) { | ||
if (NDRanges.empty()) { | ||
return false; | ||
} | ||
const auto *FirstSpecifiedLocalSize = findSpecifiedLocalSize(NDRanges); | ||
const auto &ND = FirstSpecifiedLocalSize == NDRanges.end() | ||
? NDRanges.front() | ||
: *FirstSpecifiedLocalSize; | ||
return llvm::all_of(NDRanges, [&ND](const auto &Other) { | ||
return compatibleRanges(ND, Other); | ||
}); | ||
} |
This file contains hidden or 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,37 @@ | ||
//==--------- NDRangesHelper.h - Helpers to handle ND-ranges ---------------==// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef SYCL_FUSION_COMMON_NDRANGESHELPER_H | ||
#define SYCL_FUSION_COMMON_NDRANGESHELPER_H | ||
|
||
#include "Kernel.h" | ||
|
||
#include "llvm/ADT/ArrayRef.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
|
||
namespace jit_compiler { | ||
/// | ||
/// Combine a list of ND-ranges, obtaining the resulting "fused" ND-range. | ||
NDRange combineNDRanges(llvm::ArrayRef<NDRange> NDRanges); | ||
|
||
inline llvm::ArrayRef<NDRange>::const_iterator | ||
findSpecifiedLocalSize(llvm::ArrayRef<NDRange> NDRanges) { | ||
return llvm::find_if( | ||
NDRanges, [](const auto &ND) { return ND.hasSpecificLocalSize(); }); | ||
} | ||
|
||
/// | ||
/// Returns whether the input list of ND-ranges is heterogeneous or not. | ||
bool isHeterogeneousList(llvm::ArrayRef<NDRange> NDRanges); | ||
|
||
/// | ||
/// Return whether a combination of ND-ranges is valid for fusion. | ||
bool isValidCombination(llvm::ArrayRef<NDRange> NDRanges); | ||
} // namespace jit_compiler | ||
|
||
#endif // SYCL_FUSION_COMMON_NDRANGESHELPER_H |
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.