Skip to content

Commit 5cf8bcd

Browse files
cyyeverfacebook-github-bot
authored andcommitted
Initialize variables (#4420)
Summary: X-link: facebookresearch/FBGEMM#1509 There are some changes: 1. ensure that variables are initialised by `cppcoreguidelines-init-variables` check. 2. enable "Wunused-variable" and "Wmaybe-uninitialized" warnings. 3. fix all warnings. Pull Request resolved: #4420 Differential Revision: D77746768 Pulled By: q10
1 parent e72c729 commit 5cf8bcd

File tree

69 files changed

+334
-380
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+334
-380
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ InheritParentConfig: true
77
# @nolint
88
Checks: '
99
-*,
10+
cppcoreguidelines-init-variables,
1011
bugprone-argument-comment,
1112
misc-use-internal-linkage,
1213
modernize*,

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,11 @@ else(MSVC)
208208
string(APPEND CMAKE_CXX_FLAGS " -Wunknown-pragmas")
209209
string(APPEND CMAKE_CXX_FLAGS " -Wimplicit-fallthrough")
210210
string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-aliasing")
211+
string(APPEND CMAKE_CXX_FLAGS " -Wunused-variable")
211212
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 17.0.0)
212213
string(APPEND CMAKE_CXX_FLAGS " -Wno-vla-cxx-extension")
214+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
215+
string(APPEND CMAKE_CXX_FLAGS " -Wmaybe-uninitialized")
213216
endif()
214217
target_compile_options(fbgemm_avx2 PRIVATE
215218
"-m64" "-mavx2" "-mf16c" "-mfma")

