From f55d761dd4c093f0b53a28c56b813847a83dfbe5 Mon Sep 17 00:00:00 2001 From: Pavel Durov Date: Fri, 1 Dec 2023 16:49:54 +0000 Subject: [PATCH 1/5] Add helloworld pass --- .../llvm/Transforms/Yk/HelloWorldPass.h | 10 ++++++++ llvm/lib/CodeGen/TargetPassConfig.cpp | 5 ++++ llvm/lib/Transforms/Yk/CMakeLists.txt | 1 + llvm/lib/Transforms/Yk/HelloWorldPass.cpp | 25 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 llvm/include/llvm/Transforms/Yk/HelloWorldPass.h create mode 100644 llvm/lib/Transforms/Yk/HelloWorldPass.cpp diff --git a/llvm/include/llvm/Transforms/Yk/HelloWorldPass.h b/llvm/include/llvm/Transforms/Yk/HelloWorldPass.h new file mode 100644 index 00000000000000..2fea0f498f43b7 --- /dev/null +++ b/llvm/include/llvm/Transforms/Yk/HelloWorldPass.h @@ -0,0 +1,10 @@ +#ifndef LLVM_TRANSFORMS_YK_HELLOWORLD_H +#define LLVM_TRANSFORMS_YK_HELLOWORLD_H + +#include "llvm/Pass.h" + +namespace llvm { +ModulePass *createHelloWorldPass(); +} // namespace llvm + +#endif diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp index 51add14928e723..aac0bf2cb8ed33 100644 --- a/llvm/lib/CodeGen/TargetPassConfig.cpp +++ b/llvm/lib/CodeGen/TargetPassConfig.cpp @@ -54,6 +54,7 @@ #include "llvm/Transforms/Yk/ShadowStack.h" #include "llvm/Transforms/Yk/Stackmaps.h" #include "llvm/Transforms/Yk/NoCallsInEntryBlocks.h" +#include "llvm/Transforms/Yk/HelloWorldPass.h" #include #include #include @@ -1166,6 +1167,10 @@ bool TargetPassConfig::addISelPasses() { addPass(createYkStackmapsPass()); } + if (true) { + addPass(createHelloWorldPass()); + } + addISelPrepare(); return addCoreISelPasses(); } diff --git a/llvm/lib/Transforms/Yk/CMakeLists.txt b/llvm/lib/Transforms/Yk/CMakeLists.txt index b0cdd734f09cd0..77c2179cae62c1 100644 --- a/llvm/lib/Transforms/Yk/CMakeLists.txt +++ b/llvm/lib/Transforms/Yk/CMakeLists.txt @@ -6,6 +6,7 @@ add_llvm_component_library(LLVMYkPasses StackMaps.cpp ShadowStack.cpp NoCallsInEntryBlocks.cpp + HelloWorldPass.cpp DEPENDS intrinsics_gen diff --git a/llvm/lib/Transforms/Yk/HelloWorldPass.cpp b/llvm/lib/Transforms/Yk/HelloWorldPass.cpp new file mode 100644 index 00000000000000..4f338a9fb6c985 --- /dev/null +++ b/llvm/lib/Transforms/Yk/HelloWorldPass.cpp @@ -0,0 +1,25 @@ +#include "llvm/Transforms/Yk/HelloWorldPass.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" + +#define DEBUG_TYPE "yk-helloworld" + +using namespace llvm; + +namespace { +class HelloWorldPass : public ModulePass { +public: + static char ID; + HelloWorldPass() : ModulePass(ID) { + + } + + bool runOnModule(Module &M) override { + return true; + } +}; +} // namespace + +char HelloWorldPass::ID = 0; + +ModulePass *llvm::createHelloWorldPass() { return new HelloWorldPass(); } From 25ad51c7c520fbb2fd60e52abc2c60f99b8da9c9 Mon Sep 17 00:00:00 2001 From: Pavel Durov Date: Fri, 1 Dec 2023 17:05:50 +0000 Subject: [PATCH 2/5] Add helloworld pass ykflag --- llvm/lib/CodeGen/TargetPassConfig.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp index aac0bf2cb8ed33..481d6795f1d2b7 100644 --- a/llvm/lib/CodeGen/TargetPassConfig.cpp +++ b/llvm/lib/CodeGen/TargetPassConfig.cpp @@ -285,8 +285,8 @@ static cl::opt cl::desc("Insert stackmaps for JIT deoptimisation")); static cl::opt - YkNoCallsInEntryBlocks("yk-no-calls-in-entryblocks", cl::init(false), cl::NotHidden, - cl::desc("Ensure there are no calls in the entryblock.")); + YkHelloWorldPass("yk-helloworld-pass", cl::init(false), cl::NotHidden, + cl::desc("YkHelloWorldPass")); /// Allow standard passes to be disabled by command line options. This supports /// simple binary flags that either suppress the pass or do nothing. @@ -1167,7 +1167,7 @@ bool TargetPassConfig::addISelPasses() { addPass(createYkStackmapsPass()); } - if (true) { + if (YkHelloWorldPass) { addPass(createHelloWorldPass()); } From ac15822379be10b95df6d562cec55c0cef49185a Mon Sep 17 00:00:00 2001 From: Pavel Durov Date: Mon, 4 Dec 2023 18:18:19 +0000 Subject: [PATCH 3/5] Rewrite as ModulePass --- .../Yk/{HelloWorldPass.h => SoftwareTracer.h} | 2 +- llvm/lib/CodeGen/TargetPassConfig.cpp | 10 ++-- llvm/lib/Transforms/Yk/CMakeLists.txt | 2 +- llvm/lib/Transforms/Yk/HelloWorldPass.cpp | 25 ---------- llvm/lib/Transforms/Yk/SoftwareTracer.cpp | 48 +++++++++++++++++++ 5 files changed, 55 insertions(+), 32 deletions(-) rename llvm/include/llvm/Transforms/Yk/{HelloWorldPass.h => SoftwareTracer.h} (78%) delete mode 100644 llvm/lib/Transforms/Yk/HelloWorldPass.cpp create mode 100644 llvm/lib/Transforms/Yk/SoftwareTracer.cpp diff --git a/llvm/include/llvm/Transforms/Yk/HelloWorldPass.h b/llvm/include/llvm/Transforms/Yk/SoftwareTracer.h similarity index 78% rename from llvm/include/llvm/Transforms/Yk/HelloWorldPass.h rename to llvm/include/llvm/Transforms/Yk/SoftwareTracer.h index 2fea0f498f43b7..408df0bfa648b4 100644 --- a/llvm/include/llvm/Transforms/Yk/HelloWorldPass.h +++ b/llvm/include/llvm/Transforms/Yk/SoftwareTracer.h @@ -4,7 +4,7 @@ #include "llvm/Pass.h" namespace llvm { -ModulePass *createHelloWorldPass(); +ModulePass *createSoftwareTracerPass(); } // namespace llvm #endif diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp index 481d6795f1d2b7..184e17a60776a7 100644 --- a/llvm/lib/CodeGen/TargetPassConfig.cpp +++ b/llvm/lib/CodeGen/TargetPassConfig.cpp @@ -54,7 +54,7 @@ #include "llvm/Transforms/Yk/ShadowStack.h" #include "llvm/Transforms/Yk/Stackmaps.h" #include "llvm/Transforms/Yk/NoCallsInEntryBlocks.h" -#include "llvm/Transforms/Yk/HelloWorldPass.h" +#include "llvm/Transforms/Yk/SoftwareTracer.h" #include #include #include @@ -285,8 +285,8 @@ static cl::opt cl::desc("Insert stackmaps for JIT deoptimisation")); static cl::opt - YkHelloWorldPass("yk-helloworld-pass", cl::init(false), cl::NotHidden, - cl::desc("YkHelloWorldPass")); + YkSoftwareTracer("yk-software-tracer", cl::init(false), cl::NotHidden, + cl::desc("Enables YK Software Tracer capability")); /// Allow standard passes to be disabled by command line options. This supports /// simple binary flags that either suppress the pass or do nothing. @@ -1167,8 +1167,8 @@ bool TargetPassConfig::addISelPasses() { addPass(createYkStackmapsPass()); } - if (YkHelloWorldPass) { - addPass(createHelloWorldPass()); + if (YkSoftwareTracer) { + addPass(createSoftwareTracerPass()); } addISelPrepare(); diff --git a/llvm/lib/Transforms/Yk/CMakeLists.txt b/llvm/lib/Transforms/Yk/CMakeLists.txt index 77c2179cae62c1..d8f1de65acc661 100644 --- a/llvm/lib/Transforms/Yk/CMakeLists.txt +++ b/llvm/lib/Transforms/Yk/CMakeLists.txt @@ -6,7 +6,7 @@ add_llvm_component_library(LLVMYkPasses StackMaps.cpp ShadowStack.cpp NoCallsInEntryBlocks.cpp - HelloWorldPass.cpp + SoftwareTracer.cpp DEPENDS intrinsics_gen diff --git a/llvm/lib/Transforms/Yk/HelloWorldPass.cpp b/llvm/lib/Transforms/Yk/HelloWorldPass.cpp deleted file mode 100644 index 4f338a9fb6c985..00000000000000 --- a/llvm/lib/Transforms/Yk/HelloWorldPass.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "llvm/Transforms/Yk/HelloWorldPass.h" -#include "llvm/IR/Module.h" -#include "llvm/Pass.h" - -#define DEBUG_TYPE "yk-helloworld" - -using namespace llvm; - -namespace { -class HelloWorldPass : public ModulePass { -public: - static char ID; - HelloWorldPass() : ModulePass(ID) { - - } - - bool runOnModule(Module &M) override { - return true; - } -}; -} // namespace - -char HelloWorldPass::ID = 0; - -ModulePass *llvm::createHelloWorldPass() { return new HelloWorldPass(); } diff --git a/llvm/lib/Transforms/Yk/SoftwareTracer.cpp b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp new file mode 100644 index 00000000000000..c4e22100c1e438 --- /dev/null +++ b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp @@ -0,0 +1,48 @@ +#include "llvm/Transforms/Yk/SoftwareTracer.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" + +#include "llvm/IR/BasicBlock.h" +#include "llvm/IR/Function.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/Module.h" +#include "llvm/Pass.h" +#include "llvm/Support/raw_ostream.h" + +#define DEBUG_TYPE "yk-software-tracer" + +using namespace llvm; + +namespace { + +struct SoftwareTracerPass : public ModulePass { + static char ID; + Function *monitor; + + SoftwareTracerPass() : ModulePass(ID) {} + + bool runOnModule(Module &M) override { + // TODO: locate tracing function + // llvm::StringRef ref = std::string("tracing_function"); + // Function *calleeFunction = M.getFunction(ref); + + for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { + for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { + for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; + ++BI) { + // TODO: Inject tracing call + } + } + } + + return true; + } +}; +} // namespace +char SoftwareTracerPass::ID = 0; + +ModulePass *llvm::createSoftwareTracerPass() { + return new SoftwareTracerPass(); +} From 3f619884b7cb9c2dd99931110eae684ee94c5727 Mon Sep 17 00:00:00 2001 From: Pavel Durov Date: Mon, 4 Dec 2023 19:34:11 +0000 Subject: [PATCH 4/5] Add messy code (WIP) --- llvm/lib/Transforms/Yk/SoftwareTracer.cpp | 25 ++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/Yk/SoftwareTracer.cpp b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp index c4e22100c1e438..15e8c8ea91bb32 100644 --- a/llvm/lib/Transforms/Yk/SoftwareTracer.cpp +++ b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp @@ -1,21 +1,21 @@ #include "llvm/Transforms/Yk/SoftwareTracer.h" #include "llvm/IR/Module.h" #include "llvm/Pass.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/Pass.h" -#include "llvm/Support/raw_ostream.h" #define DEBUG_TYPE "yk-software-tracer" using namespace llvm; namespace { +// void my_function() { printf("Hello from your_c_function!\n"); } struct SoftwareTracerPass : public ModulePass { static char ID; @@ -24,15 +24,26 @@ struct SoftwareTracerPass : public ModulePass { SoftwareTracerPass() : ModulePass(ID) {} bool runOnModule(Module &M) override { - // TODO: locate tracing function - // llvm::StringRef ref = std::string("tracing_function"); - // Function *calleeFunction = M.getFunction(ref); - for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { + llvm::StringRef ref = std::string("record_trace"); + Function *calleeFunction = + M.getFunction(ref); // Find the function by name + for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { - // TODO: Inject tracing call + if (calleeFunction) { + Argument *TempArg = F->getArg(0); // temp argument + Value *args[] = {TempArg}; // BI->getName() + + Instruction *newInst = CallInst::Create(calleeFunction, args); + + BasicBlock *BB = BI->getParent(); + // TODO: understand how to insert new instruction + // Instruction::insertInto(BB, newInst); // works with iterator + // BB->getInstList().insert(BI, newInst); // deal with getInstList + // being private errs() << "Inserted the function!\n"; + } } } } From 7865b74c5857246421bdb7a8444cd0979272476b Mon Sep 17 00:00:00 2001 From: Pavel Durov Date: Thu, 7 Dec 2023 17:16:09 +0000 Subject: [PATCH 5/5] Locating and injecting externally linked function --- llvm/lib/Transforms/Yk/SoftwareTracer.cpp | 33 +++++++++-------------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/llvm/lib/Transforms/Yk/SoftwareTracer.cpp b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp index 15e8c8ea91bb32..6080cd070e6d2a 100644 --- a/llvm/lib/Transforms/Yk/SoftwareTracer.cpp +++ b/llvm/lib/Transforms/Yk/SoftwareTracer.cpp @@ -10,44 +10,37 @@ #include "llvm/IR/Module.h" #include "llvm/Pass.h" -#define DEBUG_TYPE "yk-software-tracer" +#define DEBUG_TYPE "yk-software-tracer-pass" using namespace llvm; namespace { -// void my_function() { printf("Hello from your_c_function!\n"); } struct SoftwareTracerPass : public ModulePass { static char ID; - Function *monitor; + Function *externalFunc = NULL; SoftwareTracerPass() : ModulePass(ID) {} bool runOnModule(Module &M) override { for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { - llvm::StringRef ref = std::string("record_trace"); - Function *calleeFunction = - M.getFunction(ref); // Find the function by name - + LLVMContext &Context = M.getContext(); + if (externalFunc == NULL) { + FunctionType *FType = + FunctionType::get(Type::getVoidTy(Context), {}, false); + externalFunc = Function::Create(FType, GlobalVariable::ExternalLinkage, + YK_TRACE_FUNCTION, M); + } for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { - if (calleeFunction) { - Argument *TempArg = F->getArg(0); // temp argument - Value *args[] = {TempArg}; // BI->getName() - - Instruction *newInst = CallInst::Create(calleeFunction, args); - - BasicBlock *BB = BI->getParent(); - // TODO: understand how to insert new instruction - // Instruction::insertInto(BB, newInst); // works with iterator - // BB->getInstList().insert(BI, newInst); // deal with getInstList - // being private errs() << "Inserted the function!\n"; - } + // Insert function call instruction + IRBuilder<> builder(Context); + builder.SetInsertPoint(&(*BI)); + builder.CreateCall(externalFunc); } } } - return true; } };