LLVM OpenMP
omp_for_narrow_unsigned_iv.c
Go to the documentation of this file.
1// RUN: %libomp-compile-and-run
2
3// Check that the number of iterations is computed correctly for loops with an
4// unsigned loop variable that is narrower than the type of the upper bound,
5// a non-unit step, and a lower bound smaller than the step - 1.
6// See https://github.com/llvm/llvm-project/issues/70687.
7
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11
12#define N 13
13
14int test(uint64_t n_rt, uint32_t step2_rt, uint32_t step4_rt) {
15 const uint64_t n_ct = N;
16 int fails = 0;
17
18 // Constant upper bound.
19 {
20 int count = 0;
21#pragma omp parallel for reduction(+ : count)
22 for (uint32_t i = 0; i < n_ct; i += 2)
23 count++;
24 if (count != 7) {
25 printf("constant ub, step 2: count = %d (expected 7)\n", count);
26 fails++;
27 }
28 }
29
30 // Non-constant upper bound.
31 {
32 int count = 0;
33#pragma omp parallel for reduction(+ : count)
34 for (uint32_t i = 0; i < n_rt; i += 2)
35 count++;
36 if (count != 7) {
37 printf("runtime ub, step 2: count = %d (expected 7)\n", count);
38 fails++;
39 }
40 }
41
42 // Non-strict comparison, larger step. Avoid steps dividing 2^32-1, which
43 // hide the wrap-around after truncation to 32 bits.
44 {
45 int count = 0;
46#pragma omp parallel for reduction(+ : count)
47 for (uint32_t i = 1; i <= n_rt; i += 4)
48 count++;
49 if (count != 4) {
50 printf("runtime ub, step 4, <=: count = %d (expected 4)\n", count);
51 fails++;
52 }
53 }
54
55 // Non-constant step.
56 {
57 int count = 0;
58#pragma omp parallel for reduction(+ : count)
59 for (uint32_t i = 0; i < n_rt; i += step2_rt)
60 count++;
61 if (count != 7) {
62 printf("runtime ub, runtime step 2: count = %d (expected 7)\n", count);
63 fails++;
64 }
65 }
66
67 // Non-constant step, non-strict comparison.
68 {
69 int count = 0;
70#pragma omp parallel for reduction(+ : count)
71 for (uint32_t i = 1; i <= n_rt; i += step4_rt)
72 count++;
73 if (count != 4) {
74 printf("runtime ub, runtime step 4, <=: count = %d (expected 4)\n",
75 count);
76 fails++;
77 }
78 }
79
80 // Signed loop variable with an unsigned step: (Lower - Step + 1) is computed
81 // in unsigned int.
82 {
83 int count = 0;
84#pragma omp parallel for reduction(+ : count)
85 for (int32_t i = 0; i < n_rt; i += 2u)
86 count++;
87 if (count != 7) {
88 printf("signed iv, unsigned step 2: count = %d (expected 7)\n", count);
89 fails++;
90 }
91 }
92
93 // Increment written as subtraction of a negative step.
94 {
95 int count = 0;
96#pragma omp parallel for reduction(+ : count)
97 for (uint32_t i = 0; i < n_rt; i -= -2)
98 count++;
99 if (count != 7) {
100 printf("runtime ub, step -(-2): count = %d (expected 7)\n", count);
101 fails++;
102 }
103 }
104
105 // Lower bound with the top bit set must not be treated as negative.
106 {
107 int count = 0;
108#pragma omp parallel for reduction(+ : count)
109 for (uint32_t i = 0x80000000u; i < (uint64_t)0x80000000u + N; i++)
110 count++;
111 if (count != N) {
112 printf("large lb, step 1: count = %d (expected %d)\n", count, N);
113 fails++;
114 }
115 }
116
117 return fails;
118}
119
120int main(int argc, char **argv) {
121 // Hide the values from the compiler to get a non-constant upper bound and
122 // step.
123 volatile uint64_t n = N;
124 volatile uint32_t step2 = 2, step4 = 4;
125 int fails = test(n, step2, step4);
126 if (fails) {
127 printf("FAILED\n");
128 return EXIT_FAILURE;
129 }
130 printf("PASSED\n");
131 return EXIT_SUCCESS;
132}
#define N
Definition bug54082.c:13
int test()
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t count
#define i
Definition kmp_stub.cpp:88
int main()
Definition test-touch.c:21