Skip to content
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

[SYCL] Move module splitting functionality from sycl-post-link to SYCLLowerIR #12622

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
// of the split is new modules containing corresponding callgraph.
//===----------------------------------------------------------------------===//

#pragma once
#ifndef LLVM_SYCLLOWERIR_MODULE_SPLITTER_H
#define LLVM_SYCLLOWERIR_MODULE_SPLITTER_H

#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/Error.h"

#include <memory>
#include <string>
#include <vector>

namespace llvm {
Expand Down Expand Up @@ -229,8 +231,8 @@ class ModuleSplitterBase {
// For device global variables with the 'device_image_scope' property,
// the function checks that there are no usages of a single device global
// variable from kernels grouped to different modules. Otherwise, an error is
// issued and the tool is aborted.
void verifyNoCrossModuleDeviceGlobalUsage();
// returned.
Error verifyNoCrossModuleDeviceGlobalUsage();

virtual ~ModuleSplitterBase() = default;

Expand Down Expand Up @@ -262,3 +264,5 @@ void dumpEntryPoints(const Module &M, bool OnlyKernelsAreEntryPoints = false,
} // namespace module_split

} // namespace llvm

#endif // LLVM_SYCLLOWERIR_MODULE_SPLITTER_H
1 change: 1 addition & 0 deletions llvm/lib/SYCLLowerIR/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ add_llvm_component_library(LLVMSYCLLowerIR
LowerInvokeSimd.cpp
LowerWGLocalMemory.cpp
LowerWGScope.cpp
ModuleSplitter.cpp
MutatePrintfAddrspace.cpp
SYCLAddOptLevelAttribute.cpp
SYCLPropagateAspectsUsage.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
// See comments in the header.
//===----------------------------------------------------------------------===//

#include "ModuleSplitter.h"
#include "Support.h"

#include "llvm/SYCLLowerIR/ModuleSplitter.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
Expand All @@ -23,6 +21,7 @@
#include "llvm/SYCLLowerIR/DeviceGlobals.h"
#include "llvm/SYCLLowerIR/LowerInvokeSimd.h"
#include "llvm/SYCLLowerIR/SYCLUtils.h"
#include "llvm/Support/Error.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Expand Down Expand Up @@ -426,14 +425,15 @@ class ModuleSplitter : public ModuleSplitterBase {
DependencyGraph CG;
};
} // namespace

namespace llvm {
namespace module_split {

void ModuleSplitterBase::verifyNoCrossModuleDeviceGlobalUsage() {
Error ModuleSplitterBase::verifyNoCrossModuleDeviceGlobalUsage() {
const Module &M = getInputModule();
// Early exit if there is only one group
if (Groups.size() < 2)
return;
return Error::success();

// Reverse the EntryPointGroupMap to get a map of entry point -> module's name
unsigned EntryPointNumber = 0;
Expand All @@ -451,19 +451,25 @@ void ModuleSplitterBase::verifyNoCrossModuleDeviceGlobalUsage() {

std::optional<StringRef> VarEntryPointModule{};
auto CheckEntryPointModule = [&VarEntryPointModule, &EntryPointModules,
&GV](const auto *F) {
&GV](const auto *F) -> Error {
auto EntryPointModulesIt = EntryPointModules.find(F);
assert(EntryPointModulesIt != EntryPointModules.end() &&
"There is no group for an entry point");
if (EntryPointModulesIt == EntryPointModules.end())
return createStringError(inconvertibleErrorCode(),
"There is no group for an entry point");

if (!VarEntryPointModule.has_value()) {
VarEntryPointModule = EntryPointModulesIt->second;
return;
}
if (EntryPointModulesIt->second != *VarEntryPointModule) {
error("device_global variable '" + Twine(GV.getName()) +
"' with property \"device_image_scope\" is used in more "
"than one device image.");
return Error::success();
}

if (EntryPointModulesIt->second != *VarEntryPointModule)
return createStringError(
inconvertibleErrorCode(),
"device_global variable '" + Twine(GV.getName()) +
"' with property \"device_image_scope\" is used in more "
"than one device image.");

return Error::success();
};

SmallSetVector<const User *, 32> Workqueue;
Expand All @@ -478,13 +484,18 @@ void ModuleSplitterBase::verifyNoCrossModuleDeviceGlobalUsage() {
continue;
}
if (auto *F = dyn_cast<const Function>(U)) {
if (EntryPointModules.count(F))
CheckEntryPointModule(F);
if (EntryPointModules.count(F)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

A suggestion.
Going forward it might help if you can split such submissions into multiple commits: (1) Code motion without changes to error handling and (2) Handle changes to error handling. it might make it easy to triage errors later on. Thanks

auto E = CheckEntryPointModule(F);
if (E)
return E;
}
}
for (auto *UU : U->users())
Workqueue.insert(UU);
}
}

return Error::success();
}

#ifndef NDEBUG
Expand Down
1 change: 0 additions & 1 deletion llvm/tools/sycl-post-link/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ include_directories(

add_llvm_tool(sycl-post-link
sycl-post-link.cpp
ModuleSplitter.cpp
SpecConstants.cpp
SYCLDeviceLibReqMask.cpp
SYCLKernelParamOptInfo.cpp
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/sycl-post-link/SYCLDeviceRequirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
//===----------------------------------------------------------------------===//

#include "SYCLDeviceRequirements.h"
#include "ModuleSplitter.h"

#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/SYCLLowerIR/ModuleSplitter.h"
#include "llvm/Support/PropertySetIO.h"

#include <set>
Expand Down
9 changes: 6 additions & 3 deletions llvm/tools/sycl-post-link/sycl-post-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// - specialization constant intrinsic transformation
//===----------------------------------------------------------------------===//

#include "ModuleSplitter.h"
#include "SYCLDeviceLibReqMask.h"
#include "SYCLDeviceRequirements.h"
#include "SYCLKernelParamOptInfo.h"
Expand All @@ -40,6 +39,7 @@
#include "llvm/SYCLLowerIR/ESIMD/LowerESIMD.h"
#include "llvm/SYCLLowerIR/HostPipes.h"
#include "llvm/SYCLLowerIR/LowerInvokeSimd.h"
#include "llvm/SYCLLowerIR/ModuleSplitter.h"
#include "llvm/SYCLLowerIR/SYCLUtils.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -1009,8 +1009,11 @@ processInputModule(std::unique_ptr<Module> M) {
Modified |= SplitOccurred;

// FIXME: this check is not performed for ESIMD splits
if (DeviceGlobals)
Splitter->verifyNoCrossModuleDeviceGlobalUsage();
if (DeviceGlobals) {
auto E = Splitter->verifyNoCrossModuleDeviceGlobalUsage();
if (E)
error(toString(std::move(E)));
}

// It is important that we *DO NOT* preserve all the splits in memory at the
// same time, because it leads to a huge RAM consumption by the tool on bigger
Expand Down
Loading