Skip to content

Commit 57cc5fe

Browse files
[XPTI][NFC] Fix -Wconversion warnings (#18786)
`-Wconversion` is a must-have flag for us in according with internal guidelines (see `AddSecurityFlags.cmake`), but we have never built with it + `-Werror` so there are multiple places in `xpti` subproject where we did potentially incorrect implicit conversions.
1 parent 35b7cd6 commit 57cc5fe

File tree

10 files changed

+57
-46
lines changed

10 files changed

+57
-46
lines changed

sycl/source/detail/graph_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ exec_graph_impl::enqueueNodeDirect(sycl::context Ctx,
818818

819819
#ifdef XPTI_ENABLE_INSTRUMENTATION
820820
const bool xptiEnabled = xptiTraceEnabled();
821-
int32_t StreamID = xpti::invalid_id;
821+
int32_t StreamID = xpti::invalid_id<>;
822822
xpti_td *CmdTraceEvent = nullptr;
823823
uint64_t InstanceID = 0;
824824
if (xptiEnabled) {

sycl/source/detail/queue_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ void queue_impl::wait(const detail::code_location &CodeLoc) {
585585
void *TelemetryEvent = nullptr;
586586
uint64_t IId;
587587
std::string Name;
588-
int32_t StreamID = xpti::invalid_id;
588+
int32_t StreamID = xpti::invalid_id<>;
589589
if (xptiEnabled) {
590590
StreamID = xptiRegisterStream(SYCL_STREAM_NAME);
591591
TelemetryEvent = instrumentationProlog(CodeLoc, Name, StreamID, IId);

sycl/source/handler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ event handler::finalize() {
565565
#endif
566566
auto EnqueueKernel = [&]() {
567567
#ifdef XPTI_ENABLE_INSTRUMENTATION
568-
int32_t StreamID = xpti::invalid_id;
568+
int32_t StreamID = xpti::invalid_id<>;
569569
xpti_td *CmdTraceEvent = nullptr;
570570
uint64_t InstanceID = 0;
571571
if (xptiEnabled) {

xpti/include/xpti/xpti_data_types.h

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ inline xpti::uid128_t make_uid128(uint64_t FileID, uint64_t FuncID, int Line,
105105
int Col) {
106106
xpti::uid128_t UID;
107107
UID.p1 = (FileID << 32) | FuncID;
108-
UID.p2 = ((uint64_t)Col << 32) | Line;
108+
UID.p2 = ((uint64_t)Col << 32) | (uint64_t)Line;
109109
UID.instance = 0;
110110
return UID;
111111
}
@@ -147,7 +147,15 @@ struct hash_t {
147147
///
148148
/// @param value A 64-bit integer for which the bit count is to be calculated.
149149
/// @return The number of bits required to represent the input value.
150-
int bit_count(uint64_t value) { return (int)log2(value) + 1; }
150+
unsigned bit_count(uint64_t value) {
151+
// FIXME: using std::log2 is imprecise. There is no guarantee that the
152+
// implementation has actual integer overload for log2 and it is allowed to
153+
// do a static_cast<double>(value) to compute the result. Not every integer
154+
// is represetntable as floating-point, meaning that byte representation of
155+
// value as double may not be the same as for integer, resulting in
156+
// different result.
157+
return static_cast<unsigned>(std::log2(value)) + 1;
158+
}
151159

152160
/// @brief Compacts the file ID, function ID, line number, and column number
153161
/// into a single 64-bit hash value.
@@ -164,7 +172,8 @@ struct hash_t {
164172
/// @param col An integer that represents the column number.
165173
/// @return A 64-bit hash value that represents the compacted file ID,
166174
/// function ID, line number, and column number.
167-
uint64_t compact(uint64_t file_id, uint64_t func_id, int line, int col) {
175+
uint64_t compact(uint64_t file_id, uint64_t func_id, uint64_t line,
176+
uint64_t col) {
168177
uint64_t funcB, lineB, colB;
169178
// Figure out the bit counts necessary to represent the input values
170179
funcB = bit_count(func_id);
@@ -177,9 +186,9 @@ struct hash_t {
177186
hash <<= funcB;
178187
hash = hash | func_id;
179188
hash <<= lineB;
180-
hash = hash | (uint64_t)line;
189+
hash = hash | line;
181190
hash <<= colB;
182-
hash = hash | (uint64_t)col;
191+
hash = hash | col;
183192
#ifdef DEBUG
184193
uint64_t fileB = bit_count(file_id);
185194
std::cout << "Total bits: " << (fileB + funcB + lineB + colB) << "\n";
@@ -203,7 +212,7 @@ struct hash_t {
203212
/// @param line An integer that represents the line number.
204213
/// @return A 64-bit hash value that represents the compacted file ID,
205214
/// function ID, and line number.
206-
uint64_t compact_short(uint64_t file_id, uint64_t func_id, int line) {
215+
uint64_t compact_short(uint64_t file_id, uint64_t func_id, uint64_t line) {
207216
uint64_t funcB, lineB;
208217
funcB = bit_count(func_id);
209218
lineB = bit_count(line);
@@ -215,7 +224,7 @@ struct hash_t {
215224
hash <<= funcB;
216225
hash = hash | func_id;
217226
hash <<= lineB;
218-
hash = hash | (uint64_t)line;
227+
hash = hash | line;
219228
#ifdef DEBUG
220229
uint64_t fileB = bit_count(file_id);
221230
std::cout << "Total bits: " << (fileB + funcB + lineB) << "\n";
@@ -312,7 +321,8 @@ struct uid_t {
312321
} // namespace xpti
313322

314323
namespace xpti {
315-
constexpr int invalid_id = -1;
324+
template <typename T = uint32_t>
325+
constexpr T invalid_id = std::numeric_limits<T>::max();
316326
constexpr uint64_t invalid_uid = 0;
317327
constexpr uint8_t default_vendor = 0;
318328

@@ -428,11 +438,11 @@ struct payload_t {
428438
/// Absolute path of the source file; may have to to be unicode string
429439
const char *source_file = nullptr;
430440
/// Line number information to correlate the trace point
431-
uint32_t line_no = invalid_id;
441+
uint32_t line_no = invalid_id<>;
432442
/// For a complex statement, column number may be needed to resolve the
433443
/// trace point; currently none of the compiler builtins return a valid
434444
/// column no
435-
uint32_t column_no = invalid_id;
445+
uint32_t column_no = invalid_id<>;
436446
/// Kernel/lambda/function address
437447
const void *code_ptr_va = nullptr;
438448
/// Internal bookkeeping slot - do not change.
@@ -451,10 +461,10 @@ struct payload_t {
451461
// indicates a partial but valid payload.
452462
payload_t(const void *codeptr) {
453463
code_ptr_va = codeptr;
454-
name = nullptr; ///< Invalid name string pointer
455-
source_file = nullptr; ///< Invalid source file string pointer
456-
line_no = invalid_id; ///< Invalid line number
457-
column_no = invalid_id; ///< Invalid column number
464+
name = nullptr; ///< Invalid name string pointer
465+
source_file = nullptr; ///< Invalid source file string pointer
466+
line_no = invalid_id<>; ///< Invalid line number
467+
column_no = invalid_id<>; ///< Invalid column number
458468
if (codeptr) {
459469
flags = (uint64_t)payload_flag_t::CodePointerAvailable;
460470
}
@@ -517,8 +527,8 @@ struct payload_t {
517527
/// Capture the rest of the parameters
518528
name = kname;
519529
source_file = sf;
520-
line_no = line;
521-
column_no = col;
530+
line_no = static_cast<uint32_t>(line);
531+
column_no = static_cast<uint32_t>(col);
522532
if (kname && kname[0] != '\0') {
523533
flags = (uint64_t)payload_flag_t::NameAvailable;
524534
}

xpti/include/xpti/xpti_trace_framework.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ typedef uint16_t (*xpti_register_user_defined_et_t)(const char *, uint8_t);
10181018
typedef xpti::trace_event_data_t *(*xpti_make_event_t)(
10191019
const char *, xpti::payload_t *, uint16_t, xpti::trace_activity_type_t,
10201020
uint64_t *);
1021-
typedef const xpti::trace_event_data_t *(*xpti_find_event_t)(int64_t);
1021+
typedef const xpti::trace_event_data_t *(*xpti_find_event_t)(uint64_t);
10221022
typedef const xpti::payload_t *(*xpti_query_payload_t)(
10231023
xpti::trace_event_data_t *);
10241024
typedef const xpti::payload_t *(*xpti_query_payload_by_uid_t)(uint64_t uid);

xpti/include/xpti/xpti_trace_framework.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,9 +961,10 @@ class tracepoint_scope_t {
961961
MData = const_cast<xpti_tracepoint_t *>(xptiGetTracepointScopeData());
962962
if (!MData) {
963963
if (funcName && fileName)
964-
init(funcName, fileName, line, column);
964+
init(funcName, fileName, static_cast<uint32_t>(line),
965+
static_cast<uint32_t>(column));
965966
else
966-
init(callerFuncName, nullptr, 0, 0);
967+
init(callerFuncName, nullptr, 0u, 0u);
967968
} else {
968969
MTraceEvent = MData->event_ref();
969970
}

xpti/src/xpti_proxy.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <unordered_map>
1414
#include <vector>
1515

16-
enum functions_t {
16+
enum functions_t : unsigned {
1717
XPTI_FRAMEWORK_INITIALIZE,
1818
XPTI_FRAMEWORK_FINALIZE,
1919
XPTI_INITIALIZE,
@@ -69,7 +69,7 @@ enum functions_t {
6969

7070
namespace xpti {
7171
class ProxyLoader {
72-
std::unordered_map<int, const char *> m_function_names = {
72+
std::unordered_map<unsigned, const char *> m_function_names = {
7373
{XPTI_FRAMEWORK_INITIALIZE, "xptiFrameworkInitialize"},
7474
{XPTI_FRAMEWORK_FINALIZE, "xptiFrameworkFinalize"},
7575
{XPTI_INITIALIZE, "xptiInitialize"},
@@ -150,8 +150,8 @@ class ProxyLoader {
150150

151151
inline bool noErrors() { return m_loaded; }
152152

153-
void *functionByIndex(int index) {
154-
if (index >= XPTI_FRAMEWORK_INITIALIZE && index < XPTI_FW_API_COUNT) {
153+
void *functionByIndex(unsigned index) {
154+
if (index < XPTI_FW_API_COUNT) {
155155
return reinterpret_cast<void *>(m_dispatch_table[index]);
156156
}
157157
return nullptr;
@@ -233,7 +233,7 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedTracePoint(
233233
return (*(xpti_register_user_defined_tp_t)f)(tool_name, user_defined_tp);
234234
}
235235
}
236-
return xpti::invalid_id;
236+
return xpti::invalid_id<uint16_t>;
237237
}
238238

239239
XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType(
@@ -246,7 +246,7 @@ XPTI_EXPORT_API uint16_t xptiRegisterUserDefinedEventType(
246246
user_defined_event);
247247
}
248248
}
249-
return xpti::invalid_id;
249+
return xpti::invalid_id<uint16_t>;
250250
}
251251

252252
XPTI_EXPORT_API xpti::result_t xptiInitialize(const char *stream, uint32_t maj,
@@ -278,7 +278,7 @@ XPTI_EXPORT_API uint64_t xptiGetUniversalId() {
278278
return (*reinterpret_cast<xpti_get_universal_id_t>(f))();
279279
}
280280
}
281-
return xpti::invalid_id;
281+
return xpti::invalid_id<uint64_t>;
282282
}
283283

284284
XPTI_EXPORT_API void xptiSetUniversalId(uint64_t uid) {
@@ -329,7 +329,7 @@ XPTI_EXPORT_API uint64_t xptiGetUniqueId() {
329329
return (*(xpti_get_unique_id_t)f)();
330330
}
331331
}
332-
return xpti::invalid_id;
332+
return xpti::invalid_id<uint64_t>;
333333
}
334334

335335
XPTI_EXPORT_API xpti::string_id_t xptiRegisterString(const char *string,
@@ -341,7 +341,7 @@ XPTI_EXPORT_API xpti::string_id_t xptiRegisterString(const char *string,
341341
return (*(xpti_register_string_t)f)(string, table_string);
342342
}
343343
}
344-
return xpti::invalid_id;
344+
return xpti::invalid_id<xpti::string_id_t>;
345345
}
346346

347347
XPTI_EXPORT_API const char *xptiLookupString(xpti::string_id_t id) {
@@ -363,7 +363,7 @@ xptiRegisterObject(const char *data, size_t size, uint8_t type) {
363363
return (*(xpti_register_object_t)f)(data, size, type);
364364
}
365365
}
366-
return xpti::invalid_id;
366+
return xpti::invalid_id<xpti::object_id_t>;
367367
}
368368

369369
XPTI_EXPORT_API xpti::object_data_t xptiLookupObject(xpti::object_id_t id) {
@@ -395,7 +395,7 @@ XPTI_EXPORT_API uint8_t xptiRegisterStream(const char *stream_name) {
395395
return (*(xpti_register_stream_t)f)(stream_name);
396396
}
397397
}
398-
return xpti::invalid_id;
398+
return xpti::invalid_id<uint8_t>;
399399
}
400400

401401
XPTI_EXPORT_API xpti::result_t xptiUnregisterStream(const char *stream_name) {
@@ -689,7 +689,7 @@ XPTI_EXPORT_API uint8_t xptiGetDefaultStreamID() {
689689
return (*(xpti_get_default_stream_id_t)f)();
690690
}
691691
}
692-
return xpti::invalid_id;
692+
return xpti::invalid_id<uint8_t>;
693693
}
694694

695695
XPTI_EXPORT_API xpti::result_t xptiSetDefaultStreamID(uint8_t stream_id) {
@@ -747,4 +747,4 @@ xptiSetDefaultTraceType(xpti::trace_point_type_t trace_type) {
747747
}
748748
}
749749
return xpti::result_t::XPTI_RESULT_FAIL;
750-
}
750+
}

xptifw/include/xpti_int64_hash_table.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Hash64x64Table {
6363
#endif
6464
return KeyLoc->second; // We found it, so we return the Value
6565
} else
66-
return xpti::invalid_id;
66+
return xpti::invalid_id<int64_t>;
6767
}
6868

6969
// Add a <Key, Value> pair to the hash table. If the Key already exists, this
@@ -121,7 +121,7 @@ class Hash64x64Table {
121121
#endif
122122
return ValLoc->second;
123123
} else
124-
return xpti::invalid_id;
124+
return xpti::invalid_id<int64_t>;
125125
}
126126

127127
void printStatistics() {

xptifw/include/xpti_string_table.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ class StringTable {
6161
// in the string table is returned through the default argument
6262
xpti::string_id_t add(const char *str, const char **ref_str = nullptr) {
6363
if (!str)
64-
return xpti::invalid_id;
64+
return xpti::invalid_id<xpti::string_id_t>;
6565

6666
std::string LocalStr = str;
6767
return add(LocalStr, ref_str);
6868
}
6969

7070
xpti::string_id_t add(std::string str, const char **ref_str = nullptr) {
7171
if (str.empty())
72-
return xpti::invalid_id;
72+
return xpti::invalid_id<xpti::string_id_t>;
7373

7474
// Lock-free lookup to see if the string exists in the table; XPTI has
7575
// always had this as lock-free, but if instability occurs, we can use a
@@ -126,7 +126,7 @@ class StringTable {
126126
if (ref_str)
127127
*ref_str = nullptr;
128128

129-
return xpti::invalid_id;
129+
return xpti::invalid_id<xpti::string_id_t>;
130130
}
131131
}
132132

@@ -142,7 +142,7 @@ class StringTable {
142142
}
143143
// The MMutex will be released here!
144144
}
145-
return xpti::invalid_id;
145+
return xpti::invalid_id<xpti::string_id_t>;
146146
}
147147

148148
// The reverse query allows one to get the string from the string_id_t that

xptifw/src/xpti_trace_framework.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ struct TracePointImpl : xpti_payload_t,
204204
/// @brief Event data for the trace point.
205205
xpti::trace_event_data_t MEvent;
206206
/// @brief Cached Function string ID for the trace point.
207-
int32_t MFuncID = xpti::invalid_id;
207+
int32_t MFuncID = xpti::invalid_id<>;
208208
/// @brief Cached File string ID for the trace point.
209-
int32_t MFileID = xpti::invalid_id;
209+
int32_t MFileID = xpti::invalid_id<>;
210210
/// @brief Iterator for the metadata associated with the trace point.
211211
xpti::metadata_t::iterator MCurr;
212212

@@ -944,7 +944,7 @@ class Tracepoints {
944944
return xpti::result_t::XPTI_RESULT_INVALIDARG;
945945

946946
string_id_t KeyID = MStringTableRef.add(Key);
947-
if (KeyID == xpti::invalid_id) {
947+
if (KeyID == xpti::invalid_id<string_id_t>) {
948948
return xpti::result_t::XPTI_RESULT_INVALIDARG;
949949
}
950950

@@ -2115,7 +2115,7 @@ class Framework {
21152115

21162116
string_id_t registerString(const char *String, char **TableString) {
21172117
if (!TableString || !String)
2118-
return xpti::invalid_id;
2118+
return xpti::invalid_id<string_id_t>;
21192119

21202120
*TableString = 0;
21212121

@@ -2134,7 +2134,7 @@ class Framework {
21342134

21352135
object_id_t registerObject(const char *Object, size_t Size, uint8_t Type) {
21362136
if (!Object)
2137-
return xpti::invalid_id;
2137+
return xpti::invalid_id<object_id_t>;
21382138

21392139
return MObjectTable.insert(std::string_view(Object, Size), Type);
21402140
}

0 commit comments

Comments
 (0)