Skip to content

[ModuleSplitter] llvm module splitter #121543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9bc7b85
Add files.
weiweichen Dec 1, 2024
220c578
Add ModuleSplitter.h
weiweichen Dec 1, 2024
30c4c1d
checkpoint.
weiweichen Dec 1, 2024
cc3f649
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Jan 3, 2025
b84346d
checkpoint.
weiweichen Jan 6, 2025
517d8e8
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 14, 2025
a43cf9a
Move source code around.
weiweichen Mar 14, 2025
c7d9d9e
update source.
weiweichen Mar 14, 2025
a3075f2
Add llvm-module-splitter skeleton and bazel integration.
weiweichen Mar 14, 2025
fb12509
Moving source file around.
weiweichen Mar 14, 2025
a8d9173
compiles.
weiweichen Mar 14, 2025
76c9cdf
Update cmake.
weiweichen Mar 14, 2025
229c53b
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 14, 2025
be51194
Fix CMake.
weiweichen Mar 17, 2025
b884f64
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 17, 2025
4dd4aa8
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 18, 2025
ede15db
test works.
weiweichen Mar 19, 2025
8ea9ca8
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 19, 2025
c1b30e4
Add first lit test.
weiweichen Mar 19, 2025
3fa9e2d
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 19, 2025
ddf46e4
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 23, 2025
4ff4290
Add return.
weiweichen Mar 25, 2025
7efa79d
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 25, 2025
30d255d
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 25, 2025
892057b
minor fix.
weiweichen Mar 25, 2025
fce31b5
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 29, 2025
cc3f908
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Apr 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions llvm/include/llvm/ModuleSplitter/ModuleSplitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//===- ModuleSplitter.h - Module Splitter Functions -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
///
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_MODULESPLITTER_MODULESPLITTER_H
#define LLVM_MODULESPLITTER_MODULESPLITTER_H

#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/Error.h"
namespace llvm {

//===----------------------------------------------------------------------===//
// LLVMModuleAndContext
//===----------------------------------------------------------------------===//

/// A pair of an LLVM module and the LLVM context that holds ownership of the
/// objects. This is a useful class for parallelizing LLVM and managing
/// ownership of LLVM instances.
class LLVMModuleAndContext {
public:
/// Expose the underlying LLVM context to create the module. This is the only
/// way to access the LLVM context to prevent accidental sharing.
Expected<bool> create(
function_ref<Expected<std::unique_ptr<llvm::Module>>(llvm::LLVMContext &)>
CreateModule);

llvm::Module &operator*() { return *Module; }
llvm::Module *operator->() { return Module.get(); }

void reset();

private:
/// LLVM context stored in a unique pointer so that we can move this type.
std::unique_ptr<llvm::LLVMContext> Ctx =
std::make_unique<llvm::LLVMContext>();
/// The paired LLVM module.
std::unique_ptr<llvm::Module> Module;
};

//===----------------------------------------------------------------------===//
// Module Splitter
//===----------------------------------------------------------------------===//

using LLVMSplitProcessFn =
function_ref<void(llvm::unique_function<LLVMModuleAndContext()>,
std::optional<int64_t>, unsigned)>;

/// Helper to create a lambda that just forwards a preexisting Module.
inline llvm::unique_function<LLVMModuleAndContext()>
forwardModule(LLVMModuleAndContext &&Module) {
return [Module = std::move(Module)]() mutable { return std::move(Module); };
}

/// Support for splitting an LLVM module into multiple parts using anchored
/// functions (e.g. exported functions), and pull in all dependency on the
// call stack into one module.
void splitPerAnchored(LLVMModuleAndContext Module, LLVMSplitProcessFn ProcessFn,
llvm::SmallVectorImpl<llvm::Function> &Anchors);

/// Support for splitting an LLVM module into multiple parts with each part
/// contains only one function.
void splitPerFunction(
LLVMModuleAndContext Module, LLVMSplitProcessFn ProcessFn,
llvm::StringMap<llvm::GlobalValue::LinkageTypes> &SymbolLinkageTypes,
unsigned NumFunctionBase);

} // namespace llvm

#endif
1 change: 1 addition & 0 deletions llvm/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ add_subdirectory(Analysis)
add_subdirectory(LTO)
add_subdirectory(MC)
add_subdirectory(MCA)
add_subdirectory(ModuleSplitter)
add_subdirectory(ObjCopy)
add_subdirectory(Object)
add_subdirectory(ObjectYAML)
Expand Down
14 changes: 14 additions & 0 deletions llvm/lib/ModuleSplitter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
add_llvm_component_library(LLVMModuleSplitter
ModuleSplitter.cpp

ADDITIONAL_HEADER_DIRS
${LLVM_MAIN_INCLUDE_DIR}/llvm/ModuleSplitter

LINK_COMPONENTS
Core
IRReader
BitReader
BitWriter
Support
TransformUtils
)
Loading
Loading