bench/AlignedVec.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ class aligned_allocator {
107107

108108
// Mallocator wraps malloc().
109109
void* pv = nullptr;
110-
int ret;
110+
int ret = 0;
111111
#ifdef _MSC_VER
112112
pv = _aligned_malloc(n * sizeof(T), Alignment);
113-
ret = 0;
114113
#else
115114
ret = posix_memalign(&pv, Alignment, n * sizeof(T));
116115
#endif

bench/BenchUtils.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ aligned_vector<float> getRandomSparseVector(
150150
std::sort(sorted_res.begin(), sorted_res.end());
151151
int32_t numZeros =
152152
size - static_cast<int32_t>(std::round(size * fractionNonZeros));
153-
float thr;
154153
if (numZeros) {
155-
thr = sorted_res[numZeros - 1];
154+
float thr = sorted_res[numZeros - 1];
156155

157156
for (auto& f : res) {
158157
if (f <= thr) {

bench/BenchUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#ifdef _OPENMP
3030
#include <omp.h>
31+
#include <cmath>
3132
#endif
3233

3334
#ifdef USE_MKL
@@ -136,8 +137,6 @@ double measureWithWarmup(
136137
{
137138
#endif
138139
for (int i = 0; i < measuredIterations; ++i) {
139-
std::chrono::time_point<std::chrono::high_resolution_clock> start, end;
140-
141140
const auto thread_id = useOpenMP ? fbgemm_get_thread_num() : 0;
142141

143142
if (thread_id == 0) {
@@ -149,7 +148,7 @@ double measureWithWarmup(
149148
#pragma omp barrier
150149
}
151150
#endif
152-
start = std::chrono::high_resolution_clock::now();
151+
auto start = std::chrono::high_resolution_clock::now();
153152

154153
fn();
155154

@@ -159,7 +158,7 @@ double measureWithWarmup(
159158
}
160159
#endif
161160

162-
end = std::chrono::high_resolution_clock::now();
161+
auto end = std::chrono::high_resolution_clock::now();
163162
auto dur =
164163
std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
165164

@@ -256,7 +255,6 @@ void performance_test(
256255
#endif
257256

258257
std::string type;
259-
double gflops, gbs, ttot;
260258
for (auto s : shapes) {
261259
int m = s[0];
262260
int n = s[1];
@@ -266,6 +264,7 @@ void performance_test(
266264
aligned_vector<int> Aint(m * k);
267265
randFill(Aint, 0, 4);
268266
std::vector<aligned_vector<float>> A;
267+
A.reserve(num_instances);
269268
for (int i = 0; i < num_instances; ++i) {
270269
A.emplace_back(Aint.begin(), Aint.end());
271270
}
@@ -321,6 +320,7 @@ void performance_test(
321320

322321
double nflops = 2.0 * m * n * k;
323322
double nbytes = 4.0 * m * k + sizeof(btype) * 1.0 * k * n + 4.0 * m * n;
323+
double gflops = 0, gbs = 0, ttot = 0.0;
324324

325325
// warm up MKL and fbgemm
326326
// check correctness at the same time

bench/EmbeddingIndexRemappingBenchmark.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ static int run_benchmark(
135135
}
136136

137137
int main() {
138-
int batch_size;
139-
int num_rows;
140-
int average_len;
138+
int batch_size = 0;
139+
int num_rows = 0;
140+
int average_len = 0;
141141

142142
vector<vector<int>> inputs(GetInputs_());
143143

bench/EmbeddingSpMDMBenchmark.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#endif
1313
#include <algorithm>
1414
#include <cassert>
15-
#include <chrono>
1615
#include <cmath>
1716
#include <cstdint>
1817
#include <iomanip>

bench/EmbeddingSpMDMNBit2Benchmark.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#endif
1313
#include <algorithm>
1414
#include <cassert>
15-
#include <chrono>
1615
#include <cmath>
1716
#include <cstdint>
1817
#include <iomanip>

bench/EmbeddingSpMDMNBitBenchmark.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ static int run_benchmark(
492492
}
493493

494494
int main() {
495-
int batch_size;
496-
int num_rows;
497-
int embedding_dim;
498-
int average_len;
495+
int batch_size = 0;
496+
int num_rows = 0;
497+
int embedding_dim = 0;
498+
int average_len = 0;
499499

500500
vector<vector<int>> inputs(GetInputs_());
501501

bench/EmbeddingSpMDMNBitRowWiseSparseBenchmark.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ static int run_benchmark(
325325
}
326326

327327
int main() {
328-
int batch_size;
329-
int num_rows;
330-
int embedding_dim;
331-
int average_len;
328+
int batch_size = 0;
329+
int num_rows = 0;
330+
int embedding_dim = 0;
331+
int average_len = 0;
332332

333333
vector<vector<int>> inputs(GetInputs_());
334334

bench/RowwiseAdagradBenchmark.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ static void run_benchmark(
192192
}
193193

194194
int main() {
195-
int num_rows;
196-
int block_size;
197-
uint64_t param_size;
195+
int num_rows = 0;
196+
int block_size = 0;
197+
uint64_t param_size = 0;
198198
vector<vector<int>> inputs(GetInputs_());
199199

200200
for (auto isIndex64b : vector<bool>{true, false}) {

bench/SparseAdagradBenchmark.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ static void run_benchmark(
199199
}
200200

201201
int main() {
202-
int num_rows;
203-
int block_size;
204-
uint64_t param_size;
202+
int num_rows = 0;
203+
int block_size = 0;
204+
uint64_t param_size = 0;
205205
vector<vector<int>> inputs(GetInputs_());
206206

207207
for (auto isIndex64b : vector<bool>{true, false}) {

include/fbgemm/ConvUtils.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,26 @@
1616
namespace fbgemm {
1717

1818
template <int N, int... Vals>
19-
constexpr
20-
typename std::enable_if<N == sizeof...(Vals), std::array<int, N>>::type
21-
array_of_ones() {
19+
constexpr std::enable_if_t<N == sizeof...(Vals), std::array<int, N>>
20+
array_of_ones() {
2221
return std::array<int, N>{{Vals...}};
2322
}
2423

2524
template <int N, int... Vals>
26-
constexpr
27-
typename std::enable_if<N != sizeof...(Vals), std::array<int, N>>::type
28-
array_of_ones() {
25+
constexpr std::enable_if_t<N != sizeof...(Vals), std::array<int, N>>
26+
array_of_ones() {
2927
return array_of_ones<N, Vals..., 1>();
3028
}
3129

3230
template <int N, int... Vals>
33-
constexpr
34-
typename std::enable_if<N == sizeof...(Vals), std::array<int, N>>::type
35-
array_of_zeroes() {
31+
constexpr std::enable_if_t<N == sizeof...(Vals), std::array<int, N>>
32+
array_of_zeroes() {
3633
return std::array<int, N>{{Vals...}};
3734
}
3835

3936
template <int N, int... Vals>
40-
constexpr
41-
typename std::enable_if<N != sizeof...(Vals), std::array<int, N>>::type
42-
array_of_zeroes() {
37+
constexpr std::enable_if_t<N != sizeof...(Vals), std::array<int, N>>
38+
array_of_zeroes() {
4339
return array_of_zeroes<N, Vals..., 0>();
4440
}
4541

include/fbgemm/FloatConversion.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#pragma once
1010

11+
#include <math.h>
12+
1113
#include <cassert>
1214
#include <climits>
1315
#include <cstdint>
@@ -211,7 +213,7 @@ template <typename Src, typename Tgt, RoundingMode RoundingMode>
211213
} // namespace detail
212214

213215
inline float16 cpu_float2half_rn(float f) {
214-
uint32_t f_u32;
216+
uint32_t f_u32 = 0;
215217
std::memcpy(&f_u32, &f, sizeof(f_u32));
216218
return detail::ieee754_trunc<
217219
/*Src=*/detail::IEEE754Single,
@@ -220,7 +222,7 @@ inline float16 cpu_float2half_rn(float f) {
220222
}
221223

222224
inline float16 cpu_float2half_rz(float f) {
223-
uint32_t f_u32;
225+
uint32_t f_u32 = 0;
224226
std::memcpy(&f_u32, &f, sizeof(f_u32));
225227
return detail::ieee754_trunc<
226228
/*Src=*/detail::IEEE754Single,
@@ -263,7 +265,7 @@ inline float cpu_half2float_ref(const float16 h) {
263265
exponent = f32_exponent_mask;
264266
} else if (!exponent) { // Denorm or Zero
265267
if (mantissa) {
266-
uint32_t msb;
268+
uint32_t msb = 0;
267269
exponent = f32_exponent_bias - f16_exponent_bias + 1;
268270
do {
269271
msb = mantissa & f32_most_significant_bit;
@@ -279,7 +281,7 @@ inline float cpu_half2float_ref(const float16 h) {
279281
const uint32_t i = (sign_bit << f32_num_non_sign_bits) |
280282
(exponent << f32_num_mantissa_bits) | mantissa;
281283

282-
float ret;
284+
float ret = NAN;
283285
std::memcpy(&ret, &i, sizeof(float));
284286
return ret;
285287
}
@@ -288,7 +290,7 @@ inline float cpu_half2float_ref(const float16 h) {
288290
// conversion provided by the compiler
289291
inline float cpu_half2float(const float16 h) {
290292
#if defined(HAS_NATIVE_FP16_TYPE) && not defined(MISSING_GNU_F2H_IEEE)
291-
__fp16 h_fp16;
293+
__fp16 h_fp16 = NAN;
292294
std::memcpy(&h_fp16, &h, sizeof(__fp16));
293295
return h_fp16;
294296
#else
@@ -299,7 +301,7 @@ inline float cpu_half2float(const float16 h) {
299301
inline float16 cpu_float2half(const float f) {
300302
#if defined(HAS_NATIVE_FP16_TYPE) && not defined(MISSING_GNU_F2H_IEEE)
301303
__fp16 h = f;
302-
float16 res;
304+
float16 res = 0;
303305
std::memcpy(&res, &h, sizeof(__fp16));
304306
return res;
305307
#else
@@ -308,15 +310,15 @@ inline float16 cpu_float2half(const float f) {
308310
}
309311

310312
inline float cpu_bf162float(bfloat16 src) {
311-
float ret;
313+
float ret = NAN;
312314
uint32_t val_fp32 =
313315
static_cast<uint32_t>(reinterpret_cast<const uint16_t*>(&src)[0]) << 16;
314316
std::memcpy(&ret, &val_fp32, sizeof(float));
315317
return ret;
316318
}
317319

318320
inline bfloat16 cpu_float2bfloat16(float src) {
319-
uint32_t temp;
321+
uint32_t temp = 0;
320322
std::memcpy(&temp, &src, sizeof(uint32_t));
321323
return (temp + (1u << 15)) >> 16;
322324
}

include/fbgemm/QuantUtils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ T Quantize(
6868
std::int32_t zero_point,
6969
float scale,
7070
int result_precision,
71-
bool result_is_signed = std::is_signed<T>::value) {
71+
bool result_is_signed = std::is_signed_v<T>) {
7272
// Note: We want to multiply with src with inv_scale instead of
7373
// dividing src by scale. The same is done in vector code and
7474
// at other places.
@@ -162,7 +162,7 @@ void Dequantize(
162162
const TensorQuantizationParams& qparams,
163163
int thread_id = 0,
164164
int num_threads = 1) {
165-
int64_t i_begin, i_end;
165+
int64_t i_begin = 0, i_end = 0;
166166
fbgemmPartition1D(thread_id, num_threads, len, i_begin, i_end);
167167
for (int64_t i = i_begin; i < i_end; i++) {
168168
dst[i] = Dequantize(src[i], qparams);

include/fbgemm/QuantUtilsAvx2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void FusedQuantizeDequantizeAvx2(
7171
///
7272
/// Random number generator in [0, 9] based on
7373
/// <a href="https://www.jstatsoft.org/v08/i14/paper">this paper</a>.
74-
uint32_t FBGEMM_API Xor128(void);
74+
uint32_t FBGEMM_API Xor128();
7575

7676
/// @ingroup fbgemm-quant-utils-avx2
7777
///

include/fbgemm/Utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace fbgemm {
3535
template <typename T>
3636
struct is_8bit {
3737
static constexpr bool value =
38-
std::is_same<T, int8_t>::value || std::is_same<T, uint8_t>::value;
38+
std::is_same_v<T, int8_t> || std::is_same_v<T, uint8_t>;
3939
};
4040

4141
/**
@@ -263,8 +263,8 @@ std::string arrayToString(const std::array<T, SIZE>& inp) {
263263

264264
template <typename accT = std::int32_t>
265265
bool isValidBlockingFactor(const BlockingFactors* const param) {
266-
constexpr bool is_32bit = std::is_same<accT, int32_t>::value;
267-
constexpr bool is_16bit = std::is_same<accT, int16_t>::value;
266+
constexpr bool is_32bit = std::is_same_v<accT, int32_t>;
267+
constexpr bool is_16bit = std::is_same_v<accT, int16_t>;
268268
static const auto iset = fbgemmInstructionSet();
269269

270270
if constexpr (is_32bit) {
@@ -447,7 +447,7 @@ void nbit_embedding_sanity_check(
447447
assert(
448448
(input_bit_rate == 2 || input_bit_rate == 4) &&
449449
"input_bit_rate must be 2 or 4");
450-
if constexpr (std::is_same<OutType, uint8_t>::value) {
450+
if constexpr (std::is_same_v<OutType, uint8_t>) {
451451
assert(
452452
(no_bag && input_bit_rate == 4 && output_bit_rate == 4) &&
453453
"we currently only support int4 to int4 for sequential TBE");

src/CodeGenHelpers.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ template <
9191
int> = 0>
9292
void emitExtractHalfVector(
9393
x86::Emitter* a,
94-
x86::Ymm half,
95-
const x86::Zmm vec,
94+
const x86::Ymm& half,
95+
const x86::Zmm& vec,
9696
int idx) {
9797
a->vextracti32x8(half, vec, idx);
9898
}
@@ -107,8 +107,8 @@ template <
107107
int> = 0>
108108
void emitExtractHalfVector(
109109
x86::Emitter* a,
110-
x86::Xmm half,
111-
x86::Ymm vec,
110+
const x86::Xmm& half,
111+
const x86::Ymm& vec,
112112
int idx) {
113113
a->vextracti32x4(half, vec, idx);
114114
}
@@ -119,8 +119,8 @@ template <
119119
std::enable_if_t<instSet == inst_set_t::avx2, int> = 0>
120120
void emitExtractHalfVector(
121121
x86::Emitter* a,
122-
x86::Xmm half,
123-
x86::Ymm vec,
122+
const x86::Xmm& half,
123+
const x86::Ymm& vec,
124124
int idx) {
125125
a->vextracti128(half, vec, idx);
126126
}

0 commit comments

Comments
 (0)