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] Make PropertySetRegistry being owning it's content #12582

Merged
merged 4 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions clang/tools/clang-linker-wrapper/SYCLOffloadWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ struct Wrapper {
addPropertySetToModule(const PropertySet &PropSet) {
SmallVector<Constant *> PropInits;
for (const auto &Prop : PropSet) {
Constant *PropName = addStringToModule(Prop.first, "prop");
Constant *PropName = addStringToModule(Prop.first(), "prop");
Constant *PropValAddr = nullptr;
Constant *PropType =
ConstantInt::get(Type::getInt32Ty(C), Prop.second.getType());
Expand Down Expand Up @@ -497,7 +497,7 @@ struct Wrapper {
std::pair<Constant *, Constant *> Props =
addPropertySetToModule(PropSet.second);
// get the next the middle column element
auto *Category = addStringToModule(PropSet.first, "SYCL_PropSetName");
auto *Category = addStringToModule(PropSet.first(), "SYCL_PropSetName");
PropSetsInits.push_back(ConstantStruct::get(SyclPropSetTy, Category,
Props.first, Props.second));
}
Expand Down
4 changes: 2 additions & 2 deletions clang/tools/clang-offload-wrapper/ClangOffloadWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ class BinaryWrapper {
std::vector<Constant *> PropInits;

for (const auto &Prop : PropSet) {
Constant *PropName = addStringToModule(Prop.first, "prop");
Constant *PropName = addStringToModule(Prop.first(), "prop");
Constant *PropValAddr = nullptr;
Constant *PropType =
ConstantInt::get(Type::getInt32Ty(C), Prop.second.getType());
Expand Down Expand Up @@ -921,7 +921,7 @@ class BinaryWrapper {
if (!Props)
return Props.takeError();
// get the next the middle column element
auto *Category = addStringToModule(PropSet.first, "SYCL_PropSetName");
auto *Category = addStringToModule(PropSet.first(), "SYCL_PropSetName");
PropSetsInits.push_back(ConstantStruct::get(
getSyclPropSetTy(), Category, Props.get().first, Props.get().second));
}
Expand Down
56 changes: 30 additions & 26 deletions llvm/include/llvm/Support/PropertySetIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@
#ifndef LLVM_SUPPORT_PROPERTYSETIO_H
#define LLVM_SUPPORT_PROPERTYSETIO_H

#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"

#include <istream>
#include <map>
#include <memory>
#include <string>
#include <type_traits>

namespace llvm {
namespace util {

Expand Down Expand Up @@ -175,14 +169,18 @@ class PropertyValue {
} Val;
};

// A property set. Preserves insertion order when iterating elements.
using PropertySet = MapVector<StringRef, PropertyValue>;
/// A property set. Doesn't preserves order of elements.
using PropertySet = StringMap<PropertyValue>;

// A "registry" of multiple property sets. Maps a property set name to its
// contents. Can be read/written.
/// A registry of property sets. Maps a property set name to its
/// content.
///
/// The order of keys is not preserved because Hash Map is used.
/// However, write() method prints content sorted by Category names
/// and sorted by keys. This is made only for testing, don't rely on this.
class PropertySetRegistry {
public:
using MapTy = MapVector<StringRef, PropertySet>;
using MapTy = StringMap<PropertySet>;

// Specific property category names used by tools.
static constexpr char SYCL_SPECIALIZATION_CONSTANTS[] =
Expand All @@ -199,44 +197,50 @@ class PropertySetRegistry {
static constexpr char SYCL_DEVICE_REQUIREMENTS[] = "SYCL/device requirements";
static constexpr char SYCL_HOST_PIPES[] = "SYCL/host pipes";

// Function for bulk addition of an entire property set under given category
// (property set name).
/// Function for bulk addition of an entire property set in the given
/// \p Category .
template <typename MapTy> void add(StringRef Category, const MapTy &Props) {
using KeyTy = typename MapTy::value_type::first_type;
static_assert(std::is_same<typename std::remove_const<KeyTy>::type,
llvm::StringRef>::value,
"wrong key type");
assert(PropSetMap.find(Category) == PropSetMap.end() &&
"category already added");
auto &PropSet = PropSetMap[Category];

for (const auto &Prop : Props)
PropSet.insert_or_assign(Prop.first, PropertyValue(Prop.second));
}

/// Function for bulk addition of an entire property set in the given \p
/// Category .
template <typename ValueTy>
void add(StringRef Category, const StringMap<ValueTy> &Props) {
assert(PropSetMap.find(Category) == PropSetMap.end() &&
"category already added");
auto &PropSet = PropSetMap[Category];

for (const auto &Prop : Props)
PropSet.insert({Prop.first, PropertyValue(Prop.second)});
PropSet.insert_or_assign(Prop.first(), PropertyValue(Prop.getValue()));
}

// Function to add a property to a given category (property set name).
/// Adds the given \p PropVal with the given \p PropName into the given \p
/// Category .
template <typename T>
void add(StringRef Category, StringRef PropName, const T &PropVal) {
auto &PropSet = PropSetMap[Category];
PropSet.insert({PropName, PropertyValue(PropVal)});
}

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

// Dumps a property set registry to a stream.
/// Dumps the property set registry to the given \p Out stream.
void write(raw_ostream &Out) const;

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

// Retrieves a property set with given name.
/// Retrieves a property set with given \p Name .
PropertySet &operator[](StringRef Name) { return PropSetMap[Name]; }
// Constant access to the underlying map.
/// Constant access to the underlying map.
const MapTy &getPropSets() const { return PropSetMap; }

private:
Expand Down
22 changes: 17 additions & 5 deletions llvm/lib/Support/PropertySetIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,23 @@ raw_ostream &operator<<(raw_ostream &Out, const PropertyValue &Prop) {
} // namespace llvm

void PropertySetRegistry::write(raw_ostream &Out) const {
for (const auto &PropSet : PropSetMap) {
Out << "[" << PropSet.first << "]\n";

for (const auto &Props : PropSet.second) {
Out << std::string(Props.first) << "=" << Props.second << "\n";
SmallVector<StringRef, 16> Keys1(PropSetMap.size());
for (const auto &[I, PS] : enumerate(PropSetMap))
Keys1[I] = PS.first();

sort(Keys1.begin(), Keys1.end());
for (auto K1 : Keys1) {
const auto &PropSet = PropSetMap.at(K1);
Out << "[" << K1 << "]\n";

SmallVector<StringRef, 16> Keys2(PropSet.size());
for (const auto &[I, Props] : enumerate(PropSet))
Keys2[I] = Props.first();

sort(Keys2.begin(), Keys2.end());
for (auto K2 : Keys2) {
const auto &Prop = PropSet.at(K2);
Out << K2 << "=" << Prop << "\n";
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ attributes #2 = { convergent "frame-pointer"="all" "no-trapping-math"="true" "st
!9 = !{!"_ZTSN2cl4sycl14kernel_handlerE", !5, i64 0}

; CHECK-PROP: [SYCL/specialization constants]
; CHECK-PROP-NEXT: 9f47062a80eecfa7____ZL8coeff_id=2|gNAAAAAAAAAAAAAAAAAAAQAAAAQAAAAAEAAAAQAAAAgAAAAAIAAAAQAAAAwAAAAAMAAAAQAAAAABAAAAQAAAAQAAAAQBAAAAUAAAAQAAAAgBAAAAYAAAAQAAAAwBAAAAcAAAAQAAAAACAAAAgAAAAQAAAAA
; CHECK-PROP-NEXT: 405761736d5a1797____ZL9coeff_id2=2|gNAAAAAAAAQCAAAAAAAAAQAAAAgCAAAAEAAAAQAAAAwCAAAAIAAAAQAAAAADAAAAMAAAAQAAAAQDAAAAQAAAAQAAAAgDAAAAUAAAAQAAAAwDAAAAYAAAAQAAAAAEAAAAcAAAAQAAAAQEAAAAgAAAAQAAAAA
; CHECK-PROP-NEXT: 6da74a122db9f35d____ZL9coeff_id3=2|AGAAAAAAAAgEAAAAAAAAAQAAAAwEAAAAEAAAAQAAAAAFAAAAIAAAAQAAAAQFAAAAQAAAAgAAAAA
; CHECK-PROP-NEXT: 9f47062a80eecfa7____ZL8coeff_id=2|gNAAAAAAAAAAAAAAAAAAAQAAAAQAAAAAEAAAAQAAAAgAAAAAIAAAAQAAAAwAAAAAMAAAAQAAAAABAAAAQAAAAQAAAAQBAAAAUAAAAQAAAAgBAAAAYAAAAQAAAAwBAAAAcAAAAQAAAAACAAAAgAAAAQAAAAA

; CHECK-PROP: [SYCL/specialization constants default values]
; CHECK-PROP-NEXT: all=2|AMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAg/AAAAA0MzMIQzMzoAZmZGDEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
19 changes: 10 additions & 9 deletions llvm/test/tools/sycl-post-link/spec-constants/SYCL-2020.ll
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,15 @@ attributes #3 = { nounwind }
; CHECK-RT-SAME: i32 [[#SCID17]], i32 4, i32 4}

; CHECK-PROPS: [SYCL/specialization constants]
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_halfEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z6id_intEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z9id_composEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_compos2EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_vectorEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marrayEE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray2EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray3EE=2|
; CHECK-PROPS: _ZTS14name_generatorIL_Z10id_marray4EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_compos2EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_compos3EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray2EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray3EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marray4EE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_marrayEE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z10id_vectorEE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z6id_intEE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z9id_composEE=2|
; CHECK-PROPS-NEXT: _ZTS14name_generatorIL_Z9id_halfEE=2|
; CHECK-PROPS-DEF: [SYCL/specialization constants default values]
; CHECK-PROPS-DEF: all=2|
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ attributes #4 = { convergent }
; CHECK-IR: ![[#MN3]] = !{%struct.coeff2_str_aligned_t { %"class.std::array" zeroinitializer, i64 0, [7 x i8] undef, i8 undef }}

; CHECK-PROP: [SYCL/specialization constants]
; CHECK-PROP-NEXT: df991fa0adf9bad8____ZL8coeff_id2=2|
; CHECK-PROP-NEXT: ef880fa09cf7a9d7____ZL8coeff_id=2|

; CHECK-PROP: [SYCL/specialization constants default values]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ attributes #5 = { nounwind }
; Most important information from the corresponding encoded data is the size of
; the specialization constants, i.e. 8 and 1 bytes respectively.
; CHECK: [SYCL/specialization constants]
; CHECK-NEXT: 9d329ad59055e972____ZL12StructSpecId=2|gBAAAAAAAAAAAAAAAAAAAgAAAAA
; CHECK-NEXT: 9d329ad59055e972____ZL10BoolSpecId=2|gBAAAAAAAAQAAAAAAAAAAEAAAAA
; CHECK-NEXT: 9d329ad59055e972____ZL12StructSpecId=2|gBAAAAAAAAAAAAAAAAAAAgAAAAA

; Ensure that the default values are correct.
; IBAAAAAAAAAFAAAAjBAAAEA is decoded to "0x48 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x14
Expand Down
19 changes: 11 additions & 8 deletions llvm/tools/sycl-post-link/sycl-post-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
if (KernelReqdWorkGroupSize.empty())
continue;
MetadataNames.push_back(Func.getName().str() + "@reqd_work_group_size");
ProgramMetadata.insert({MetadataNames.back(), KernelReqdWorkGroupSize});
ProgramMetadata.insert_or_assign(MetadataNames.back(),
KernelReqdWorkGroupSize);
}

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

StringRef GlobalID = getGlobalVariableUniqueId(GV);
MetadataNames.push_back(GlobalID.str() + "@global_id_mapping");
ProgramMetadata.insert({MetadataNames.back(), GV.getName()});
ProgramMetadata.insert_or_assign(MetadataNames.back(), GV.getName());
}
}
if (MD.isESIMD()) {
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"isEsimdImage", true});
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert_or_assign("isEsimdImage",
true);
}
{
StringRef RegAllocModeAttr = "sycl-register-alloc-mode";
Expand Down Expand Up @@ -534,17 +536,18 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
}

if (OptLevel != -1)
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"optLevel", OptLevel});
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert_or_assign("optLevel",
OptLevel);
}
{
std::vector<StringRef> FuncNames = getKernelNamesUsingAssert(M);
for (const StringRef &FName : FuncNames)
PropSet[PropSetRegTy::SYCL_ASSERT_USED].insert({FName, true});
PropSet[PropSetRegTy::SYCL_ASSERT_USED].insert_or_assign(FName, true);
}

{
if (isModuleUsingAsan(M))
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert({"asanUsed", true});
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert_or_assign("asanUsed", true);
}

if (GlobProps.EmitDeviceGlobalPropSet) {
Expand All @@ -560,8 +563,8 @@ std::string saveModuleProperties(module_split::ModuleDesc &MD,
}

if (MD.isSpecConstantDefault())
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert(
{"specConstsReplacedWithDefault", 1});
PropSet[PropSetRegTy::SYCL_MISC_PROP].insert_or_assign(
"specConstsReplacedWithDefault", 1);

std::error_code EC;
std::string SCFile = makeResultFileName(".prop", I, Suff);
Expand Down
6 changes: 3 additions & 3 deletions sycl/test/basic_tests/SYCL-2020-spec-const-ids-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ int main() {
}

// CHECK-PROP: [SYCL/specialization constants]
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX:[a-z0-9]+]]____ZL5Val23
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL10ConstantId
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX:[a-z0-9]+]]____ZL10ConstantId
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL11SecondValue
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL11SpecConst42
//
// CHECK-PROP-NEXT: [[UNIQUE_PREFIX]]____ZL5Val23

// CHECK-IR: !sycl.specialization-constants = !{![[#MD0:]], ![[#MD1:]], ![[#MD2:]], ![[#MD3:]]}
// CHECK-IR: ![[#MD0]] = !{!"[[UNIQUE_PREFIX:[a-z0-9]+]]____ZL5Val23", i32 [[#ID:]]
// CHECK-IR: ![[#MD1]] = !{!"[[UNIQUE_PREFIX]]____ZL10ConstantId", i32 [[#ID+1]]
Expand Down
Loading