Skip to content

Commit

Permalink
[feat] Add basic address datagraph extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
seanzw committed Sep 22, 2018
1 parent 796f4ee commit c602b98
Show file tree
Hide file tree
Showing 16 changed files with 609 additions and 100 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ build/*
*.s
*.pyc
*.o
*.cache
*.data
*.exe
*.txt
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_library(LLVMTDGPass MODULE
stream/MemStream.cpp
stream/StreamPass.cpp
stream/StreamPrefetchPass.cpp
stream/ae/AddressDataGraph.cpp
# CCA.cpp
# StreamAnalyzeTrace.cpp
)
Expand Down
8 changes: 8 additions & 0 deletions src/LoopUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ std::string LoopUtils::formatLLVMInst(const llvm::Instruction *Inst) {
}
}

std::string LoopUtils::formatLLVMValue(const llvm::Value *Value) {
if (auto Inst = llvm::dyn_cast<llvm::Instruction>(Value)) {
return LoopUtils::formatLLVMInst(Inst);
} else {
return Value->getName();
}
}

void LoopIterCounter::configure(llvm::Loop *_Loop) {
this->Loop = _Loop;
this->Iter = -1;
Expand Down
1 change: 1 addition & 0 deletions src/LoopUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class LoopUtils {
* Print an static instruction.
*/
static std::string formatLLVMInst(const llvm::Instruction *Inst);
static std::string formatLLVMValue(const llvm::Value *Value);

static const std::unordered_set<std::string> SupportedMathFunctions;
};
Expand Down
11 changes: 11 additions & 0 deletions src/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ class Utils {
return llvm::isa<llvm::StoreInst>(Inst) || llvm::isa<llvm::LoadInst>(Inst);
}

static const llvm::Value *getMemAddrValue(const llvm::Instruction *Inst) {
assert(
Utils::isMemAccessInst(Inst) &&
"This is not a memory access instruction to get memory address value.");
if (llvm::isa<llvm::StoreInst>(Inst)) {
return Inst->getOperand(1);
} else {
return Inst->getOperand(0);
}
}

static uint64_t getMemAddr(const DynamicInstruction *DynamicInst) {
auto StaticInst = DynamicInst->getStaticInstruction();
assert(Utils::isMemAccessInst(StaticInst) &&
Expand Down
4 changes: 3 additions & 1 deletion src/stream/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ add_library(ProtobufStreamMessage OBJECT
)
set_target_properties(ProtobufStreamMessage PROPERTIES
COMPILE_FLAGS "-std=c++14 -O3 -fPIC -fno-rtti -DGOOGLE_PROTOBUF_NO_RTTI"
)
)

add_subdirectory(ae)
7 changes: 3 additions & 4 deletions src/stream/InductionVarStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class InductionVarStream : public Stream {
public:
InductionVarStream(
const std::string &_Folder, const llvm::PHINode *_PHIInst,
const llvm::Loop *_Loop, size_t _Level,
const llvm::Loop *_Loop, const llvm::Loop *_InnerMostLoop, size_t _Level,
std::unordered_set<const llvm::Instruction *> &&_ComputeInsts)
: Stream(TypeT::IV, _Folder, _PHIInst, _Loop, _Level), PHIInst(_PHIInst),
ComputeInsts(std::move(_ComputeInsts)) {}
: Stream(TypeT::IV, _Folder, _PHIInst, _Loop, _InnerMostLoop, _Level),
PHIInst(_PHIInst), ComputeInsts(std::move(_ComputeInsts)) {}

InductionVarStream(const InductionVarStream &Other) = delete;
InductionVarStream(InductionVarStream &&Other) = delete;
Expand Down Expand Up @@ -47,7 +47,6 @@ class InductionVarStream : public Stream {
const llvm::PHINode *PHINode,
const std::unordered_set<const llvm::Instruction *> &ComputeInsts);


std::string format() const {
std::stringstream ss;
ss << "InductionVarStream " << LoopUtils::formatLLVMInst(this->PHIInst)
Expand Down
10 changes: 9 additions & 1 deletion src/stream/MemStream.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "stream/MemStream.h"

void MemStream::searchAddressComputeInstructions(
std::function<bool(llvm::PHINode *)> IsInductionVar) {
std::function<bool(const llvm::PHINode *)> IsInductionVar) {

std::list<llvm::Instruction *> Queue;

Expand Down Expand Up @@ -58,4 +58,12 @@ void MemStream::searchAddressComputeInstructions(
}
}
}
}

void MemStream::formatAdditionalInfoText(std::ostream &OStream) const {
this->AddrDG.format(OStream);
}

void MemStream::generateComputeFunction(llvm::Module *Module) const {
this->AddrDG.generateComputeFunction(this->AddressFunctionName, Module);
}
28 changes: 24 additions & 4 deletions src/stream/MemStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@

#include "MemoryFootprint.h"
#include "stream/Stream.h"
#include "stream/ae/AddressDataGraph.h"

class MemStream : public Stream {
public:
MemStream(const std::string &_Folder, const llvm::Instruction *_Inst,
const llvm::Loop *_Loop, size_t _LoopLevel,
std::function<bool(llvm::PHINode *)> IsInductionVar)
: Stream(TypeT::MEM, _Folder, _Inst, _Loop, _LoopLevel) {
const llvm::Loop *_Loop, const llvm::Loop *_InnerMostLoop,
size_t _LoopLevel,
std::function<bool(const llvm::PHINode *)> IsInductionVar)
: Stream(TypeT::MEM, _Folder, _Inst, _Loop, _InnerMostLoop, _LoopLevel),
AddrDG(_InnerMostLoop, Utils::getMemAddrValue(_Inst), IsInductionVar) {
assert(Utils::isMemAccessInst(this->Inst) &&
"Should be load/store instruction to build a stream.");
this->AddressFunctionName =
(this->Inst->getFunction()->getName() + "_" +
this->Inst->getParent()->getName() + "_" + this->Inst->getName() +
"_" + this->Inst->getOpcodeName())
.str();
this->searchAddressComputeInstructions(IsInductionVar);
}

Expand Down Expand Up @@ -61,14 +69,26 @@ class MemStream : public Stream {
return false;
}

std::string getAddressFunctionName() const {
return this->AddressFunctionName;
}
void generateComputeFunction(llvm::Module *Module) const;

protected:
void formatAdditionalInfoText(std::ostream &OStream) const override;

private:
MemoryFootprint Footprint;
std::unordered_set<llvm::LoadInst *> BaseLoads;
std::unordered_set<llvm::PHINode *> BaseInductionVars;
std::unordered_set<const llvm::Instruction *> AddrInsts;
std::unordered_set<llvm::Instruction *> AliasInsts;

std::string AddressFunctionName;

AddressDataGraph AddrDG;

void searchAddressComputeInstructions(
std::function<bool(llvm::PHINode *)> IsInductionVar);
std::function<bool(const llvm::PHINode *)> IsInductionVar);
};
#endif
12 changes: 8 additions & 4 deletions src/stream/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

