Skip to content

Commit

Permalink
Fix several issues due to uncommitted changes previously. Can identif…
Browse files Browse the repository at this point in the history
…y the problem, but still debugging
  • Loading branch information
tpatki committed Sep 26, 2024
1 parent ebb9067 commit e7dc6df
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 190 deletions.
2 changes: 2 additions & 0 deletions src/c/weaver/weave/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ if (PERFFLOWASPECT_CLANG_14_NEWER)
# Add the new pass
add_library(WeavePass MODULE
# List your source files here.
perfflow_weave_common.cpp
perfflow_weave_new_pass.cpp
)
else()
# Add the legacy pass if we have Clang < 14.
add_library(WeavePass MODULE
# List your source files here.
perfflow_weave_common.cpp
perfflow_weave_legacy_pass.cpp
)
endif()
Expand Down
38 changes: 23 additions & 15 deletions src/c/weaver/weave/perfflow_weave_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@

#include "perfflow_weave_common.hpp"

using namespace llvm;

/******************************************************************************
* *
* Public Method of WeaveCommon Class *
* *
******************************************************************************/

bool WeaveCommon::modifyAnnotatedFunctions(Module &m)
bool weave_ns::WeaveCommon::modifyAnnotatedFunctions(Module &m)
{
outs() << "I am in modifyAnnotatedFunctions \n";
outs() << "I am in modifyAnnotatedFunctions \n";

auto annotations = m.getNamedGlobal("llvm.global.annotations");
if (!annotations)
Expand All @@ -27,29 +29,35 @@ bool WeaveCommon::modifyAnnotatedFunctions(Module &m)
}

bool changed = false;
if (annotations.getNumOperands() > 0)

if (annotations->getNumOperands() <= 0)
{
outs() << "I have %d operands!" << annotations.getNumOperands() << "\n";
auto a = cast<ConstantArray> (annotations->getOperand(0));
outs() << "I have failed as there are no operands!\n";
return changed;
}
else
{
outs() << "I have failed as there are no operands!\n";
return changed;
outs() << "I have " << annotations->getNumOperands() << " operands.\n";
}

auto a = cast<ConstantArray> (annotations->getOperand(0));
for (unsigned int i = 0; i < a->getNumOperands(); i++)
{
auto e = cast<ConstantStruct> (a->getOperand(i));
if (!e)
{
outs() << "I failed here at obtaining the struct. \n";
outs() << "I failed here at obtaining the struct. \n";
}
else
{
outs() << "e has " << e->getNumOperands() << " operands.\n";
outs() << "e's first operand has " << e->getOperand(0)->getNumOperands() <<
" operands.\n";
}

if (auto *fn = dyn_cast<Function> (e->getOperand(0)->getOperand(0)))
{
outs() << "I entered the part where we parse annotations. \n";
outs() << "I entered the part where we parse annotations. \n";
auto anno = cast<ConstantDataArray>(
cast<GlobalVariable>(e->getOperand(1)->getOperand(0))
->getOperand(0))
Expand Down Expand Up @@ -96,7 +104,7 @@ bool WeaveCommon::modifyAnnotatedFunctions(Module &m)
}
}
}
return changed;
return changed;

}

Expand All @@ -106,8 +114,8 @@ bool WeaveCommon::modifyAnnotatedFunctions(Module &m)
* *
******************************************************************************/

