Skip to content

Commit bfd0fdf

Browse files
committed
improvements
1 parent 2fc19e1 commit bfd0fdf

File tree

2 files changed

+57
-85
lines changed

2 files changed

+57
-85
lines changed

include/tsar/Transform/Clang/RemoveFirstPrivate.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

lib/Transform/Clang/RemoveFirstPrivate.cpp

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
//
2323
//===----------------------------------------------------------------------===//
2424

25-
#include "tsar/Transform/Clang/RemoveFirstPrivate.h"
2625
#include "tsar/Analysis/Clang/GlobalInfoExtractor.h"
2726
#include "tsar/Analysis/Clang/NoMacroAssert.h"
2827
#include "tsar/Core/Query.h"
@@ -31,15 +30,17 @@
3130
#include <clang/AST/Decl.h>
3231
#include <clang/AST/RecursiveASTVisitor.h>
3332
#include <clang/AST/Stmt.h>
33+
#include "tsar/Analysis/Memory/Utils.h"
34+
#include "tsar/Support/MetadataUtils.h"
3435
#include <clang/Basic/SourceLocation.h>
35-
#include <llvm/ADT/DenseMap.h>
36-
#include <llvm/ADT/DenseSet.h>
3736
#include <llvm/ADT/SmallVector.h>
3837
#include <llvm/IR/Function.h>
3938
#include <llvm/IR/Module.h>
4039
#include <llvm/Support/Debug.h>
4140
#include <llvm/Support/raw_ostream.h>
42-
#include <vector>
41+
#include "tsar/Transform/Clang/Passes.h"
42+
#include <bcl/utility.h>
43+
#include <llvm/Pass.h>
4344
#include <stack>
4445

4546
using namespace clang;
@@ -49,50 +50,58 @@ using namespace tsar;
4950
#undef DEBUG_TYPE
5051
#define DEBUG_TYPE "clang-rfp"
5152

