36 #include <jlm/tooling/CommandPaths.hpp>
44 #include <llvm/IR/LLVMContext.h>
45 #include <llvm/IR/Module.h>
46 #include <llvm/IRReader/IRReader.h>
47 #include <llvm/Support/raw_os_ostream.h>
48 #include <llvm/Support/SourceMgr.h>
53 #include <unordered_map>
64 const auto returnCode = system(command.c_str());
65 if (returnCode != EXIT_SUCCESS)
68 util::strfmt(
"Subcommand failed with status code ", returnCode,
": ", command));
77 return "PrintCommands";
86 std::cout << node->GetCommand().ToString() <<
"\n";
90 std::unique_ptr<CommandGraph>
93 std::unique_ptr<CommandGraph> newCommandGraph(
new CommandGraph());
94 auto & printCommandsNode =
96 newCommandGraph->GetEntryNode().AddEdge(printCommandsNode);
97 printCommandsNode.AddEdge(newCommandGraph->GetExitNode());
98 return newCommandGraph;
106 std::string inputFiles;
108 inputFiles += inputFile.to_str() +
" ";
110 std::string libraryPaths;
112 libraryPaths +=
"-L" + libraryPath +
" ";
114 std::string libraries;
116 libraries +=
"-l" + library +
" ";
118 std::string includePaths;
120 includePaths +=
"-I" + includePath +
" ";
122 std::string macroDefinitions;
124 macroDefinitions +=
"-D" + macroDefinition +
" ";
126 std::string warnings;
128 warnings +=
"-W" + warning +
" ";
131 for (
auto & flag :
Flags_)
132 flags +=
"-f" + flag +
" ";
134 std::string arguments;
136 arguments +=
"-pthread ";
142 arguments +=
"-rdynamic ";
151 arguments +=
"-MT " +
Mt_ +
" ";
158 std::string clangArguments;
162 clangArguments +=
"-Xclang " +
ToString(clangArgument) +
" ";
171 clangpath.to_str() +
" ",
184 clangpath.to_str() +
" ",
191 languageStandardArgument,
192 ReplaceAll(macroDefinitions, std::string(
"\""), std::string(
"\\\"")),
208 static std::unordered_map<LanguageStandard, const char *> map(
220 JLM_ASSERT(map.find(languageStandard) != map.end());
221 return map[languageStandard];
227 static std::unordered_map<ClangArgument, const char *> map({
231 JLM_ASSERT(map.find(clangArgument) != map.end());
232 return map[clangArgument];
238 size_t start_pos = 0;
239 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
241 str.replace(start_pos, from.length(), to);
242 start_pos += to.length();
253 llcpath.to_str() +
" ",
257 "--relocation-model=",
270 static std::unordered_map<OptimizationLevel, const char *> map(
276 JLM_ASSERT(map.find(optimizationLevel) != map.end());
277 return map[optimizationLevel];
283 static std::unordered_map<RelocationModel, const char *> map({
288 JLM_ASSERT(map.find(relocationModel) != map.end());
289 return map[relocationModel];
295 std::string programName,
297 : ProgramName_(std::move(programName)),
298 CommandLineOptions_(std::move(commandLineOptions))
304 std::string optimizationArguments;
306 optimizationArguments +=
309 auto inputFormatArgument =
"--input-format="
314 auto outputFormatArgument =
"--output-format="
323 std::string statisticsArguments;
324 auto & demandedStatistics =
326 for (
auto & statisticsId : demandedStatistics.Items())
328 statisticsArguments +=
332 std::string statisticsDirArgument =
340 outputFormatArgument,
341 optimizationArguments,
342 statisticsDirArgument,
376 std::vector<std::shared_ptr<rvsdg::Transformation>>
381 std::shared_ptr<rvsdg::Transformation>>
384 std::vector<std::shared_ptr<rvsdg::Transformation>> transformationSequence;
387 if (
auto it = transformations.find(optimizationId); it != transformations.end())
389 transformationSequence.push_back(it->second);
394 transformationSequence.push_back(transformation);
395 transformations[optimizationId] = transformation;
399 return transformationSequence;
402 std::shared_ptr<rvsdg::Transformation>
409 switch (optimizationId)
412 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, AgnosticMrs>>();
414 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, RegionAwareMrs>>();
416 return std::make_shared<llvm::AggregateAllocaSplitting>();
418 return std::make_shared<llvm::CommonNodeElimination>();
420 return std::make_shared<llvm::DeadNodeElimination>();
422 return std::make_shared<llvm::FunctionInlining>();
424 return std::make_shared<llvm::IfConversion>();
426 return std::make_shared<llvm::InvariantValueRedirection>(
429 return std::make_shared<llvm::LoadChainSeparation>();
431 return std::make_shared<llvm::LoopStrengthReduction>();
433 return std::make_shared<llvm::LoopUnrolling>(4);
437 return std::make_shared<llvm::NodeSinking>();
439 return std::make_shared<llvm::NodeHoisting>();
441 return std::make_shared<llvm::NodeReduction>();
443 return std::make_shared<llvm::PredicateCorrelation>();
445 return std::make_shared<llvm::RvsdgTreePrinter>(
448 return std::make_shared<llvm::ScalarEvolution>();
450 return std::make_shared<llvm::StoreValueForwarding>();
456 std::unique_ptr<llvm::LlvmRvsdgModule>
461 ::llvm::LLVMContext llvmContext;
462 ::llvm::SMDiagnostic diagnostic;
463 auto llvmModule = ::llvm::parseIRFile(llvmIrFile.
to_str(), diagnostic, llvmContext);
465 if (llvmModule ==
nullptr)
468 ::llvm::raw_string_ostream os(errors);
484 std::unique_ptr<llvm::LlvmRvsdgModule>
492 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
496 std::unique_ptr<llvm::LlvmRvsdgModule>
524 if (outputFile ==
"")
526 std::cout << ascii << std::flush;
530 std::ofstream fileStream(outputFile.
to_str());
545 ::llvm::LLVMContext ctx;
548 if (outputFile ==
"")
550 ::llvm::raw_os_ostream os(std::cout);
551 llvm_module->print(os,
nullptr);
556 ::llvm::raw_fd_ostream os(outputFile.
to_str(), ec);
557 llvm_module->print(os,
nullptr);
570 mlirgen.
Print(omega, outputFile);
573 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
586 if (outputFile ==
"")
588 std::cout << tree << std::flush;
593 fs.open(outputFile.
to_str());
612 if (outputFile ==
"")
619 fs.open(outputFile.
to_str());
634 if (outputFile ==
"")
636 std::cout << json << std::flush;
641 fs.open(outputFile.
to_str());
699 if (mkdir(
Path_.
to_str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
708 std::string optimizationArguments;
709 if (!Optimizations_.empty())
711 optimizationArguments =
"-passes=";
713 for (
auto & optimization : Optimizations_)
717 optimizationArguments +=
ToString(optimization) +
" ";
722 optimizationArguments +=
"," +
ToString(optimization) +
" ";
728 clangpath.Dirname().Join(
"opt").to_str(),
730 optimizationArguments,
731 WriteLlvmAssembly_ ?
"-S " :
"",
735 InputFile_.to_str());
741 static std::unordered_map<Optimization, const char *> map({
745 JLM_ASSERT(map.find(optimization) != map.end());
746 return map[optimization];
754 std::string inputFilesArgument;
755 for (
auto & inputFile : InputFiles_)
756 inputFilesArgument += inputFile.to_str() +
" ";
759 clangpath.Dirname().Join(
"llvm-link").to_str(),
761 WriteLlvmAssembly_ ?
"-S " :
"",
762 Verbose_ ?
"-v " :
"",
764 OutputFile_.to_str(),
775 for (
auto & o : Options)
779 return util::strfmt(
"jlm-hls ", options,
"-o ", OutputFolder_.to_str(),
" ", InputFile_.to_str());
794 OutputFolder_.to_str(),
static jlm::util::StatisticsCollector statisticsCollector
static std::unique_ptr<::llvm::Module > CreateAndConvertModule(InterProceduralGraphModule &ipGraphModule, ::llvm::LLVMContext &ctx)
static std::shared_ptr< const LoopUnswitchingDefaultHeuristic > create()
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 toJson(const Region ®ion, const util::AnnotationMap &annotationMap) 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)
static std::string strfmt(Args... args)