PandA-2024.02
CTestbenchExecution.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
40 #include "CTestbenchExecution.hpp"
41 
42 #include "Parameter.hpp"
44 #include "behavioral_helper.hpp"
45 #include "c_backend.hpp"
48 #include "call_graph_manager.hpp"
49 #include "compiler_wrapper.hpp"
50 #include "custom_set.hpp"
51 #include "design_flow_graph.hpp"
52 #include "design_flow_manager.hpp"
53 #include "fileIO.hpp"
55 #include "function_behavior.hpp"
56 #include "hls_manager.hpp"
57 #include "string_manipulation.hpp"
58 
59 #include <filesystem>
60 #include <list>
61 #include <string>
62 #include <tuple>
63 
65  const DesignFlowManagerConstRef _design_flow_manager,
66  const HLSFlowStepSpecializationConstRef _hls_flow_step_specialization)
67  : HLS_step(Param, AppM, _design_flow_manager, HLSFlowStep_Type::C_TESTBENCH_EXECUTION),
68  c_backend_info(RefcountCast<const CBackendInformation>(_hls_flow_step_specialization))
69 {
70  debug_level = parameters->get_class_debug_level(GET_CLASS(*this));
71 }
72 
74  const DesignFlowStep::RelationshipType relationship_type)
75 {
76  HLS_step::ComputeRelationships(relationship, relationship_type);
77  switch(relationship_type)
78  {
80  {
81  const auto c_backend_factory =
82  GetPointer<const CBackendStepFactory>(design_flow_manager.lock()->CGetDesignFlowStepFactory("CBackend"));
83  relationship.insert(c_backend_factory->CreateCBackendStep(c_backend_info));
84  }
86  {
87  break;
88  }
90  {
91  break;
92  }
93  default:
94  {
96  }
97  }
98 }
99 
101 {
102  return true;
103 }
104 
106 {
107  INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "-->Executing C testbench");
108  // compute top function name and use it to setup the artificial main for cosimulation
109  const auto is_discrepancy = c_backend_info->type == CBackendInformation::CB_DISCREPANCY_ANALYSIS;
110  const auto default_compiler = parameters->getOption<CompilerWrapper_CompilerTarget>(OPT_default_compiler);
111  // NOTE: starting from version 13 on it seems clang is not respecting the -fno-strict-aliasing flag generating
112  // incorrect code when type punning is present
113  const auto opt_set = default_compiler == CompilerWrapper_CompilerTarget::CT_I386_CLANG13 ||
117  const CompilerWrapperConstRef compiler_wrapper(new CompilerWrapper(parameters, default_compiler, opt_set));
118  const auto is_clang = CompilerWrapper::isClangCheck(default_compiler);
119  std::string compiler_flags = "-fwrapv -flax-vector-conversions -msse2 -mfpmath=sse -fno-strict-aliasing "
120  "-D'__builtin_bambu_time_start()=' -D'__builtin_bambu_time_stop()=' ";
121  if(!is_clang)
122  {
123  compiler_flags += "-ffloat-store ";
124  }
125 
126  if(!parameters->isOption(OPT_input_format) ||
127  parameters->getOption<Parameters_FileFormat>(OPT_input_format) == Parameters_FileFormat::FF_C)
128  {
129  if(!is_clang)
130  {
131  compiler_flags += "-fexcess-precision=standard ";
132  }
133  }
134  if(parameters->isOption(OPT_tb_extra_gcc_options))
135  {
136  compiler_flags += parameters->getOption<std::string>(OPT_tb_extra_gcc_options) + " ";
137  }
138 
139  if(parameters->isOption(OPT_gcc_optimizations))
140  {
141  const auto gcc_parameters = parameters->getOption<CustomSet<std::string>>(OPT_gcc_optimizations);
142  if(gcc_parameters.find("tree-vectorize") != gcc_parameters.end())
143  {
144  boost::replace_all(compiler_flags, "-msse2", "");
145  compiler_flags += "-m32 ";
146  }
147  }
148 
149  if(is_discrepancy && (!parameters->isOption(OPT_discrepancy_permissive_ptrs) ||
150  !parameters->getOption<bool>(OPT_discrepancy_permissive_ptrs)))
151  {
153  {
154  compiler_flags += "-g -fsanitize=address -fno-omit-frame-pointer -fno-common ";
155  }
157  {
158  compiler_flags += "-fsanitize=undefined -fsanitize-recover=undefined ";
159  }
160  }
161  // setup source files
162  std::list<std::string> file_sources = {c_backend_info->src_filename};
163  // add source files to interface with python golden reference, if any
164  if(parameters->isOption(OPT_no_parse_c_python))
165  {
166  const auto no_parse_files = parameters->getOption<CustomSet<std::string>>(OPT_no_parse_c_python);
167  for(const auto& no_parse_file : no_parse_files)
168  {
169  file_sources.push_back(no_parse_file);
170  }
171  }
172 
173  auto exec_name = std::filesystem::path(c_backend_info->src_filename).replace_extension().string();
174  INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---create executable: " + exec_name);
175  // compile the source file to get an executable
176  compiler_wrapper->CreateExecutable(file_sources, exec_name, compiler_flags);
177  // executing the test to generate inputs and expected outputs values
178  if(is_discrepancy)
179  {
181  {
182  exec_name = "ASAN_OPTIONS='symbolize=1:redzone=2048' " + exec_name;
183  }
184  }
185  INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "---exec executable: " + exec_name);
186  const auto ret = PandaSystem(parameters, exec_name, false, c_backend_info->out_filename);
187  if(IsError(ret))
188  {
189  THROW_ERROR("Error in generating the expected test results");
190  }
191  INDENT_DBG_MEX(DEBUG_LEVEL_MINIMUM, debug_level, "<--Executed C testbench");
193 }
Data structure representing the entire HLS information.
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
Factory class to create c backend.
#define GET_CLASS(obj)
Macro returning the actual type of an object.
RelationshipType
The relationship type.
Source must be executed to satisfy target.
static bool isCurrentOrNewer(CompilerWrapper_CompilerTarget ct, CompilerWrapper_CompilerTarget compare)
Base class to pass information to a c backend.
Simple class used to drive the backend in order to be able to print c source code.
Auxiliary methods for manipulating string.
bool IsError(const int error_value)
Utility include.
Definition: exceptions.cpp:58
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
HLSFlowStep_Type
Definition: hls_step.hpp:95
Classes to describe design flow graph.
redefinition of set to manage ordered/unordered structures
utility function used to read files.
CompilerWrapper_CompilerTarget
target of the compiler
const Wrefcount< const DesignFlowManager > design_flow_manager
The design flow manager.
const ParameterConstRef parameters
Set of input parameters.
DesignFlowStep_Status
The status of a step.
static bool isClangCheck(CompilerWrapper_CompilerTarget ct)
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
Wrapper of design_flow.
void ComputeRelationships(DesignFlowStepSet &design_flow_step_set, const DesignFlowStep::RelationshipType relationship_type) override
Compute the relationships of a step with other steps.
Definition: hls_step.cpp:302
int PandaSystem(const ParameterConstRef Param, const std::string &system_command, bool host_exec, const std::string &output, const unsigned int type, const bool background, const size_t timeout)
System call forcing execution with bash.
Definition: fileIO.cpp:78
refcount< T > lock() const
Definition: refcount.hpp:212
DesignFlowStep_Status Exec() override
Execute the step.
This class contains the methods to create a frontend flow step.
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
Parameters_FileFormat
File formats.
Definition: Parameter.hpp:261
void ComputeRelationships(DesignFlowStepSet &relationship, const DesignFlowStep::RelationshipType relationship_type) override
Compute the relationships of a step with other steps.
this class is used to manage the command-line or XML options.
Main class for wrapping the frontend compiler.
bool HasToBeExecuted() const override
Check if this step has actually to be executed.
Wrapper to call graph.
int debug_level
The debug level.
Base class to pass information to a c backend.
const CBackendInformationConstRef c_backend_info
This class contains the base representation for a generic frontend flow step which works on the whole...
CTestbenchExecution(const ParameterConstRef Param, const HLS_managerRef AppM, const DesignFlowManagerConstRef design_flow_manager, const HLSFlowStepSpecializationConstRef hls_flow_step_specialization)
A brief description of the C++ Header File.
#define DEBUG_LEVEL_MINIMUM
minimum debugging print is performed.
Implementation of the wrapper to Gcc for C sources.

Generated on Mon Feb 12 2024 13:02:54 for PandA-2024.02 by doxygen 1.8.13