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 NodeHoisting,
83 NodePullIn,
84
89 NodePushOut,
90
91 NodeReduction,
92 PredicateCorrelation,
93 RvsdgTreePrinter,
94 ScalarEvolution,
95 StoreValueForwarding,
96
97 LastEnumValue // must always be the last enum value, used for iteration
98 };
99
101 util::FilePath inputFile,
102 InputFormat inputFormat,
103 util::FilePath outputFile,
104 OutputFormat outputFormat,
105 util::StatisticsCollectorSettings statisticsCollectorSettings,
106 llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
107 std::vector<OptimizationId> optimizations,
108 const bool dumpRvsdgGraphs)
109 : InputFile_(std::move(inputFile)),
110 InputFormat_(inputFormat),
111 OutputFile_(std::move(outputFile)),
112 OutputFormat_(outputFormat),
113 StatisticsCollectorSettings_(std::move(statisticsCollectorSettings)),
114 OptimizationIds_(std::move(optimizations)),
115 RvsdgTreePrinterConfiguration_(std::move(rvsdgTreePrinterConfiguration)),
116 dumpRvsdgGraphs_(dumpRvsdgGraphs)
117 {}
118
119 void
120 Reset() noexcept override;
121
122 [[nodiscard]] const util::FilePath &
123 GetInputFile() const noexcept
124 {
125 return InputFile_;
126 }
127
128 [[nodiscard]] InputFormat
129 GetInputFormat() const noexcept
130 {
131 return InputFormat_;
132 }
133
134 [[nodiscard]] const util::FilePath &
135 GetOutputFile() const noexcept
136 {
137 return OutputFile_;
138 }
139
140 [[nodiscard]] OutputFormat
141 GetOutputFormat() const noexcept
142 {
143 return OutputFormat_;
144 }
145
146 [[nodiscard]] const util::StatisticsCollectorSettings &
148 {
149 return StatisticsCollectorSettings_;
150 }
151
152 [[nodiscard]] const std::vector<OptimizationId> &
153 GetOptimizationIds() const noexcept
154 {
155 return OptimizationIds_;
156 }
157
158 [[nodiscard]] const llvm::RvsdgTreePrinter::Configuration &
160 {
161 return RvsdgTreePrinterConfiguration_;
162 }
163
164 [[nodiscard]] bool
165 dumpRvsdgGraphs() const noexcept
166 {
167 return dumpRvsdgGraphs_;
168 }
169
170 static OptimizationId
171 FromCommandLineArgumentToOptimizationId(std::string_view commandLineArgument);
172
174 FromCommandLineArgumentToStatisticsId(std::string_view commandLineArgument);
175
176 static std::string_view
177 ToCommandLineArgument(OptimizationId optimizationId);
178
179 static std::string_view
180 ToCommandLineArgument(util::Statistics::Id statisticsId);
181
182 static std::string_view
183 ToCommandLineArgument(InputFormat inputFormat);
184
185 static std::string_view
186 ToCommandLineArgument(OutputFormat outputFormat);
187
188 static std::unique_ptr<JlmOptCommandLineOptions>
190 util::FilePath inputFile,
191 InputFormat inputFormat,
192 util::FilePath outputFile,
193 OutputFormat outputFormat,
194 util::StatisticsCollectorSettings statisticsCollectorSettings,
195 llvm::RvsdgTreePrinter::Configuration rvsdgTreePrinterConfiguration,
196 std::vector<OptimizationId> optimizations,
197 bool dumpRvsdgGraphs)
198 {
199 return std::make_unique<JlmOptCommandLineOptions>(
200 std::move(inputFile),
201 inputFormat,
202 std::move(outputFile),
203 outputFormat,
204 std::move(statisticsCollectorSettings),
205 std::move(rvsdgTreePrinterConfiguration),
206 std::move(optimizations),
207 dumpRvsdgGraphs);
208 }
209
210private:
216 std::vector<OptimizationId> OptimizationIds_;
219
221 GetStatisticsIdCommandLineArguments();
222
223 static const std::unordered_map<OutputFormat, std::string_view> &
224 GetOutputFormatCommandLineArguments();
225
227 GetOptimizationIdCommandLineMap();
228};
229
231{
232public:
233 class Compilation;
234
236 {
237 O0,
238 O1,
239 O2,
240 O3,
241 };
242
244 {
245 None,
246 Gnu89,
247 Gnu99,
248 C89,
249 C99,
250 C11,
251 Cpp98,
252 Cpp03,
253 Cpp11,
254 Cpp14,
255 };
256
258 : OnlyPrintCommands_(false),
259 GenerateDebugInformation_(false),
260 Verbose_(false),
261 Rdynamic_(false),
262 Suppress_(false),
263 UsePthreads_(false),
264 Md_(false),
265 OptimizationLevel_(OptimizationLevel::O0),
266 LanguageStandard_(LanguageStandard::None),
267 OutputFile_("a.out")
268 {}
269
270 static std::string_view
271 ToString(const OptimizationLevel & optimizationLevel);
272
273 static std::string_view
274 ToString(const LanguageStandard & languageStandard);
275
276 void
277 Reset() noexcept override;
278
279 bool OnlyPrintCommands_;
280 bool GenerateDebugInformation_;
281 bool Verbose_;
282 bool Rdynamic_;
283 bool Suppress_;
284 bool UsePthreads_;
285
286 bool Md_;
287
288 OptimizationLevel OptimizationLevel_;
289 LanguageStandard LanguageStandard_;
290
291 util::FilePath OutputFile_;
292 std::vector<std::string> Libraries_;
293 std::vector<std::string> MacroDefinitions_;
294 std::vector<std::string> LibraryPaths_;
295 std::vector<std::string> Warnings_;
296 std::vector<std::string> IncludePaths_;
297 std::vector<std::string> Flags_;
298 std::vector<JlmOptCommandLineOptions::OptimizationId> JlmOptOptimizations_;
299 util::HashSet<util::Statistics::Id> JlmOptPassStatistics_;
300
301 std::vector<Compilation> Compilations_;
302};
303
305{
306public:
308 util::FilePath inputFile,
309 util::FilePath dependencyFile,
310 util::FilePath outputFile,
311 std::string mT,
312 bool requiresParsing,
313 bool requiresOptimization,
314 bool requiresAssembly,
315 bool requiresLinking)
316 : RequiresLinking_(requiresLinking),
317 RequiresParsing_(requiresParsing),
318 RequiresOptimization_(requiresOptimization),
319 RequiresAssembly_(requiresAssembly),
320 InputFile_(std::move(inputFile)),
321 OutputFile_(std::move(outputFile)),
322 DependencyFile_(std::move(dependencyFile)),
323 Mt_(std::move(mT))
324 {}
325
326 [[nodiscard]] const util::FilePath &
327 InputFile() const noexcept
328 {
329 return InputFile_;
330 }
331
332 [[nodiscard]] const util::FilePath &
333 DependencyFile() const noexcept
334 {
335 return DependencyFile_;
336 }
337
338 [[nodiscard]] const util::FilePath &
339 OutputFile() const noexcept
340 {
341 return OutputFile_;
342 }
343
344 [[nodiscard]] const std::string &
345 Mt() const noexcept
346 {
347 return Mt_;
348 }
349
350 void
351 SetOutputFile(const util::FilePath & outputFile)
352 {
353 OutputFile_ = outputFile;
354 }
355
356 [[nodiscard]] bool
357 RequiresParsing() const noexcept
358 {
359 return RequiresParsing_;
360 }
361
362 [[nodiscard]] bool
363 RequiresOptimization() const noexcept
364 {
365 return RequiresOptimization_;
366 }
367
368 [[nodiscard]] bool
369 RequiresAssembly() const noexcept
370 {
371 return RequiresAssembly_;
372 }
373
374 [[nodiscard]] bool
375 RequiresLinking() const noexcept
376 {
377 return RequiresLinking_;
378 }
379
380private:
388 const std::string Mt_;
389};
390
395{
396public:
397 enum class OutputFormat
398 {
399 Firrtl,
400 Dot
401 };
402
404 : InputFile_(""),
405 OutputFiles_(""),
406 OutputFormat_(OutputFormat::Firrtl),
407 ExtractHlsFunction_(false),
408 MemoryLatency_(10),
409 dumpRvsdgGraphs_(false)
410 {
411 JLM_ASSERT(MemoryLatency_ > 0);
412 }
413
414 void
415 Reset() noexcept override;
416
417 util::FilePath InputFile_;
418 util::FilePath OutputFiles_;
419 OutputFormat OutputFormat_;
420 std::string HlsFunction_;
421 bool ExtractHlsFunction_;
422 size_t MemoryLatency_;
423 bool dumpRvsdgGraphs_;
424};
425
430{
431public:
432 class Compilation;
433
435 {
436 O0,
437 O1,
438 O2,
439 O3
440 };
441
443 {
444 None,
445 Gnu89,
446 Gnu99,
447 C89,
448 C99,
449 C11,
450 Cpp98,
451 Cpp03,
452 Cpp11,
453 Cpp14
454 };
455
457 : OnlyPrintCommands_(false),
458 GenerateDebugInformation_(false),
459 Verbose_(false),
460 Rdynamic_(false),
461 Suppress_(false),
462 UsePthreads_(false),
463 GenerateFirrtl_(false),
464 Hls_(false),
465 Md_(false),
466 OptimizationLevel_(OptimizationLevel::O0),
467 LanguageStandard_(LanguageStandard::None),
468 OutputFile_("a.out")
469 {}
470
471 void
472 Reset() noexcept override;
473
474 bool OnlyPrintCommands_;
475 bool GenerateDebugInformation_;
476 bool Verbose_;
477 bool Rdynamic_;
478 bool Suppress_;
479 bool UsePthreads_;
480 bool GenerateFirrtl_;
481 bool Hls_;
482
483 bool Md_;
484
485 OptimizationLevel OptimizationLevel_;
486 LanguageStandard LanguageStandard_;
487 util::FilePath OutputFile_;
488 std::vector<std::string> Libraries_;
489 std::vector<std::string> MacroDefinitions_;
490 std::vector<std::string> LibraryPaths_;
491 std::vector<std::string> Warnings_;
492 std::vector<std::string> IncludePaths_;
493 std::vector<std::string> Flags_;
494 std::vector<std::string> JlmHls_;
495
496 std::vector<Compilation> Compilations_;
497 std::string HlsFunctionRegex_;
498};
499
501{
502public:
504 util::FilePath inputFile,
505 util::FilePath dependencyFile,
506 util::FilePath outputFile,
507 std::string mT,
508 bool parse,
509 bool optimize,
510 bool assemble,
511 bool link)
512 : RequiresLinking_(link),
513 RequiresParsing_(parse),
514 RequiresOptimization_(optimize),
515 RequiresAssembly_(assemble),
516 InputFile_(std::move(inputFile)),
517 OutputFile_(std::move(outputFile)),
518 DependencyFile_(std::move(dependencyFile)),
519 Mt_(std::move(mT))
520 {}
521
522 [[nodiscard]] const util::FilePath &
523 InputFile() const noexcept
524 {
525 return InputFile_;
526 }
527
528 [[nodiscard]] const util::FilePath &
529 DependencyFile() const noexcept
530 {
531 return DependencyFile_;
532 }
533
534 [[nodiscard]] const util::FilePath &
535 OutputFile() const noexcept
536 {
537 return OutputFile_;
538 }
539
540 [[nodiscard]] const std::string &
541 Mt() const noexcept
542 {
543 return Mt_;
544 }
545
546 void
547 SetOutputFile(const util::FilePath & outputFile)
548 {
549 OutputFile_ = outputFile;
550 }
551
552 [[nodiscard]] bool
553 RequiresParsing() const noexcept
554 {
555 return RequiresParsing_;
556 }
557
558 [[nodiscard]] bool
559 RequiresOptimization() const noexcept
560 {
561 return RequiresOptimization_;
562 }
563
564 [[nodiscard]] bool
565 RequiresAssembly() const noexcept
566 {
567 return RequiresAssembly_;
568 }
569
570 [[nodiscard]] bool
571 RequiresLinking() const noexcept
572 {
573 return RequiresLinking_;
574 }
575
576private:
584 const std::string Mt_;
585};
586
591{
592public:
596 class Exception : public util::Error
597 {
598 public:
599 ~Exception() noexcept override;
600
601 explicit Exception(const std::string & message)
602 : Error(message)
603 {}
604 };
605
606 virtual ~CommandLineParser() noexcept;
607
608 CommandLineParser() = default;
609
610 virtual const CommandLineOptions &
611 ParseCommandLineArguments(int argc, const char * const * argv) = 0;
612};
613
618{
619public:
620 ~JlcCommandLineParser() noexcept override;
621
623 ParseCommandLineArguments(int argc, const char * const * argv) override;
624
625private:
626 static bool
627 IsObjectFile(const util::FilePath & file)
628 {
629 return file.suffix() == "o";
630 }
631
632 static util::FilePath
634 {
635 return file.Dirname().Join(file.base() + ".o");
636 }
637
638 static util::FilePath
640 {
641 return file.Dirname().Join(file.base() + ".d");
642 }
643
645};
646
651{
652public:
653 ~JlmOptCommandLineParser() noexcept override;
654
656 ParseCommandLineArguments(int argc, const char * const * argv) override;
657
658 static const JlmOptCommandLineOptions &
659 Parse(int argc, const char * const * argv);
660
661private:
662 std::unique_ptr<JlmOptCommandLineOptions> CommandLineOptions_;
663};
664
669{
670public:
671 ~JlmHlsCommandLineParser() noexcept override;
672
674 ParseCommandLineArguments(int argc, const char * const * argv) override;
675
676 static const JlmHlsCommandLineOptions &
677 Parse(int argc, const char * const * argv);
678
679private:
680 JlmHlsCommandLineOptions CommandLineOptions_;
681};
682
687{
688public:
689 ~JhlsCommandLineParser() noexcept override;
690
692 ParseCommandLineArguments(int argc, const char * const * argv) override;
693
694 static const JhlsCommandLineOptions &
695 Parse(int argc, const char * const * arv);
696
697private:
698 static bool
699 IsObjectFile(const util::FilePath & file);
700
701 static util::FilePath
702 CreateObjectFileFromFile(const util::FilePath & f);
703
704 static util::FilePath
705 CreateDependencyFileFromFile(const util::FilePath & f);
706
707 JhlsCommandLineOptions CommandLineOptions_;
708};
709
710}
711
712#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