Make dispatcher more efficient
Example of current ISPC dispatch
@__system_best_isa = internal unnamed_addr global i32 -1, align 4
...
declare i1 @foo_sse4(float, ptr)
declare i1 @foo_avx2(float, ptr)
define i1 @foo(float %0, ptr %1) {
entry:
call void @__set_system_isa()
%system_isa = load i32, ptr @__system_best_isa, align 4
%isa_ok = icmp sge i32 %system_isa, 5
br i1 %isa_ok, label %do_call, label %next_try
do_call: ; preds = %entry
%ret_value = call i1 @foo_avx2(float %0, ptr %1)
ret i1 %ret_value
next_try: ; preds = %entry
%isa_ok1 = icmp sge i32 %system_isa, 2
br i1 %isa_ok1, label %do_call2, label %next_try3
do_call2: ; preds = %next_try
%ret_value4 = call i1 @foo_sse4(float %0, ptr %1)
ret i1 %ret_value4
next_try3: ; preds = %next_try
call void @__terminate_now()
ret i1 undef
}
The more targets a given source is compiled for, the more bloated the logic for function selection. It would be good for ISPC to perform the cpuid check once, cache the target function address, and perform an indirect branch which should predict perfectly.
My proposal here is to create a global array of function pointers for each target ISA and another global pointer that will point to one of these global arrays depending on the hardware the program is executed on (initially uninitialized):
@__system_func_ptrs = internal global ptr null
@__system_func_ptrs_5 = internal constant [3 x ptr] [ptr @bar_avx2, ptr @foo_avx2, ptr @noise_ispc_avx2]
@__system_func_ptrs_3 = internal constant [3 x ptr] [ptr @bar_avx, ptr @foo_avx, ptr @noise_ispc_avx]
@__system_func_ptrs_2 = internal constant [3 x ptr] [ptr @bar_sse4, ptr @foo_sse4, ptr @noise_ispc_sse4]
In the dispatch function, we check this global pointer value: if it's already initialized, it points to the proper function pointers array and we can perform an indirect call using a unique offset in this array for each function. Otherwise, we initialize the pointer by selecting the best available ISA variant and then perform the indirect call:
declare void @foo_sse4(ptr, ptr)
declare void @foo_avx(ptr, ptr)
declare void @foo_avx2(ptr, ptr)
define void @foo(ptr %0, ptr %1) {
entry:
%2 = load ptr, ptr @__system_func_ptrs, align 8
%3 = icmp ne ptr %2, null
br i1 %3, label %skip_init, label %do_init
skip_init: ; preds = %do_call5, %do_call2, %do_call, %entry
%4 = load ptr, ptr @__system_func_ptrs, align 8
%5 = getelementptr ptr, ptr %4, i64 1
%6 = load ptr, ptr %5, align 8
call void %6(ptr %0, ptr %1)
ret void
do_init: ; preds = %entry
call void @__set_system_isa()
%system_isa = load i32, ptr @__system_best_isa, align 4
%isa_ok = icmp sge i32 %system_isa, 5
br i1 %isa_ok, label %do_call, label %next_try
do_call: ; preds = %do_init
store ptr @__system_func_ptrs_5, ptr @__system_func_ptrs, align 8
br label %skip_init
next_try: ; preds = %do_init
%isa_ok1 = icmp sge i32 %system_isa, 3
br i1 %isa_ok1, label %do_call2, label %next_try3
do_call2: ; preds = %next_try
store ptr @__system_func_ptrs_3, ptr @__system_func_ptrs, align 8
br label %skip_init
next_try3: ; preds = %next_try
%isa_ok4 = icmp sge i32 %system_isa, 2
br i1 %isa_ok4, label %do_call5, label %next_try6
do_call5: ; preds = %next_try3
store ptr @__system_func_ptrs_2, ptr @__system_func_ptrs, align 8
br label %skip_init
next_try6: ; preds = %next_try3
call void @__terminate_now()
unreachable
}
The key advantage of this approach is that we only need to perform the initialization of the global pointer once. As a further optimization, we could perform this initialization at program startup, in which case the dispatcher function would require only a single indirect call without any branching.
The proposal sounds good and it is more superior to what was originally proposed. Looking forward to see the performance comparison with current version!
An update on the current status of this task. First, I had to fall back to a simpler cache-based approach because the solution I initially proposed causes difficult-to-resolve relocation errors at the linkage stage when compiled without --pic. However, looking ahead, both solutions have very similar performance characteristics, so this fallback doesn't compromise the end result.
Regarding performance: On one hand, I can see significant performance gains in the dispatcher code after adding caching. My measurements show performance improvements of about 12% on the host machine and about 38% on SDE for the SSE4 target using a specialized microbenchmark I prepared.
I also refactored the __set_system_isa function to avoid unnecessary loads and added code to run LLVM optimization passes on the dispatcher module (which also optimizes out extra load instructions). With these optimizations, dispatcher performance gains about 42% on the host and about 55% on SDE compared to the current implementation.
On the other hand, this doesn't significantly affect real application performance. It appears that dispatcher code is not a real bottleneck since ISPC code typically performs much heavier computations compared to the dispatcher overhead. That's why I had to create a specialized microbenchmark that performs many lightweight ISPC function calls to observe the performance difference. On real benchmarks executed by perf.py, the performance difference is difficult to measure but is most likely around 1%.
Would be great to see the microbenchmark as part of your PR. Also if you're expecting some particular instructions to be generated (or not generated), please cover it by lit tests.
I had to create a specialized microbenchmark that performs many lightweight ISPC function calls to observe the performance difference.
That's the right way to do. The goal here is only optimize dispatcher overhead.
Thanks @aneshlya for the feedback! Sure, I'll add a LIT test to verify the new dispatcher code. However, I have doubts about whether it's worth uploading my microbenchmark. It's quite simple and doesn't provide much meaningful insight. The code is as follows:
// math.ispc
export uniform int add(uniform int x, uniform int y)
{
return x + y;
}
export uniform int sub(uniform int x, uniform int y)
{
return x - y;
}
export uniform int mul(uniform int x, uniform int y)
{
return x * y;
}
// main.c
#include <assert.h>
#include <stdio.h>
#include "math.h"
#define M (3 * 1000)
#define N (3 * 1000)
#define K 100
int A[M][N];
int B[M][N];
int C[M][N];
int main()
{
for (int k = 0; k < K; k++)
{
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
A[i][j] = add(i, j);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
B[i][j] = sub(i, j);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
C[i][j] = mul(A[i][j], B[i][j]);
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
assert(C[i][j] == (i + j) * (i - j));
}
printf("DONE\n");
}
But if you still think it's worth adding this benchmark to the repository, I'll need a little bit more details on where and how to place it (and when and how it would be executed).
Our usual approach for microbenchmarking is using google microbenchmarks, see benchmarks/01_trivial folder.
The benchmark may look like:
16_dispatcher_overhead.cpp:
// Copyright (c) 2025, Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
#include <benchmark/benchmark.h>
#include <cstdint>
#include <stdio.h>
#include "../common.h"
#include "16_dispatcher_overhead_ispc.h"
static Docs docs("Measure ISPC dispatcher overhead with minimal computation.\n");
WARM_UP_RUN();
#define ARGS Arg(256)->Arg(256 << 4)->Arg(256 << 7)->Arg(256 << 12)
static void dispatcher_overhead_test(benchmark::State &state) {
int count = static_cast<int>(state.range(0));
for (auto _ : state) {
for (int i = 0; i < count; i++) {
benchmark::DoNotOptimize(ispc::dispatcher_overhead_test());
}
}
}
BENCHMARK(dispatcher_overhead_test)->ARGS;
BENCHMARK_MAIN();
16_dispatcher_overhead.ispc:
// Copyright (c) 2025, Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
export uniform int dispatcher_overhead_test() {
return programCount;
}
By default the benchmarks are running only for one target but you can change it using BENCHMARKS_ISPC_TARGETS for example -DBENCHMARKS_ISPC_TARGETS="sse4-i32x4,avx2-i32x8,avx512skx-x8".
@aneshlya Thanks for providing the very helpful template for adding a new benchmark! I've incorporated my custom benchmark into this template and performed measurements. Please find them below.
Current implementation:
BENCHMARKS_ISPC_TARGETS: sse2-i32x4,sse4-i32x8,avx1-i32x8,avx2-i32x8,avx512skx-x16
BENCHMARKS_ISPC_FLAGS: -O3 --woff
Measure ISPC dispatcher overhead with minimal computation.
2025-10-15T11:55:24-07:00
Running ./build-ispc/bin/16_dispatcher_overhead
Run on (96 X 3108.69 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x48)
L1 Instruction 32 KiB (x48)
L2 Unified 1024 KiB (x48)
L3 Unified 36608 KiB (x2)
Load Average: 27.24, 14.60, 9.17
--------------------------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------------------------
warm_up/256 0.000 ms 0.000 ms 4067030
warm_up/4096 0.003 ms 0.003 ms 263503
warm_up/32768 0.021 ms 0.021 ms 33007
warm_up/1048576 0.680 ms 0.679 ms 1031
dispatcher_overhead_test/256 33.6 ms 33.6 ms 21
dispatcher_overhead_test/4096 536 ms 536 ms 1
dispatcher_overhead_test/32768 4287 ms 4283 ms 1
dispatcher_overhead_test/131072 17173 ms 17156 ms 1
New implementation:
BENCHMARKS_ISPC_TARGETS: sse2-i32x4,sse4-i32x8,avx1-i32x8,avx2-i32x8,avx512skx-x16
BENCHMARKS_ISPC_FLAGS: -O3 --woff
Measure ISPC dispatcher overhead with minimal computation.
2025-10-15T11:57:16-07:00
Running ./build-ispc/bin/16_dispatcher_overhead
Run on (96 X 3110.35 MHz CPU s)
CPU Caches:
L1 Data 32 KiB (x48)
L1 Instruction 32 KiB (x48)
L2 Unified 1024 KiB (x48)
L3 Unified 36608 KiB (x2)
Load Average: 24.91, 16.81, 10.51
--------------------------------------------------------------------------
Benchmark Time CPU Iterations
--------------------------------------------------------------------------
warm_up/256 0.000 ms 0.000 ms 4029337
warm_up/4096 0.003 ms 0.003 ms 263350
warm_up/32768 0.021 ms 0.021 ms 33004
warm_up/1048576 0.681 ms 0.679 ms 1030
dispatcher_overhead_test/256 18.0 ms 18.0 ms 39
dispatcher_overhead_test/4096 289 ms 288 ms 2
dispatcher_overhead_test/32768 2310 ms 2308 ms 1
dispatcher_overhead_test/131072 9239 ms 9230 ms 1
The overall improvement is around 46%, which matches very well with my previous measurements. You can also see in my comment above that I measured the impact of each optimization I implemented (~12% from indirect call, ~30% from code refactoring & optimization passes enabled). I can re-do these measurements with the new benchmark I just added if you'd like, but I'd expect very similar results.
Thanks for the update, @ivankelarev. With the current benchmark I see similar results! I think we can proceed with finalizing the PR.
@aneshlya Great, thanks for the confirmation! I believe I have addressed all the review comments and the patch is ready to be checked in. Please let me know if anything else needs to be done there.