Skip to content

Commit 0759009

Browse files
alibeklfcmeta-codesync[bot]
authored andcommitted
Fix compiler warnings for pip wheel builds (macOS + Windows) (facebookresearch#4989)
Summary: Pull Request resolved: facebookresearch#4989 Fix compiler warnings that appear during pip wheel builds on macOS (AppleClang) and Windows (MSVC). **macOS fixes (AppleClang):** 1. Replace deprecated `sprintf` calls with `snprintf` in all three simdlib headers: - `simdlib_emulated.h` (4 call sites) - `simdlib_avx2.h` (4 call sites) - `simdlib_avx512.h` (3 call sites) AppleClang marks `sprintf` as deprecated. The fix replaces `ptr += sprintf(ptr, fmt, val)` with `ptr += snprintf(ptr, (size_t)(res + sizeof(res) - ptr), fmt, val)`, where the second argument computes the remaining buffer space. This is safe and behavior-preserving: these are debug-only `tostring()` methods that format small integers/floats into 1000-2000 byte buffers, using at most ~256 bytes in the worst case (64 elements × ~4 chars). When output fits (which it always does), `snprintf` returns the same value as `sprintf`, so `ptr` advances identically. 2. Add missing `override` specifiers to fix `-Winconsistent-missing-override`: - `IVFFlatScanner::scan_codes()` in `IndexIVFFlat.h` - `RaBitQHeapHandler::begin()` and `end()` in `IndexRaBitQFastScan.h` - `IDSelectorModulo::is_member()` in `faiss_example_external_module.swig` These methods override virtual base class methods but lacked the `override` keyword. Adding it is purely declarative — no behavioral change, but enables compile-time checking if the base signature changes. **Windows fixes (MSVC):** 3. Guard `#pragma GCC diagnostic` blocks with `#if defined(__GNUC__) || defined(__clang__)` to silence MSVC C4068 "unknown pragma" warnings in: - `simdlib_emulated.h`, `simdlib_avx2.h`, `simdlib_avx512.h` - `IVFlib.cpp`, `PolysemousTraining.cpp` The pragmas only control warning suppression (for `-Wformat-nonliteral`) and have zero effect on code generation. GCC/Clang still see them and behave identically; MSVC skips them (it doesn't have that warning anyway). 4. Fix `%ld` printf format specifiers in `partitioning.cpp` to use `%zu` for `size_t` and `%" PRId64 "` for `int64_t` (MSVC C4477: `long` is 32-bit on Windows, so `%ld` is wrong for both types). These are debug-only `IFV printf` calls gated behind a verbose flag. 5. Fix `size_t` to `int` narrowing in `IndexIVFSpectralHash.cpp` (MSVC C4267): `std::make_unique<RandomRotationMatrix>(d_in, nbit_in)` where `d_in` is `size_t` but the constructor takes `int`. Added `static_cast<int>(d_in)`. Safe because `d_in` is vector dimensionality (typically 64-2048). 6. Fix pointer-to-`long` truncation in `distances_sse-inl.h` (MSVC C4311): Alignment check `(long)ptr & 15` truncates 64-bit pointers on Windows where `long` is 32-bit. Changed to `(uintptr_t)ptr` which is correct on all platforms. The alignment test logic is unchanged. Reviewed By: mnorris11 Differential Revision: D98197799 fbshipit-source-id: 3523ce1d93b7e68c41f9c974e3a3f992f0dbcf39
1 parent 8a80b3d commit 0759009

File tree

11 files changed

+91
-22
lines changed

11 files changed

+91
-22
lines changed

faiss/IVFlib.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,14 @@ void handle_ivf(
601601
sharded_centroids[i].data());
602602
}
603603
char fname[256];
604+
#if defined(__GNUC__) || defined(__clang__)
604605
#pragma GCC diagnostic push
605606
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
607+
#endif
606608
snprintf(fname, 256, filename_template.c_str(), i);
609+
#if defined(__GNUC__) || defined(__clang__)
607610
#pragma GCC diagnostic pop
611+
#endif
608612
faiss::write_index(sharded_index, fname);
609613
delete sharded_index;
610614
}
@@ -655,10 +659,14 @@ void handle_binary_ivf(
655659
sharded_centroids[i].data());
656660
}
657661
char fname[256];
662+
#if defined(__GNUC__) || defined(__clang__)
658663
#pragma GCC diagnostic push
659664
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
665+
#endif
660666
snprintf(fname, 256, filename_template.c_str(), i);
667+
#if defined(__GNUC__) || defined(__clang__)
661668
#pragma GCC diagnostic pop
669+
#endif
662670
faiss::write_index_binary(sharded_index, fname);
663671
delete sharded_index;
664672
}

