Skip to content

Commit 43201db

Browse files
authored
[SYCL] Make PropertySetRegistry being owning it's content (#12582)
Before this patch PropertySetRegistry used StringRefs that point at external data that was supposed to outlive the instance of PropertySetRegistry. It wasn't obvious and it might lead to memory bugs. This patch makes PropertySetRegistry owning it's content as containers usually do. Also documentation is aligned with doxygen BKSM.
1 parent a105055 commit 43201db

File tree

6 files changed

+57
-52
lines changed

6 files changed

+57
-52
lines changed

llvm/include/llvm/Support/PropertySetIO.h

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@
3434
#define LLVM_SUPPORT_PROPERTYSETIO_H
3535

3636
#include "llvm/ADT/MapVector.h"
37+
#include "llvm/ADT/SmallString.h"
3738
#include "llvm/ADT/StringRef.h"
3839
#include "llvm/Support/Error.h"
3940
#include "llvm/Support/MemoryBuffer.h"
4041
#include "llvm/Support/raw_ostream.h"
41-
42-
#include <istream>
43-
#include <map>
44-
#include <memory>
45-
#include <string>
46-
#include <type_traits>
42+
#include "llvm/Support/xxhash.h"
4743

4844
namespace llvm {
4945
namespace util {
@@ -175,14 +171,28 @@ class PropertyValue {
175171
} Val;
176172
};
177173

178-
// A property set. Preserves insertion order when iterating elements.
179-
using PropertySet = MapVector<StringRef, PropertyValue>;
174+
/// Structure for specialization of DenseMap in PropertySetRegistry.
175+
struct PropertySetKeyInfo {
176+
static unsigned getHashValue(const SmallString<16> &K) { return xxHash64(K); }
177+
178+
static SmallString<16> getEmptyKey() { return SmallString<16>(""); }
180179

181-
// A "registry" of multiple property sets. Maps a property set name to its
182-
// contents. Can be read/written.
180+
static SmallString<16> getTombstoneKey() { return SmallString<16>("_"); }
181+
182+
static bool isEqual(StringRef L, StringRef R) { return L == R; }
183+
};
184+
185+
using PropertyMapTy = DenseMap<SmallString<16>, unsigned, PropertySetKeyInfo>;
186+
/// A property set. Preserves insertion order when iterating elements.
187+
using PropertySet = MapVector<SmallString<16>, PropertyValue, PropertyMapTy>;
188+
189+
/// A registry of property sets. Maps a property set name to its
190+
/// content.
191+
///
192+
/// The order of keys is preserved and corresponds to the order of insertion.
183193
class PropertySetRegistry {
184194
public:
185-
using MapTy = MapVector<StringRef, PropertySet>;
195+
using MapTy = MapVector<SmallString<16>, PropertySet, PropertyMapTy>;
186196

187197
// Specific property category names used by tools.
188198
static constexpr char SYCL_SPECIALIZATION_CONSTANTS[] =
@@ -199,44 +209,38 @@ class PropertySetRegistry {
199209
static constexpr char SYCL_DEVICE_REQUIREMENTS[] = "SYCL/device requirements";
200210
static constexpr char SYCL_HOST_PIPES[] = "SYCL/host pipes";
201211

202-
// Function for bulk addition of an entire property set under given category
203-
// (property set name).
212+
/// Function for bulk addition of an entire property set in the given
213+
/// \p Category .
204214
template <typename MapTy> void add(StringRef Category, const MapTy &Props) {
205-
using KeyTy = typename MapTy::value_type::first_type;
206-
static_assert(std::is_same<typename std::remove_const<KeyTy>::type,
207-
llvm::StringRef>::value,
208-
"wrong key type");
209-
210215
assert(PropSetMap.find(Category) == PropSetMap.end() &&
211216
"category already added");
212217
auto &PropSet = PropSetMap[Category];
213218

214219
for (const auto &Prop : Props)
215-
PropSet.insert({Prop.first, PropertyValue(Prop.second)});
220+
PropSet.insert_or_assign(Prop.first, PropertyValue(Prop.second));
216221
}
217222

218-
// Function to add a property to a given category (property set name).
223+
/// Adds the given \p PropVal with the given \p PropName into the given \p
224+
/// Category .
219225
template <typename T>
220226
void add(StringRef Category, StringRef PropName, const T &PropVal) {
221227
auto &PropSet = PropSetMap[Category];
222228
PropSet.insert({PropName, PropertyValue(PropVal)});
223229
}
224230

225-
// Parses and creates a property set registry.
231+
/// Parses from the given \p Buf a property set registry.
226232
static Expected<std::unique_ptr<PropertySetRegistry>>
227233
read(const MemoryBuffer *Buf);
228234

229-
// Dumps a property set registry to a stream.
235+
/// Dumps the property set registry to the given \p Out stream.
230236
void write(raw_ostream &Out) const;
231237

232-
// Start iterator of all preperty sets in the registry.
233238
MapTy::const_iterator begin() const { return PropSetMap.begin(); }
234-
// End iterator of all preperty sets in the registry.
235239
MapTy::const_iterator end() const { return PropSetMap.end(); }
236240

237-
// Retrieves a property set with given name.
241+
/// Retrieves a property set with given \p Name .
238242
PropertySet &operator[](StringRef Name) { return PropSetMap[Name]; }
239-
// Constant access to the underlying map.
243+
/// Constant access to the underlying map.
240244
const MapTy &getPropSets() const { return PropSetMap; }
241245

242246
private:

llvm/lib/Support/PropertySetIO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void PropertySetRegistry::write(raw_ostream &Out) const {
126126
Out << "[" << PropSet.first << "]\n";
127127

128128
for (const auto &Props : PropSet.second) {
129-
Out << std::string(Props.first) << "=" << Props.second << "\n";
129+
Out << Props.first << "=" << Props.second << "\n";
130130
}
131131
}
132132
}

llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,16 @@ attributes #3 = { nounwind }
268268
; CHECK-RT-SAME: i32 [[#SCID17]], i32 4, i32 4}
269269

270270
; CHECK-PROPS: [SYCL/specialization constants]
271-
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_halfEE=2|
272-
; CHECK-PROPS: _ZTS14name_generatorIL_Z6id_intEE=2|
273-
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_composEE=2|
274-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_compos2EE=2|
275-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_vectorEE=2|
276-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marrayEE=2|
277-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray2EE=2|
278-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray3EE=2|
279-
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray4EE=2|
271+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z9id_halfEE=2|
272+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z6id_intEE=2|
273+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z9id_composEE=2|
274+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_compos2EE=2|
275+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_compos3EE=2|
276+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_vectorEE=2|
277+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marrayEE=2|
278+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray2EE=2|
279+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray3EE=2|
280+
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray4EE=2|
280281
; CHECK-PROPS-DEF: [SYCL/specialization constants default values]
281282
; CHECK-PROPS-DEF: all=2|
282283
; CHECK-LOG: sycl.specialization-constants

llvm/test/tools/sycl-post-link/spec-constants/SYCL2020-struct-with-undef-padding.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ attributes #4 = { convergent }
8585

8686
; CHECK-PROP: [SYCL/specialization constants]
8787
; CHECK-PROP-NEXT: ef880fa09cf7a9d7____ZL8coeff_id=2|
88+
; CHECK-PROP-NEXT: df991fa0adf9bad8____ZL8coeff_id2=2|
8889
; CHECK-LOG: sycl.specialization-constants
8990
; CHECK-LOG:[[UNIQUE_PREFIX:[0-9a-zA-Z]+]]={0, 0, 4}
9091
; CHECK-LOG:[[UNIQUE_PREFIX]]={1, 4, 4}

llvm/tools/sycl-post-link/sycl-post-link.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
434434
// so they won't make it into the export list. Should the check be
435435
// F->getCallingConv() != CallingConv::SPIR_KERNEL?
436436
if (F->getCallingConv() == CallingConv::SPIR_FUNC) {
437-
PropSet[PropSetRegTy::SYCL_EXPORTED_SYMBOLS].insert(
438-
{F->getName(), true});
437+
PropSet.add(PropSetRegTy::SYCL_EXPORTED_SYMBOLS, F->getName(), true);
439438
}
440439
}
441440
}
@@ -444,16 +443,15 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
444443
SmallVector<std::string, 4> MetadataNames;
445444

446445
if (GlobProps.EmitProgramMetadata) {
447-
auto &ProgramMetadata = PropSet[PropSetRegTy::SYCL_PROGRAM_METADATA];
448-
449446
// Add reqd_work_group_size information to program metadata
450447
for (const Function &Func : M.functions()) {
451448
std::vector<uint32_t> KernelReqdWorkGroupSize =
452449
getKernelReqdWorkGroupSizeMetadata(Func);
453450
if (KernelReqdWorkGroupSize.empty())
454451
continue;
455452
MetadataNames.push_back(Func.getName().str() + "@reqd_work_group_size");
456-
ProgramMetadata.insert({MetadataNames.back(), KernelReqdWorkGroupSize});
453+
PropSet.add(PropSetRegTy::SYCL_PROGRAM_METADATA, MetadataNames.back(),
454+
KernelReqdWorkGroupSize);
457455
}
458456

459457
// Add global_id_mapping information with mapping between device-global
@@ -464,11 +462,12 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
464462

465463
StringRef GlobalID = getGlobalVariableUniqueId(GV);
466464
MetadataNames.push_back(GlobalID.str() + "@global_id_mapping");
467-
ProgramMetadata.insert({MetadataNames.back(), GV.getName()});
465+
PropSet.add(PropSetRegTy::SYCL_PROGRAM_METADATA, MetadataNames.back(),
466+
GV.getName());
468467
}
469468
}
470469
if (MD.isESIMD()) {
471-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"isEsimdImage", true});
470+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, "isEsimdImage", true);
472471
}
473472
{
474473
StringRef RegAllocModeAttr = "sycl-register-alloc-mode";
@@ -482,8 +481,8 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
482481
return true;
483482
});
484483
if (HasRegAllocMode) {
485-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert(
486-
{RegAllocModeAttr, RegAllocModeVal});
484+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, RegAllocModeAttr,
485+
RegAllocModeVal);
487486
}
488487
}
489488