52-
char ClangRemoveFirstPrivate::ID = 0;
53-
54-
INITIALIZE_PASS_IN_GROUP_BEGIN(ClangRemoveFirstPrivate, "remove-firstprivate",
55-
"Initialize variables in for", false, false,
56-
TransformationQueryManager::getPassRegistry())
57-
INITIALIZE_PASS_DEPENDENCY(TransformationEnginePass)
58-
INITIALIZE_PASS_DEPENDENCY(ClangGlobalInfoPass)
59-
INITIALIZE_PASS_IN_GROUP_END(ClangRemoveFirstPrivate, "remove-firstprivate",
60-
"Initialize variables in for", false, false,
61-
TransformationQueryManager::getPassRegistry())
62-
63-
64-
bool isNameOfArray(std::string type) {
53+
static bool isNameOfArray(std::string type) {
6554
if (type.find('[') != std::string::npos || type.find('*') != std::string::npos) {
6655
return true;
6756
}
6857
return false;
6958
}
7059

71-
void replaceSqrBrWithAsterisk(std::string &str) {
60+
static void replaceSqrBrWithAsterisk(std::string &str) {
7261
size_t openingSqrBracket = str.find("[");
7362
size_t closingSqrBracket = str.find("]");
7463
str.erase(openingSqrBracket, closingSqrBracket - openingSqrBracket + 1);
7564
str += "*";
7665
}
7766

67+
namespace {
68+
69+
class ClangRemoveFirstPrivate : public FunctionPass, private bcl::Uncopyable {
70+
public:
71+
static char ID;
72+
73+
ClangRemoveFirstPrivate() : FunctionPass(ID) {
74+
initializeClangRemoveFirstPrivatePass(*PassRegistry::getPassRegistry());
75+
}
76+
77+
bool runOnFunction(Function &F) override;
78+
void getAnalysisUsage(AnalysisUsage &AU) const override;
79+
};
80+
7881
struct vars { // contains information about variables in
7982
std::string var1Type; // removefirstprivate clause
8083
std::string var2Type;
8184
std::string var1Name;
8285
std::string var2Name;
8386
std::string count = "";
8487
};
88+
}
89+
90+
char ClangRemoveFirstPrivate::ID = 0;
91+
92+
INITIALIZE_PASS_IN_GROUP_BEGIN(ClangRemoveFirstPrivate, "remove-firstprivate",
93+
"Initialize variables in for", false, false,
94+
TransformationQueryManager::getPassRegistry())
95+
INITIALIZE_PASS_DEPENDENCY(TransformationEnginePass)
96+
INITIALIZE_PASS_DEPENDENCY(ClangGlobalInfoPass)
97+
INITIALIZE_PASS_IN_GROUP_END(ClangRemoveFirstPrivate, "remove-firstprivate",
98+
"Initialize variables in for", false, false,
99+
TransformationQueryManager::getPassRegistry())
85100

86101
namespace {
87102

88103
class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
89104

90-
std::string getStrByLoc(SourceLocation begin, SourceLocation end) {
91-
SourceRange SR(begin, end);
92-
CharSourceRange CSR(SR, true);
93-
return mRewriter.getRewrittenText(CSR);
94-
}
95-
96105
struct DeclarationInfo {
97106
DeclarationInfo(Stmt *S) : Scope(S) {}
98107

@@ -129,24 +138,18 @@ class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
129138
varStack.pop();
130139
continue; // count is mandatory for arrays, skip initialization if no count found
131140
}
132-
type1 = varStack.top().var1Type;
133-
type2 = varStack.top().var2Type;
134141
if (type1.find('[') != std::string::npos) {
135142
replaceSqrBrWithAsterisk(type1);
136143
}
137144
if (type2.find('[') != std::string::npos) {
138145
replaceSqrBrWithAsterisk(type2);
139146
}
140147
if (isNameOfArray(varStack.top().var2Type)) { // arr1 = arr2
141-
beforeFor = type1 + "var1Ptr" + " = " + varStack.top().var1Name + ";\n" +
142-
type2 + "var2Ptr" + " = " + varStack.top().var2Name + ";\n";
143-
forBody = "var1Ptr[i] = var2Ptr[i];\n";
148+
forBody = varStack.top().var1Name + "[i] = " + varStack.top().var2Name + "[i];\n";
144149
} else { // arr1 = val
145-
beforeFor = type1 + "var1Ptr" + " = " + varStack.top().var1Name + ";\n";
146-
forBody = "var1Ptr[i] = " + varStack.top().var2Name + ";\n";
150+
forBody = varStack.top().var1Name + "[i] = " + varStack.top().var2Name + ";\n";
147151
}
148-
txtStr = beforeFor;
149-
txtStr += "for (int i = 0; i < " + varStack.top().count + "; i++) {\n" + forBody + "\n}\n";
152+
txtStr = "for (int i = 0; i < " + varStack.top().count + "; i++) {\n" + forBody + "\n}\n";
150153
} else { // Initialize non-array variable
151154
txtStr = varStack.top().var1Name + " = " + varStack.top().var2Name + ";\n";
152155
}
@@ -183,8 +186,11 @@ class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
183186
}
184187

185188
bool TraverseDeclRefExpr(clang::DeclRefExpr *Ex) {
189+
std::string varName;
186190
if (isInPragma) {
187-
std::string varName = getStrByLoc(Ex -> getBeginLoc(), Ex -> getEndLoc());
191+
if (auto *Var{dyn_cast<VarDecl>(Ex->getDecl())}) {
192+
varName = Var -> getName();
193+
}
188194
if (waitingForVar) {
189195
ValueDecl *vd = Ex -> getDecl();
190196
QualType qt = vd -> getType();
@@ -212,7 +218,7 @@ class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
212218
bool TraverseIntegerLiteral(IntegerLiteral *IL) {
213219
if (isInPragma && waitingForVar) {
214220
if (varStack.size()) {
215-
varStack.top().count = getStrByLoc(IL -> getBeginLoc(), IL -> getEndLoc());
221+
varStack.top().count = std::to_string(IL -> getValue().getLimitedValue());
216222
}
217223
}
218224
return RecursiveASTVisitor::TraverseIntegerLiteral(IL);
@@ -251,13 +257,25 @@ class DeclVisitor : public RecursiveASTVisitor<DeclVisitor> {
251257

252258
bool ClangRemoveFirstPrivate::runOnFunction(Function &F) {
253259
auto *M = F.getParent();
254-
auto &TfmInfo = getAnalysis<TransformationEnginePass>();
255-
auto *TfmCtx{TfmInfo ? TfmInfo->getContext(*M) : nullptr};
260+
261+
auto *DISub{findMetadata(&F)};
262+
if (!DISub)
263+
return false;
264+
auto *CU{DISub->getUnit()};
265+
if (isC(CU->getSourceLanguage()) && isCXX(CU->getSourceLanguage()))
266+
return false;
267+
auto &TfmInfo{getAnalysis<TransformationEnginePass>()};
268+
auto *TfmCtx{TfmInfo ? dyn_cast_or_null<ClangTransformationContext>(
269+
TfmInfo->getContext(*CU))
270+
: nullptr};
256271
if (!TfmCtx || !TfmCtx->hasInstance()) {
257-
M->getContext().emitError("can not transform sources"
258-
": transformation context is not available");
272+
F.getContext().emitError(
273+
"cannot transform sources"
274+
": transformation context is not available for the '" +
275+
F.getName() + "' function");
259276
return false;
260277
}
278+
261279
auto FuncDecl = TfmCtx->getDeclForMangledName(F.getName());
262280
if (!FuncDecl)
263281
return false;

0 commit comments

Comments
 (0)