Fletchgen
The Fletcher Design Generator
options.cc
1 // Copyright 2018-2019 Delft University of Technology
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "fletchgen/options.h"
16 
17 #include <fletcher/common.h>
18 #include <CLI/CLI.hpp>
19 
20 namespace fletchgen {
21 
22 bool Options::Parse(Options *options, int argc, char **argv) {
23  CLI::App app{"Fletchgen - The Fletcher Design Generator"};
24 
25  app.get_formatter()->column_width(34);
26 
27  // Required options:
28  app.add_option("-i,--input", options->schema_paths,
29  "List of files with Arrow Schemas to base design on."
30  "Example: --input file1.fbs file2.fbs file3.fbs")
31  ->check(CLI::ExistingFile);
32 
33  // Naming options:
34  app.add_option("-n,--kernel_name", options->kernel_name,
35  "Name of the accelerator kernel.");
36 
37  // Simulation options:
38  app.add_option("-r,--recordbatch_input", options->recordbatch_paths,
39  "List of files with Arrow RecordBatches to base design on and use in simulation memory models."
40  "Schemas contained in these RecordBatches may be skipped for the --input option.");
41  app.add_option("-s,--recordbatch_output", options->srec_out_path,
42  "Memory model contents output file (formatted as SREC).");
43  app.add_option("-t,--srec_dump", options->srec_sim_dump,
44  "Path to dump memory model contents to after simulation (formatted as SREC).");
45 
46  // Output options:
47  app.add_option("-o,--output_path", options->output_dir,
48  "Path to the output directory to place the generated files. (Default: . )");
49 
50  app.add_option("-l,--language", options->languages,
51  "Select the output languages for your design. Each type of output will be stored in a "
52  "seperate subfolder (e.g. <output folder>/vhdl/...). \n"
53  "Available languages:\n"
54  " vhdl : Export as VHDL files (default).\n"
55  " dot : Export as DOT graphs.");
56 
57  app.add_flag("-b,--backup", options->backup,
58  "Backup generated source code files if they exists already. If this flag is used and the source "
59  "file exists already in the specified path, the output filename will be <filename>.bak. This "
60  "file is always overwritten.");
61 
62  app.add_option("--regs", options->regs,
63  "Names of custom registers in the following format: \"<behavior>:<width>:<name>:<init>\", "
64  "where <behavior> is one character from the following options:\n"
65  " c : (control) register content is controlled by host-side software.\n"
66  " s : (status) register content is controlled by hardware kernel.\n"
67  "<init> is optional, and can be used to automatically write to the register in the initialization "
68  "step of the simulation. Init must be a hexadecimal value in the form of 0x01234ABCD.\n"
69  "Example: \"-reg32 c:32:myh2kreg:0xDEADBEEF s:64:mk2hreg\"");
70 
71  app.add_option("-e,--external", options->externals_yaml, "Path to YAML file describing external signals to drag "
72  "between kernel and top-level.");
73 
74  app.add_option("--bus_specs", options->bus_dims,
75  "Specify top-level bus parameters.\n"
76  "Value must be a tuple of the following form: \"aw,dw,lw,bs,bm\"\n"
77  "Where:\n"
78  " aw : Bus address width.\n"
79  " dw : Bus data width.\n"
80  " lw : Bus burst length width.\n"
81  " bs : Bus minimum burst size.\n"
82  " bm : Bus maximum burst size.\n"
83  "Currently supports only one top-level bus specification. Default: \"64,512,64,8,1,16\"");
84 
85  app.add_flag("--mmio64", options->mmio64, "Use a 64-bits AXI4-lite MMIO data bus instead of 32-bits.");
86  app.add_option("--mmio-offset", options->mmio_offset, "AXI4 offset address for Fletcher registers.");
87  //app.add_option("--axi4l-addr-width", options->axi4_lite_aw, "TODO: Width of the AXI4-lite address bus (Default:32).");
88 
89  app.add_flag("--axi", options->axi_top, "Generate AXI top-level template (VHDL only).");
90 
91  app.add_flag("--sim", options->sim_top,
92  "Generate simulation top-level template (VHDL only).");
93  app.add_flag("--vivado_hls", options->vivado_hls,
94  "Generate a Vivado HLS kernel template.");
95 
96  app.add_flag("--static-vhdl", options->static_vhdl, "Write static VHDL support files.");
97 
98  // Other options:
99  app.add_flag("-v,--version", options->version,
100  "Show version.");
101 
102  // TODO(johanpel): implement the quiet and verbose options
103  /*
104  app.add_flag("-q,--quiet", options->quiet,
105  "Surpress all stdout.");
106  app.add_flag("-v,--verbose", options->verbose,
107  "More detailed information on stdout.");
108  */
109 
110  // Try to parse and quit if parsing failed.
111  try {
112  app.parse(argc, argv);
113  } catch (CLI::CallForHelp &e) {
114  std::cout << app.help() << std::endl;
115  options->quit = true;
116  return true;
117  } catch (CLI::Error &e) {
118  FLETCHER_LOG(ERROR, e.get_name() + ":\n" + e.what());
119  return false;
120  }
121 
122  // Also quit when version is called.
123  if (options->version) {
124  options->quit = true;
125  }
126 
127  return true;
128 }
129 
131  if (!srec_out_path.empty()) {
132  if (recordbatches.empty()) {
133  FLETCHER_LOG(WARNING, "SREC output flag set, but no RecordBatches were supplied.");
134  return false;
135  }
136  return true;
137  }
138  return false;
139 }
140 
141 static bool HasLanguage(const std::vector<std::string> &languages, const std::string &lang) {
142  for (const auto &l : languages) {
143  if (l == lang) {
144  return true;
145  }
146  }
147  return false;
148 }
149 
150 bool Options::MustGenerate(const std::string &lang) const {
151  return HasLanguage(languages, lang) && Options::MustGenerateDesign();
152 }
153 
155  return !schema_paths.empty() || !recordbatch_paths.empty();
156 }
157 
159  for (const auto &path : recordbatch_paths) {
160  std::vector<std::shared_ptr<arrow::RecordBatch>> rbs;
161  FLETCHER_LOG(INFO, "Loading RecordBatch(es) from " + path);
162  if (!fletcher::ReadRecordBatchesFromFile(path, &rbs)) {
163  return false;
164  }
165  recordbatches.insert(recordbatches.end(), rbs.begin(), rbs.end());
166  }
167  return true;
168 }
169 
171  for (const auto &path : schema_paths) {
172  std::shared_ptr<arrow::Schema> schema;
173  FLETCHER_LOG(INFO, "Loading Schema from " + path);
174  if (!fletcher::ReadSchemaFromFile(path, &schema)) {
175  return false;
176  }
177  schemas.push_back(schema);
178  }
179  return true;
180 }
181 
182 std::string Options::ToString() const {
183  std::stringstream str;
184  str << "Schema paths:\n";
185  for (const auto &p : schema_paths) {
186  str << " " << p << "\n";
187  }
188  str << "RecordBatch paths:\n";
189  for (const auto &p : schema_paths) {
190  str << " " << p << "\n";
191  }
192  return str.str();
193 }
194 
195 } // namespace fletchgen
Contains all classes and functions related to Fletchgen.
Definition: array.cc:29
Fletcher program options.
Definition: options.h:26
bool quit
Whether to quit the program without doing anything (useful for just showing help or version).
Definition: options.h:71
std::string output_dir
Output directory.
Definition: options.h:36
bool LoadSchemas()
Load all specified Schemas. Returns true if successful, false otherwise.
Definition: options.cc:170
std::vector< std::shared_ptr< arrow::Schema > > schemas
Loaded schemas.
Definition: options.h:30
bool MustGenerateDesign() const
Return true if a design must be generated.
Definition: options.cc:154
bool LoadRecordBatches()
Load all specified RecordBatches. Returns true if successful, false otherwise.
Definition: options.cc:158
std::vector< std::string > languages
Output languages.
Definition: options.h:38
bool axi_top
Whether to generate an AXI top level.
Definition: options.h:59
bool MustGenerateSREC() const
Return true if an SREC file must be generated.
Definition: options.cc:130
std::vector< std::string > recordbatch_paths
Paths to RecordBatches.
Definition: options.h:32
std::string srec_out_path
SREC output path. This is the path where an SREC file based on input RecordBatches will be placed.
Definition: options.h:40
bool mmio64
Use 64-bits data width for AXI4-lite MMIO bus when true.
Definition: options.h:52
bool backup
Whether to backup any existing generated files.
Definition: options.h:65
bool static_vhdl
Whether to generate static VHDL files (copied from hardware directory, embedded as resources).
Definition: options.h:63
bool version
Show version information.
Definition: options.h:79
static bool Parse(Options *options, int argc, char **argv)
Parse command line options and store the result.
Definition: options.cc:22
std::vector< std::string > schema_paths
Paths to the schema files.
Definition: options.h:28
std::vector< std::string > bus_dims
Bus dimensions strings.
Definition: options.h:50
std::string srec_sim_dump
SREC simulation output path, where the simulation should dump the memory contents of written RecordBa...
Definition: options.h:42
bool sim_top
Whether to simulate an AXI top level.
Definition: options.h:61
bool MustGenerate(const std::string &target) const
Return true if generation must take place for some target.
Definition: options.cc:150
std::string externals_yaml
File to parse for external signals to/from top level to kernel.
Definition: options.h:48
std::vector< std::string > regs
Custom 32-bit registers.
Definition: options.h:46
std::vector< std::shared_ptr< arrow::RecordBatch > > recordbatches
Loaded RecordBatches.
Definition: options.h:34
size_t mmio_offset
AXI4-lite offset address for Fletcher registers.
Definition: options.h:56
std::string ToString() const
Return human-readable options.
Definition: options.cc:182
bool vivado_hls
Vivado HLS template. TODO(johanpel): not yet implemented.
Definition: options.h:68
std::string kernel_name
Name of the Kernel.
Definition: options.h:44