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  Json,
57  JsonTree,
58  Llvm,
59  Mlir,
60  Tree,
61 
62  LastEnumValue // must always be the last enum value, used for iteration
63  };
64 
65  enum class OptimizationId
66  {
67  FirstEnumValue, // must always be the first enum value, used for iteration
68 
69  AAAndersenAgnostic,
70  AAAndersenRegionAware,
71  AggregateAllocaSplitting,
72  CommonNodeElimination,
73  DeadNodeElimination,
74  FunctionInlining,
75  IfConversion,
76  InvariantValueRedirection,
77  LoadChainSeparation,
78  LoopStrengthReduction,
79  LoopUnrolling,
80  LoopUnswitching,
81  NodePullIn,
82  NodePushOut,
83  NodeReduction,
84  PredicateCorrelation,
85  RvsdgTreePrinter,
86  ScalarEvolution,
87  StoreValueForwarding,
88 
89  LastEnumValue // must always be the last enum value, used for iteration
90  };
91 
93  util::FilePath inputFile,
94  InputFormat inputFormat,
95  util::FilePath outputFile,
96  OutputFormat outputFormat,
97  util::StatisticsCollectorSettings statisticsCollectorSettings,
98  llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
99  std::vector<OptimizationId> optimizations,
100  const bool dumpRvsdgGraphs)
101  : InputFile_(std::move(inputFile)),
102  InputFormat_(inputFormat),
103  OutputFile_(std::move(outputFile)),
104  OutputFormat_(outputFormat),
105  StatisticsCollectorSettings_(std::move(statisticsCollectorSettings)),
106  OptimizationIds_(std::move(optimizations)),
107  RvsdgTreePrinterConfiguration_(std::move(rvsdgTreePrinterConfiguration)),
108  dumpRvsdgGraphs_(dumpRvsdgGraphs)
109  {}
110 
111  void
112  Reset() noexcept override;
113 
114  [[nodiscard]] const util::FilePath &
115  GetInputFile() const noexcept
116  {
117  return InputFile_;
118  }
119 
120  [[nodiscard]] InputFormat
121  GetInputFormat() const noexcept
122  {
123  return InputFormat_;
124  }
125 
126  [[nodiscard]] const util::FilePath &
127  GetOutputFile() const noexcept
128  {
129  return OutputFile_;
130  }
131 
132  [[nodiscard]] OutputFormat
133  GetOutputFormat() const noexcept
134  {
135  return OutputFormat_;
136  }
137 
138  [[nodiscard]] const util::StatisticsCollectorSettings &
140  {
141  return StatisticsCollectorSettings_;
142  }
143 
144  [[nodiscard]] const std::vector<OptimizationId> &
145  GetOptimizationIds() const noexcept
146  {
147  return OptimizationIds_;
148  }
149 
150  [[nodiscard]] const llvm::RvsdgTreePrinter::Configuration &
152  {
153  return RvsdgTreePrinterConfiguration_;
154  }
155 
156  [[nodiscard]] bool
157  dumpRvsdgGraphs() const noexcept
158  {
159  return dumpRvsdgGraphs_;
160  }
161 
162  static OptimizationId
163  FromCommandLineArgumentToOptimizationId(std::string_view commandLineArgument);
164 
165  static util::Statistics::Id
166  FromCommandLineArgumentToStatisticsId(std::string_view commandLineArgument);
167 
168  static std::string_view
169  ToCommandLineArgument(OptimizationId optimizationId);
170 
171  static std::string_view
172  ToCommandLineArgument(util::Statistics::Id statisticsId);
173 
174  static std::string_view
175  ToCommandLineArgument(InputFormat inputFormat);
176 
177  static std::string_view
178  ToCommandLineArgument(OutputFormat outputFormat);
179 
180  static std::unique_ptr<JlmOptCommandLineOptions>
182  util::FilePath inputFile,
183  InputFormat inputFormat,
184  util::FilePath outputFile,
185  OutputFormat outputFormat,
186  util::StatisticsCollectorSettings statisticsCollectorSettings,
187  llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
188  std::vector<OptimizationId> optimizations,
189  bool dumpRvsdgGraphs)
190  {
191  return std::make_unique<JlmOptCommandLineOptions>(
192  std::move(inputFile),
193  inputFormat,
194  std::move(outputFile),
195  outputFormat,
196  std::move(statisticsCollectorSettings),
197  std::move(rvsdgTreePrinterConfiguration),
198  std::move(optimizations),
199  dumpRvsdgGraphs);
200  }
201 
202 private:
208  std::vector<OptimizationId> OptimizationIds_;
211 
213  GetStatisticsIdCommandLineArguments();
214 
215  static const std::unordered_map<OutputFormat, std::string_view> &
216  GetOutputFormatCommandLineArguments();
217 
219  GetOptimizationIdCommandLineMap();
220 };
221 
223 {
224 public:
225  class Compilation;
226 
227  enum class OptimizationLevel
228  {
229  O0,
230  O1,
231  O2,
232  O3,
233  };
234 
235  enum class LanguageStandard
236  {
237  None,
238  Gnu89,
239  Gnu99,
240  C89,
241  C99,
242  C11,
243  Cpp98,
244  Cpp03,
245  Cpp11,
246  Cpp14,
247  };
248 
250  : OnlyPrintCommands_(false),
251  GenerateDebugInformation_(false),
252  Verbose_(false),
253  Rdynamic_(false),
254  Suppress_(false),
255  UsePthreads_(false),
256  Md_(false),
257  OptimizationLevel_(OptimizationLevel::O0),
258  LanguageStandard_(LanguageStandard::None),
259  OutputFile_("a.out")
260  {}
261 
262  static std::string_view
263  ToString(const OptimizationLevel & optimizationLevel);
264 
265  static std::string_view
266  ToString(const LanguageStandard & languageStandard);
267 
268  void
269  Reset() noexcept override;
270 
271  bool OnlyPrintCommands_;
272  bool GenerateDebugInformation_;
273  bool Verbose_;
274  bool Rdynamic_;
275  bool Suppress_;
276  bool UsePthreads_;
277 
278  bool Md_;
279 
280  OptimizationLevel OptimizationLevel_;
281  LanguageStandard LanguageStandard_;
282 
283  util::FilePath OutputFile_;
284  std::vector<std::string> Libraries_;
285  std::vector<std::string> MacroDefinitions_;
286  std::vector<std::string> LibraryPaths_;
287  std::vector<std::string> Warnings_;
288  std::vector<std::string> IncludePaths_;
289  std::vector<std::string> Flags_;
290  std::vector<JlmOptCommandLineOptions::OptimizationId> JlmOptOptimizations_;
291  util::HashSet<util::Statistics::Id> JlmOptPassStatistics_;
292 
293  std::vector<Compilation> Compilations_;
294 };
295 
297 {
298 public:
300  util::FilePath inputFile,
301  util::FilePath dependencyFile,
302  util::FilePath outputFile,
303  std::string mT,
304  bool requiresParsing,
305  bool requiresOptimization,
306  bool requiresAssembly,
307  bool requiresLinking)
308  : RequiresLinking_(requiresLinking),
309  RequiresParsing_(requiresParsing),
310  RequiresOptimization_(requiresOptimization),
311  RequiresAssembly_(requiresAssembly),
312  InputFile_(std::move(inputFile)),
313  OutputFile_(std::move(outputFile)),
314  DependencyFile_(std::move(dependencyFile)),
315  Mt_(std::move(mT))
316  {}
317 
318  [[nodiscard]] const util::FilePath &
319  InputFile() const noexcept
320  {
321  return InputFile_;
322  }
323 
324  [[nodiscard]] const util::FilePath &
325  DependencyFile() const noexcept
326  {
327  return DependencyFile_;
328  }
329 
330  [[nodiscard]] const util::FilePath &
331  OutputFile() const noexcept
332  {
333  return OutputFile_;
334  }
335 
336  [[nodiscard]] const std::string &
337  Mt() const noexcept
338  {
339  return Mt_;
340  }
341 
342  void
343  SetOutputFile(const util::FilePath & outputFile)
344  {
345  OutputFile_ = outputFile;
346  }
347 
348  [[nodiscard]] bool
349  RequiresParsing() const noexcept
350  {
351  return RequiresParsing_;
352  }
353 
354  [[nodiscard]] bool
355  RequiresOptimization() const noexcept
356  {
357  return RequiresOptimization_;
358  }
359 
360  [[nodiscard]] bool
361  RequiresAssembly() const noexcept
362  {
363  return RequiresAssembly_;
364  }
365 
366  [[nodiscard]] bool
367  RequiresLinking() const noexcept
368  {
369  return RequiresLinking_;
370  }
371 
372 private:
380  const std::string Mt_;
381 };
382 
387 {
388 public:
389  enum class OutputFormat
390  {
391  Firrtl,
392  Dot
393  };
394 
396  : InputFile_(""),
397  OutputFiles_(""),
398  OutputFormat_(OutputFormat::Firrtl),
399  ExtractHlsFunction_(false),
400  MemoryLatency_(10),
401  dumpRvsdgGraphs_(false)
402  {
403  JLM_ASSERT(MemoryLatency_ > 0);
404  }
405 
406  void
407  Reset() noexcept override;
408 
409  util::FilePath InputFile_;
410  util::FilePath OutputFiles_;
411  OutputFormat OutputFormat_;
412  std::string HlsFunction_;
413  bool ExtractHlsFunction_;
414  size_t MemoryLatency_;
415  bool dumpRvsdgGraphs_;
416 };
417 
422 {
423 public:
424  class Compilation;
425 
426  enum class OptimizationLevel
427  {
428  O0,
429  O1,
430  O2,
431  O3
432  };
433 
434  enum class LanguageStandard
435  {
436  None,
437  Gnu89,
438  Gnu99,
439  C89,
440  C99,
441  C11,
442  Cpp98,
443  Cpp03,
444  Cpp11,
445  Cpp14
446  };
447 
449  : OnlyPrintCommands_(false),
450  GenerateDebugInformation_(false),
451  Verbose_(false),
452  Rdynamic_(false),
453  Suppress_(false),
454  UsePthreads_(false),
455  GenerateFirrtl_(false),
456  Hls_(false),
457  Md_(false),
458  OptimizationLevel_(OptimizationLevel::O0),
459  LanguageStandard_(LanguageStandard::None),
460  OutputFile_("a.out")
461  {}
462 
463  void
464  Reset() noexcept override;
465 
466  bool OnlyPrintCommands_;
467  bool GenerateDebugInformation_;
468  bool Verbose_;
469  bool Rdynamic_;
470  bool Suppress_;
471  bool UsePthreads_;
472  bool GenerateFirrtl_;
473  bool Hls_;
474 
475  bool Md_;
476 
477  OptimizationLevel OptimizationLevel_;
478  LanguageStandard LanguageStandard_;
479  util::FilePath OutputFile_;
480  std::vector<std::string> Libraries_;
481  std::vector<std::string> MacroDefinitions_;
482  std::vector<std::string> LibraryPaths_;
483  std::vector<std::string> Warnings_;
484  std::vector<std::string> IncludePaths_;
485  std::vector<std::string> Flags_;
486  std::vector<std::string> JlmHls_;
487 
488  std::vector<Compilation> Compilations_;
489  std::string HlsFunctionRegex_;
490 };
491 
493 {
494 public:
496  util::FilePath inputFile,
497  util::FilePath dependencyFile,
498  util::FilePath outputFile,
499  std::string mT,
500  bool parse,
501  bool optimize,
502  bool assemble,
503  bool link)
504  : RequiresLinking_(link),
505  RequiresParsing_(parse),
506  RequiresOptimization_(optimize),
507  RequiresAssembly_(assemble),
508  InputFile_(std::move(inputFile)),
509  OutputFile_(std::move(outputFile)),
510  DependencyFile_(std::move(dependencyFile)),
511  Mt_(std::move(mT))
512  {}
513 
514  [[nodiscard]] const util::FilePath &
515  InputFile() const noexcept
516  {
517  return InputFile_;
518  }
519 
520  [[nodiscard]] const util::FilePath &
521  DependencyFile() const noexcept
522  {
523  return DependencyFile_;
524  }
525 
526  [[nodiscard]] const util::FilePath &
527  OutputFile() const noexcept
528  {
529  return OutputFile_;
530  }
531 
532  [[nodiscard]] const std::string &
533  Mt() const noexcept
534  {
535  return Mt_;
536  }
537 
538  void
539  SetOutputFile(const util::FilePath & outputFile)
540  {
541  OutputFile_ = outputFile;
542  }
543 
544  [[nodiscard]] bool
545  RequiresParsing() const noexcept
546  {
547  return RequiresParsing_;
548  }
549 
550  [[nodiscard]] bool
551  RequiresOptimization() const noexcept
552  {
553  return RequiresOptimization_;
554  }
555 
556  [[nodiscard]] bool
557  RequiresAssembly() const noexcept
558  {
559  return RequiresAssembly_;
560  }
561 
562  [[nodiscard]] bool
563  RequiresLinking() const noexcept
564  {
565  return RequiresLinking_;
566  }
567 
568 private:
576  const std::string Mt_;
577 };
578 
583 {
584 public:
588  class Exception : public util::Error
589  {
590  public:
591  ~Exception() noexcept override;
592 
593  explicit Exception(const std::string & message)
594  : Error(message)
595  {}
596  };
597 
598  virtual ~CommandLineParser() noexcept;
599 
600  CommandLineParser() = default;
601 
602  virtual const CommandLineOptions &
603  ParseCommandLineArguments(int argc, const char * const * argv) = 0;
604 };
605 
610 {
611 public:
612  ~JlcCommandLineParser() noexcept override;
613 
614  const JlcCommandLineOptions &
615  ParseCommandLineArguments(int argc, const char * const * argv) override;
616 
617 private:
618  static bool
619  IsObjectFile(const util::FilePath & file)
620  {
621  return file.suffix() == "o";
622  }
623 
624  static util::FilePath
626  {
627  return file.Dirname().Join(file.base() + ".o");
628  }
629 
630  static util::FilePath
632  {
633  return file.Dirname().Join(file.base() + ".d");
634  }
635 
637 };
638 
643 {
644 public:
645  ~JlmOptCommandLineParser() noexcept override;
646 
648  ParseCommandLineArguments(int argc, const char * const * argv) override;
649 
650  static const JlmOptCommandLineOptions &
651  Parse(int argc, const char * const * argv);
652 
653 private:
654  std::unique_ptr<JlmOptCommandLineOptions> CommandLineOptions_;
655 };
656 
661 {
662 public:
663  ~JlmHlsCommandLineParser() noexcept override;
664 
666  ParseCommandLineArguments(int argc, const char * const * argv) override;
667 
668  static const JlmHlsCommandLineOptions &
669  Parse(int argc, const char * const * argv);
670 
671 private:
672  JlmHlsCommandLineOptions CommandLineOptions_;
673 };
674 
679 {
680 public:
681  ~JhlsCommandLineParser() noexcept override;
682 
683  const JhlsCommandLineOptions &
684  ParseCommandLineArguments(int argc, const char * const * argv) override;
685 
686  static const JhlsCommandLineOptions &
687  Parse(int argc, const char * const * arv);
688 
689 private:
690  static bool
691  IsObjectFile(const util::FilePath & file);
692 
693  static util::FilePath
694  CreateObjectFileFromFile(const util::FilePath & f);
695 
696  static util::FilePath
697  CreateDependencyFileFromFile(const util::FilePath & f);
698 
699  JhlsCommandLineOptions CommandLineOptions_;
700 };
701 
702 }
703 
704 #endif // JLM_TOOLING_COMMANDLINE_HPP
static const jlm::tooling::JlcCommandLineOptions & ParseCommandLineArguments(const std::vector< std::string > &commandLineArguments)
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
const util::FilePath & GetOutputFile() const noexcept
const llvm::RvsdgTreePrinter::Configuration & GetRvsdgTreePrinterConfiguration() const noexcept
llvm::RvsdgTreePrinter::Configuration RvsdgTreePrinterConfiguration_
InputFormat GetInputFormat() 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 dumpRvsdgGraphs)
OutputFormat GetOutputFormat() const noexcept
const std::vector< OptimizationId > & GetOptimizationIds() const noexcept
std::vector< OptimizationId > OptimizationIds_
const util::StatisticsCollectorSettings & GetStatisticsCollectorSettings() 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 dumpRvsdgGraphs)
Definition: CommandLine.hpp:92
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)