File: bench_memory_access.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (107 lines) | stat: -rw-r--r-- 2,789 bytes parent folder | download | duplicates (24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// RUN: %clangxx_tsan %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// bench.h needs pthread barriers which are not available on OS X
// UNSUPPORTED: darwin

#include "bench.h"
#include <memory.h>

void thread(int tid) {
  volatile long x = 0;
  switch (bench_mode) {
  case 0:
    for (int i = 0; i < bench_niter; i++)
      *(volatile char *)&x = 1;
    break;
  case 1:
    for (int i = 0; i < bench_niter; i++)
      *(volatile short *)&x = 1;
    break;
  case 2:
    for (int i = 0; i < bench_niter; i++)
      *(volatile int *)&x = 1;
    break;
  case 3:
    for (int i = 0; i < bench_niter; i++)
      *(volatile long *)&x = 1;
    break;
  case 4:
    for (int i = 0; i < bench_niter; i++)
      *(volatile char *)&x;
    break;
  case 5:
    for (int i = 0; i < bench_niter; i++)
      *(volatile short *)&x;
    break;
  case 6:
    for (int i = 0; i < bench_niter; i++)
      *(volatile int *)&x;
    break;
  case 7:
    for (int i = 0; i < bench_niter; i++)
      *(volatile long *)&x;
  case 8:
    for (int i = 0; i < bench_niter / 10; i++) {
      ((volatile long *)&x)[0];
      ((volatile int *)&x)[0];
      ((volatile short *)&x)[2];
      ((volatile char *)&x)[6];
      ((volatile char *)&x)[7];
      ((volatile long *)&x)[0] = 1;
      ((volatile int *)&x)[0] = 1;
      ((volatile short *)&x)[2] = 1;
      ((volatile char *)&x)[6] = 1;
      ((volatile char *)&x)[7] = 1;
    }
    break;
  case 9: {
    volatile long size = sizeof(x);
    for (int i = 0; i < bench_niter; i++)
      memset((void *)&x, i, size);
    break;
  }
  case 10: {
    volatile long data[2] = {};
    volatile long size = sizeof(data) - 2;
    for (int i = 0; i < bench_niter; i++)
      memset(((char *)data) + 1, i, size);
    break;
  }
  case 11: {
    volatile long data[2] = {};
    for (int i = 0; i < bench_niter / 8 / 3; i++) {
      for (int off = 0; off < 8; off++) {
        __sanitizer_unaligned_store16(((char *)data) + off, i);
        __sanitizer_unaligned_store32(((char *)data) + off, i);
        __sanitizer_unaligned_store64(((char *)data) + off, i);
      }
    }
    break;
  }
#if TSAN_VECTORIZE
  case 12: {
    // The compiler wants to optimize all this away.
    // Use volatile to prevent optimization, but then use kBlock
    // to avoid the additional non-vector load in the inner loop.
    // Also use only even indexes to prevent compiler from
    // inserting memset.
    const int kBlock = 128;
    __m128i data[kBlock * 2];
    __m128i *volatile vptr = data;
    for (int i = 0; i < bench_niter / kBlock; i++) {
      __m128i *ptr = vptr;
      for (int j = 0; j < kBlock; j++)
        _mm_store_si128(&ptr[j * 2], _mm_setzero_si128());
    }
    break;
  }
#endif
  }
}

void bench() {
  start_thread_group(bench_nthread, thread);
}

// CHECK: DONE