Skip to content

Commit

Permalink
LLVM plugin: add Tau_init and Tau_set_node at the beginning of the ma…
Browse files Browse the repository at this point in the history
…in function

Former-commit-id: 6efd540d2c7bc117f02a8f046a1739c484de972f
  • Loading branch information
coti committed Mar 5, 2022
1 parent 9753323 commit baa77dd
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions plugins/llvm/src/Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/IR/InstIterator.h"

#include "llvm/ADT/Triple.h"

#include <llvm/IR/DebugLoc.h>
#include <llvm/IR/DebugInfoMetadata.h>
#include <clang/Basic/SourceManager.h>
Expand Down Expand Up @@ -124,6 +122,9 @@ TauDryRun("tau-dry-run",



auto TauInitFunc = "Tau_init"; // arguments to pass: argc, argv
auto TauSetNodeFunc = "Tau_set_node"; // argument to pass: 0

// Demangling technique borrowed/modified from
// https://github.com/eklitzke/demangle/blob/master/src/demangle.cc
static StringRef normalize_name(StringRef mangled_name) {
Expand Down Expand Up @@ -226,7 +227,6 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu
#else
Instrument() : PassInfoMixin<Instrument>() {
#endif
errs() <<"TauInputFile: "<<TauInputFile<<"\n";
if(!TauInputFile.empty()) {
std::ifstream ifile{TauInputFile};
if( !ifile ){
Expand Down Expand Up @@ -489,22 +489,13 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu
* \param calls Vector to add to, if the CallInst should be profiled
*/
bool maybeSaveForProfiling( Function& call ){
StringRef callName = call.getName();
StringRef callName = call.getName();
std::string filename = getFilename( call );
StringRef prettycallName = normalize_name(callName);
auto *module = call.getParent();
const std::string triple = module->getTargetTriple();
bool is_host_func = triple.compare(std::string("amdgcn-amd-amdhsa")); // returns 0 if it matches
// Compare similarly for other GPUs. If it matches, do not instrument it.


/* This big test was explanded for readability */
bool instrumentHere = false;

if (is_host_func == false) {
errs() << "Name " << prettycallName << " GPU bound, instrument = "<<is_host_func<<"\n";
return false;
}
//errs() << "Name " << prettycallName << " full " << callName << "\n";

if( prettycallName == "" ) return false;

Expand Down Expand Up @@ -575,7 +566,6 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu
// Declare and get handles to the runtime profiling functions
auto &context = func.getContext();
auto *module = func.getParent();

StringRef prettyname = normalize_name(func.getName());
#if( LLVM_VERSION_MAJOR <= 8 )
Constant
Expand All @@ -588,7 +578,38 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu
#endif // LLVM_VERSION_MAJOR <= 8

errs() << "Adding instrumentation in " << prettyname << '\n';
bool mutated = false; // TODO

/* Add TAU init in main */

if( 0 == prettyname.compare( "main" ) ){
errs() << "\tmain function: adding init\n";
auto initfun = getVoidFunc( TauInitFunc, context, module );
auto setnodefun = getVoidFunc( TauSetNodeFunc, context, module );

auto beg = inst_begin( &func );
Instruction* b = &*beg;
IRBuilder<> b4( b );

/* TauInitFunc takes two arguments: argc and argv */

SmallVector<Value *, 2> mainArgsVect;
for( Argument &arg : func.args() ){
mainArgsVect.push_back( &arg );
}
b4.CreateCall( initfun, mainArgsVect );

/* TauSetNodeFunc takes one argument: 0 */

Value* z = ConstantInt::get( context, llvm::APInt( 32, 0, false ) );
SmallVector<Value *, 1> zero{ z };
b4.CreateCall( setnodefun, zero );

mutated = true;
}

/* Add regular TAU calls */

std::string filename = getFilename( func );
std::string location( "[{" + getFilename( func ) + "} {" + getLineAndCol( func ) + "}]" );

Expand All @@ -597,8 +618,6 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu
Instruction* i = &*pi;
IRBuilder<> before( i );

bool mutated = false; // TODO

// This is the recommended way of creating a string constant (to be used
// as an argument to runtime functions)
Value *strArg = before.CreateGlobalStringPtr( ( prettyname + " " + location ).str() );
Expand Down Expand Up @@ -626,7 +645,7 @@ static FunctionCallee getVoidFunc(StringRef funcname, LLVMContext &context, Modu

char Instrument::ID = 0;

static RegisterPass<Instrument> X("TAU", "TAU Profiling", false, false);
static RegisterPass<Instrument> X("tau-prof", "TAU Profiling", false, false);

// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
Expand All @@ -642,16 +661,14 @@ RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible, registerInstrumentPass);
class PluginInstrument : public clang::PluginASTAction {
protected:
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &CI, StringRef file) {
errs() <<"INSIDE PluginInstrument::CreateASTConsumer\n"; // VERBOSE
return std::make_unique<Instrument>();
}

bool ParseArgs(const clang::CompilerInstance &CI, const std::vector<std::string> &args) {
errs() <<"INSIDE PluginInstrument::ParseArgs "<<args[0] <<"\n"; // VERBOSE
return true;
}
};

static clang::FrontendPluginRegistry::Add<PluginInstrument> X("TAU", "TAU profiling");
static clang::FrontendPluginRegistry::Add<PluginInstrument> X("tau-prof", "TAU profiling");

#endif

0 comments on commit baa77dd

Please sign in to comment.