Skip to content

Commit c2e5f9a

Browse files
cyyeverfacebook-github-bot
authored andcommitted
Initialize variables (#4420)
Summary: X-link: facebookresearch/FBGEMM#1510 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: D77812012 Pulled By: q10
1 parent e72c729 commit c2e5f9a

File tree

69 files changed

+334
-372
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
-372
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

0 commit comments

Comments
 (0)