|
| 1 | +// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Regression benchmark for https://github.com/dart-lang/sdk/issues/63217 and |
| 6 | +// https://github.com/dart-lang/sdk/issues/53662: the five binary operators on |
| 7 | +// Int32x4 (+, -, |, &, ^) were not specialized in AOT mode, so `Int32x4List` |
| 8 | +// loops fell back to boxed runtime calls that ran 10-70x slower than the |
| 9 | +// hand-written scalar version or than the JIT. |
| 10 | +// |
| 11 | +// For every operator there is a scalar variant and a SIMD variant over the |
| 12 | +// same Uint32List buffer, so the benchmark suite exposes both the absolute |
| 13 | +// cost of each SIMD op and its speedup over the scalar baseline. |
| 14 | + |
| 15 | +import 'dart:typed_data'; |
| 16 | + |
| 17 | +import 'package:benchmark_harness/benchmark_harness.dart'; |
| 18 | + |
| 19 | +const int words = 2048; |
| 20 | + |
| 21 | +abstract class SimdBench extends BenchmarkBase { |
| 22 | + SimdBench(String name) : super('SimdInt32x4.$name'); |
| 23 | + |
| 24 | + late final Uint32List a; |
| 25 | + late final Uint32List b; |
| 26 | + |
| 27 | + @override |
| 28 | + void setup() { |
| 29 | + a = Uint32List(words); |
| 30 | + b = Uint32List(words); |
| 31 | + for (int i = 0; i < words; i++) { |
| 32 | + a[i] = 0xA5A5A5A5 ^ i; |
| 33 | + b[i] = 0x5A5A5A5A ^ (i * 31); |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class OrScalar extends SimdBench { |
| 39 | + OrScalar() : super('orScalar'); |
| 40 | + @override |
| 41 | + void run() { |
| 42 | + final n = a.length; |
| 43 | + for (int i = 0; i < n; i++) { |
| 44 | + a[i] |= b[i]; |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class OrSimd extends SimdBench { |
| 50 | + OrSimd() : super('orSimd'); |
| 51 | + @override |
| 52 | + void run() { |
| 53 | + final la = Int32x4List.view(a.buffer, a.offsetInBytes, a.length >> 2); |
| 54 | + final lb = Int32x4List.view(b.buffer, b.offsetInBytes, b.length >> 2); |
| 55 | + for (int j = 0; j < la.length; j++) { |
| 56 | + la[j] = la[j] | lb[j]; |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class AndScalar extends SimdBench { |
| 62 | + AndScalar() : super('andScalar'); |
| 63 | + @override |
| 64 | + void run() { |
| 65 | + final n = a.length; |
| 66 | + for (int i = 0; i < n; i++) { |
| 67 | + a[i] &= b[i]; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class AndSimd extends SimdBench { |
| 73 | + AndSimd() : super('andSimd'); |
| 74 | + @override |
| 75 | + void run() { |
| 76 | + final la = Int32x4List.view(a.buffer, a.offsetInBytes, a.length >> 2); |
| 77 | + final lb = Int32x4List.view(b.buffer, b.offsetInBytes, b.length >> 2); |
| 78 | + for (int j = 0; j < la.length; j++) { |
| 79 | + la[j] = la[j] & lb[j]; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +class XorScalar extends SimdBench { |
| 85 | + XorScalar() : super('xorScalar'); |
| 86 | + @override |
| 87 | + void run() { |
| 88 | + final n = a.length; |
| 89 | + for (int i = 0; i < n; i++) { |
| 90 | + a[i] ^= b[i]; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +class XorSimd extends SimdBench { |
| 96 | + XorSimd() : super('xorSimd'); |
| 97 | + @override |
| 98 | + void run() { |
| 99 | + final la = Int32x4List.view(a.buffer, a.offsetInBytes, a.length >> 2); |
| 100 | + final lb = Int32x4List.view(b.buffer, b.offsetInBytes, b.length >> 2); |
| 101 | + for (int j = 0; j < la.length; j++) { |
| 102 | + la[j] = la[j] ^ lb[j]; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class AddScalar extends SimdBench { |
| 108 | + AddScalar() : super('addScalar'); |
| 109 | + @override |
| 110 | + void run() { |
| 111 | + final n = a.length; |
| 112 | + for (int i = 0; i < n; i++) { |
| 113 | + a[i] = a[i] + b[i]; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +class AddSimd extends SimdBench { |
| 119 | + AddSimd() : super('addSimd'); |
| 120 | + @override |
| 121 | + void run() { |
| 122 | + final la = Int32x4List.view(a.buffer, a.offsetInBytes, a.length >> 2); |
| 123 | + final lb = Int32x4List.view(b.buffer, b.offsetInBytes, b.length >> 2); |
| 124 | + for (int j = 0; j < la.length; j++) { |
| 125 | + la[j] = la[j] + lb[j]; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +class SubScalar extends SimdBench { |
| 131 | + SubScalar() : super('subScalar'); |
| 132 | + @override |
| 133 | + void run() { |
| 134 | + final n = a.length; |
| 135 | + for (int i = 0; i < n; i++) { |
| 136 | + a[i] = a[i] - b[i]; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +class SubSimd extends SimdBench { |
| 142 | + SubSimd() : super('subSimd'); |
| 143 | + @override |
| 144 | + void run() { |
| 145 | + final la = Int32x4List.view(a.buffer, a.offsetInBytes, a.length >> 2); |
| 146 | + final lb = Int32x4List.view(b.buffer, b.offsetInBytes, b.length >> 2); |
| 147 | + for (int j = 0; j < la.length; j++) { |
| 148 | + la[j] = la[j] - lb[j]; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +void main() { |
| 154 | + final benchmarks = <BenchmarkBase Function()>[ |
| 155 | + OrScalar.new, |
| 156 | + OrSimd.new, |
| 157 | + AndScalar.new, |
| 158 | + AndSimd.new, |
| 159 | + XorScalar.new, |
| 160 | + XorSimd.new, |
| 161 | + AddScalar.new, |
| 162 | + AddSimd.new, |
| 163 | + SubScalar.new, |
| 164 | + SubSimd.new, |
| 165 | + ]; |
| 166 | + |
| 167 | + for (final bm in benchmarks) { |
| 168 | + bm() |
| 169 | + ..setup() |
| 170 | + ..run() |
| 171 | + ..run(); |
| 172 | + } |
| 173 | + |
| 174 | + for (final bm in benchmarks) { |
| 175 | + bm().report(); |
| 176 | + } |
| 177 | +} |
0 commit comments