Skip to content

Commit eaf83b4

Browse files
authored
[SYCL][NFC] Use unique_ptr for storing ModuleDesc. (#20358)
This patch resolves a warning about expensive copy in function invocation which accepts ModuleDesc by value, thus taking ownership of ModuleDesc. Now ownership is transferred into functions by using unuqie_ptr. Also, the patch removes many usages of r-value references in function's arguments because r-value reference supposes a ownership transfer but does not oblige that.
1 parent febf259 commit eaf83b4

File tree

8 files changed

+138
-125
lines changed

8 files changed

+138
-125
lines changed

llvm/include/llvm/SYCLPostLink/ESIMDPostSplitProcessing.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include "llvm/ADT/SmallVector.h"
1818
#include "llvm/Support/Error.h"
1919

20+
#include <memory>
21+
2022
namespace llvm {
2123
namespace sycl {
2224

@@ -54,8 +56,8 @@ bool lowerESIMDConstructs(llvm::module_split::ModuleDesc &MD,
5456
/// \p Modified value indicates whether the Module has been modified.
5557
/// \p SplitOccurred value indicates whether split has occurred before or during
5658
/// function's invocation.
57-
Expected<SmallVector<module_split::ModuleDesc, 2>>
58-
handleESIMD(llvm::module_split::ModuleDesc MDesc,
59+
Expected<SmallVector<std::unique_ptr<module_split::ModuleDesc>, 2>>
60+
handleESIMD(std::unique_ptr<llvm::module_split::ModuleDesc> MDesc,
5961
const ESIMDProcessingOptions &Options, bool &Modified,
6062
bool &SplitOccurred);
6163

llvm/include/llvm/SYCLPostLink/ModuleSplitter.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class ModuleDesc {
141141
std::string Name = "";
142142
Properties Props;
143143

144-
ModuleDesc(std::unique_ptr<Module> &&M, StringRef Name = "TOP-LEVEL")
144+
ModuleDesc(std::unique_ptr<Module> M, StringRef Name = "TOP-LEVEL")
145145
: M(std::move(M)), IsTopLevel(true), Name(Name) {
146146
// DeviceLib module doesn't include any entry point,it can be constructed
147147
// using ctor without any entry point related parameter.
@@ -153,13 +153,13 @@ class ModuleDesc {
153153
}
154154
}
155155

156-
ModuleDesc(std::unique_ptr<Module> &&M, EntryPointGroup &&EntryPoints,
156+
ModuleDesc(std::unique_ptr<Module> M, EntryPointGroup &&EntryPoints,
157157
const Properties &Props)
158158
: M(std::move(M)), EntryPoints(std::move(EntryPoints)), Props(Props) {
159159
Name = this->EntryPoints.GroupId;
160160
}
161161

162-
ModuleDesc(std::unique_ptr<Module> &&M, const std::vector<std::string> &Names,
162+
ModuleDesc(std::unique_ptr<Module> M, const std::vector<std::string> &Names,
163163
StringRef Name = "NoName")
164164
: M(std::move(M)), Name(Name) {
165165
rebuildEntryPoints(Names);
@@ -225,7 +225,7 @@ class ModuleDesc {
225225
bool isSpecConstantDefault() const;
226226
void setSpecConstantDefault(bool Value);
227227

228-
ModuleDesc clone() const;
228+
std::unique_ptr<ModuleDesc> clone() const;
229229

230230
std::string makeSymbolTable() const;
231231

@@ -252,7 +252,7 @@ class ModuleDesc {
252252
// from input module that should be included in a split module.
253253
class ModuleSplitterBase {
254254
protected:
255-
ModuleDesc Input;
255+
std::unique_ptr<ModuleDesc> Input;
256256
EntryPointGroupVec Groups;
257257
bool AllowDeviceImageDependencies;
258258

@@ -264,14 +264,15 @@ class ModuleSplitterBase {
264264
return Res;
265265
}
266266

267-
Module &getInputModule() { return Input.getModule(); }
267+
Module &getInputModule() { return Input->getModule(); }
268268

269269
std::unique_ptr<Module> releaseInputModule() {
270-
return Input.releaseModulePtr();
270+
return Input->releaseModulePtr();
271271
}
272272

273273
public:
274-
ModuleSplitterBase(ModuleDesc &&MD, EntryPointGroupVec &&GroupVec,
274+
ModuleSplitterBase(std::unique_ptr<ModuleDesc> MD,
275+
EntryPointGroupVec &&GroupVec,
275276
bool AllowDeviceImageDependencies)
276277
: Input(std::move(MD)), Groups(std::move(GroupVec)),
277278
AllowDeviceImageDependencies(AllowDeviceImageDependencies) {
@@ -288,7 +289,7 @@ class ModuleSplitterBase {
288289

289290
// Gets next subsequence of entry points in an input module and provides split
290291
// submodule containing these entry points and their dependencies.
291-
virtual ModuleDesc nextSplit() = 0;
292+
virtual std::unique_ptr<ModuleDesc> nextSplit() = 0;
292293

293294
// Returns a number of remaining modules, which can be split out using this
294295
// splitter. The value is reduced by 1 each time nextSplit is called.
@@ -298,13 +299,13 @@ class ModuleSplitterBase {
298299
bool hasMoreSplits() const { return remainingSplits() > 0; }
299300
};
300301

301-
SmallVector<ModuleDesc, 2> splitByESIMD(ModuleDesc &&MD,
302-
bool EmitOnlyKernelsAsEntryPoints,
303-
bool AllowDeviceImageDependencies);
302+
SmallVector<std::unique_ptr<ModuleDesc>, 2>
303+
splitByESIMD(std::unique_ptr<ModuleDesc> MD, bool EmitOnlyKernelsAsEntryPoints,
304+
bool AllowDeviceImageDependencies);
304305

305306
std::unique_ptr<ModuleSplitterBase>
306-
getDeviceCodeSplitter(ModuleDesc &&MD, IRSplitMode Mode, bool IROutputOnly,
307-
bool EmitOnlyKernelsAsEntryPoints,
307+
getDeviceCodeSplitter(std::unique_ptr<ModuleDesc> MD, IRSplitMode Mode,
308+
bool IROutputOnly, bool EmitOnlyKernelsAsEntryPoints,
308309
bool AllowDeviceImageDependencies);
309310

310311
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/include/llvm/SYCLPostLink/SpecializationConstants.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "llvm/SYCLLowerIR/SpecConstants.h"
1717
#include "llvm/SYCLPostLink/ModuleSplitter.h"
1818

19+
#include <memory>
1920
#include <optional>
2021

2122
namespace llvm {
@@ -31,9 +32,10 @@ namespace sycl {
3132
/// \returns Boolean value indicating whether the lowering has changed the input
3233
/// modules.
3334
bool handleSpecializationConstants(
34-
llvm::SmallVectorImpl<module_split::ModuleDesc> &MDs,
35+
llvm::SmallVectorImpl<std::unique_ptr<module_split::ModuleDesc>> &MDs,
3536
std::optional<SpecConstantsPass::HandlingMode> Mode,
36-
llvm::SmallVectorImpl<module_split::ModuleDesc> &NewModuleDescs,
37+
llvm::SmallVectorImpl<std::unique_ptr<module_split::ModuleDesc>>
38+
&NewModuleDescs,
3739
bool GenerateModuleDescWithDefaultSpecConsts);
3840

3941
} // namespace sycl

llvm/lib/SYCLPostLink/ESIMDPostSplitProcessing.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,22 @@ buildESIMDLoweringPipeline(const sycl::ESIMDProcessingOptions &Options) {
6363
return MPM;
6464
}
6565

66-
Expected<ModuleDesc> linkModules(ModuleDesc MD1, ModuleDesc MD2) {
66+
Expected<std::unique_ptr<ModuleDesc>>
67+
linkModules(std::unique_ptr<ModuleDesc> MD1, std::unique_ptr<ModuleDesc> MD2) {
6768
std::vector<std::string> Names;
68-
MD1.saveEntryPointNames(Names);
69-
MD2.saveEntryPointNames(Names);
69+
MD1->saveEntryPointNames(Names);
70+
MD2->saveEntryPointNames(Names);
7071
bool LinkError =
71-
llvm::Linker::linkModules(MD1.getModule(), MD2.releaseModulePtr());
72+
llvm::Linker::linkModules(MD1->getModule(), MD2->releaseModulePtr());
7273

7374
if (LinkError)
7475
return createStringError(
75-
formatv("link failed. Module names: {0}, {1}", MD1.Name, MD2.Name));
76+
formatv("link failed. Module names: {0}, {1}", MD1->Name, MD2->Name));
7677

77-
ModuleDesc Res(MD1.releaseModulePtr(), std::move(Names));
78-
Res.assignMergedProperties(MD1, MD2);
79-
Res.Name = (Twine("linked[") + MD1.Name + "," + MD2.Name + "]").str();
78+
auto Res =
79+
std::make_unique<ModuleDesc>(MD1->releaseModulePtr(), std::move(Names));
80+
Res->assignMergedProperties(*MD1, *MD2);
81+
Res->Name = (Twine("linked[") + MD1->Name + "," + MD2->Name + "]").str();
8082
return std::move(Res);
8183
}
8284

@@ -110,11 +112,11 @@ bool sycl::lowerESIMDConstructs(ModuleDesc &MD,
110112
return !Res.areAllPreserved();
111113
}
112114

113-
Expected<SmallVector<ModuleDesc, 2>>
114-
llvm::sycl::handleESIMD(ModuleDesc MDesc,
115+
Expected<SmallVector<std::unique_ptr<ModuleDesc>, 2>>
116+
llvm::sycl::handleESIMD(std::unique_ptr<ModuleDesc> MDesc,
115117
const sycl::ESIMDProcessingOptions &Options,
116118
bool &Modified, bool &SplitOccurred) {
117-
SmallVector<ModuleDesc, 2> Result =
119+
SmallVector<std::unique_ptr<ModuleDesc>, 2> Result =
118120
splitByESIMD(std::move(MDesc), Options.EmitOnlyKernelsAsEntryPoints,
119121
Options.AllowDeviceImageDependencies);
120122

@@ -123,32 +125,32 @@ llvm::sycl::handleESIMD(ModuleDesc MDesc,
123125

124126
SplitOccurred |= Result.size() > 1;
125127

126-
for (ModuleDesc &MD : Result)
127-
if (Options.LowerESIMD && MD.isESIMD())
128-
Modified |= lowerESIMDConstructs(MD, Options);
128+
for (std::unique_ptr<ModuleDesc> &MD : Result)
129+
if (Options.LowerESIMD && MD->isESIMD())
130+
Modified |= lowerESIMDConstructs(*MD, Options);
129131

130132
if (Options.SplitESIMD || Result.size() == 1)
131133
return std::move(Result);
132134

133135
// SYCL/ESIMD splitting is not requested, link back into single module.
134-
int ESIMDInd = Result[0].isESIMD() ? 0 : 1;
136+
int ESIMDInd = Result[0]->isESIMD() ? 0 : 1;
135137
int SYCLInd = 1 - ESIMDInd;
136-
assert(Result[SYCLInd].isSYCL() &&
137-
"Result[SYCLInd].isSYCL() expected to be true.");
138+
assert(Result[SYCLInd]->isSYCL() &&
139+
"Result[SYCLInd]->isSYCL() expected to be true.");
138140

139141
// Make sure that no link conflicts occur.
140-
Result[ESIMDInd].renameDuplicatesOf(Result[SYCLInd].getModule(), ".esimd");
142+
Result[ESIMDInd]->renameDuplicatesOf(Result[SYCLInd]->getModule(), ".esimd");
141143
auto LinkedOrErr = linkModules(std::move(Result[0]), std::move(Result[1]));
142144
if (!LinkedOrErr)
143145
return LinkedOrErr.takeError();
144146

145-
ModuleDesc &Linked = *LinkedOrErr;
146-
Linked.restoreLinkageOfDirectInvokeSimdTargets();
147+
std::unique_ptr<ModuleDesc> &Linked = *LinkedOrErr;
148+
Linked->restoreLinkageOfDirectInvokeSimdTargets();
147149
std::vector<std::string> Names;
148-
Linked.saveEntryPointNames(Names);
150+
Linked->saveEntryPointNames(Names);
149151
// Cleanup may remove some entry points, need to save/rebuild.
150-
Linked.cleanup(Options.AllowDeviceImageDependencies);
151-
Linked.rebuildEntryPoints(Names);
152+
Linked->cleanup(Options.AllowDeviceImageDependencies);
153+
Linked->rebuildEntryPoints(Names);
152154
Result.clear();
153155
Result.emplace_back(std::move(Linked));
154156
Modified = true;

0 commit comments

Comments
 (0)