37#include <jlm/tooling/CommandPaths.hpp>
45#include <llvm/IR/LLVMContext.h>
46#include <llvm/IR/Module.h>
47#include <llvm/IRReader/IRReader.h>
48#include <llvm/Support/raw_os_ostream.h>
49#include <llvm/Support/SourceMgr.h>
54#include <unordered_map>
65 const auto returnCode = system(command.c_str());
66 if (returnCode != EXIT_SUCCESS)
69 util::strfmt(
"Subcommand failed with status code ", returnCode,
": ", command));
78 return "PrintCommands";
87 std::cout << node->GetCommand().ToString() <<
"\n";
91std::unique_ptr<CommandGraph>
94 std::unique_ptr<CommandGraph> newCommandGraph(
new CommandGraph());
95 auto & printCommandsNode =
97 newCommandGraph->GetEntryNode().AddEdge(printCommandsNode);
98 printCommandsNode.AddEdge(newCommandGraph->GetExitNode());
99 return newCommandGraph;
107 std::string inputFiles;
109 inputFiles += inputFile.to_str() +
" ";
111 std::string libraryPaths;
113 libraryPaths +=
"-L" + libraryPath +
" ";
115 std::string libraries;
117 libraries +=
"-l" + library +
" ";
119 std::string includePaths;
121 includePaths +=
"-I" + includePath +
" ";
123 std::string macroDefinitions;
125 macroDefinitions +=
"-D" + macroDefinition +
" ";
127 std::string warnings;
129 warnings +=
"-W" + warning +
" ";
132 for (
auto & flag :
Flags_)
133 flags +=
"-f" + flag +
" ";
159 std::string clangArguments;
163 clangArguments +=
"-Xclang " +
ToString(clangArgument) +
" ";
172 clangpath.to_str() +
" ",
185 clangpath.to_str() +
" ",
192 languageStandardArgument,
193 ReplaceAll(macroDefinitions, std::string(
"\""), std::string(
"\\\"")),
209 static std::unordered_map<LanguageStandard, const char *> map(
221 JLM_ASSERT(map.find(languageStandard) != map.end());
222 return map[languageStandard];
228 static std::unordered_map<ClangArgument, const char *> map({
232 JLM_ASSERT(map.find(clangArgument) != map.end());
233 return map[clangArgument];
239 size_t start_pos = 0;
240 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
242 str.replace(start_pos, from.length(), to);
243 start_pos += to.length();
254 llcpath.to_str() +
" ",
258 "--relocation-model=",
271 static std::unordered_map<OptimizationLevel, const char *> map(
277 JLM_ASSERT(map.find(optimizationLevel) != map.end());
278 return map[optimizationLevel];
284 static std::unordered_map<RelocationModel, const char *> map({
289 JLM_ASSERT(map.find(relocationModel) != map.end());
290 return map[relocationModel];
296 std::string programName,
298 : ProgramName_(std::move(programName)),
299 CommandLineOptions_(std::move(commandLineOptions))
305 std::string optimizationArguments;
307 optimizationArguments +=
310 auto inputFormatArgument =
"--input-format="
315 auto outputFormatArgument =
"--output-format="
324 std::string statisticsArguments;
325 auto & demandedStatistics =
327 for (
auto & statisticsId : demandedStatistics.Items())
329 statisticsArguments +=
333 std::string statisticsDirArgument =
341 outputFormatArgument,
342 optimizationArguments,
343 statisticsDirArgument,
377std::vector<std::shared_ptr<rvsdg::Transformation>>
382 std::shared_ptr<rvsdg::Transformation>>
385 std::vector<std::shared_ptr<rvsdg::Transformation>> transformationSequence;
388 if (
auto it = transformations.find(optimizationId); it != transformations.end())
390 transformationSequence.push_back(it->second);
395 transformationSequence.push_back(transformation);
396 transformations[optimizationId] = transformation;
400 return transformationSequence;
403std::shared_ptr<rvsdg::Transformation>
410 switch (optimizationId)
413 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, AgnosticMrs>>();
415 return std::make_shared<llvm::aa::PointsToAnalysisStateEncoder<Andersen, RegionAwareMrs>>();
417 return std::make_shared<llvm::AggregateAllocaSplitting>();
419 return std::make_shared<llvm::CommonNodeElimination>();
421 return std::make_shared<llvm::DeadNodeElimination>();
423 return std::make_shared<llvm::FunctionInlining>();
425 return std::make_shared<llvm::IfConversion>();
427 return std::make_shared<llvm::IOBarrierElimination>();
429 return std::make_shared<llvm::InvariantValueRedirection>(
432 return std::make_shared<llvm::LoadChainSeparation>();
434 return std::make_shared<llvm::LoopStrengthReduction>();
436 return std::make_shared<llvm::LoopUnrolling>(4);
440 return std::make_shared<llvm::NodeSinking>();
442 return std::make_shared<llvm::NodeHoisting>();
444 return std::make_shared<llvm::NodeReduction>();
446 return std::make_shared<llvm::PredicateCorrelation>();
448 return std::make_shared<llvm::RvsdgTreePrinter>(
451 return std::make_shared<llvm::ScalarEvolution>();
453 return std::make_shared<llvm::StoreValueForwarding>();
459std::unique_ptr<llvm::LlvmRvsdgModule>
464 ::llvm::LLVMContext llvmContext;
465 ::llvm::SMDiagnostic diagnostic;
466 auto llvmModule = ::llvm::parseIRFile(llvmIrFile.
to_str(), diagnostic, llvmContext);
468 if (llvmModule ==
nullptr)
471 ::llvm::raw_string_ostream os(errors);
487std::unique_ptr<llvm::LlvmRvsdgModule>
495 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
499std::unique_ptr<llvm::LlvmRvsdgModule>
527 if (outputFile ==
"")
529 std::cout << ascii << std::flush;
533 std::ofstream fileStream(outputFile.
to_str());
548 ::llvm::LLVMContext ctx;
551 if (outputFile ==
"")
553 ::llvm::raw_os_ostream os(std::cout);
554 llvm_module->print(os,
nullptr);
559 ::llvm::raw_fd_ostream os(outputFile.
to_str(), ec);
560 llvm_module->print(os,
nullptr);
573 mlirgen.
Print(omega, outputFile);
576 "This version of jlm-opt has not been compiled with support for the MLIR backend\n");
589 if (outputFile ==
"")
591 std::cout << tree << std::flush;
596 fs.open(outputFile.
to_str());
615 if (outputFile ==
"")
622 fs.open(outputFile.
to_str());
637 if (outputFile ==
"")
639 std::cout << json << std::flush;
644 fs.open(outputFile.
to_str());
702 if (mkdir(
Path_.
to_str().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
711 std::string optimizationArguments;
712 if (!Optimizations_.empty())
714 optimizationArguments =
"-passes=";
716 for (
auto & optimization : Optimizations_)
720 optimizationArguments += ToString(optimization) +
" ";
725 optimizationArguments +=
"," + ToString(optimization) +
" ";
731 clangpath.Dirname().Join(
"opt").to_str(),
733 optimizationArguments,
734 WriteLlvmAssembly_ ?
"-S " :
"",
736 OutputFile().to_str(),
738 InputFile_.to_str());
744 static std::unordered_map<Optimization, const char *> map({
748 JLM_ASSERT(map.find(optimization) != map.end());
749 return map[optimization];
757 std::string inputFilesArgument;
758 for (
auto & inputFile : InputFiles_)
759 inputFilesArgument += inputFile.to_str() +
" ";
762 clangpath.Dirname().Join(
"llvm-link").to_str(),
764 WriteLlvmAssembly_ ?
"-S " :
"",
765 Verbose_ ?
"-v " :
"",
767 OutputFile_.to_str(),
778 for (
auto & o : Options)
782 return util::strfmt(
"jlm-hls ", options,
"-o ", OutputFolder_.to_str(),
" ", InputFile_.to_str());
797 OutputFolder_.to_str(),
799 InputFile().to_str());
static jlm::util::StatisticsCollector statisticsCollector
util::HashSet< rvsdg::Output * > arguments
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)
static std::unique_ptr< LlvmRvsdgModule > ConvertInterProceduralGraphModule(InterProceduralGraphModule &interProceduralGraphModule, InterProceduralGraphToRvsdgStatisticsCollector &statisticsCollector)
std::unique_ptr< InterProceduralGraphModule > ConvertLlvmModule(::llvm::Module &llvmModule)
static std::string strfmt(Args... args)