forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbc1aca
commit bdb2a25
Showing
7 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef LLVM_TRANSFORMS_YK_MODULE_CLONE_H | ||
#define LLVM_TRANSFORMS_YK_MODULE_CLONE_H | ||
|
||
#include "llvm/Pass.h" | ||
|
||
namespace llvm { | ||
ModulePass *createYkModuleClonePass(); | ||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "llvm/Transforms/Yk/ModuleClone.h" | ||
#include "llvm/IR/BasicBlock.h" | ||
#include "llvm/IR/Function.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/IR/LLVMContext.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/InitializePasses.h" | ||
#include "llvm/Linker/Linker.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm/Transforms/Utils/Cloning.h" | ||
|
||
#define DEBUG_TYPE "yk-module-clone-pass" | ||
|
||
using namespace llvm; | ||
|
||
namespace llvm { | ||
void initializeYkModuleClonePass(PassRegistry &); | ||
} // namespace llvm | ||
|
||
namespace { | ||
struct YkModuleClone : public ModulePass { | ||
static char ID; | ||
const std::string ClonePrefix = "__yk_clone_"; | ||
|
||
YkModuleClone() : ModulePass(ID) { | ||
initializeYkModuleClonePass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
void updateClonedFunctions(Module &M) { | ||
for (llvm::Function &F : M) { | ||
if (F.hasExternalLinkage() && F.isDeclaration()) { | ||
continue; | ||
} | ||
F.setName(ClonePrefix + F.getName().str()); | ||
} | ||
} | ||
|
||
void updateClonedGlobals(Module &M) { | ||
for (llvm::GlobalVariable &GV : M.globals()) { | ||
std::string GlobalName = GV.getName().str(); | ||
if (GlobalName.find('.') == 0) { | ||
continue; // This is likely not user-defined. Example: `.L.str`. | ||
} | ||
GV.setInitializer(nullptr); // Remove the initializer | ||
GV.setLinkage(llvm::GlobalValue::ExternalLinkage); // Set external linkage | ||
} | ||
} | ||
|
||
bool runOnModule(Module &M) override { | ||
if (M.getName() == "src/perf/collect.c") { | ||
return false; | ||
} | ||
std::unique_ptr<Module> Cloned = CloneModule(M); | ||
if (!Cloned) { | ||
errs() << "Error: CloneModule returned null for module: " << M.getName() | ||
<< "\n"; | ||
return false; | ||
} | ||
updateClonedFunctions(*Cloned); | ||
updateClonedGlobals(*Cloned); | ||
|
||
llvm::Linker TheLinker(M); | ||
if (TheLinker.linkInModule(std::move(Cloned))) { | ||
llvm::report_fatal_error("Error linking the modules"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
}; | ||
} // namespace | ||
|
||
char YkModuleClone::ID = 0; | ||
|
||
INITIALIZE_PASS(YkModuleClone, DEBUG_TYPE, "yk module clone", false, false) | ||
|
||
ModulePass *llvm::createYkModuleClonePass() { return new YkModuleClone(); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ add_llvm_component_library(LLVMYkIR | |
Core | ||
MC | ||
Support | ||
TransformUtils | ||
Linker | ||
) |