Skip to content

Commit 72f2103

Browse files
committed
Merge branch 'sotoc-issue-44' into 'aurora_offloading_prototype'
Resolve sotoc issue 44 Closes llvm#44 See merge request NEC-RWTH-Projects/clang!24
2 parents bd13b45 + 43c01c3 commit 72f2103

13 files changed

+82
-20
lines changed

clang/tools/sotoc/src/DeclResolver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void TypeDeclResolver::runOwnVisitor(clang::Decl *D,
182182

183183
void FunctionDeclResolver::runOwnVisitor(
184184
clang::Decl *D, std::function<void(clang::Decl *Dep)> Fn) {
185-
DEBUGPDECL(D, "Searching for referred decls in function " );
185+
DEBUGPDECL(D, "Searching for referred decls in function ");
186186
DiscoverFunctionsInDeclVisitor Visitor(Fn);
187187
Visitor.TraverseDecl(D);
188188
}

clang/tools/sotoc/src/TargetCode.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@ void TargetCode::generateFunctionPrologue(TargetCodeRegion *TCR,
125125
int dim = 0;
126126

127127
std::vector<int> VariableDimensions;
128-
handleArrays(&t, DimString, dim, VariableDimensions, TCR, elemType, VarName);
128+
handleArrays(&t, DimString, dim, VariableDimensions, TCR, elemType,
129+
VarName);
129130

130131
for (int d : VariableDimensions) {
131-
Out << "unsigned long long __sotoc_vla_dim" << d << "_" << VarName << ", ";
132+
Out << "unsigned long long __sotoc_vla_dim" << d << "_" << VarName
133+
<< ", ";
132134
}
133135

134136
// set type to void* to avoid warnings from the compiler
@@ -178,6 +180,13 @@ void TargetCode::generateFunctionPrologue(TargetCodeRegion *TCR,
178180

179181
Out << " = __sotoc_var_" << VarName << ";\n";
180182

183+
auto LowerBound = TCR->CapturedLowerBounds.find(*I);
184+
if (LowerBound != TCR->CapturedLowerBounds.end()) {
185+
Out << VarName << " = " << VarName << " - (";
186+
LowerBound->second->printPretty(Out, NULL, TCR->getPP());
187+
Out << ");\n";
188+
}
189+
181190
} else {
182191
if (!(*I)->getType().getTypePtr()->isPointerType()) {
183192
if (C) {
@@ -267,8 +276,7 @@ std::string TargetCode::generateFunctionName(TargetCodeRegion *TCR) {
267276
void TargetCode::handleArrays(const clang::ArrayType **t,
268277
std::list<std::string> &DimString, int &dim,
269278
std::vector<int> &VariableDims,
270-
TargetCodeRegion *TCR,
271-
std::string &elemType,
279+
TargetCodeRegion *TCR, std::string &elemType,
272280
const std::string &ArrayName) {
273281
auto OrigT = *t;
274282

clang/tools/sotoc/src/TargetCode.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class TargetCode {
8585
/// \param returns the last element type (i.e., the type of the array)
8686
void handleArrays(const clang::ArrayType **t,
8787
std::list<std::string> &DimString, int &dim,
88-
std::vector<int> &VariableDimensions,
89-
TargetCodeRegion *TCR, std::string &elemType,
90-
const std::string &ArrayName);
88+
std::vector<int> &VariableDimensions, TargetCodeRegion *TCR,
89+
std::string &elemType, const std::string &ArrayName);
9190
};

clang/tools/sotoc/src/TargetCodeFragment.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ class TargetCodeRegion : public TargetCodeFragment {
159159
/// location of the first pragma of the target region to compose the name of
160160
/// the function generated for that region)
161161
clang::SourceLocation getTargetDirectiveLocation();
162+
/// Lower bounds of mapped array slices (if lower then 0).
163+
/// If the captured variable is an array, of which only a slice is mapped
164+
/// (by a map() clause), the incoming pointer argument will need to be
165+
/// shifted to the right if the lower bound of that slice is not 0.
166+
/// If this is the case, the lower bound is saved into this map.
167+
std::map<clang::VarDecl *, clang::Expr *> CapturedLowerBounds;
162168
};
163169

164170
/// This class represents a declaration, i.e. a function, global varialbe, or

clang/tools/sotoc/src/Visitors.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "clang/AST/ASTContext.h"
2020
#include "clang/AST/Attr.h"
2121
#include "clang/AST/Decl.h"
22+
#include "clang/AST/ExprOpenMP.h"
2223
#include "clang/AST/Stmt.h"
2324
#include "clang/AST/StmtOpenMP.h"
2425
#include "clang/Basic/OpenMPKinds.h"
@@ -119,11 +120,12 @@ bool FindTargetCodeVisitor::VisitStmt(clang::Stmt *S) {
119120
return true;
120121
}
121122

122-
class CollectOMPClausesVisitor : public clang::RecursiveASTVisitor<CollectOMPClausesVisitor> {
123+
class CollectOMPClausesVisitor
124+
: public clang::RecursiveASTVisitor<CollectOMPClausesVisitor> {
123125
std::shared_ptr<TargetCodeRegion> TCR;
126+
124127
public:
125-
CollectOMPClausesVisitor(std::shared_ptr<TargetCodeRegion> &TCR)
126-
: TCR(TCR) {};
128+
CollectOMPClausesVisitor(std::shared_ptr<TargetCodeRegion> &TCR) : TCR(TCR){};
127129
bool VisitStmt(clang::Stmt *S) {
128130
if (auto *OED = llvm::dyn_cast<clang::OMPExecutableDirective>(S)) {
129131
for (auto *Clause : OED->clauses()) {
@@ -153,6 +155,9 @@ bool FindTargetCodeVisitor::processTargetRegion(
153155
// if the target region cannot be added we dont want to parse its args
154156
if (TargetCodeInfo.addCodeFragment(TCR)) {
155157

158+
FindArraySectionVisitor ArraySectionVisitor(TCR->CapturedLowerBounds);
159+
ArraySectionVisitor.TraverseStmt(TargetDirective);
160+
156161
// look for nested clause
157162
CollectOMPClausesVisitor(TCR).TraverseStmt(CS);
158163
for (auto C : TargetDirective->clauses()) {
@@ -198,7 +203,7 @@ void FindTargetCodeVisitor::addTargetRegionArgs(
198203
DEBUGP("Iterating var set");
199204
// i->print(llvm::outs());
200205
if (Context.getSourceManager().isBeforeInTranslationUnit(
201-
S->getBeginLoc(),i->getSourceRange().getBegin())) {
206+
S->getBeginLoc(), i->getSourceRange().getBegin())) {
202207
tmpSet.insert(i);
203208
continue;
204209
}
@@ -365,3 +370,35 @@ bool DiscoverFunctionsInDeclVisitor::VisitExpr(clang::Expr *E) {
365370
}
366371
return true;
367372
}
373+
374+
bool FindArraySectionVisitor::VisitExpr(clang::Expr *E) {
375+
if (auto *ASE = llvm::dyn_cast<clang::OMPArraySectionExpr>(E)) {
376+
clang::Expr *Base = ASE->getBase();
377+
if (llvm::isa<clang::OMPArraySectionExpr>(Base)) {
378+
return true;
379+
}
380+
if (auto *CastBase = llvm::dyn_cast<clang::CastExpr>(Base)) {
381+
Base = CastBase->getSubExpr();
382+
if (auto *DRE = llvm::dyn_cast<clang::DeclRefExpr>(Base)) {
383+
auto *VarDecl = llvm::dyn_cast<clang::VarDecl>(DRE->getDecl());
384+
if (!VarDecl) {
385+
llvm::errs() << "VALDECL != VARDECL\n";
386+
return true;
387+
}
388+
clang::Expr *LowerBound = ASE->getLowerBound();
389+
if (!LowerBound) {
390+
return true;
391+
}
392+
393+
if (auto *IntegerLiteral =
394+
llvm::dyn_cast<clang::IntegerLiteral>(LowerBound)) {
395+
if (IntegerLiteral->getValue() == 0) {
396+
return true;
397+
}
398+
}
399+
LowerBoundsMap.emplace(VarDecl, LowerBound);
400+
}
401+
}
402+
}
403+
return true;
404+
}

clang/tools/sotoc/src/Visitors.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,15 @@ class FindTargetCodeVisitor
149149
void addTargetRegionArgs(clang::CapturedStmt *S,
150150
std::shared_ptr<TargetCodeRegion> TCR);
151151
};
152+
153+
class FindArraySectionVisitor
154+
: public clang::RecursiveASTVisitor<FindArraySectionVisitor> {
155+
156+
std::map<clang::VarDecl *, clang::Expr *> &LowerBoundsMap;
157+
158+
public:
159+
FindArraySectionVisitor(
160+
std::map<clang::VarDecl *, clang::Expr *> &LowerBoundsMap)
161+
: LowerBoundsMap(LowerBoundsMap) {}
162+
bool VisitExpr(clang::Expr *E);
163+
};

clang/tools/sotoc/test/arrays/slice_mixed_length_array_2d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){

clang/tools/sotoc/test/arrays/slice_static_length_array_1d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){

clang/tools/sotoc/test/arrays/slice_variable_length_array_1d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){

clang/tools/sotoc/test/arrays/slice_variable_length_array_2d.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){
@@ -8,7 +8,7 @@ int main(){
88
int sizeY=512;
99
float A[sizeX][sizeY];
1010

11-
#pragma omp target map(tofrom:A[:sizeX/2][:sizeY])
11+
#pragma omp target map(tofrom:A[0:sizeX/2][:sizeY])
1212
{
1313
int i;
1414
int j;

clang/tools/sotoc/test/arrays/slice_variable_length_array_2d_1_colon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){

clang/tools/sotoc/test/arrays/slice_variable_length_array_2d_1_explicit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %sotoc-transform-compile
2-
// #RUN#: %run-on-host | %filecheck %s
2+
// RUN: %run-on-host | %filecheck %s
33

44
#include <stdio.h>
55
int main(){

clang/tools/sotoc/test/arrays/slice_variable_length_array_3d.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ int main(){
1515
int j;
1616
int k;
1717
for(i=0; i< 512; i++){
18-
for(j=0 ; j< 256; j+=2){
18+
for(j=0 ; j< 512; j+=1){
1919
for(k=0; k < (sizeZ - 2); ++k) {
2020
A[i][j][k]=i-j;
2121
}

0 commit comments

Comments
 (0)