Skip to content

Commit 0d5af17

Browse files
committed
Use unique_ptr for Byte Array
Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent 50de423 commit 0d5af17

File tree

2 files changed

+63
-53
lines changed

2 files changed

+63
-53
lines changed

llvm/include/llvm/Support/SYCLPropertySetIO.h

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
namespace llvm {
4545
namespace util {
4646

47+
typedef struct Deleter {
48+
void operator()(std::byte *ptr) const { delete[] ptr; }
49+
} Deleter;
50+
4751
// Represents a SYCL property value. SYCLPropertyValue name is stored in the
4852
// encompassing container.
4953
class SYCLPropertyValue {
@@ -66,20 +70,14 @@ class SYCLPropertyValue {
6670
return static_cast<Type>(T);
6771
}
6872

69-
~SYCLPropertyValue() {
70-
if (std::holds_alternative<std::byte *>(Val)) {
71-
auto ByteArrayVal = std::get<std::byte *>(Val);
72-
if (ByteArrayVal)
73-
delete[] ByteArrayVal;
74-
}
75-
}
73+
~SYCLPropertyValue() {}
7674
SYCLPropertyValue() = default;
7775

7876
SYCLPropertyValue(Type Ty) {
7977
if (Ty == UInt32)
8078
Val = (uint32_t)0;
8179
else if (Ty == ByteArray)
82-
Val = (std::byte *)(0);
80+
Val = std::unique_ptr<std::byte, Deleter>(new std::byte[1], Deleter{});
8381
else
8482
llvm_unreachable_internal("unsupported SYCL property type");
8583
}
@@ -107,14 +105,13 @@ class SYCLPropertyValue {
107105

108106
// Get raw data size in bits.
109107
SizeTy getByteArraySizeInBits() const {
110-
assert(std::holds_alternative<std::byte *>(Val) &&
111-
"must be a byte array value");
112108
SizeTy Res = 0;
113-
114-
for (size_t I = 0; I < sizeof(SizeTy); ++I) {
115-
auto ByteArrayVal = std::get<std::byte *>(Val);
116-
Res |= (SizeTy)ByteArrayVal[I] << (8 * I);
117-
}
109+
if (auto ByteArrayVal =
110+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val)) {
111+
for (size_t I = 0; I < sizeof(SizeTy); ++I)
112+
Res |= (SizeTy)(*ByteArrayVal).get()[I] << (8 * I);
113+
} else
114+
llvm_unreachable("must be a byte array value");
118115
return Res;
119116
}
120117

@@ -133,19 +130,20 @@ class SYCLPropertyValue {
133130

134131
// Get byte array data including the leading bytes encoding the size.
135132
const std::byte *asRawByteArray() const {
136-
assert(std::holds_alternative<std::byte *>(Val) &&
137-
"must be a byte array value");
138-
auto *ByteArrayVal = std::get<std::byte *>(Val);
139-
return ByteArrayVal;
133+
if (auto ByteArrayVal =
134+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val))
135+
return (*ByteArrayVal).get();
136+
else
137+
llvm_unreachable("must be a byte array value");
140138
}
141139

142140
// Get byte array data excluding the leading bytes encoding the size.
143141
const std::byte *asByteArray() const {
144-
assert(std::holds_alternative<std::byte *>(Val) &&
145-
"must be a byte array value");
146-
147-
auto ByteArrayVal = std::get<std::byte *>(Val);
148-
return ByteArrayVal + sizeof(SizeTy);
142+
if (auto ByteArrayVal =
143+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val))
144+
return (*ByteArrayVal).get() + sizeof(SizeTy);
145+
else
146+
llvm_unreachable("must be a byte array value");
149147
}
150148

151149
bool isValid() const { return getType() != None; }
@@ -158,44 +156,45 @@ class SYCLPropertyValue {
158156

159157
// Set property value when data type is 'std::byte *'
160158
void set(std::byte *V, int DataSize) {
161-
assert(std::holds_alternative<std::byte *>(Val) &&
162-
"must be a byte array value");
163159
size_t DataBitSize = DataSize * CHAR_BIT;
164160
constexpr size_t SizeFieldSize = sizeof(SizeTy);
165161
// Allocate space for size and data.
166-
Val = new std::byte[SizeFieldSize + DataSize];
162+
Val = std::unique_ptr<std::byte, Deleter>(
163+
new std::byte[SizeFieldSize + DataSize], Deleter{});
167164

168165
// Write the size into first bytes.
169-
for (size_t I = 0; I < SizeFieldSize; ++I) {
170-
auto ByteArrayVal = std::get<std::byte *>(Val);
171-
ByteArrayVal[I] = (std::byte)DataBitSize;
172-
DataBitSize >>= CHAR_BIT;
173-
}
174-
// Append data.
175-
auto ByteArrayVal = std::get<std::byte *>(Val);
176-
std::memcpy(ByteArrayVal + SizeFieldSize, V, DataSize);
166+
if (auto ByteArrayVal =
167+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val)) {
168+
for (size_t I = 0; I < SizeFieldSize; ++I) {
169+
(*ByteArrayVal).get()[I] = (std::byte)DataBitSize;
170+
DataBitSize >>= CHAR_BIT;
171+
}
172+
// Append data.
173+
std::memcpy((*ByteArrayVal).get() + SizeFieldSize, V, DataSize);
174+
} else
175+
llvm_unreachable("must be a byte array value");
177176
}
178177

179178
Type getType() const {
180179
if (std::holds_alternative<uint32_t>(Val))
181180
return UInt32;
182-
if (std::holds_alternative<std::byte *>(Val))
181+
if (std::holds_alternative<std::unique_ptr<std::byte, Deleter>>(Val))
183182
return ByteArray;
184183
return None;
185184
}
186185

187186
SizeTy size() const {
188187
if (std::holds_alternative<uint32_t>(Val))
189188
return sizeof(uint32_t);
190-
if (std::holds_alternative<std::byte *>(Val))
189+
if (std::holds_alternative<std::unique_ptr<std::byte, Deleter>>(Val))
191190
return getRawByteArraySize();
192191
llvm_unreachable_internal("unsupported SYCL property type");
193192
}
194193

195194
private:
196195
void copy(const SYCLPropertyValue &P);
197196

198-
std::variant<uint32_t, std::byte *> Val;
197+
std::variant<uint32_t, std::unique_ptr<std::byte, Deleter>> Val;
199198
};
200199

201200
/// Structure for specialization of DenseMap in SYCLPropertySetRegistry.

llvm/lib/Support/SYCLPropertySetIO.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,20 @@ SYCLPropertyValue::SYCLPropertyValue(const std::byte *Data,
127127
constexpr size_t SizeFieldSize = sizeof(SizeTy);
128128

129129
// Allocate space for size and data.
130-
Val = new std::byte[SizeFieldSize + DataSize];
130+
Val = std::unique_ptr<std::byte, Deleter>(
131+
new std::byte[SizeFieldSize + DataSize], Deleter{});
131132

132133
// Write the size into first bytes.
133-
for (size_t I = 0; I < SizeFieldSize; ++I) {
134-
auto ByteArrayVal = std::get<std::byte *>(Val);
135-
ByteArrayVal[I] = (std::byte)DataBitSize;
136-
DataBitSize >>= CHAR_BIT;
137-
}
138-
// Append data.
139-
auto ByteArrayVal = std::get<std::byte *>(Val);
140-
std::memcpy(ByteArrayVal + SizeFieldSize, Data, DataSize);
134+
if (auto ByteArrayVal =
135+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val)) {
136+
for (size_t I = 0; I < SizeFieldSize; ++I) {
137+
(*ByteArrayVal).get()[I] = (std::byte)DataBitSize;
138+
DataBitSize >>= CHAR_BIT;
139+
}
140+
// Append data.
141+
std::memcpy((*ByteArrayVal).get() + SizeFieldSize, Data, DataSize);
142+
} else
143+
llvm_unreachable("must be a byte array value");
141144
}
142145

