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