summaryrefslogtreecommitdiff
path: root/tools/opt/Passes.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2014-01-11 08:16:35 +0000
committerChandler Carruth <chandlerc@gmail.com>2014-01-11 08:16:35 +0000
commit4e728be2bfb9b2dee209b563c573dc5a7ae00730 (patch)
tree2fc2fb9f6a4e774249ac6434be59def5d016ebf8 /tools/opt/Passes.cpp
parent4ab3e6c16417897c5f5ec5de457644388418425c (diff)
downloadllvm-4e728be2bfb9b2dee209b563c573dc5a7ae00730.tar.gz
llvm-4e728be2bfb9b2dee209b563c573dc5a7ae00730.tar.bz2
llvm-4e728be2bfb9b2dee209b563c573dc5a7ae00730.tar.xz
[PM] Add (very skeletal) support to opt for running the new pass
manager. I cannot emphasize enough that this is a WIP. =] I expect it to change a great deal as things stabilize, but I think its really important to get *some* functionality here so that the infrastructure can be tested more traditionally from the commandline. The current design is looking something like this: ./bin/opt -passes='module(pass_a,pass_b,function(pass_c,pass_d))' So rather than custom-parsed flags, there is a single flag with a string argument that is parsed into the pass pipeline structure. This makes it really easy to have nice structural properties that are very explicit. There is one obvious and important shortcut. You can start off the pipeline with a pass, and the minimal context of pass managers will be built around the entire specified pipeline. This makes the common case for tests super easy: ./bin/opt -passes=instcombine,sroa,gvn But this won't introduce any of the complexity of the fully inferred old system -- we only ever do this for the *entire* argument, and we only look at the first pass. If the other passes don't fit in the pass manager selected it is a hard error. The other interesting aspect here is that I'm not relying on any registration facilities. Such facilities may be unavoidable for supporting plugins, but I have alternative ideas for plugins that I'd like to try first. My plan is essentially to build everything without registration until we hit an absolute requirement. Instead of registration of pass names, there will be a library dedicated to parsing pass names and the pass pipeline strings described above. Currently, this is directly embedded into opt for simplicity as it is very early, but I plan to eventually pull this into a library that opt, bugpoint, and even Clang can depend on. It should end up as a good home for things like the existing PassManagerBuilder as well. There are a bunch of FIXMEs in the code for the parts of this that are just stubbed out to make the patch more incremental. A quick list of what's coming up directly after this: - Support for function passes and building the structured nesting. - Support for printing the pass structure, and FileCheck tests of all of this code. - The .def-file based pass name parsing. - IR priting passes and the corresponding tests. Some obvious things that I'm not going to do right now, but am definitely planning on as the pass manager work gets a bit further: - Pull the parsing into library, including the builders. - Thread the rest of the target stuff into the new pass manager. - Wire support for the new pass manager up to llc. - Plugin support. Some things that I'd like to have, but are significantly lower on my priority list. I'll get to these eventually, but they may also be places where others want to contribute: - Adding nice error reporting for broken pass pipeline descriptions. - Typo-correction for pass names. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198998 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/opt/Passes.cpp')
-rw-r--r--tools/opt/Passes.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/tools/opt/Passes.cpp b/tools/opt/Passes.cpp
new file mode 100644
index 0000000000..49751269df
--- /dev/null
+++ b/tools/opt/Passes.cpp
@@ -0,0 +1,93 @@
+//===- Passes.cpp - Parsing, selection, and running of passes -------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+///
+/// This file provides the infrastructure to parse and build a custom pass
+/// manager based on a commandline flag. It also provides helpers to aid in
+/// analyzing, debugging, and testing pass structures.
+///
+//===----------------------------------------------------------------------===//
+
+#include "Passes.h"
+#include "llvm/IR/PassManager.h"
+
+using namespace llvm;
+
+namespace {
+
+ /// \brief No-op module pass which does nothing.
+struct NoOpModulePass {
+ PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
+};
+
+} // End anonymous namespace.
+
+// FIXME: Factor all of the parsing logic into a .def file that we include
+// under different macros.
+static bool isModulePassName(StringRef Name) {
+ if (Name == "no-op-module") return true;
+
+ return false;
+}
+
+static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
+ assert(isModulePassName(Name));
+ if (Name == "no-op-module") {
+ MPM.addPass(NoOpModulePass());
+ return true;
+ }
+ return false;
+}
+
+static bool parseModulePassPipeline(ModulePassManager &MPM,
+ StringRef &PipelineText) {
+ for (;;) {
+ // Parse nested pass managers by recursing.
+ if (PipelineText.startswith("module(")) {
+ PipelineText = PipelineText.substr(strlen("module("));
+ if (!parseModulePassPipeline(MPM, PipelineText))
+ return false;
+ assert(!PipelineText.empty() && PipelineText[0] == ')');
+ PipelineText = PipelineText.substr(1);
+ } else {
+ // Otherwise try to parse a pass name.
+ size_t End = PipelineText.find_first_of(",)");
+ if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
+ return false;
+
+ PipelineText = PipelineText.substr(End);
+ }
+
+ if (PipelineText.empty() || PipelineText[0] == ')')
+ return true;
+
+ assert(PipelineText[0] == ',');
+ PipelineText = PipelineText.substr(1);
+ }
+}
+
+// Primary pass pipeline description parsing routine.
+// FIXME: Should this routine accept a TargetMachine or require the caller to
+// pre-populate the analysis managers with target-specific stuff?
+bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
+ // Look at the first entry to figure out which layer to start parsing at.
+ if (PipelineText.startswith("module("))
+ return parseModulePassPipeline(MPM, PipelineText);
+
+ // FIXME: Support parsing function pass manager nests.
+
+ // This isn't a direct pass manager name, look for the end of a pass name.
+ StringRef FirstName = PipelineText.substr(0, PipelineText.find_first_of(","));
+ if (isModulePassName(FirstName))
+ return parseModulePassPipeline(MPM, PipelineText);
+
+ // FIXME: Support parsing function pass names.
+
+ return false;
+}