forked from ferrandi/PandA-bambu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTestbenchExecution.cpp
440 lines (407 loc) · 17.3 KB
/
CTestbenchExecution.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
*
* _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
* _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
* _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
* _/ _/ _/ _/ _/ _/ _/ _/ _/
* _/ _/ _/ _/ _/ _/_/_/ _/ _/
*
* ***********************************************
* PandA Project
* URL: http://panda.dei.polimi.it
* Politecnico di Milano - DEIB
* System Architectures Group
* ***********************************************
* Copyright (C) 2004-2023 Politecnico di Milano
*
* This file is part of the PandA framework.
*
* The PandA framework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @author Fabrizio Ferrandi <[email protected]>
* @author Marco Minutoli <[email protected]>
* @author Manuel Beniani <[email protected]>
* @author Christian Pilato <[email protected]>
* @author Pietro Fezzardi <[email protected]>
*/
// include autoheaders
#include "config_HAVE_I386_GCC47_COMPILER.hpp"
#include "config_HAVE_I386_GCC48_COMPILER.hpp"
#include "config_HAVE_I386_GCC49_COMPILER.hpp"
#include "config_HAVE_I386_GCC5_COMPILER.hpp"
#include "config_HAVE_I386_GCC6_COMPILER.hpp"
#include "config_HAVE_I386_GCC7_COMPILER.hpp"
#include "config_HAVE_I386_GCC8_COMPILER.hpp"
// include class header
#include "CTestbenchExecution.hpp"
// include from ./
#include "Parameter.hpp"
// include from behavior/
#include "call_graph_manager.hpp"
#include "function_behavior.hpp"
// include from design_flows/
#include "design_flow_graph.hpp"
#include "design_flow_manager.hpp"
// include from design_flows/backend/ToC/progModels/
#include "c_backend.hpp"
// include from design_flows/backend/ToC/
#include "c_backend_step_factory.hpp"
#include "hls_c_backend_information.hpp"
// include from frontend_analysis/
#include "application_frontend_flow_step.hpp"
#include "frontend_flow_step_factory.hpp"
// include from HLS/
#include "hls_manager.hpp"
/// STD include
#include <string>
/// STL includes
#include "custom_set.hpp"
#include <list>
#include <tuple>
// include from tree/
#include "behavioral_helper.hpp"
// include from utility/
#include "fileIO.hpp"
// include from wrapper/compiler/
#include "compiler_wrapper.hpp"
#include "string_manipulation.hpp" // for GET_CLASS
CTestbenchExecution::CTestbenchExecution(const ParameterConstRef Param, const HLS_managerRef AppM,
const DesignFlowManagerConstRef _design_flow_manager,
const std::string& _testbench_basename)
: HLS_step(Param, AppM, _design_flow_manager, HLSFlowStep_Type::C_TESTBENCH_EXECUTION),
output_directory(Param->getOption<std::string>(OPT_output_directory) + "/simulation/"),
testbench_basename(_testbench_basename)
{
debug_level = parameters->get_class_debug_level(GET_CLASS(*this));
}
const CustomUnorderedSet<std::tuple<HLSFlowStep_Type, HLSFlowStepSpecializationConstRef, HLSFlowStep_Relationship>>
CTestbenchExecution::ComputeHLSRelationships(const DesignFlowStep::RelationshipType relationship_type) const
{
CustomUnorderedSet<std::tuple<HLSFlowStep_Type, HLSFlowStepSpecializationConstRef, HLSFlowStep_Relationship>> ret;
switch(relationship_type)
{
case DEPENDENCE_RELATIONSHIP:
{
ret.insert(std::make_tuple(HLSFlowStep_Type::TEST_VECTOR_PARSER, HLSFlowStepSpecializationConstRef(),
HLSFlowStep_Relationship::TOP_FUNCTION));
ret.insert(std::make_tuple(HLSFlowStep_Type::TESTBENCH_MEMORY_ALLOCATION, HLSFlowStepSpecializationConstRef(),
HLSFlowStep_Relationship::TOP_FUNCTION));
if(parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy))
{
ret.insert(std::make_tuple(HLSFlowStep_Type::VCD_SIGNAL_SELECTION, HLSFlowStepSpecializationConstRef(),
HLSFlowStep_Relationship::TOP_FUNCTION));
}
break;
}
case INVALIDATION_RELATIONSHIP:
case PRECEDENCE_RELATIONSHIP:
{
break;
}
default:
{
THROW_UNREACHABLE("");
break;
}
}
return ret;
}
DesignFlowStep_Status CTestbenchExecution::Exec()
{
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "-->Executing C testbench");
// compute top function name and use it to setup the artificial main for cosimulation
const auto top_function_ids = HLSMgr->CGetCallGraphManager()->GetRootFunctions();
THROW_ASSERT(top_function_ids.size() == 1, "Multiple top functions");
const auto top_function_id = *(top_function_ids.begin());
const auto top_function_name =
HLSMgr->CGetFunctionBehavior(top_function_id)->CGetBehavioralHelper()->get_function_name();
const auto default_compiler = parameters->getOption<CompilerWrapper_CompilerTarget>(OPT_default_compiler);
// NOTE: clang 13 seems to not respect the -fno-strict-aliasing flag generating incorrect code when type punning is
// present
const auto opt_lvl =
#if HAVE_I386_CLANG13_COMPILER
default_compiler == CompilerWrapper_CompilerTarget::CT_I386_CLANG13 ? CompilerWrapper_OptimizationSet::O0 :
#endif
CompilerWrapper_OptimizationSet::O2;
const CompilerWrapperConstRef compiler_wrapper(new CompilerWrapper(parameters, default_compiler, opt_lvl));
std::string compiler_flags = "-fwrapv -flax-vector-conversions -msse2 -mfpmath=sse -fno-strict-aliasing "
"-D'__builtin_bambu_time_start()=' -D'__builtin_bambu_time_stop()=' -D__BAMBU_SIM__ ";
if(!CompilerWrapper::isClangCheck(default_compiler))
{
compiler_flags += "-ffloat-store ";
}
if(!parameters->isOption(OPT_input_format) ||
parameters->getOption<Parameters_FileFormat>(OPT_input_format) == Parameters_FileFormat::FF_C ||
parameters->isOption(OPT_pretty_print))
{
if(!CompilerWrapper::isClangCheck(default_compiler))
{
compiler_flags += "-fexcess-precision=standard ";
}
}
if(parameters->isOption(OPT_testbench_extra_gcc_flags))
{
compiler_flags += parameters->getOption<std::string>(OPT_testbench_extra_gcc_flags) + " ";
}
if(parameters->isOption(OPT_gcc_optimizations))
{
const auto gcc_parameters = parameters->getOption<const CustomSet<std::string>>(OPT_gcc_optimizations);
if(gcc_parameters.find("tree-vectorize") != gcc_parameters.end())
{
boost::replace_all(compiler_flags, "-msse2", "");
compiler_flags += "-m32 ";
}
}
if(((parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy)) or
(parameters->isOption(OPT_discrepancy_hw) and parameters->getOption<bool>(OPT_discrepancy_hw))) and
(!parameters->isOption(OPT_discrepancy_permissive_ptrs) ||
!parameters->getOption<bool>(OPT_discrepancy_permissive_ptrs)))
{
if(CompilerWrapper::isClangCheck(default_compiler)
#if HAVE_I386_GCC48_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC48
#endif
#if HAVE_I386_GCC49_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC49
#endif
#if HAVE_I386_GCC5_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC5
#endif
#if HAVE_I386_GCC6_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC6
#endif
#if HAVE_I386_GCC7_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC7
#endif
#if HAVE_I386_GCC8_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC8
#endif
)
{
compiler_flags += "-g -fsanitize=address -fno-omit-frame-pointer -fno-common ";
}
if(false
#if HAVE_I386_GCC48_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC48
#endif
#if HAVE_I386_GCC49_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC49
#endif
#if HAVE_I386_GCC5_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC5
#endif
#if HAVE_I386_GCC6_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC6
#endif
#if HAVE_I386_GCC7_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC7
#endif
#if HAVE_I386_GCC8_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC8
#endif
)
{
compiler_flags += "-static-libasan ";
}
if(CompilerWrapper::isClangCheck(default_compiler)
#if HAVE_I386_GCC5_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC5
#endif
#if HAVE_I386_GCC6_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC6
#endif
#if HAVE_I386_GCC7_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC7
#endif
#if HAVE_I386_GCC8_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC8
#endif
)
{
compiler_flags += "-fsanitize=undefined -fsanitize-recover=undefined ";
}
if(false
#if HAVE_I386_GCC5_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC5
#endif
#if HAVE_I386_GCC6_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC6
#endif
#if HAVE_I386_GCC7_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC7
#endif
#if HAVE_I386_GCC8_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC8
#endif
)
{
compiler_flags += "-static-libubsan ";
}
}
// setup source files
std::list<std::string> file_sources;
file_sources.push_front(output_directory + testbench_basename + ".c");
// add source files to interface with python golden reference, if any
if(parameters->isOption(OPT_no_parse_c_python))
{
const auto no_parse_files = parameters->getOption<const CustomSet<std::string>>(OPT_no_parse_c_python);
for(const auto& no_parse_file : no_parse_files)
{
file_sources.push_back(no_parse_file);
}
}
if((parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy)) or
(parameters->isOption(OPT_discrepancy_hw) and parameters->getOption<bool>(OPT_discrepancy_hw)))
{
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---with discrepancy");
/// Nothing to do
}
else if(top_function_name != "main")
{
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---without discrepancy and not main");
if(parameters->isOption(OPT_pretty_print))
{
file_sources.push_back(parameters->getOption<std::string>(OPT_pretty_print));
}
else
{
compiler_flags += "-Dmain=_undefined_main ";
for(const auto& input_file : parameters->getOption<const CustomSet<std::string>>(OPT_input_file))
{
file_sources.push_back(input_file);
}
}
}
else
{
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---without discrepancy and main");
const std::string main_file_name = output_directory + "main_exec";
CustomSet<std::string> main_sources;
if(parameters->isOption(OPT_pretty_print))
{
main_sources.insert(parameters->getOption<std::string>(OPT_pretty_print));
}
else
{
for(const auto& input_file : parameters->getOption<const CustomSet<std::string>>(OPT_input_file))
{
main_sources.insert(input_file);
}
}
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---create executable: " + main_file_name);
compiler_wrapper->CreateExecutable(main_sources, main_file_name, compiler_flags);
}
std::string exec_name = output_directory + "test";
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---create executable: " + exec_name);
// compile the source file to get an executable
compiler_wrapper->CreateExecutable(file_sources, exec_name, compiler_flags);
// set some parameters for redirection of discrepancy statistics
std::string c_stdout_file = "";
if(parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy))
{
c_stdout_file = output_directory + "dynamic_discrepancy_stats";
}
// executing the test to generate inputs and expected outputs values
if((parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy)) or
(parameters->isOption(OPT_discrepancy_hw) and parameters->getOption<bool>(OPT_discrepancy_hw)))
{
if(CompilerWrapper::isClangCheck(default_compiler)
#if HAVE_I386_GCC49_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC49
#endif
#if HAVE_I386_GCC5_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC5
#endif
#if HAVE_I386_GCC6_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC6
#endif
#if HAVE_I386_GCC7_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC7
#endif
#if HAVE_I386_GCC8_COMPILER
|| default_compiler == CompilerWrapper_CompilerTarget::CT_I386_GCC8
#endif
)
{
exec_name.insert(0, "ASAN_OPTIONS='symbolize=1:redzone=2048' ");
}
}
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---exec executable: " + exec_name);
int ret = PandaSystem(parameters, exec_name, c_stdout_file);
if(IsError(ret))
{
THROW_ERROR("Error in generating the expected test results");
}
INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "<--Executed C testbench");
return DesignFlowStep_Status::SUCCESS;
}
bool CTestbenchExecution::HasToBeExecuted() const
{
return true;
}
void CTestbenchExecution::ComputeRelationships(DesignFlowStepSet& relationship,
const DesignFlowStep::RelationshipType relationship_type)
{
HLS_step::ComputeRelationships(relationship, relationship_type);
switch(relationship_type)
{
case DEPENDENCE_RELATIONSHIP:
{
const auto* frontend_step_factory = GetPointer<const FrontendFlowStepFactory>(
design_flow_manager.lock()->CGetDesignFlowStepFactory("Frontend"));
const vertex call_graph_computation_step = design_flow_manager.lock()->GetDesignFlowStep(
ApplicationFrontendFlowStep::ComputeSignature(COMPLETE_CALL_GRAPH));
const DesignFlowGraphConstRef design_flow_graph = design_flow_manager.lock()->CGetDesignFlowGraph();
const DesignFlowStepRef cg_design_flow_step =
call_graph_computation_step ?
design_flow_graph->CGetDesignFlowStepInfo(call_graph_computation_step)->design_flow_step :
frontend_step_factory->CreateApplicationFrontendFlowStep(COMPLETE_CALL_GRAPH);
relationship.insert(cg_design_flow_step);
// Root function cannot be computed at the beginning so if the
// call graph is not ready yet we exit. The relationships will
// be computed again after the call graph computation.
const CallGraphManagerConstRef call_graph_manager = HLSMgr->CGetCallGraphManager();
if(boost::num_vertices(*(call_graph_manager->CGetCallGraph())) == 0)
{
return;
}
const auto* c_backend_factory =
GetPointer<const CBackendStepFactory>(design_flow_manager.lock()->CGetDesignFlowStepFactory("CBackend"));
const bool is_discrepancy =
(parameters->isOption(OPT_discrepancy) and parameters->getOption<bool>(OPT_discrepancy)) or
(parameters->isOption(OPT_discrepancy_hw) and parameters->getOption<bool>(OPT_discrepancy_hw));
CBackend::Type hls_c_backend_type = is_discrepancy ? CBackend::CB_DISCREPANCY_ANALYSIS : CBackend::CB_HLS;
vertex hls_c_backend_step =
design_flow_manager.lock()->GetDesignFlowStep(CBackend::ComputeSignature(hls_c_backend_type));
const DesignFlowStepRef design_flow_step =
hls_c_backend_step ?
design_flow_graph->CGetDesignFlowStepInfo(hls_c_backend_step)->design_flow_step :
c_backend_factory->CreateCBackendStep(hls_c_backend_type, output_directory + testbench_basename + ".c",
CBackendInformationConstRef(new HLSCBackendInformation(
output_directory + testbench_basename + ".txt", HLSMgr)));
relationship.insert(design_flow_step);
}
case PRECEDENCE_RELATIONSHIP:
case INVALIDATION_RELATIONSHIP:
{
break;
}
default:
{
THROW_UNREACHABLE("");
}
}
}