bool WeaveCommon::insertAfter(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
bool weave_ns::WeaveCommon::insertAfter(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
{
if (m.empty() || f.empty())
{
Expand Down Expand Up @@ -159,8 +167,8 @@ bool WeaveCommon::insertAfter(Module &m, Function &f, StringRef &a,
return true;
}

bool WeaveCommon::insertBefore(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
bool weave_ns::WeaveCommon::insertBefore(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
{
if (m.empty() || f.empty())
{
Expand Down
178 changes: 3 additions & 175 deletions src/c/weaver/weave/perfflow_weave_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@

#include "../../parser/perfflow_parser.hpp"

namespace {
using namespace llvm;

namespace weave_ns {

class WeaveCommon
{
Expand All @@ -45,180 +47,6 @@ class WeaveCommon

};

/*
Public Member definition.
Need to clean this up so it reads better, and doesn't look like C++ jargon with random casts and is explainable.
*/

bool WeaveCommon::modifyAnnotatedFunctions(Module &m)
{
auto annotations = m.getNamedGlobal("llvm.global.annotations");
if (!annotations)
{
return false;
}

bool changed = false;
auto a = cast<ConstantArray> (annotations->getOperand(0));
for (unsigned int i = 0; i < a->getNumOperands(); i++)
{
auto e = cast<ConstantStruct> (a->getOperand(i));
if (auto *fn = dyn_cast<Function> (e->getOperand(0)->getOperand(0)))
{
auto anno = cast<ConstantDataArray>(
cast<GlobalVariable>(e->getOperand(1)->getOperand(0))
->getOperand(0))
->getAsCString();
std::string pcut, scope, flow;
if (perfflow_parser_parse(anno.data(), pcut, scope, flow) == 0)
{
if (pcut == "around" || pcut == "before")
changed = insertBefore(m, *fn,
anno, 0, scope, flow, pcut) || changed;
else if (pcut == "around_async" || pcut == "before_async")
{
changed = insertBefore(m, *fn,
anno, 1, scope, flow, pcut) || changed;
}
if (pcut == "around" || pcut == "after")
{
if (pcut == "around")
{
if (flow == "in" || flow == "out")
{
flow = "NA";
}
}
changed = insertAfter(m, *fn,
anno, 0, scope, flow, pcut) || changed;
}
else if (pcut == "around_async" || pcut == "after_async")
{
if (pcut == "around")
{
if (flow == "in" || flow == "out")
{
flow = "NA";
}
}
changed = insertAfter(m, *fn,
anno, 1, scope, flow, pcut) || changed;
}
}
else
{
errs() << "WeavePass[WARN]: Ignoring " << anno << "\n";
}
}
}
return changed;

}

/*
Private Member Function Definitions
*/

bool WeaveCommon::insertAfter(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
{
if (m.empty() || f.empty())
{
return false;
}

auto &context = m.getContext();
Type *voidType = Type::getVoidTy(context);
Type *int32Type = Type::getInt32Ty(context);
Type *int8PtrType = Type::getInt8PtrTy(context);
std::vector<llvm::Type *> params;
params.push_back(int32Type);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);

// voidType is return type, params are parameters and no variable length args
FunctionType *weaveFuncTy = FunctionType::get(voidType, params, false);
// Note: Use FunctionCallee after for Clang 9 or higher
FunctionCallee after = m.getOrInsertFunction("perfflow_weave_after",
weaveFuncTy);
// Constant *after = m.getOrInsertFunction("perfflow_weave_after",
// weaveFuncTy);

// iterate through blocks
for (BasicBlock &bb : f)
{
Instruction *inst = bb.getTerminator();
if (isa<ReturnInst>(inst) || isa<ResumeInst>(inst))
{
IRBuilder<> builder(inst);
Value *v1 = builder.CreateGlobalStringPtr(m.getName(), "str");
Value *v2 = builder.CreateGlobalStringPtr(f.getName(), "str");
Value *v3 = builder.CreateGlobalStringPtr(StringRef(scope), "str");
Value *v4 = builder.CreateGlobalStringPtr(StringRef(flow), "str");
Value *v5 = builder.CreateGlobalStringPtr(StringRef(pcut), "str");
std::vector<Value *> args;
args.push_back(ConstantInt::get(Type::getInt32Ty(context), async));
args.push_back(v1);
args.push_back(v2);
args.push_back(v3);
args.push_back(v4);
args.push_back(v5);
builder.CreateCall(after, args);
}
}
return true;
}

bool WeaveCommon::insertBefore(Module &m, Function &f, StringRef &a,
int async, std::string &scope, std::string &flow, std::string pcut)
{
if (m.empty() || f.empty())
{
return false;
}

auto &context = m.getContext();
Type *voidType = Type::getVoidTy(context);
Type *int32Type = Type::getInt32Ty(context);
Type *int8PtrType = Type::getInt8PtrTy(context);
std::vector<llvm::Type *> params;
params.push_back(int32Type);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);
params.push_back(int8PtrType);

// voidType is return type, params are parameters and no variable length args
FunctionType *weaveFuncTy = FunctionType::get(voidType, params, false);
// Note: User FunctionCallee before for Clang >= 9.0
FunctionCallee before = m.getOrInsertFunction("perfflow_weave_before",
weaveFuncTy);
//Constant *before = m.getOrInsertFunction ("perfflow_weave_before",
// weaveFuncTy);
auto &entry = f.getEntryBlock();
IRBuilder<> builder(&entry);
Value *v1 = builder.CreateGlobalStringPtr(m.getName(), "str");
Value *v2 = builder.CreateGlobalStringPtr(f.getName(), "str");
Value *v3 = builder.CreateGlobalStringPtr(StringRef(scope), "str");
Value *v4 = builder.CreateGlobalStringPtr(StringRef(flow), "str");
Value *v5 = builder.CreateGlobalStringPtr(StringRef(pcut), "str");
builder.SetInsertPoint(&entry, entry.begin());
std::vector<Value *> args;
args.push_back(ConstantInt::get(Type::getInt32Ty(context), async));
args.push_back(v1);
args.push_back(v2);
args.push_back(v3);
args.push_back(v4);
args.push_back(v5);
builder.CreateCall(before, args);

return true;
}

} // End namespace

#endif // PERFFLOW_WEAVE_COMMON_H
Expand Down
1 change: 1 addition & 0 deletions src/c/weaver/weave/perfflow_weave_legacy_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "perfflow_weave_common.hpp"

using namespace llvm;
using namespace weave_ns;

/******************************************************************************
* *
Expand Down
1 change: 1 addition & 0 deletions src/c/weaver/weave/perfflow_weave_new_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <cstring>

using namespace llvm;
using namespace weave_ns;

/******************************************************************************
* *
Expand Down

0 comments on commit e7dc6df

Please sign in to comment.