@@ -21,3 +21,5 @@
// Options for pipeline
extern llvm::cl::list<std::string> InputFiles;
+// Options for PrepareForOptPass
+extern llvm::cl::opt<bool> TranslateAllHelpers;
new file mode 100644
@@ -0,0 +1,34 @@
+//
+// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, see <http://www.gnu.org/licenses/>.
+//
+
+#pragma once
+
+#include <llvm/IR/PassManager.h>
+
+//
+// PrepareForOptPass
+//
+// Pass that performs either early information collection or basic culling of
+// the input module. simplify the module, or to allow for further optimization.
+//
+
+class PrepareForOptPass : public llvm::PassInfoMixin<PrepareForOptPass> {
+public:
+ PrepareForOptPass() {}
+ llvm::PreservedAnalyses run(llvm::Module &M,
+ llvm::ModuleAnalysisManager &MAM);
+};
@@ -43,6 +43,8 @@ endif
sources = [
'pipeline/Pipeline.cpp',
'passes/llvm-compat.cpp',
+ 'pipeline/Pipeline.cpp',
+ 'passes/PrepareForOptPass/PrepareForOptPass.cpp',
]
clang = bindir / 'clang'
new file mode 100644
@@ -0,0 +1,25 @@
+//
+// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, see <http://www.gnu.org/licenses/>.
+//
+
+#include <PrepareForOptPass.h>
+
+using namespace llvm;
+
+PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager &MAM)
+{
+ return PreservedAnalyses::none();
+}
@@ -34,7 +34,9 @@
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Target/TargetMachine.h>
+#include <llvm/Transforms/Scalar/SROA.h>
+#include <PrepareForOptPass.h>
#include "llvm-compat.h"
using namespace llvm;
@@ -155,5 +157,30 @@ int main(int argc, char **argv)
ModulePassManager MPM;
+ //
+ // Start by Filtering out functions we don't want to translate,
+ // following by a pass that removes `noinline`s that are inserted
+ // by clang on -O0. We finally run a UnifyExitNodesPass to make sure
+ // the helpers we parse only has a single exit.
+ //
+
+ {
+ FunctionPassManager FPM;
+#if LLVM_VERSION_MAJOR < 14
+ FPM.addPass(SROA());
+#else
+ FPM.addPass(SROAPass());
+#endif
+ MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
+ }
+
+ MPM.addPass(PrepareForOptPass());
+
+ {
+ FunctionPassManager FPM;
+ FPM.addPass(compat::UnifyFunctionExitNodesPass());
+ MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
+ }
+
return 0;
}
Adds a new LLVM pass that runs early in the pipeline with the goal of preparing the input module for optimization by doing some early culling of functions and information gathering. This commits sets up a new LLVM pass over the IR module and runs it from the pipeline. Signed-off-by: Anton Johansson <anjo@rev.ng> --- .../helper-to-tcg/include/CmdLineOptions.h | 2 ++ .../helper-to-tcg/include/PrepareForOptPass.h | 34 +++++++++++++++++++ subprojects/helper-to-tcg/meson.build | 2 ++ .../PrepareForOptPass/PrepareForOptPass.cpp | 25 ++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 27 +++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 subprojects/helper-to-tcg/include/PrepareForOptPass.h create mode 100644 subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.cpp