[PATCH v2 12/50] helper-to-tcg: Introduce PrepareForOptPass
Anton Johansson via qemu development <[email protected]>
| Newsgroups | gmane.comp.emulators.qemu |
|---|---|
| Message-ID | <[email protected]> |
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 <[email protected]> --- .../include/PrepareForOptPass.hpp | 34 +++++++++++++++++++ subprojects/helper-to-tcg/meson.build | 1 + subprojects/helper-to-tcg/src/Pipeline.cpp | 27 +++++++++++++++ .../PrepareForOptPass/PrepareForOptPass.cpp | 29 ++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 subprojects/helper-to-tcg/include/PrepareForOptPass.hpp create mode 100644 subprojects/helper-to-tcg/src/PrepareForOptPass/PrepareForOptPass.cpp diff --git a/subprojects/helper-to-tcg/include/PrepareForOptPass.hpp b/subprojects/helper-to-tcg/include/PrepareForOptPass.hpp new file mode 100644 index 0000000000..2b3694c536 --- /dev/null +++ b/subprojects/helper-to-tcg/include/PrepareForOptPass.hpp @@ -0,0 +1,34 @@ +// +// Copyright(c) 2026 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); +}; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-tcg/meson.build index ca46578cb3..4fd0ddb778 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -42,6 +42,7 @@ endif sources = [ 'src/LlvmCompat.cpp', 'src/Pipeline.cpp', + 'src/PrepareForOptPass/PrepareForOptPass.cpp', ] clang = bindir / 'clang' diff --git a/subprojects/helper-to-tcg/src/Pipeline.cpp b/subprojects/helper-to-tcg/src/Pipeline.cpp index 4b9523ad6f..59de572bf6 100644 --- a/subprojects/helper-to-tcg/src/Pipeline.cpp +++ b/subprojects/helper-to-tcg/src/Pipeline.cpp @@ -17,6 +17,7 @@ #include "CmdLineOptions.hpp" #include "LlvmCompat.hpp" +#include "PrepareForOptPass.hpp" #if LLVM_VERSION_MAJOR == 15 #include <llvm/ADT/Triple.h> @@ -42,6 +43,7 @@ #include <llvm/Support/SourceMgr.h> #include <llvm/Support/TargetSelect.h> #include <llvm/Target/TargetMachine.h> +#include <llvm/Transforms/Scalar/SROA.h> #define DEBUG_TYPE "pipeline" @@ -165,5 +167,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 >= 16 + FPM.addPass(SROAPass(SROAOptions::ModifyCFG)); +#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; } diff --git a/subprojects/helper-to-tcg/src/PrepareForOptPass/PrepareForOptPass.cpp b/subprojects/helper-to-tcg/src/PrepareForOptPass/PrepareForOptPass.cpp new file mode 100644 index 0000000000..4a7e82b7bd --- /dev/null +++ b/subprojects/helper-to-tcg/src/PrepareForOptPass/PrepareForOptPass.cpp @@ -0,0 +1,29 @@ +// +// Copyright(c) 2026 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" + +#include <llvm/Support/Debug.h> + +#define DEBUG_TYPE "prepare-for-opt" + +using namespace llvm; + +PreservedAnalyses PrepareForOptPass::run(Module &M, + ModuleAnalysisManager &MAM) { + return PreservedAnalyses::none(); +} -- 2.52.0