-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtest_cthread_const_prop.cpp
194 lines (164 loc) · 4.83 KB
/
test_cthread_const_prop.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
* Copyright (c) 2020, Intel Corporation. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception.
*
*****************************************************************************/
#include "systemc.h"
#include <sct_assert.h>
using namespace sc_core;
// Constant propagation special cases
class A : public sc_module
{
public:
sc_in<bool> clk{"clk"};
sc_in<bool> nrst{"nrst"};
sc_in<bool> a{"a"};
sc_out<bool> b{"b"};
sc_signal<bool> c{"c"};
int m;
const int* p = nullptr;
int ci = 42;
const int* q = &ci;
SC_CTOR(A)
{
SC_CTHREAD(pointer_compare, clk.pos());
async_reset_signal_is(nrst, false);
SC_CTHREAD(pointer_compare2, clk.pos());
async_reset_signal_is(nrst, false);
SC_CTHREAD(fifoSyncProc, clk.pos());
async_reset_signal_is(nrst, false);
SC_CTHREAD(priorityProc, clk.pos());
async_reset_signal_is(nrst, false);
SC_METHOD(nested_loops);
sensitive << a;
}
sc_signal<int> t0;
void pointer_compare() {
wait();
while (true) {
const bool b1 = (p == nullptr);
if (b1) {
int i = 1;
t0 = i;
}
sct_assert_const(b1);
bool b2 = (p == nullptr);
t0 = b2;
sct_assert_const(b2);
wait();
}
}
sc_signal<int> t1;
void pointer_compare2() {
wait();
while (true) {
const bool b1 = (q != nullptr);
if (b1) {
int i = 1;
t1 = i;
}
sct_assert_const(b1);
bool b2 = (q != nullptr);
t1 = b2;
sct_assert_const(b2);
wait();
}
}
//----------------------------------------------------------------------
// Bug in real design module, CPA stop analysis after first iteration
// as popIndx not changed, so @state is stable -- fixed
static const unsigned FIFO_LENGTH = 2;
sc_uint<2> popIndx;
void fifoSyncProc()
{
popIndx = 0;
wait();
while (true) {
if (a) {
if (popIndx == FIFO_LENGTH-1) {
popIndx = 0;
} else {
popIndx += 1; // Produce NO_VALUE here
}
}
wait();
}
}
//----------------------------------------------------------------------
// Bug in real design -- fixed
sc_signal<sc_uint<2>> rr_first_indx{"rr_first_indx"};
const unsigned PORT_NUM = 2;
void priorityProc()
{
rr_first_indx = 0;
wait();
while (true) {
sct_assert_level(1);
if (PORT_NUM > 1) {
// Normal priority, shift port priorities
sct_assert_level(2);
if (true) {
sct_assert_level(3);
rr_first_indx = (rr_first_indx.read() == 1)
? 2 : ((rr_first_indx.read() == PORT_NUM - 1)
? 0 : rr_first_indx.read() + 1);
} else {
sct_assert_level(3);
rr_first_indx = (rr_first_indx.read() == PORT_NUM - 1)
? 0 : rr_first_indx.read() + 1;
}
}
sct_assert_level(1);
wait();
}
}
//----------------------------------------------------------------------
// Check CP performance on nested loops
sc_signal<int> t2;
void nested_loops()
{
const unsigned SIZE = 5;
int arr[SIZE];
// Four nested loops with some unimportant logic
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int k = 0; k < SIZE; k++) {
unsigned ll = 0;
for (int l = 0; l < SIZE; l++) {
ll = l*l;
}
if (a.read()) {
m = k + ll;
}
}
if (a.read()) {
m = j;
}
}
arr[i] = m;
}
t2 = m + arr[0];
}
};
class B_top : public sc_module
{
sc_signal<bool> a{"a"};
sc_signal<bool> b{"b"};
sc_signal<bool> clk{"clk"};
sc_signal<bool> nrst{"nrst"};
public:
A a_mod{"a_mod"};
SC_CTOR(B_top) {
a_mod.clk(clk);
a_mod.nrst(nrst);
a_mod.a(a);
a_mod.b(b);
}
};
int sc_main(int argc, char* argv[])
{
B_top b_mod{"b_mod"};
sc_start();
return 0;
}