33 #include <jlm/tooling/CommandPaths.hpp>
35 #include <llvm/IR/Module.h>
42 #include <llvm/IR/LLVMContext.h>
43 #include <llvm/IRReader/IRReader.h>
44 #include <llvm/Support/raw_os_ostream.h>
45 #include <llvm/Support/SourceMgr.h>
50 #include <unordered_map>
61 const auto returnCode = system(command.c_str());
62 if (returnCode != EXIT_SUCCESS)
65 util::strfmt(
"Subcommand failed with status code ", returnCode,
": ", command));
74 return "PrintCommands";
83 std::cout << node->GetCommand().ToString() <<
"\n";
87 std::unique_ptr<CommandGraph>
90 std::unique_ptr<CommandGraph> newCommandGraph(
new CommandGraph());
91 auto & printCommandsNode =
93 newCommandGraph->GetEntryNode().AddEdge(printCommandsNode);
94 printCommandsNode.AddEdge(newCommandGraph->GetExitNode());
95 return newCommandGraph;
103 std::string inputFiles;
105 inputFiles += inputFile.to_str() +
" ";
107 std::string libraryPaths;
109 libraryPaths +=
"-L" + libraryPath +
" ";
111 std::string libraries;
113 libraries +=
"-l" + library +
" ";
115 std::string includePaths;
117 includePaths +=
"-I" + includePath +
" ";
119 std::string macroDefinitions;
121 macroDefinitions +=
"-D" + macroDefinition +
" ";
123 std::string warnings;
125 warnings +=
"-W" + warning +
" ";
128 for (
auto & flag :
Flags_)
129 flags +=
"-f" + flag +
" ";
131 std::string arguments;
133 arguments +=
"-pthread ";
139 arguments +=
"-rdynamic ";
148 arguments +=
"-MT " +
Mt_ +
" ";
155 std::string clangArguments;
159 clangArguments +=
"-Xclang " +
ToString(clangArgument) +
" ";
168 clangpath.to_str() +
" ",
181 clangpath.to_str() +
" ",
188 languageStandardArgument,
189 ReplaceAll(macroDefinitions, std::string(
"\""), std::string(
"\\\"")),
205 static std::unordered_map<LanguageStandard, const char *> map(
217 JLM_ASSERT(map.find(languageStandard) != map.end());
218 return map[languageStandard];
224 static std::unordered_map<ClangArgument, const char *> map({
228 JLM_ASSERT(map.find(clangArgument) != map.end());
229 return map[clangArgument];
235 size_t start_pos = 0;
236 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
238 str.replace(start_pos, from.length(), to);
239 start_pos += to.length();
250 llcpath.to_str() +
" ",
254 "--relocation-model=",
267 static std::unordered_map<OptimizationLevel, const char *> map(
273 JLM_ASSERT(map.find(optimizationLevel) != map.end());
274 return map[optimizationLevel];
280 static std::unordered_map<RelocationModel, const char *> map({
285 JLM_ASSERT(map.find(relocationModel) != map.end());
286 return map[relocationModel];
292 std::string programName,
294 : ProgramName_(std::move(programName)),
295 CommandLineOptions_(std::move(commandLineOptions))
301 std::string optimizationArguments;
303 optimizationArguments +=
306 auto inputFormatArgument =
"--input-format="
311 auto outputFormatArgument =
"--output-format="
320 std::string statisticsArguments;
321 auto & demandedStatistics =
323 for (
auto & statisticsId : demandedStatistics.Items())
325 statisticsArguments +=
329 std::string statisticsDirArgument =
337 outputFormatArgument,
338 optimizationArguments,
339 statisticsDirArgument,
354 statisticsCollector);
368 statisticsCollector);
373 std::vector<std::shared_ptr<rvsdg::Transformation>>
378 std::shared_ptr<rvsdg::Transformation>>
381 std::vector<std::shared_ptr<rvsdg::Transformation>> transformationSequence;
384 if (
auto it = transformations.find(optimizationId); it != transformations.end())
386 transformationSequence.push_back(it->second);
391 transformationSequence.push_back(transformation);
392 transformations[optimizationId] = transformation;
396 return transformationSequence;
399 std::shared_ptr<rvsdg::Transformation>
406 switch (optimizationId)
409 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, AgnosticMrs>>();
411 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, RegionAwareMrs>>();
413 return std::make_shared<llvm::CommonNodeElimination>();
415 return std::make_shared<llvm::DeadNodeElimination>();
417 return std::make_shared<llvm::FunctionInlining>();
419 return std::make_shared<llvm::IfConversion>();
421 return std::make_shared<llvm::InvariantValueRedirection>();
423 return std::make_shared<llvm::LoadChainSeparation>();
425 return std::make_shared<llvm::LoopUnrolling>(4);
427 return std::make_shared<llvm::LoopUnswitching>();
429 return std::make_shared<llvm::NodeSinking>();
431 return std::make_shared<llvm::NodeHoisting>();
433 return std::make_shared<llvm::NodeReduction>();
435 return std::make_shared<llvm::PredicateCorrelation>();
437 return std::make_shared<llvm::RvsdgTreePrinter>(
440 return std::make_shared<llvm::ScalarEvolution>();
446 std::unique_ptr<llvm::LlvmRvsdgModule>
451 ::llvm::LLVMContext llvmContext;
452 ::llvm::SMDiagnostic diagnostic;
453 auto llvmModule = ::llvm::parseIRFile(llvmIrFile.
to_str(), diagnostic, llvmContext);
455 if (llvmModule ==
nullptr)
458 ::llvm::raw_string_ostream os(errors);
474 std::unique_ptr<llvm::LlvmRvsdgModule>
482 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
486 std::unique_ptr<llvm::LlvmRvsdgModule>
514 if (outputFile ==
"")
516 std::cout << ascii << std::flush;
520 std::ofstream fileStream(outputFile.
to_str());
532 auto fd = outputFile ==
"" ? stdout : fopen(outputFile.
to_str().c_str(),
"w");
549 ::llvm::LLVMContext ctx;
552 if (outputFile ==
"")
554 ::llvm::raw_os_ostream os(std::cout);
555 llvm_module->print(os,
nullptr);
560 ::llvm::raw_fd_ostream os(outputFile.
to_str(), ec);
561 llvm_module->print(os,
nullptr);
574 mlirgen.
Print(omega, outputFile);
577 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
590 if (outputFile ==
"")
592 std::cout << tree << std::flush;
597 fs.open(outputFile.
to_str());
615 if (outputFile ==
"")
622 fs.open(outputFile.
to_str());
637 PrintAsAscii(rvsdgModule, outputFile, statisticsCollector);
641 PrintAsXml(rvsdgModule, outputFile, statisticsCollector);
645 PrintAsLlvm(rvsdgModule, outputFile, statisticsCollector);
649 PrintAsMlir(rvsdgModule, outputFile, statisticsCollector);
657 PrintAsDot(rvsdgModule, outputFile, statisticsCollector);
676 if (mkdir(
Path_.
to_str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
685 std::string optimizationArguments;
686 if (!Optimizations_.empty())
688 optimizationArguments =
"-passes=";
690 for (
auto & optimization : Optimizations_)
694 optimizationArguments +=
ToString(optimization) +
" ";
699 optimizationArguments +=
"," +
ToString(optimization) +
" ";
705 clangpath.Dirname().Join(
"opt").to_str(),
707 optimizationArguments,
708 WriteLlvmAssembly_ ?
"-S " :
"",
712 InputFile_.to_str());
718 static std::unordered_map<Optimization, const char *> map({
722 JLM_ASSERT(map.find(optimization) != map.end());
723 return map[optimization];
731 std::string inputFilesArgument;
732 for (
auto & inputFile : InputFiles_)
733 inputFilesArgument += inputFile.to_str() +
" ";
736 clangpath.Dirname().Join(
"llvm-link").to_str(),
738 WriteLlvmAssembly_ ?
"-S " :
"",
739 Verbose_ ?
"-v " :
"",
741 OutputFile_.to_str(),
752 for (
auto & o : Options)
756 return util::strfmt(
"jlm-hls ", options,
"-o ", OutputFolder_.to_str(),
" ", InputFile_.to_str());
771 OutputFolder_.to_str(),
static std::unique_ptr<::llvm::Module > CreateAndConvertModule(InterProceduralGraphModule &ipGraphModule, ::llvm::LLVMContext &ctx)
static std::unique_ptr< InterProceduralGraphModule > CreateAndConvertModule(LlvmRvsdgModule &rvsdgModule, util::StatisticsCollector &statisticsCollector)
Agnostic mod/ref summarizer.
Region-aware mod/ref summarizer.
static void Print(::mlir::rvsdg::OmegaNode &omega, const util::FilePath &filePath)
::mlir::rvsdg::OmegaNode ConvertModule(const llvm::LlvmRvsdgModule &rvsdgModule)
std::unique_ptr< llvm::LlvmRvsdgModule > ReadAndConvertMlir(const util::FilePath &filePath)
util::graph::Graph & WriteGraphs(util::graph::Writer &writer, const Region ®ion, bool emitTypeGraph)
Region & GetRootRegion() const noexcept
static std::string ToTree(const rvsdg::Region ®ion, const util::AnnotationMap &annotationMap) noexcept
const std::string & to_str() const noexcept
const HashSet< Statistics::Id > & GetDemandedStatistics() const noexcept
const FilePath & GetOutputDirectory() const noexcept
void PrintStatistics()
Print collected statistics to file. If no statistics have been collected, this is a no-op.
void outputAllGraphs(std::ostream &out, OutputFormat format)
#define JLM_UNREACHABLE(msg)
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)
static std::unique_ptr< LlvmRvsdgModule > ConvertInterProceduralGraphModule(InterProceduralGraphModule &interProceduralGraphModule, InterProceduralGraphToRvsdgStatisticsCollector &statisticsCollector)
std::string to_str(const InterProceduralGraph &clg)
static std::string ToString(const std::vector< MemoryNodeId > &memoryNodeIds)
std::string view(const rvsdg::Region *region)
void view_xml(const rvsdg::Region *region, FILE *out)
static std::string strfmt(Args... args)