faiss/IndexIVFFlat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ struct IVFFlatScanner : InvertedListScanner {
9191
size_t list_size,
9292
const uint8_t* codes,
9393
const idx_t* ids,
94-
ResultHandler& handler) const;
94+
ResultHandler& handler) const override;
9595
};
9696

9797
struct IndexIVFFlatDedup : IndexIVFFlat {

faiss/IndexIVFSpectralHash.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ IndexIVFSpectralHash::IndexIVFSpectralHash(
3838
own_invlists_in),
3939
nbit(nbit_in),
4040
period(period_in) {
41-
auto rr = std::make_unique<RandomRotationMatrix>(d_in, nbit_in);
41+
auto rr = std::make_unique<RandomRotationMatrix>(
42+
static_cast<int>(d_in), nbit_in);
4243
rr->init(1234);
4344
vt = rr.release();
4445
own_fields = true;

faiss/IndexRaBitQFastScan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,11 @@ struct RaBitQHeapHandler
279279
rabitq_stats.n_multibit_evaluations += local_multibit_evaluations;
280280
}
281281

282-
void begin(const float* norms) {
282+
void begin(const float* norms) override {
283283
normalizers = norms;
284284
}
285285

286-
void end() {
286+
void end() override {
287287
#pragma omp parallel for if (nq > 100)
288288
for (int64_t q = 0; q < static_cast<int64_t>(nq); q++) {
289289
float* heap_dis = heap_distances + q * k;

faiss/impl/PolysemousTraining.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,14 @@ void PolysemousTraining::optimize_reproduce_distances(
823823

824824
if (log_pattern.size()) {
825825
char fname[256];
826+
#if defined(__GNUC__) || defined(__clang__)
826827
#pragma GCC diagnostic push
827828
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
829+
#endif
828830
snprintf(fname, 256, log_pattern.c_str(), m);
831+
#if defined(__GNUC__) || defined(__clang__)
829832
#pragma GCC diagnostic pop
833+
#endif
830834
printf("opening log file %s\n", fname);
831835
optim.logfile = fopen(fname, "w");
832836
FAISS_THROW_IF_NOT_MSG(optim.logfile, "could not open logfile");
@@ -938,10 +942,14 @@ void PolysemousTraining::optimize_ranking(
938942

939943
if (log_pattern.size()) {
940944
char fname[256];
945+
#if defined(__GNUC__) || defined(__clang__)
941946
#pragma GCC diagnostic push
942947
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
948+
#endif
943949
snprintf(fname, 256, log_pattern.c_str(), m);
950+
#if defined(__GNUC__) || defined(__clang__)
944951
#pragma GCC diagnostic pop
952+
#endif
945953
printf("opening log file %s\n", fname);
946954
optim.logfile = fopen(fname, "w");
947955
FAISS_THROW_IF_NOT_FMT(

faiss/impl/simdlib/simdlib_avx2.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,17 @@ struct simd16uint16_tpl<SIMDLevel::AVX2> : simd256bit_tpl<SIMDLevel::AVX2> {
141141
storeu((void*)bytes);
142142
char res[1000];
143143
char* ptr = res;
144+
#if defined(__GNUC__) || defined(__clang__)
144145
#pragma GCC diagnostic push
145146
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
147+
#endif
146148
for (int idx = 0; idx < 16; idx++) {
147-
ptr += sprintf(ptr, fmt, bytes[idx]);
149+
ptr += snprintf(
150+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
148151
}
152+
#if defined(__GNUC__) || defined(__clang__)
149153
#pragma GCC diagnostic pop
154+
#endif
150155
// strip last ,
151156
ptr[-1] = 0;
152157
return std::string(res);
@@ -462,12 +467,17 @@ struct simd32uint8_tpl<SIMDLevel::AVX2> : simd256bit_tpl<SIMDLevel::AVX2> {
462467
storeu((void*)bytes);
463468
char res[1000];
464469
char* ptr = res;
470+
#if defined(__GNUC__) || defined(__clang__)
465471
#pragma GCC diagnostic push
466472
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
473+
#endif
467474
for (int idx = 0; idx < 32; idx++) {
468-
ptr += sprintf(ptr, fmt, bytes[idx]);
475+
ptr += snprintf(
476+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
469477
}
478+
#if defined(__GNUC__) || defined(__clang__)
470479
#pragma GCC diagnostic pop
480+
#endif
471481
// strip last ,
472482
ptr[-1] = 0;
473483
return std::string(res);
@@ -601,12 +611,17 @@ struct simd8uint32_tpl<SIMDLevel::AVX2> : simd256bit_tpl<SIMDLevel::AVX2> {
601611
storeu((void*)bytes);
602612
char res[1000];
603613
char* ptr = res;
614+
#if defined(__GNUC__) || defined(__clang__)
604615
#pragma GCC diagnostic push
605616
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
617+
#endif
606618
for (int idx = 0; idx < 8; idx++) {
607-
ptr += sprintf(ptr, fmt, bytes[idx]);
619+
ptr += snprintf(
620+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[idx]);
608621
}
622+
#if defined(__GNUC__) || defined(__clang__)
609623
#pragma GCC diagnostic pop
624+
#endif
610625
// strip last ,
611626
ptr[-1] = 0;
612627
return std::string(res);
@@ -727,7 +742,8 @@ struct simd8float32_tpl<SIMDLevel::AVX2> : simd256bit_tpl<SIMDLevel::AVX2> {
727742
char res[1000];
728743
char* ptr = res;
729744
for (int idx = 0; idx < 8; idx++) {
730-
ptr += sprintf(ptr, "%g,", tab[idx]);
745+
ptr += snprintf(
746+
ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[idx]);
731747
}
732748
// strip last ,
733749
ptr[-1] = 0;

faiss/impl/simdlib/simdlib_avx512.h

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,17 @@ struct simd32uint16_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
127127
storeu((void*)bytes);
128128
char res[2000];
129129
char* ptr = res;
130+
#if defined(__GNUC__) || defined(__clang__)
131+
#pragma GCC diagnostic push
132+
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
133+
#endif
130134
for (int i = 0; i < 32; i++) {
131-
ptr += sprintf(ptr, fmt, bytes[i]);
135+
ptr += snprintf(
136+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[i]);
132137
}
138+
#if defined(__GNUC__) || defined(__clang__)
139+
#pragma GCC diagnostic pop
140+
#endif
133141
// strip last ,
134142
ptr[-1] = 0;
135143
return std::string(res);
@@ -263,9 +271,17 @@ struct simd64uint8_tpl<SIMDLevel::AVX512> : simd512bit_tpl<SIMDLevel::AVX512> {
263271
storeu((void*)bytes);
264272
char res[2000];
265273
char* ptr = res;
274+
#if defined(__GNUC__) || defined(__clang__)
275+
#pragma GCC diagnostic push
276+
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
277+
#endif
266278
for (int i = 0; i < 64; i++) {
267-
ptr += sprintf(ptr, fmt, bytes[i]);
279+
ptr += snprintf(
280+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, bytes[i]);
268281
}
282+
#if defined(__GNUC__) || defined(__clang__)
283+
#pragma GCC diagnostic pop
284+
#endif
269285
// strip last ,
270286
ptr[-1] = 0;
271287
return std::string(res);
@@ -374,7 +390,8 @@ struct simd16float32_tpl<SIMDLevel::AVX512>
374390
char res[1000];
375391
char* ptr = res;
376392
for (int i = 0; i < 16; i++) {
377-
ptr += sprintf(ptr, "%g,", tab[i]);
393+
ptr += snprintf(
394+
ptr, (size_t)(res + sizeof(res) - ptr), "%g,", tab[i]);
378395
}
379396
ptr[-1] = 0;
380397
return std::string(res);

faiss/impl/simdlib/simdlib_emulated.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,17 @@ struct simd16uint16_tpl<SIMDLevel::NONE> : simd256bit_tpl<SIMDLevel::NONE> {
129129

130130
std::string elements_to_string(const char* fmt) const {
131131
char res[1000], *ptr = res;
132+
#if defined(__GNUC__) || defined(__clang__)
132133
#pragma GCC diagnostic push
133134
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
135+
#endif
134136
for (int i = 0; i < 16; i++) {
135-
ptr += sprintf(ptr, fmt, u16[i]);
137+
ptr += snprintf(
138+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, u16[i]);
136139
}
140+
#if defined(__GNUC__) || defined(__clang__)
137141
#pragma GCC diagnostic pop
142+
#endif
138143
// strip last ,
139144
ptr[-1] = 0;
140145
return std::string(res);
@@ -507,12 +512,16 @@ struct simd32uint8_tpl<SIMDLevel::NONE> : simd256bit_tpl<SIMDLevel::NONE> {
507512

508513
std::string elements_to_string(const char* fmt) const {
509514
char res[1000], *ptr = res;
515+
#if defined(__GNUC__) || defined(__clang__)
510516
#pragma GCC diagnostic push
511517
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
518+
#endif
512519
for (int i = 0; i < 32; i++) {
513-
ptr += sprintf(ptr, fmt, u8[i]);
520+
ptr += snprintf(ptr, (size_t)(res + sizeof(res) - ptr), fmt, u8[i]);
514521
}
522+
#if defined(__GNUC__) || defined(__clang__)
515523
#pragma GCC diagnostic pop
524+
#endif
516525
// strip last ,
517526
ptr[-1] = 0;
518527
return std::string(res);
@@ -707,12 +716,17 @@ struct simd8uint32_tpl<SIMDLevel::NONE> : simd256bit_tpl<SIMDLevel::NONE> {
707716

708717
std::string elements_to_string(const char* fmt) const {
709718
char res[1000], *ptr = res;
719+
#if defined(__GNUC__) || defined(__clang__)
710720
#pragma GCC diagnostic push
711721
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
722+
#endif
712723
for (int i = 0; i < 8; i++) {
713-
ptr += sprintf(ptr, fmt, u32[i]);
724+
ptr += snprintf(
725+
ptr, (size_t)(res + sizeof(res) - ptr), fmt, u32[i]);
714726
}
727+
#if defined(__GNUC__) || defined(__clang__)
715728
#pragma GCC diagnostic pop
729+
#endif
716730
// strip last ,
717731
ptr[-1] = 0;
718732
return std::string(res);
@@ -863,7 +877,8 @@ struct simd8float32_tpl<SIMDLevel::NONE> : simd256bit_tpl<SIMDLevel::NONE> {
863877
std::string tostring() const {
864878
char res[1000], *ptr = res;
865879
for (int i = 0; i < 8; i++) {
866-
ptr += sprintf(ptr, "%g,", f32[i]);
880+
ptr += snprintf(
881+
ptr, (size_t)(res + sizeof(res) - ptr), "%g,", f32[i]);
867882
}
868883
// strip last ,
869884
ptr[-1] = 0;

faiss/python/faiss_example_external_module.swig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ struct IDSelectorModulo : faiss::IDSelector {
7777

7878
IDSelectorModulo(int mod): mod(mod) {}
7979

80-
bool is_member(faiss::idx_t id) const {
80+
bool is_member(faiss::idx_t id) const override {
8181
return id % mod == 0;
8282
}
8383

faiss/utils/partitioning.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <faiss/utils/partitioning.h>
99

1010
#include <cassert>
11+
#include <cinttypes>
1112
#include <cmath>
1213

1314
#include <faiss/impl/FaissAssert.h>
@@ -432,7 +433,7 @@ uint16_t simd_partition_fuzzy_with_bounds(
432433
// lower bound inclusive, upper exclusive
433434
size_t s0 = s0i, s1 = s1i + 1;
434435

435-
IFV printf("bounds: %ld %ld\n", s0, s1 - 1);
436+
IFV printf("bounds: %zu %zu\n", s0, s1 - 1);
436437

437438
int thresh;
438439
size_t n_eq = 0, n_lt = 0;
@@ -444,7 +445,7 @@ uint16_t simd_partition_fuzzy_with_bounds(
444445
count_lt_and_eq<C>(vals, n, thresh, n_lt, n_eq);
445446

446447
IFV printf(
447-
" [%ld %ld] thresh=%d n_lt=%ld n_eq=%ld, q=%ld:%ld/%ld\n",
448+
" [%zu %zu] thresh=%d n_lt=%zu n_eq=%zu, q=%zu:%zu/%zu\n",
448449
s0,
449450
s1,
450451
thresh,
@@ -481,7 +482,8 @@ uint16_t simd_partition_fuzzy_with_bounds(
481482
// number of equal values to keep
482483
int64_t n_eq_1 = q - n_lt;
483484

484-
IFV printf("shrink: thresh=%d q=%ld n_eq_1=%ld\n", thresh, q, n_eq_1);
485+
IFV printf(
486+
"shrink: thresh=%d q=%zu n_eq_1=%" PRId64 "\n", thresh, q, n_eq_1);
485487
if (n_eq_1 < 0) { // happens when > q elements are at lower bound
486488
assert(s0 + 1 == s1);
487489
q = q_min;
@@ -491,14 +493,15 @@ uint16_t simd_partition_fuzzy_with_bounds(
491493
thresh++;
492494
}
493495
n_eq_1 = q;
494-
IFV printf(" override: thresh=%d n_eq_1=%ld\n", thresh, n_eq_1);
496+
IFV printf(
497+
" override: thresh=%d n_eq_1=%" PRId64 "\n", thresh, n_eq_1);
495498
} else {
496499
assert(n_eq_1 <= n_eq);
497500
}
498501

499502
size_t wp = simd_compress_array<C>(vals, ids, n, thresh, n_eq_1);
500503

501-
IFV printf("wp=%ld\n", wp);
504+
IFV printf("wp=%zu\n", wp);
502505
assert(wp == q);
503506
if (q_out) {
504507
*q_out = q;

0 commit comments

Comments
 (0)