Skip to content

Commit

Permalink
[SYCL] Move module splitting functionality from sycl-post-link into S…
Browse files Browse the repository at this point in the history
…YCLLowerIR

This is a part of migration to New Offloading model and
clang-linker-wrapper tool.
  • Loading branch information
maksimsab committed Feb 6, 2024
1 parent f7bdae8 commit b34ffd5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
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)) {
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
8 changes: 5 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,10 @@ 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();
CHECK_AND_EXIT(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

0 comments on commit b34ffd5

Please sign in to comment.