2020using namespace llvm ::util;
2121using namespace llvm ;
2222
23- namespace {
24-
25- ::llvm::Error makeError (const Twine &Msg) {
23+ static llvm::Error makeError (const Twine &Msg) {
2624 return createStringError (std::error_code{}, Msg);
2725}
2826
29- } // anonymous namespace
30-
3127Expected<std::unique_ptr<SYCLPropertySetRegistry>>
3228SYCLPropertySetRegistry::read (const MemoryBuffer *Buf) {
3329 auto Res = std::make_unique<SYCLPropertySetRegistry>();
@@ -37,7 +33,7 @@ SYCLPropertySetRegistry::read(const MemoryBuffer *Buf) {
3733 // See if this line starts a new property set
3834 if (LI->starts_with (" [" )) {
3935 // Parse the category (property name)
40- auto EndPos = LI->rfind (' ]' );
36+ size_t EndPos = LI->rfind (' ]' );
4137 if (EndPos == StringRef::npos)
4238 return makeError (" invalid line: " + *LI);
4339 StringRef Category = LI->substr (1 , EndPos - 1 );
@@ -47,49 +43,46 @@ SYCLPropertySetRegistry::read(const MemoryBuffer *Buf) {
4743 if (!CurPropSet)
4844 return makeError (" property category missing" );
4945 // Parse name and type+value
50- auto Parts = LI->split (' =' );
46+ auto [PropName, PropTypeAndValue] = LI->split (' =' );
5147
52- if (Parts. first . empty () || Parts. second .empty ())
48+ if (PropName. empty () || PropTypeAndValue .empty ())
5349 return makeError (" invalid property line: " + *LI);
54- auto TypeVal = Parts. second .split (' |' );
50+ auto [PropType, PropVal] = PropTypeAndValue .split (' |' );
5551
56- if (TypeVal. first . empty () || TypeVal. second .empty ())
57- return makeError (" invalid property value: " + Parts. second );
52+ if (PropType. empty () || PropVal .empty ())
53+ return makeError (" invalid property value: " + PropTypeAndValue );
5854 APInt Tint;
5955
6056 // Parse type
61- if (TypeVal. first .getAsInteger (10 , Tint))
62- return makeError (" invalid property type: " + TypeVal. first );
57+ if (PropType .getAsInteger (10 , Tint))
58+ return makeError (" invalid property type: " + PropType );
6359 Expected<SYCLPropertyValue::Type> Ttag =
6460 SYCLPropertyValue::getTypeTag (static_cast <int >(Tint.getSExtValue ()));
65- StringRef Val = TypeVal. second ;
61+ StringRef Val = PropVal ;
6662
6763 if (!Ttag)
6864 return Ttag.takeError ();
6965 SYCLPropertyValue Prop (Ttag.get ());
7066
7167 // Parse value depending on its type
72- switch (Ttag.get ()) {
73- case SYCLPropertyValue::Type::UINT32: {
68+ if (Prop.getType () == SYCLPropertyValue::Type::UInt32) {
7469 APInt ValV;
7570 if (Val.getAsInteger (10 , ValV))
7671 return createStringError (std::error_code{},
7772 " invalid property value: " , Val.data ());
7873 Prop.set (static_cast <uint32_t >(ValV.getZExtValue ()));
79- break ;
80- }
81- case SYCLPropertyValue::Type::BYTE_ARRAY: {
74+ } else if (Prop.getType () == SYCLPropertyValue::Type::ByteArray) {
8275 std::vector<char > Output;
76+ // Output resized to maximum output size for base64 decoding
77+ Output.resize (((Val.size () + 3 ) / 4 ) * 3 );
8378 if (Error Err = decodeBase64 (Val, Output))
8479 return std::move (Err);
8580 Prop.set (reinterpret_cast <std::byte *>(Output.data ()), Output.size ());
86- break ;
87- }
88- default :
81+ } else {
8982 return createStringError (std::error_code{},
90- " unsupported property type: " , Ttag. get () );
83+ " unsupported property type\n " );
9184 }
92- (*CurPropSet)[Parts. first ] = std::move (Prop);
85+ (*CurPropSet)[PropName ] = std::move (Prop);
9386 }
9487 if (!CurPropSet)
9588 return makeError (" invalid property set registry" );
@@ -101,50 +94,36 @@ namespace llvm {
10194// Output a property to a stream
10295raw_ostream &operator <<(raw_ostream &Out, const SYCLPropertyValue &Prop) {
10396 Out << static_cast <int >(Prop.getType ()) << ' |' ;
104- switch (Prop.getType ()) {
105- case SYCLPropertyValue::Type::UINT32:
97+ if (Prop.getType () == SYCLPropertyValue::Type::UInt32) {
10698 Out << Prop.asUint32 ();
107- break ;
108- case SYCLPropertyValue::Type::BYTE_ARRAY: {
109- auto PropArr = Prop.asByteArray ();
99+ return Out;
100+ }
101+ if (Prop.getType () == SYCLPropertyValue::Type::ByteArray) {
102+ const std::byte *PropArr = Prop.asByteArray ();
110103 std::vector<std::byte> V (PropArr, PropArr + Prop.getByteArraySize () /
111104 sizeof (std::byte));
112105 Out << encodeBase64 (V);
113- break ;
114- }
115- default :
116- llvm_unreachable (
117- (" unsupported property type: " + utostr (Prop.getType ())).c_str ());
106+ return Out;
118107 }
119- return Out ;
108+ llvm_unreachable ( " unsupported property type " ) ;
120109}
121110} // namespace llvm
122111
123112void SYCLPropertySetRegistry::write (raw_ostream &Out) const {
124- for (const auto &PropSet : PropSetMap) {
125- Out << ' [' << PropSet. first << " ]\n " ;
113+ for (const auto &[PropCategory, Props] : PropSetMap) {
114+ Out << ' [' << PropCategory << " ]\n " ;
126115
127- for (const auto &Props : PropSet. second )
128- Out << Props. first << ' =' << Props. second << ' \n ' ;
116+ for (const auto &[PropName, PropVal] : Props )
117+ Out << PropName << ' =' << PropVal << ' \n ' ;
129118 }
130119}
131120
132121namespace llvm {
133122namespace util {
134123
135- template <> SYCLPropertyValue::Type SYCLPropertyValue::getTypeTag<uint32_t >() {
136- return UINT32;
137- }
138-
139- template <>
140- SYCLPropertyValue::Type SYCLPropertyValue::getTypeTag<std::byte *>() {
141- return BYTE_ARRAY;
142- }
143-
144- SYCLPropertyValue::SYCLPropertyValue (const std::byte *Data, SizeTy DataBitSize)
145- : Ty(BYTE_ARRAY) {
146- constexpr int ByteSizeInBits = 8 ;
147- SizeTy DataSize = (DataBitSize + (ByteSizeInBits - 1 )) / ByteSizeInBits;
124+ SYCLPropertyValue::SYCLPropertyValue (const std::byte *Data,
125+ SizeTy DataBitSize) {
126+ SizeTy DataSize = (DataBitSize + (CHAR_BIT - 1 )) / CHAR_BIT;
148127 constexpr size_t SizeFieldSize = sizeof (SizeTy);
149128
150129 // Allocate space for size and data.
@@ -154,7 +133,7 @@ SYCLPropertyValue::SYCLPropertyValue(const std::byte *Data, SizeTy DataBitSize)
154133 for (size_t I = 0 ; I < SizeFieldSize; ++I) {
155134 auto ByteArrayVal = std::get<std::byte *>(Val);
156135 ByteArrayVal[I] = (std::byte)DataBitSize;
157- DataBitSize >>= ByteSizeInBits ;
136+ DataBitSize >>= CHAR_BIT ;
158137 }
159138 // Append data.
160139 auto ByteArrayVal = std::get<std::byte *>(Val);
@@ -170,38 +149,20 @@ SYCLPropertyValue::SYCLPropertyValue(SYCLPropertyValue &&P) {
170149SYCLPropertyValue &SYCLPropertyValue::operator =(SYCLPropertyValue &&P) {
171150 copy (P);
172151
173- if (P. getType () == BYTE_ARRAY )
152+ if (std::holds_alternative<std::byte *>(Val) )
174153 P.Val = nullptr ;
175- P.Ty = NONE;
176154 return *this ;
177155}
178156
179157SYCLPropertyValue &SYCLPropertyValue::operator =(const SYCLPropertyValue &P) {
180- if (P. getType () == BYTE_ARRAY )
158+ if (std::holds_alternative<std::byte *>(Val) )
181159 *this = SYCLPropertyValue (P.asByteArray (), P.getByteArraySizeInBits ());
182160 else
183161 copy (P);
184162 return *this ;
185163}
186164
187- void SYCLPropertyValue::copy (const SYCLPropertyValue &P) {
188- Ty = P.Ty ;
189- Val = P.Val ;
190- }
191-
192- constexpr char SYCLPropertySetRegistry::SYCL_SPECIALIZATION_CONSTANTS[];
193- constexpr char SYCLPropertySetRegistry::SYCL_DEVICELIB_REQ_MASK[];
194- constexpr char SYCLPropertySetRegistry::SYCL_SPEC_CONSTANTS_DEFAULT_VALUES[];
195- constexpr char SYCLPropertySetRegistry::SYCL_KERNEL_PARAM_OPT_INFO[];
196- constexpr char SYCLPropertySetRegistry::SYCL_PROGRAM_METADATA[];
197- constexpr char SYCLPropertySetRegistry::SYCL_MISC_PROP[];
198- constexpr char SYCLPropertySetRegistry::SYCL_ASSERT_USED[];
199- constexpr char SYCLPropertySetRegistry::SYCL_EXPORTED_SYMBOLS[];
200- constexpr char SYCLPropertySetRegistry::SYCL_IMPORTED_SYMBOLS[];
201- constexpr char SYCLPropertySetRegistry::SYCL_DEVICE_GLOBALS[];
202- constexpr char SYCLPropertySetRegistry::SYCL_DEVICE_REQUIREMENTS[];
203- constexpr char SYCLPropertySetRegistry::SYCL_HOST_PIPES[];
204- constexpr char SYCLPropertySetRegistry::SYCL_VIRTUAL_FUNCTIONS[];
165+ void SYCLPropertyValue::copy (const SYCLPropertyValue &P) { Val = P.Val ; }
205166
206167} // namespace util
207168} // namespace llvm
0 commit comments