Skip to content

Commit a11e775

Browse files
Allow index-less benchmarks via BenchmarkTarget wrapper
1 parent 96e0bae commit a11e775

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

libc/benchmarks/gpu/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ add_unittest_framework_library(
4949
libc.src.__support.CPP.string
5050
libc.src.__support.CPP.string_view
5151
libc.src.__support.CPP.type_traits
52-
libc.src.__support.CPP.functional
5352
libc.src.__support.CPP.limits
5453
libc.src.__support.CPP.algorithm
5554
libc.src.__support.CPP.atomic

libc/benchmarks/gpu/LibcGpuBenchmark.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "LibcGpuBenchmark.h"
2+
23
#include "hdr/stdint_proxy.h"
34
#include "src/__support/CPP/algorithm.h"
45
#include "src/__support/CPP/array.h"
@@ -161,9 +162,8 @@ void Benchmark::run_benchmarks() {
161162
gpu::sync_threads();
162163
}
163164

164-
BenchmarkResult
165-
benchmark(const BenchmarkOptions &options,
166-
const cpp::function<uint64_t(uint32_t)> &wrapper_func) {
165+
BenchmarkResult benchmark(const BenchmarkOptions &options,
166+
const BenchmarkTarget &target) {
167167
BenchmarkResult result;
168168
RuntimeEstimationProgression rep;
169169
uint32_t iterations = options.initial_iterations;
@@ -183,7 +183,7 @@ benchmark(const BenchmarkOptions &options,
183183

184184
const clock_t start = clock();
185185
while (sample_estimator.get_iterations() < iterations) {
186-
auto current_result = wrapper_func(call_index++);
186+
auto current_result = target(call_index++);
187187
max = cpp::max(max, current_result);
188188
min = cpp::min(min, current_result);
189189
sample_estimator.update(current_result);

libc/benchmarks/gpu/LibcGpuBenchmark.h

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "hdr/stdint_proxy.h"
77
#include "src/__support/CPP/algorithm.h"
88
#include "src/__support/CPP/array.h"
9-
#include "src/__support/CPP/functional.h"
109
#include "src/__support/CPP/limits.h"
1110
#include "src/__support/CPP/string_view.h"
1211
#include "src/__support/CPP/type_traits.h"
@@ -121,21 +120,48 @@ struct BenchmarkResult {
121120
clock_t total_time = 0;
122121
};
123122

124-
BenchmarkResult
125-
benchmark(const BenchmarkOptions &options,
126-
const cpp::function<uint64_t(uint32_t)> &wrapper_func);
123+
struct BenchmarkTarget {
124+
using IndexedFnPtr = uint64_t (*)(uint32_t);
125+
using IndexlessFnPtr = uint64_t (*)();
126+
127+
enum class Kind : uint8_t { Indexed, Indexless } kind;
128+
union {
129+
IndexedFnPtr indexed_fn_ptr;
130+
IndexlessFnPtr indexless_fn_ptr;
131+
};
132+
133+
LIBC_INLINE BenchmarkTarget(IndexedFnPtr func)
134+
: kind(Kind::Indexed), indexed_fn_ptr(func) {}
135+
LIBC_INLINE BenchmarkTarget(IndexlessFnPtr func)
136+
: kind(Kind::Indexless), indexless_fn_ptr(func) {}
137+
138+
LIBC_INLINE uint64_t operator()([[maybe_unused]] uint32_t call_index) const {
139+
return kind == Kind::Indexed ? indexed_fn_ptr(call_index)
140+
: indexless_fn_ptr();
141+
}
142+
};
143+
144+
BenchmarkResult benchmark(const BenchmarkOptions &options,
145+
const BenchmarkTarget &target);
127146

128147
class Benchmark {
129-
const cpp::function<uint64_t(uint32_t)> func;
148+
const BenchmarkTarget target;
130149
const cpp::string_view suite_name;
131150
const cpp::string_view test_name;
132151
const uint32_t num_threads;
133152

134153
public:
135-
Benchmark(cpp::function<uint64_t(uint32_t)> func, char const *suite_name,
154+
Benchmark(uint64_t (*f)(), const char *suite, const char *test,
155+
uint32_t threads)
156+
: target(BenchmarkTarget(f)), suite_name(suite), test_name(test),
157+
num_threads(threads) {
158+
add_benchmark(this);
159+
}
160+
161+
Benchmark(uint64_t (*f)(uint32_t), char const *suite_name,
136162
char const *test_name, uint32_t num_threads)
137-
: func(func), suite_name(suite_name), test_name(test_name),
138-
num_threads(num_threads) {
163+
: target(BenchmarkTarget(f)), suite_name(suite_name),
164+
test_name(test_name), num_threads(num_threads) {
139165
add_benchmark(this);
140166
}
141167

@@ -149,7 +175,7 @@ class Benchmark {
149175
private:
150176
BenchmarkResult run() {
151177
BenchmarkOptions options;
152-
return benchmark(options, func);
178+
return benchmark(options, target);
153179
}
154180
};
155181

libc/benchmarks/gpu/src/ctype/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ add_benchmark(
77
SRCS
88
isalnum_benchmark.cpp
99
DEPENDS
10-
libc.hdr.stdint_proxy
1110
libc.src.ctype.isalnum
1211
LOADER_ARGS
1312
--threads 64
@@ -20,6 +19,5 @@ add_benchmark(
2019
SRCS
2120
isalpha_benchmark.cpp
2221
DEPENDS
23-
libc.hdr.stdint_proxy
2422
libc.src.ctype.isalpha
2523
)

libc/benchmarks/gpu/src/ctype/isalnum_benchmark.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "benchmarks/gpu/LibcGpuBenchmark.h"
22

3-
#include "hdr/stdint_proxy.h"
43
#include "src/ctype/isalnum.h"
54

6-
uint64_t BM_IsAlnum(uint32_t /*call_index*/) {
5+
uint64_t BM_IsAlnum() {
76
char x = 'c';
87
return LIBC_NAMESPACE::latency(LIBC_NAMESPACE::isalnum, x);
98
}
@@ -13,13 +12,13 @@ SINGLE_THREADED_BENCHMARK(LlvmLibcIsAlNumGpuBenchmark, IsAlnumSingleThread,
1312
SINGLE_WAVE_BENCHMARK(LlvmLibcIsAlNumGpuBenchmark, IsAlnumSingleWave,
1413
BM_IsAlnum);
1514

16-
uint64_t BM_IsAlnumCapital(uint32_t /*call_index*/) {
15+
uint64_t BM_IsAlnumCapital() {
1716
char x = 'A';
1817
return LIBC_NAMESPACE::latency(LIBC_NAMESPACE::isalnum, x);
1918
}
2019
BENCHMARK(LlvmLibcIsAlNumGpuBenchmark, IsAlnumCapital, BM_IsAlnumCapital);
2120

22-
uint64_t BM_IsAlnumNotAlnum(uint32_t /*call_index*/) {
21+
uint64_t BM_IsAlnumNotAlnum() {
2322
char x = '{';
2423
return LIBC_NAMESPACE::latency(LIBC_NAMESPACE::isalnum, x);
2524
}

libc/benchmarks/gpu/src/ctype/isalpha_benchmark.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "benchmarks/gpu/LibcGpuBenchmark.h"
22

3-
#include "hdr/stdint_proxy.h"
43
#include "src/ctype/isalpha.h"
54

6-
uint64_t BM_IsAlpha(uint32_t /*call_index*/) {
5+
uint64_t BM_IsAlpha() {
76
char x = 'c';
87
return LIBC_NAMESPACE::latency(LIBC_NAMESPACE::isalpha, x);
98
}

0 commit comments

Comments
 (0)