Stream::Stream(TypeT _Type, const std::string &_Folder,
const llvm::Instruction *_Inst, const llvm::Loop *_Loop,
size_t _LoopLevel)
const llvm::Loop *_InnerMostLoop, size_t _LoopLevel)
: Type(_Type), Folder(_Folder), Inst(_Inst), Loop(_Loop),
LoopLevel(_LoopLevel), Qualified(false), Chosen(false), TotalIters(0),
TotalAccesses(0), TotalStreams(0), Iters(1), LastAccessIters(0),
StartId(DynamicInstruction::InvalidId), Pattern() {
InnerMostLoop(_InnerMostLoop), LoopLevel(_LoopLevel), Qualified(false),
Chosen(false), TotalIters(0), TotalAccesses(0), TotalStreams(0), Iters(1),
LastAccessIters(0), StartId(DynamicInstruction::InvalidId), Pattern() {
this->PatternFullPath = this->Folder + "/" + this->formatName() + ".pattern";
this->PatternTextFullPath = this->PatternFullPath + ".txt";
this->InfoFullPath = this->Folder + "/" + this->formatName() + ".info";
Expand Down Expand Up @@ -98,6 +98,10 @@ void Stream::finalize(llvm::DataLayout *DataLayout) {
<< AllChosenBaseStream->formatName() << '\n';
}
InfoTextFStream << "------------------------------\n";
/**
* Formatting any additional text information for the subclass.
*/
this->formatAdditionalInfoText(InfoTextFStream);
InfoTextFStream.close();

// Also serialize with protobuf.
Expand Down
9 changes: 8 additions & 1 deletion src/stream/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Stream {
const TypeT Type;
Stream(TypeT _Type, const std::string &_Folder,
const llvm::Instruction *_Inst, const llvm::Loop *_Loop,
size_t _LoopLevel);
const llvm::Loop *_InnerMostLoop, size_t _LoopLevel);

bool hasNoBaseStream() const { return this->BaseStreams.empty(); }
const std::unordered_set<Stream *> &getBaseStreams() const {
Expand All @@ -45,6 +45,7 @@ class Stream {
void markChosen() { this->Chosen = true; }
bool isChosen() const { return this->Chosen; }
const llvm::Loop *getLoop() const { return this->Loop; }
const llvm::Loop *getInnerMostLoop() const { return this->InnerMostLoop; }
const llvm::Instruction *getInst() const { return this->Inst; }
uint64_t getStreamId() const { return reinterpret_cast<uint64_t>(this); }
const std::string &getPatternPath() const { return this->PatternFullPath; }
Expand Down Expand Up @@ -140,6 +141,7 @@ class Stream {
*/
const llvm::Instruction *Inst;
const llvm::Loop *Loop;
const llvm::Loop *InnerMostLoop;
const size_t LoopLevel;

std::unordered_set<Stream *> BaseStreams;
Expand Down Expand Up @@ -178,6 +180,11 @@ class Stream {
StreamPattern Pattern;

int getElementSize(llvm::DataLayout *DataLayout) const;

/**
* Used for subclass to add additionl dumping information.
*/
virtual void formatAdditionalInfoText(std::ostream &OStream) const {}
};

#endif
Loading

0 comments on commit c602b98

Please sign in to comment.