Jlm
CommandLine.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 Nico Reißmann <nico.reissmann@gmail.com>
3  * See COPYING for terms of redistribution.
4  */
5 
6 #ifndef JLM_TOOLING_COMMANDLINE_HPP
7 #define JLM_TOOLING_COMMANDLINE_HPP
8 
11 #include <jlm/util/file.hpp>
12 #include <jlm/util/Statistics.hpp>
13 
14 #include <vector>
15 
16 namespace jlm::tooling
17 {
18 
23 {
24 public:
26 
27  CommandLineOptions() = default;
28 
32  virtual void
33  Reset() noexcept = 0;
34 };
35 
36 class optimization;
37 
42 {
43 public:
44  enum class InputFormat
45  {
46  Llvm,
47  Mlir,
48  };
49 
50  enum class OutputFormat
51  {
52  FirstEnumValue, // must always be the first enum value, used for iteration
53 
54  Ascii,
55  Dot,
56  Llvm,
57  Mlir,
58  Tree,
59  Xml,
60 
61  LastEnumValue // must always be the last enum value, used for iteration
62  };
63 
64  enum class OptimizationId
65  {
66  FirstEnumValue, // must always be the first enum value, used for iteration
67 
68  AAAndersenAgnostic,
69  AAAndersenRegionAware,
70  CommonNodeElimination,
71  DeadNodeElimination,
72  FunctionInlining,
73  IfConversion,
74  InvariantValueRedirection,
75  LoadChainSeparation,
76  LoopUnrolling,
77  LoopUnswitching,
78  NodePullIn,
79  NodePushOut,
80  NodeReduction,
81  PredicateCorrelation,
82  RvsdgTreePrinter,
83  ScalarEvolution,
84 
85  LastEnumValue // must always be the last enum value, used for iteration
86  };
87 
89  util::FilePath inputFile,
90  InputFormat inputFormat,
91  util::FilePath outputFile,
92  OutputFormat outputFormat,
93  util::StatisticsCollectorSettings statisticsCollectorSettings,
94  llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
95  std::vector<OptimizationId> optimizations,
96  const bool dumpRvsdgDotGraphs)
97  : InputFile_(std::move(inputFile)),
98  InputFormat_(inputFormat),
99  OutputFile_(std::move(outputFile)),
100  OutputFormat_(outputFormat),
101  StatisticsCollectorSettings_(std::move(statisticsCollectorSettings)),
102  OptimizationIds_(std::move(optimizations)),
103  RvsdgTreePrinterConfiguration_(std::move(rvsdgTreePrinterConfiguration)),
104  DumpRvsdgDotGraphs_(dumpRvsdgDotGraphs)
105  {}
106 
107  void
108  Reset() noexcept override;
109 
110  [[nodiscard]] const util::FilePath &
111  GetInputFile() const noexcept
112  {
113  return InputFile_;
114  }
115 
116  [[nodiscard]] InputFormat
117  GetInputFormat() const noexcept
118  {
119  return InputFormat_;
120  }
121 
122  [[nodiscard]] const util::FilePath &
123  GetOutputFile() const noexcept
124  {
125  return OutputFile_;
126  }
127 
128  [[nodiscard]] OutputFormat
129  GetOutputFormat() const noexcept
130  {
131  return OutputFormat_;
132  }
133 
134  [[nodiscard]] const util::StatisticsCollectorSettings &
136  {
137  return StatisticsCollectorSettings_;
138  }
139 
140  [[nodiscard]] const std::vector<OptimizationId> &
141  GetOptimizationIds() const noexcept
142  {
143  return OptimizationIds_;
144  }
145 
146  [[nodiscard]] const llvm::RvsdgTreePrinter::Configuration &
148  {
149  return RvsdgTreePrinterConfiguration_;
150  }
151 
152  [[nodiscard]] bool
153  DumpRvsdgDotGraphs() const noexcept
154  {
155  return DumpRvsdgDotGraphs_;
156  }
157 
158  static OptimizationId
159  FromCommandLineArgumentToOptimizationId(std::string_view commandLineArgument);
160 
161  static util::Statistics::Id
162  FromCommandLineArgumentToStatisticsId(std::string_view commandLineArgument);
163 
164  static std::string_view
165  ToCommandLineArgument(OptimizationId optimizationId);
166 
167  static std::string_view
168  ToCommandLineArgument(util::Statistics::Id statisticsId);
169 
170  static std::string_view
171  ToCommandLineArgument(InputFormat inputFormat);
172 
173  static std::string_view
174  ToCommandLineArgument(OutputFormat outputFormat);
175 
176  static std::unique_ptr<JlmOptCommandLineOptions>
178  util::FilePath inputFile,
179  InputFormat inputFormat,
180  util::FilePath outputFile,
181  OutputFormat outputFormat,
182  util::StatisticsCollectorSettings statisticsCollectorSettings,
183  llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
184  std::vector<OptimizationId> optimizations,
185  bool dumpRvsdgDotGraphs)
186  {
187  return std::make_unique<JlmOptCommandLineOptions>(
188  std::move(inputFile),
189  inputFormat,
190  std::move(outputFile),
191  outputFormat,
192  std::move(statisticsCollectorSettings),
193  std::move(rvsdgTreePrinterConfiguration),
194  std::move(optimizations),
195  dumpRvsdgDotGraphs);
196  }
197 
198 private:
204  std::vector<OptimizationId> OptimizationIds_;
207 
209  GetStatisticsIdCommandLineArguments();
210 
211  static const std::unordered_map<OutputFormat, std::string_view> &
212  GetOutputFormatCommandLineArguments();
213 
215  GetOptimizationIdCommandLineMap();
216 };
217 
219 {
220 public:
221  class Compilation;
222 
223  enum class OptimizationLevel
224  {
225  O0,
226  O1,
227  O2,
228  O3,
229  };
230 
231  enum class LanguageStandard
232  {
233  None,
234  Gnu89,
235  Gnu99,
236  C89,
237  C99,
238  C11,
239  Cpp98,
240  Cpp03,
241  Cpp11,
242  Cpp14,
243  };
244 
246  : OnlyPrintCommands_(false),
247  GenerateDebugInformation_(false),
248  Verbose_(false),
249  Rdynamic_(false),
250  Suppress_(false),
251  UsePthreads_(false),
252  Md_(false),
253  OptimizationLevel_(OptimizationLevel::O0),
254  LanguageStandard_(LanguageStandard::None),
255  OutputFile_("a.out")
256  {}
257 
258  static std::string_view
259  ToString(const OptimizationLevel & optimizationLevel);
260 
261  static std::string_view
262  ToString(const LanguageStandard & languageStandard);
263 
264  void
265  Reset() noexcept override;
266 
267  bool OnlyPrintCommands_;
268  bool GenerateDebugInformation_;
269  bool Verbose_;
270  bool Rdynamic_;
271  bool Suppress_;
272  bool UsePthreads_;
273 
274  bool Md_;
275 
276  OptimizationLevel OptimizationLevel_;
277  LanguageStandard LanguageStandard_;
278 
279  util::FilePath OutputFile_;
280  std::vector<std::string> Libraries_;
281  std::vector<std::string> MacroDefinitions_;
282  std::vector<std::string> LibraryPaths_;
283  std::vector<std::string> Warnings_;
284  std::vector<std::string> IncludePaths_;
285  std::vector<std::string> Flags_;
286  std::vector<JlmOptCommandLineOptions::OptimizationId> JlmOptOptimizations_;
287  util::HashSet<util::Statistics::Id> JlmOptPassStatistics_;
288 
289  std::vector<Compilation> Compilations_;
290 };
291 
293 {
294 public:
296  util::FilePath inputFile,
297  util::FilePath dependencyFile,
298  util::FilePath outputFile,
299  std::string mT,
300  bool requiresParsing,
301  bool requiresOptimization,
302  bool requiresAssembly,
303  bool requiresLinking)
304  : RequiresLinking_(requiresLinking),
305  RequiresParsing_(requiresParsing),
306  RequiresOptimization_(requiresOptimization),
307  RequiresAssembly_(requiresAssembly),
308  InputFile_(std::move(inputFile)),
309  OutputFile_(std::move(outputFile)),
310  DependencyFile_(std::move(dependencyFile)),
311  Mt_(std::move(mT))
312  {}
313 
314  [[nodiscard]] const util::FilePath &
315  InputFile() const noexcept
316  {
317  return InputFile_;
318  }
319 
320  [[nodiscard]] const util::FilePath &
321  DependencyFile() const noexcept
322  {
323  return DependencyFile_;
324  }
325 
326  [[nodiscard]] const util::FilePath &
327  OutputFile() const noexcept
328  {
329  return OutputFile_;
330  }
331 
332  [[nodiscard]] const std::string &
333  Mt() const noexcept
334  {
335  return Mt_;
336  }
337 
338  void
339  SetOutputFile(const util::FilePath & outputFile)
340  {
341  OutputFile_ = outputFile;
342  }
343 
344  [[nodiscard]] bool
345  RequiresParsing() const noexcept
346  {
347  return RequiresParsing_;
348  }
349 
350  [[nodiscard]] bool
351  RequiresOptimization() const noexcept
352  {
353  return RequiresOptimization_;
354  }
355 
356  [[nodiscard]] bool
357  RequiresAssembly() const noexcept
358  {
359  return RequiresAssembly_;
360  }
361 
362  [[nodiscard]] bool
363  RequiresLinking() const noexcept
364  {
365  return RequiresLinking_;
366  }
367 
368 private:
376  const std::string Mt_;
377 };
378 
383 {
384 public:
385  enum class OutputFormat
386  {
387  Firrtl,
388  Dot
389  };
390 
392  : InputFile_(""),
393  OutputFiles_(""),
394  OutputFormat_(OutputFormat::Firrtl),
395  ExtractHlsFunction_(false),
396  MemoryLatency_(10),
397  dumpRvsdgDotGraphs_(false)
398  {
399  JLM_ASSERT(MemoryLatency_ > 0);
400  }
401 
402  void
403  Reset() noexcept override;
404 
405  util::FilePath InputFile_;
406  util::FilePath OutputFiles_;
407  OutputFormat OutputFormat_;
408  std::string HlsFunction_;
409  bool ExtractHlsFunction_;
410  size_t MemoryLatency_;
411  bool dumpRvsdgDotGraphs_;
412 };
413 
418 {
419 public:
420  class Compilation;
421 
422  enum class OptimizationLevel
423  {
424  O0,
425  O1,
426  O2,
427  O3
428  };
429 
430  enum class LanguageStandard
431  {
432  None,
433  Gnu89,
434  Gnu99,
435  C89,
436  C99,
437  C11,
438  Cpp98,
439  Cpp03,
440  Cpp11,
441  Cpp14
442  };
443 
445  : OnlyPrintCommands_(false),
446  GenerateDebugInformation_(false),
447  Verbose_(false),
448  Rdynamic_(false),
449  Suppress_(false),
450  UsePthreads_(false),
451  GenerateFirrtl_(false),
452  Hls_(false),
453  Md_(false),
454  OptimizationLevel_(OptimizationLevel::O0),
455  LanguageStandard_(LanguageStandard::None),
456  OutputFile_("a.out")
457  {}
458 
459  void
460  Reset() noexcept override;
461 
462  bool OnlyPrintCommands_;
463  bool GenerateDebugInformation_;
464  bool Verbose_;
465  bool Rdynamic_;
466  bool Suppress_;
467  bool UsePthreads_;
468  bool GenerateFirrtl_;
469  bool Hls_;
470 
471  bool Md_;
472 
473  OptimizationLevel OptimizationLevel_;
474  LanguageStandard LanguageStandard_;
475  util::FilePath OutputFile_;
476  std::vector<std::string> Libraries_;
477  std::vector<std::string> MacroDefinitions_;
478  std::vector<std::string> LibraryPaths_;
479  std::vector<std::string> Warnings_;
480  std::vector<std::string> IncludePaths_;
481  std::vector<std::string> Flags_;
482  std::vector<std::string> JlmHls_;
483 
484  std::vector<Compilation> Compilations_;
485  std::string HlsFunctionRegex_;
486 };
487 
489 {
490 public:
492  util::FilePath inputFile,
493  util::FilePath dependencyFile,
494  util::FilePath outputFile,
495  std::string mT,
496  bool parse,
497  bool optimize,
498  bool assemble,
499  bool link)
500  : RequiresLinking_(link),
501  RequiresParsing_(parse),
502  RequiresOptimization_(optimize),
503  RequiresAssembly_(assemble),
504  InputFile_(std::move(inputFile)),
505  OutputFile_(std::move(outputFile)),
506  DependencyFile_(std::move(dependencyFile)),
507  Mt_(std::move(mT))
508  {}
509 
510  [[nodiscard]] const util::FilePath &
511  InputFile() const noexcept
512  {
513  return InputFile_;
514  }
515 
516  [[nodiscard]] const util::FilePath &
517  DependencyFile() const noexcept
518  {
519  return DependencyFile_;
520  }
521 
522  [[nodiscard]] const util::FilePath &
523  OutputFile() const noexcept
524  {
525  return OutputFile_;
526  }
527 
528  [[nodiscard]] const std::string &
529  Mt() const noexcept
530  {
531  return Mt_;
532  }
533 
534  void
535  SetOutputFile(const util::FilePath & outputFile)
536  {
537  OutputFile_ = outputFile;
538  }
539 
540  [[nodiscard]] bool
541  RequiresParsing() const noexcept
542  {
543  return RequiresParsing_;
544  }
545 
546  [[nodiscard]] bool
547  RequiresOptimization() const noexcept
548  {
549  return RequiresOptimization_;
550  }
551 
552  [[nodiscard]] bool
553  RequiresAssembly() const noexcept
554  {
555  return RequiresAssembly_;
556  }
557 
558  [[nodiscard]] bool
559  RequiresLinking() const noexcept
560  {
561  return RequiresLinking_;
562  }
563 
564 private:
572  const std::string Mt_;
573 };
574 
579 {
580 public:
584  class Exception : public util::Error
585  {
586  public:
587  ~Exception() noexcept override;
588 
589  explicit Exception(const std::string & message)
590  : Error(message)
591  {}
592  };
593 
594  virtual ~CommandLineParser() noexcept;
595 
596  CommandLineParser() = default;
597 
598  virtual const CommandLineOptions &
599  ParseCommandLineArguments(int argc, const char * const * argv) = 0;
600 };
601 
606 {
607 public:
608  ~JlcCommandLineParser() noexcept override;
609 
610  const JlcCommandLineOptions &
611  ParseCommandLineArguments(int argc, const char * const * argv) override;
612 
613 private:
614  static bool
615  IsObjectFile(const util::FilePath & file)
616  {
617  return file.suffix() == "o";
618  }
619 
620  static util::FilePath
622  {
623  return file.Dirname().Join(file.base() + ".o");
624  }
625 
626  static util::FilePath
628  {
629  return file.Dirname().Join(file.base() + ".d");
630  }
631 
633 };
634 
639 {
640 public:
641  ~JlmOptCommandLineParser() noexcept override;
642 
644  ParseCommandLineArguments(int argc, const char * const * argv) override;
645 
646  static const JlmOptCommandLineOptions &
647  Parse(int argc, const char * const * argv);
648 
649 private:
650  std::unique_ptr<JlmOptCommandLineOptions> CommandLineOptions_;
651 };
652 
657 {
658 public:
659  ~JlmHlsCommandLineParser() noexcept override;
660 
662  ParseCommandLineArguments(int argc, const char * const * argv) override;
663 
664  static const JlmHlsCommandLineOptions &
665  Parse(int argc, const char * const * argv);
666 
667 private:
668  JlmHlsCommandLineOptions CommandLineOptions_;
669 };
670 
675 {
676 public:
677  ~JhlsCommandLineParser() noexcept override;
678 
679  const JhlsCommandLineOptions &
680  ParseCommandLineArguments(int argc, const char * const * argv) override;
681 
682  static const JhlsCommandLineOptions &
683  Parse(int argc, const char * const * arv);
684 
685 private:
686  static bool
687  IsObjectFile(const util::FilePath & file);
688 
689  static util::FilePath
690  CreateObjectFileFromFile(const util::FilePath & f);
691 
692  static util::FilePath
693  CreateDependencyFileFromFile(const util::FilePath & f);
694 
695  JhlsCommandLineOptions CommandLineOptions_;
696 };
697 
698 }
699 
700 #endif // JLM_TOOLING_COMMANDLINE_HPP
virtual void Reset() noexcept=0
virtual ~CommandLineParser() noexcept
const std::string & Mt() const noexcept
Compilation(util::FilePath inputFile, util::FilePath dependencyFile, util::FilePath outputFile, std::string mT, bool parse, bool optimize, bool assemble, bool link)
void SetOutputFile(const util::FilePath &outputFile)
const util::FilePath & OutputFile() const noexcept
const util::FilePath & InputFile() const noexcept
const util::FilePath & DependencyFile() const noexcept
~JhlsCommandLineParser() noexcept override
const util::FilePath & DependencyFile() const noexcept
void SetOutputFile(const util::FilePath &outputFile)
const std::string & Mt() const noexcept
const util::FilePath & OutputFile() const noexcept
const util::FilePath & InputFile() const noexcept
Compilation(util::FilePath inputFile, util::FilePath dependencyFile, util::FilePath outputFile, std::string mT, bool requiresParsing, bool requiresOptimization, bool requiresAssembly, bool requiresLinking)
JlcCommandLineOptions CommandLineOptions_
~JlcCommandLineParser() noexcept override
static util::FilePath ToObjectFile(const util::FilePath &file)
static util::FilePath ToDependencyFile(const util::FilePath &file)
~JlmHlsCommandLineParser() noexcept override
bool DumpRvsdgDotGraphs() const noexcept
const util::FilePath & GetOutputFile() const noexcept
const llvm::RvsdgTreePrinter::Configuration & GetRvsdgTreePrinterConfiguration() const noexcept
llvm::RvsdgTreePrinter::Configuration RvsdgTreePrinterConfiguration_
InputFormat GetInputFormat() const noexcept
OutputFormat GetOutputFormat() const noexcept
JlmOptCommandLineOptions(util::FilePath inputFile, InputFormat inputFormat, util::FilePath outputFile, OutputFormat outputFormat, util::StatisticsCollectorSettings statisticsCollectorSettings, llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration, std::vector< OptimizationId > optimizations, const bool dumpRvsdgDotGraphs)
Definition: CommandLine.hpp:88
const std::vector< OptimizationId > & GetOptimizationIds() const noexcept
static std::unique_ptr< JlmOptCommandLineOptions > Create(util::FilePath inputFile, InputFormat inputFormat, util::FilePath outputFile, OutputFormat outputFormat, util::StatisticsCollectorSettings statisticsCollectorSettings, llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration, std::vector< OptimizationId > optimizations, bool dumpRvsdgDotGraphs)
std::vector< OptimizationId > OptimizationIds_
const util::StatisticsCollectorSettings & GetStatisticsCollectorSettings() const noexcept
util::StatisticsCollectorSettings StatisticsCollectorSettings_
~JlmOptCommandLineParser() noexcept override
FilePath Join(const std::string &other) const
Definition: file.hpp:193
std::string base() const noexcept
Returns the base name of the file without the path.
Definition: file.hpp:61
FilePath Dirname() const noexcept
Returns the path to the file or directory's parent directory. Emulates the behavior of the GNU coreut...
Definition: file.hpp:148
#define JLM_ASSERT(x)
Definition: common.hpp:16
static std::string ToString(const std::vector< MemoryNodeId > &memoryNodeIds)