143146
SYCLPropertyValue::SYCLPropertyValue(const SYCLPropertyValue &P) { *this = P; }
@@ -149,20 +152,28 @@ SYCLPropertyValue::SYCLPropertyValue(SYCLPropertyValue &&P) {
149152
SYCLPropertyValue &SYCLPropertyValue::operator=(SYCLPropertyValue &&P) {
150153
copy(P);
151154

152-
if (std::holds_alternative<std::byte *>(Val))
155+
if (std::holds_alternative<std::unique_ptr<std::byte, Deleter>>(Val))
153156
P.Val = nullptr;
154157
return *this;
155158
}
156159

157160
SYCLPropertyValue &SYCLPropertyValue::operator=(const SYCLPropertyValue &P) {
158-
if (std::holds_alternative<std::byte *>(Val))
159-
*this = SYCLPropertyValue(P.asByteArray(), P.getByteArraySizeInBits());
160-
else
161-
copy(P);
161+
copy(P);
162162
return *this;
163163
}
164164

165-
void SYCLPropertyValue::copy(const SYCLPropertyValue &P) { Val = P.Val; }
165+
void SYCLPropertyValue::copy(const SYCLPropertyValue &P) {
166+
if (std::holds_alternative<std::unique_ptr<std::byte, Deleter>>(P.Val)) {
167+
// Allocate space for size and data.
168+
Val = std::unique_ptr<std::byte, Deleter>(
169+
new std::byte[P.getRawByteArraySize()], Deleter{});
170+
if (auto ByteArrayVal =
171+
std::get_if<std::unique_ptr<std::byte, Deleter>>(&Val))
172+
std::memcpy((*ByteArrayVal).get(), P.asRawByteArray(),
173+
P.getRawByteArraySize());
174+
} else
175+
Val = P.asUint32();
176+
}
166177

167178
} // namespace util
168179
} // namespace llvm

0 commit comments

Comments
 (0)