@@ -499,7 +498,7 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
499498
return true;
500499
});
501500
if (HasGRFSize) {
502-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({GRFSizeAttr, GRFSizeVal});
501+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, GRFSizeAttr, GRFSizeVal);
503502
}
504503
}
505504

@@ -534,17 +533,17 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
534533
}
535534

536535
if (OptLevel != -1)
537-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"optLevel", OptLevel});
536+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, "optLevel", OptLevel);
538537
}
539538
{
540539
std::vector<StringRef> FuncNames = getKernelNamesUsingAssert(M);
541540
for (const StringRef &FName : FuncNames)
542-
PropSet[PropSetRegTy::SYCL_ASSERT_USED].insert({FName, true});
541+
PropSet.add(PropSetRegTy::SYCL_ASSERT_USED, FName, true);
543542
}
544543

545544
{
546545
if (isModuleUsingAsan(M))
547-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"asanUsed", true});
546+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, "asanUsed", true);
548547
}
549548

550549
if (GlobProps.EmitDeviceGlobalPropSet) {
@@ -560,8 +559,8 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
560559
}
561560

562561
if (MD.isSpecConstantDefault())
563-
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert(
564-
{"specConstsReplacedWithDefault", 1});
562+
PropSet.add(PropSetRegTy::SYCL_MISC_PROP, "specConstsReplacedWithDefault",
563+
1);
565564

566565
std::error_code EC;
567566
std::string SCFile = makeResultFileName(".prop", I, Suff);

sycl/test/basic_tests/SYCL-2020-spec-const-ids-order.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main() {
5050
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL10ConstantId
5151
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL11SecondValue
5252
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL11SpecConst42
53-
//
53+
5454
// CHECK-IR: !sycl.specialization-constants = !{![[#MD0:]], ![[#MD1:]], ![[#MD2:]], ![[#MD3:]]}
5555
// CHECK-IR: ![[#MD0]] = !{!"[[UNIQUE_PREFIX:[a-z0-9]+]]____ZL5Val23", i32 [[#ID:]]
5656
// CHECK-IR: ![[#MD1]] = !{!"[[UNIQUE_PREFIX]]____ZL10ConstantId", i32 [[#ID+1]]

0 commit comments

Comments
 (0)