Skip to content

Commit 1988fb5

Browse files
cyyeverfacebook-github-bot
authored andcommitted
Modernize more C++ code (#4437)
Summary: Pull Request resolved: #4437 X-link: facebookresearch/FBGEMM#1499 Pull Request resolved: #4431 Reviewed By: cthi Differential Revision: D77635288 Pulled By: q10 fbshipit-source-id: 956697c56c6ed739144c19ebc703354290f8d587
1 parent 5be0723 commit 1988fb5

29 files changed

+116
-130
lines changed

.clang-tidy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
InheritParentConfig: true
77
# @nolint
88
Checks: '
9+
-*,
910
bugprone-argument-comment,
1011
misc-use-internal-linkage,
12+
modernize*,
13+
-modernize-use-auto,
14+
-modernize-use-constraints,
15+
-modernize-use-trailing-return-type,
16+
-modernize-avoid-c-arrays,
17+
-modernize-avoid-bind,
18+
-modernize-use-designated-initializers,
19+
-modernize-use-ranges,
20+
-modernize-use-integer-sign-comparison
21+
-modernize-use-nodiscard,
1122
'
1223
CheckOptions:
1324
- key: facebook-cuda-safe-api-call-check.HandlerName

bench/AlignedVec.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ template <typename T, std::size_t Alignment>
2424
class aligned_allocator {
2525
public:
2626
// The following will be the same for virtually all allocators.
27-
typedef T* pointer;
28-
typedef const T* const_pointer;
29-
typedef T& reference;
30-
typedef const T& const_reference;
31-
typedef T value_type;
32-
typedef std::size_t size_type;
33-
typedef std::ptrdiff_t difference_type;
27+
using pointer = T*;
28+
using const_pointer = T*;
29+
using reference = T&;
30+
using const_reference = T&;
31+
using value_type = T;
32+
using size_type = std::size_t;
33+
using difference_type = std::ptrdiff_t;
3434

3535
T* address(T& r) const {
3636
return &r;
@@ -50,7 +50,7 @@ class aligned_allocator {
5050
// The following must be the same for all allocators.
5151
template <typename U>
5252
struct rebind {
53-
typedef aligned_allocator<U, Alignment> other;
53+
using other = aligned_allocator<U, Alignment>;
5454
};
5555

5656
bool operator!=(const aligned_allocator& other) const {
@@ -76,14 +76,14 @@ class aligned_allocator {
7676

7777
// Default constructor, copy constructor, rebinding constructor, and
7878
// destructor. Empty for stateless allocators.
79-
aligned_allocator() {}
79+
aligned_allocator() = default;
8080

81-
aligned_allocator(const aligned_allocator&) {}
81+
aligned_allocator(const aligned_allocator&) = default;
8282

8383
template <typename U>
8484
aligned_allocator(const aligned_allocator<U, Alignment>&) {}
8585

86-
~aligned_allocator() {}
86+
~aligned_allocator() = default;
8787

8888
// The following will be different for each allocator.
8989
T* allocate(const std::size_t n) const {

bench/BenchUtils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void performance_test(
267267
randFill(Aint, 0, 4);
268268
std::vector<aligned_vector<float>> A;
269269
for (int i = 0; i < num_instances; ++i) {
270-
A.push_back(aligned_vector<float>(Aint.begin(), Aint.end()));
270+
A.emplace_back(Aint.begin(), Aint.end());
271271
}
272272

273273
aligned_vector<int> Bint(k * n);
@@ -309,13 +309,13 @@ void performance_test(
309309
aligned_vector<int> Cint(m * n);
310310
randFill(Cint, 0, 4);
311311
for (int i = 0; i < num_instances; ++i) {
312-
C_ref.push_back(aligned_vector<float>(Cint.begin(), Cint.end()));
313-
C_fb.push_back(aligned_vector<float>(Cint.begin(), Cint.end()));
312+
C_ref.emplace_back(Cint.begin(), Cint.end());
313+
C_fb.emplace_back(Cint.begin(), Cint.end());
314314
}
315315
} else {
316316
for (int i = 0; i < num_instances; ++i) {
317-
C_ref.push_back(aligned_vector<float>(m * n, 1.f));
318-
C_fb.push_back(aligned_vector<float>(m * n, NAN));
317+
C_ref.emplace_back(m * n, 1.f);
318+
C_fb.emplace_back(m * n, NAN);
319319
}
320320
}
321321

bench/ConvUnifiedBenchmark.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,14 @@ static void performance_test(
505505
} // shapes
506506
}
507507

508-
typedef struct {
508+
using user_args_t = struct {
509509
bool no_flush; /* if true, llc won't be flushed inbetween benchmark iterations
510510
*/
511511
bool run_extended_shapes; /* if true, runs additional shapes on top of the
512512
default set */
513513
int benchmark_repetitions; /* specified number of timed benchmark iterations
514514
*/
515-
} user_args_t;
515+
};
516516

517517
int main(int argc, const char* argv[]) {
518518
user_args_t user_args;

bench/EmbeddingQuantizeBenchmark.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void performance_test() {
2929
constexpr int NWARMUP = 4;
3030
constexpr int NITER = 256;
3131

32-
if (is_same<T, float16>::value) {
32+
if (is_same_v<T, float16>) {
3333
cout << "With scale and bias as float16" << endl;
3434
} else {
3535
cout << "With scale and bias as float" << endl;
@@ -38,7 +38,7 @@ static void performance_test() {
3838
<< "cols" << "," << setw(16) << "elems_per_usec" << "," << setw(10)
3939
<< "GB/Sec" << endl;
4040
std::vector<int> bit_rates;
41-
if (is_same<T, float16>::value) {
41+
if (is_same_v<T, float16>) {
4242
bit_rates = {2, 4, 8};
4343
} else {
4444
// float

bench/EmbeddingQuantizeFloatToFloatOrHalfBenchmark.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void performance_test() {
2929
constexpr int NWARMUP = 4;
3030
constexpr int NITER = 256;
3131

32-
if (is_same<T, float16>::value) {
32+
if (is_same_v<T, float16>) {
3333
cout << "With result as float16" << endl;
3434
} else {
3535
cout << "With result as float" << endl;

bench/EmbeddingSpMDMNBit2Benchmark.cc

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,17 @@ struct BenchmarkSpec {
8888
};
8989

9090
struct BenchmarkResult {
91-
float ref_bw;
92-
float ref_eff_bw;
93-
float ref_time;
94-
float asmjit_bw;
95-
float asmjit_eff_bw;
96-
float asmjit_time;
97-
float autovec_bw;
98-
float autovec_eff_bw;
99-
float autovec_time;
100-
101-
BenchmarkResult()
102-
: ref_bw(0.0),
103-
ref_eff_bw(0.0),
104-
ref_time(0.0),
105-
asmjit_bw(0.0),
106-
asmjit_eff_bw(0.0),
107-
asmjit_time(0.0),
108-
autovec_bw(0.0),
109-
autovec_eff_bw(0.0),
110-
autovec_time(0.0) {}
91+
float ref_bw{0.0};
92+
float ref_eff_bw{0.0};
93+
float ref_time{0.0};
94+
float asmjit_bw{0.0};
95+
float asmjit_eff_bw{0.0};
96+
float asmjit_time{0.0};
97+
float autovec_bw{0.0};
98+
float autovec_eff_bw{0.0};
99+
float autovec_time{0.0};
100+
101+
BenchmarkResult() = default;
111102

112103
void set_ref_result(float bw, float eff_bw, float time) {
113104
ref_bw = bw;
@@ -141,7 +132,7 @@ static BenchmarkResult& find_benchmark_record(const BenchmarkSpec& spec) {
141132
return benchmarks[i].second;
142133
}
143134
}
144-
benchmarks.push_back(std::make_pair(spec, BenchmarkResult()));
135+
benchmarks.emplace_back(spec, BenchmarkResult());
145136
return benchmarks.back().second;
146137
}
147138

bench/EmbeddingSpMDMNBitBenchmark.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,10 @@ static int run_benchmark(
376376
for (size_t i = 0; i < output.size(); ++i) {
377377
float tmp1 = 0;
378378
float tmp2 = 0;
379-
if constexpr (std::is_same<OutType, float>::value) {
379+
if constexpr (std::is_same_v<OutType, float>) {
380380
tmp1 = output[i];
381381
tmp2 = output_ref[i];
382-
} else if constexpr (std::is_same<OutType, uint16_t>::value) {
382+
} else if constexpr (std::is_same_v<OutType, uint16_t>) {
383383
if (is_bf16_out) {
384384
tmp1 = cpu_bf162float(output[i]);
385385
tmp2 = cpu_bf162float(output_ref[i]);
@@ -412,10 +412,10 @@ static int run_benchmark(
412412
for (size_t i = 0; i < output_autovec.size(); ++i) {
413413
float tmp1 = 0;
414414
float tmp2 = 0;
415-
if constexpr (std::is_same<OutType, float>::value) {
415+
if constexpr (std::is_same_v<OutType, float>) {
416416
tmp1 = output_autovec[i];
417417
tmp2 = output_ref[i];
418-
} else if constexpr (std::is_same<OutType, uint16_t>::value) {
418+
} else if constexpr (std::is_same_v<OutType, uint16_t>) {
419419
if (is_bf16_out) {
420420
tmp1 = cpu_bf162float(output_autovec[i]);
421421
tmp2 = cpu_bf162float(output_ref[i]);
@@ -438,9 +438,9 @@ static int run_benchmark(
438438
#endif
439439
}
440440

441-
if constexpr (std::is_same<OutType, float>::value) {
441+
if constexpr (std::is_same_v<OutType, float>) {
442442
cout << "out type fp32, ";
443-
} else if constexpr (std::is_same<OutType, uint16_t>::value) {
443+
} else if constexpr (std::is_same_v<OutType, uint16_t>) {
444444
if (is_bf16_out) {
445445
cout << "out type bf16, ";
446446
} else {

bench/TransposeBenchmark.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void performance_test() {
2929
default_random_engine engine;
3030

3131
string runType;
32-
if (is_same<T, float>::value) {
32+
if (is_same_v<T, float>) {
3333
runType = "float";
3434
} else {
3535
runType = "i8";

src/CodeCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class CodeCache {
5454
CodeCache(const CodeCache&) = delete;
5555
CodeCache& operator=(const CodeCache&) = delete;
5656

57-
CodeCache() {}
57+
CodeCache() = default;
5858

5959
template <typename GENFUNC>
6060
VALUE getOrCreate(const KEY& key, GENFUNC generatorFunction) {
@@ -116,7 +116,7 @@ class CodeCache<KEY, VALUE, /*THREAD_LOCAL=*/true> {
116116
CodeCache(const CodeCache&) = delete;
117117
CodeCache& operator=(const CodeCache&) = delete;
118118

119-
CodeCache() {}
119+
CodeCache() = default;
120120

121121
template <typename GENFUNC>
122122
VALUE getOrCreate(const KEY& key, GENFUNC generatorFunction) {

0 commit comments

Comments
 (0)