Commit 16d91546 authored by Jimmy Lu's avatar Jimmy Lu
Browse files

Add For Loop Data Structure Analysis

commit 453e6079297905edef497e815c3dda460961ec57
    Track Class Fields

commit 3dd96976abb13a9bbad8e3fd376410dd62d3fa32
    Move For Loop Analysis to FindGPUMacro

commit f0fd250462abf91945a33e384d0f335bff53da79
    Keep Track of All Local Array Variables on LHS and RHS

commit acb0dfb8d5f6e39526c00a54d37b8197586c6e07
    Analyze RHS of a simpe forloop

commit fde982fe6f4d269bd7b37b2ae175788cce1423be
    Figure Out Type for Value Accessed/Mutated in Simple Loop
parent 96f6e106
#include "PluginAction.h"
//#include "SystemCClang.h"
#include "TimeAnalysis.h"
using namespace scpar;
//using namespace clang::tooling;
int main(int argc, const char **argv)
{
PluginAction<SystemCConsumer> scc(argc, argv);
PluginAction<TimeAnalysis> scc(argc, argv);
return 0;
}
#include "SCModules.h"
#include "FindModule.h"
#include "clang/AST/ASTContext.h"
using namespace scpar;
SCModules::SCModules(TranslationUnitDecl * tuDecl, llvm::raw_ostream & os):
......
......@@ -2,6 +2,7 @@
#define _SC_MODULES_H_
#include <map>
#include <string>
#include <set>
#include "clang/AST/DeclCXX.h"
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/RecursiveASTVisitor.h"
......@@ -20,11 +21,10 @@ namespace scpar {
virtual bool VisitCXXRecordDecl(CXXRecordDecl *);
moduleMapType getSystemCModulesMap();
void printSystemCModulesMap();
private:
llvm::raw_ostream & _os;
moduleMapType _moduleMap;
};
}
#endif
#include "FindGPUMacro.h"
#include "clang/AST/ASTContext.h"
using namespace scpar;
GPUMacro::GPUMacro(int blockIdx, int blockIdy, int blockIdz, int threadIdx, int threadIdy, int threadIdz, int gpuTime, int cpuTime) :
......@@ -102,6 +103,9 @@ FindGPUMacro::~FindGPUMacro()
bool FindGPUMacro::VisitForStmt(ForStmt *fstmt) {
Stmt *body = fstmt->getBody();
analyze_data_struct(body);
int tx = 1, ty = 1, tz = 1 , bx = 1, by = 1, bz = 1, gpu_time = 0, cpu_time = 0, instanceNum = 0;
for (Stmt::child_iterator it = body->child_begin(), eit = body->child_end();
it != eit;
......@@ -194,3 +198,172 @@ void FindGPUMacro::dump(){
it->second->dump(_os);
}
}
void FindGPUMacro::analyze_value_decl(ValueDecl *val) {
QualType type = val->getType();
std::pair<uint64_t, unsigned> fieldInfo =
val->getASTContext().getTypeInfo(val->getType());
uint64_t typeSize = fieldInfo.first;
unsigned fieldAlign = fieldInfo.second;
_os << "base type: " << type.getCanonicalType().getAsString()
<< ", size (bits): " << typeSize
<< ", align (bits): " << fieldAlign
<< "\n";
}
void FindGPUMacro::analyze_array_base(Expr *base, bool isLHS) {
if (CastExpr *cast = dyn_cast<CastExpr>(base)) {
Expr *subExpr = cast->getSubExpr();
if (DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(subExpr)) {
if (isLHS) {
lhs_decls.insert(declRef->getDecl());
_os << "LHS: ";
}
else {
rhs_decls.insert(declRef->getDecl());
_os << "RHS: ";
}
analyze_value_decl(declRef->getDecl());
}
else if (MemberExpr *member = dyn_cast<MemberExpr>(subExpr)) {
if (isLHS) {
lhs_decls.insert(member->getMemberDecl());
_os << "LHS: ";
}
else {
rhs_decls.insert(member->getMemberDecl());
_os << "RHS: ";
}
analyze_value_decl(member->getMemberDecl());
}
else {
_os << "Type not a DeclRefExpr"
<< cast->getStmtClassName()
<< "\n";
}
}
else {
_os << "Type not a CastExpr"
<< cast->getStmtClassName()
<< "\n";
}
}
void FindGPUMacro::analyze_lhs(Expr *expr) {
if (ArraySubscriptExpr *array = dyn_cast<ArraySubscriptExpr>(expr)) {
Expr *base = array->getBase();
Expr *idx = array->getIdx();
analyze_array_base(base, true /* isLHS */);
}
else if (CastExpr *cast = dyn_cast<CastExpr>(expr)) {
analyze_lhs(cast->getSubExpr());
}
else if (ParenExpr *paren = dyn_cast<ParenExpr>(expr)) {
analyze_lhs(paren->getSubExpr());
}
else if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
if (binOp->isAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else if (binOp->isCompoundAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else {
_os << "Operation without effects on LHS: "
<< binOp->getStmtClassName()
<< "\n";
}
}
}
void FindGPUMacro::analyze_rhs(Expr *expr) {
if (ArraySubscriptExpr *array = dyn_cast<ArraySubscriptExpr>(expr)) {
Expr *base = array->getBase();
Expr *idx = array->getIdx();
analyze_array_base(base, false /* isLHS */);
}
else if (CastExpr *cast = dyn_cast<CastExpr>(expr)) {
analyze_rhs(cast->getSubExpr());
}
else if (ParenExpr *paren = dyn_cast<ParenExpr>(expr)) {
analyze_rhs(paren->getSubExpr());
}
else if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
if (binOp->isAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else if (binOp->isCompoundAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else {
analyze_rhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
}
}
void FindGPUMacro::analyze_expr(Expr *expr) {
if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
if (binOp->isAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else if (binOp->isCompoundAssignmentOp()) {
analyze_lhs(binOp->getLHS());
analyze_rhs(binOp->getRHS());
}
else {
_os << "Operation without effects: "
<< binOp->getStmtClassName()
<< "\n";
}
}
}
void FindGPUMacro::analyze_data_struct(Stmt *stmtList) {
if (!stmtList) return;
_os << "\nNEW FORLOOP BEGIN\n";
for (Stmt::child_iterator itr = stmtList->child_begin();
itr != stmtList->child_end();
++itr) {
Stmt *stmt = *itr;
if (!stmt) continue;
Expr *expr = dyn_cast<Expr>(stmt);
if (!expr) continue;
// Dump out the AST tree
expr->dump();
analyze_expr(expr);
}
for (std::set<ValueDecl*>::iterator itr = lhs_decls.begin();
itr != lhs_decls.end();
++itr) {
_os << "LHS ValueDecl AST Node: " << *itr
<< ", Name: '" << (*itr)->getNameAsString() << "', ";
analyze_value_decl(*itr);
}
for (std::set<ValueDecl*>::iterator itr = rhs_decls.begin();
itr != rhs_decls.end();
++itr) {
_os << "RHS ValueDecl AST Node: " << *itr
<< ", Name: '" << (*itr)->getNameAsString() << "', ";
analyze_value_decl(*itr);
}
_os << "NEW FORLOOP END\n";
}
......@@ -5,6 +5,7 @@
#include "llvm/Support/raw_ostream.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include <map>
#include <set>
namespace scpar {
......@@ -63,13 +64,30 @@ namespace scpar {
void dump();
// ===
// Analyze the simple for loop
//
// int a[5];
// for (int i = 0; i < 5; ++i) {
// a[i] = i;
// }
// ===
void analyze_data_struct(Stmt *stmtList);
void analyze_expr(Expr *expr);
void analyze_lhs(Expr *expr);
void analyze_rhs(Expr *expr);
void analyze_array_base(Expr *base, bool isLHS);
void analyze_value_decl(ValueDecl *val);
private:
forStmtGPUMacroMapType _forStmtGPUMacroMap;
CXXMethodDecl* _entryFunction;
int _instanceNum;
raw_ostream& _os;
std::set<ValueDecl*> lhs_decls;
std::set<ValueDecl*> rhs_decls;
};
}
#endif
#include "systemc.h"
#include "gpumacro.h"
sc_event e1;
SC_MODULE(consumer) {
sc_in<int> in;
int field1[25];
int field2[30];
void f2() {
int x;
while(true) {
int a[5] = { 0 };
int b[10] = { 1 };
int c[15] = { 2 };
int d[20] = { 3 };
for (int i = 0; i < 5; ++i) {
a[i] = b[i * 2];
a[i] = b[i * 2] + a[i] * 5;
a[i] = c[i * 3] % (b[i * 2] + i);
d[i * 4] = a[i] % (c[i * 3] + i);
field1[i * 5] = a[i];
field2[i * 6] = a[i];
}
}
}
SC_CTOR(consumer) {
SC_THREAD(f2);
}
};
int sc_main(int argc, char *argv[]) {
sc_signal<int> s1;
consumer c1("consumer1");
c1.in(s1);
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment