Skip to content

Commit 0a570f2

Browse files
author
Raúl Peñacoba Veigas
committed
Add support for global variable dependencies. See more.
Example: int array[10]; int x; in(x) in(array[x]) All variables are lowered as shared. In other clauses like cost/priority, global variables are not valid to use. In this commit we've added checks to catch them. Closes llvm#58
1 parent 58f311b commit 0a570f2

File tree

10 files changed

+291
-28
lines changed

10 files changed

+291
-28
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11466,6 +11466,8 @@ def err_oss_expected_base_var_name : Error<
1146611466
def err_oss_reduction_wrong_type : Error<"reduction type cannot be %select{qualified with 'const', 'volatile' or 'restrict'|a function|a reference|an array}0 type">;
1146711467
def err_oss_declare_reduction_redefinition : Error<"redefinition of user-defined reduction for type %0">;
1146811468

11469+
def err_oss_global_variable : Error<
11470+
"global variables are not allowed here">;
1146911471
def err_oss_non_const_variable : Error<
1147011472
"this variable cannot be used here">;
1147111473
def err_oss_const_variable : Error<

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11904,9 +11904,10 @@ class Sema final {
1190411904
ExprResult
1190511905
CheckNonNegativeIntegerValue(Expr *ValExpr,
1190611906
OmpSsClauseKind CKind,
11907-
bool StrictlyPositive);
11907+
bool StrictlyPositive,
11908+
bool Outline);
1190811909
ExprResult
11909-
CheckSignedIntegerValue(Expr *ValExpr);
11910+
CheckSignedIntegerValue(Expr *ValExpr, bool Outline);
1191011911
ExprResult
1191111912
CheckIsConstCharPtrConvertibleExpr(Expr *E, bool ConstConstraint = false);
1191211913
ExprResult

clang/lib/CodeGen/CGOmpSsRuntime.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,6 +2985,91 @@ RValue CGOmpSsRuntime::emitTaskFunction(CodeGenFunction &CGF,
29852985
TaskInfo.emplace_back(getBundleStr(OSSB_shared), DSABundleList);
29862986
}
29872987

2988+
// This class is used to inspect task outline clauses looking
2989+
// for global variables, which have to be captured as shared.
2990+
class OSSOutlineGlobalVarVisitor
2991+
: public ConstStmtVisitor<OSSOutlineGlobalVarVisitor, void> {
2992+
CodeGenFunction &CGF;
2993+
llvm::SetVector<const DeclRefExpr *> SharedGlobals;
2994+
public:
2995+
OSSOutlineGlobalVarVisitor(CodeGenFunction &CGF)
2996+
: CGF(CGF)
2997+
{}
2998+
2999+
//===--------------------------------------------------------------------===//
3000+
// Visitor Methods
3001+
//===--------------------------------------------------------------------===//
3002+
3003+
void VisitStmt(const Stmt *S) {
3004+
for (const Stmt *C : S->children()) {
3005+
if (C) {
3006+
Visit(C);
3007+
}
3008+
}
3009+
}
3010+
3011+
void VisitOSSMultiDepExpr(const OSSMultiDepExpr *E) {
3012+
Visit(E->getDepExpr());
3013+
3014+
for (size_t i = 0; i < E->getDepInits().size(); ++i) {
3015+
Visit(E->getDepInits()[i]);
3016+
if (E->getDepSizes()[i])
3017+
Visit(E->getDepSizes()[i]);
3018+
if (E->getDepSteps()[i])
3019+
Visit(E->getDepSteps()[i]);
3020+
}
3021+
}
3022+
3023+
void VisitOSSArrayShapingExpr(const OSSArrayShapingExpr *E) {
3024+
Visit(E->getBase());
3025+
3026+
for (const Expr *S : E->getShapes())
3027+
Visit(S);
3028+
}
3029+
3030+
void VisitOSSArraySectionExpr(const OSSArraySectionExpr *E) {
3031+
Visit(E->getBase());
3032+
3033+
if (E->getLowerBound())
3034+
Visit(E->getLowerBound());
3035+
if (E->getLengthUpper())
3036+
Visit(E->getLengthUpper());
3037+
}
3038+
3039+
void VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
3040+
Visit(E->getBase());
3041+
Visit(E->getIdx());
3042+
}
3043+
3044+
void VisitUnaryOperator(const UnaryOperator *E) {
3045+
Visit(E->getSubExpr());
3046+
}
3047+
3048+
void VisitMemberExpr(const MemberExpr *E) {
3049+
Visit(E->getBase());
3050+
}
3051+
3052+
void VisitDeclRefExpr(const DeclRefExpr *E) {
3053+
if (E->isNonOdrUse() == NOUR_Unevaluated)
3054+
return;
3055+
3056+
CodeGenFunction::ConstantEmission CE = CGF.tryEmitAsConstant(const_cast<DeclRefExpr*>(E));
3057+
if (CE && !CE.isReference())
3058+
// Constant value, no need to annotate it.
3059+
return;
3060+
3061+
if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
3062+
if (VD->hasGlobalStorage())
3063+
SharedGlobals.insert(E);
3064+
}
3065+
}
3066+
3067+
ArrayRef<const DeclRefExpr *> getSharedGlobals() const {
3068+
return SharedGlobals.getArrayRef();
3069+
}
3070+
3071+
};
3072+
29883073
// NOTE: this should do only one iteration
29893074
for (const auto *Attr : FD->specific_attrs<OSSTaskDeclAttr>()) {
29903075
SmallVector<llvm::Value*, 4> CapturedList;
@@ -3004,6 +3089,9 @@ RValue CGOmpSsRuntime::emitTaskFunction(CodeGenFunction &CGF,
30043089
FpDataTy.Copy = FpDataTy.Init = nullptr;
30053090
EmitDSAFirstprivate(CGF, FpDataTy, TaskInfo, CapturedList);
30063091
}
3092+
3093+
OSSOutlineGlobalVarVisitor OutlineGlobalVarVisitor(CGF);
3094+
30073095
if (const Expr *E = Attr->getIfExpr()) {
30083096
TaskInfo.emplace_back(getBundleStr(OSSB_if), CGF.EvaluateExprAsBool(E));
30093097
}
@@ -3056,126 +3144,175 @@ RValue CGOmpSsRuntime::emitTaskFunction(CodeGenFunction &CGF,
30563144
}
30573145
// in()
30583146
for (const Expr *E : Attr->ins()) {
3147+
OutlineGlobalVarVisitor.Visit(E);
3148+
30593149
OSSDepDataTy Dep;
30603150
Dep.OSSSyntax = true;
30613151
Dep.E = E;
30623152
EmitDependency(getBundleStr(OSSB_in), CGF, FD, Dep, TaskInfo);
30633153
}
30643154
for (const Expr *E : Attr->outs()) {
3155+
OutlineGlobalVarVisitor.Visit(E);
3156+
30653157
OSSDepDataTy Dep;
30663158
Dep.OSSSyntax = true;
30673159
Dep.E = E;
30683160
EmitDependency(getBundleStr(OSSB_out), CGF, FD, Dep, TaskInfo);
30693161
}
30703162
for (const Expr *E : Attr->inouts()) {
3163+
OutlineGlobalVarVisitor.Visit(E);
3164+
30713165
OSSDepDataTy Dep;
30723166
Dep.OSSSyntax = true;
30733167
Dep.E = E;
30743168
EmitDependency(getBundleStr(OSSB_inout), CGF, FD, Dep, TaskInfo);
30753169
}
30763170
for (const Expr *E : Attr->concurrents()) {
3171+
OutlineGlobalVarVisitor.Visit(E);
3172+
30773173
OSSDepDataTy Dep;
30783174
Dep.OSSSyntax = true;
30793175
Dep.E = E;
30803176
EmitDependency(getBundleStr(OSSB_concurrent), CGF, FD, Dep, TaskInfo);
30813177
}
30823178
for (const Expr *E : Attr->commutatives()) {
3179+
OutlineGlobalVarVisitor.Visit(E);
3180+
30833181
OSSDepDataTy Dep;
30843182
Dep.OSSSyntax = true;
30853183
Dep.E = E;
30863184
EmitDependency(getBundleStr(OSSB_commutative), CGF, FD, Dep, TaskInfo);
30873185
}
30883186
for (const Expr *E : Attr->weakIns()) {
3187+
OutlineGlobalVarVisitor.Visit(E);
3188+
30893189
OSSDepDataTy Dep;
30903190
Dep.OSSSyntax = true;
30913191
Dep.E = E;
30923192
EmitDependency(getBundleStr(OSSB_weakin), CGF, FD, Dep, TaskInfo);
30933193
}
30943194
for (const Expr *E : Attr->weakOuts()) {
3195+
OutlineGlobalVarVisitor.Visit(E);
3196+
30953197
OSSDepDataTy Dep;
30963198
Dep.OSSSyntax = true;
30973199
Dep.E = E;
30983200
EmitDependency(getBundleStr(OSSB_weakout), CGF, FD, Dep, TaskInfo);
30993201
}
31003202
for (const Expr *E : Attr->weakInouts()) {
3203+
OutlineGlobalVarVisitor.Visit(E);
3204+
31013205
OSSDepDataTy Dep;
31023206
Dep.OSSSyntax = true;
31033207
Dep.E = E;
31043208
EmitDependency(getBundleStr(OSSB_weakinout), CGF, FD, Dep, TaskInfo);
31053209
}
31063210
for (const Expr *E : Attr->weakConcurrents()) {
3211+
OutlineGlobalVarVisitor.Visit(E);
3212+
31073213
OSSDepDataTy Dep;
31083214
Dep.OSSSyntax = true;
31093215
Dep.E = E;
31103216
EmitDependency(getBundleStr(OSSB_weakconcurrent), CGF, FD, Dep, TaskInfo);
31113217
}
31123218
for (const Expr *E : Attr->weakCommutatives()) {
3219+
OutlineGlobalVarVisitor.Visit(E);
3220+
31133221
OSSDepDataTy Dep;
31143222
Dep.OSSSyntax = true;
31153223
Dep.E = E;
31163224
EmitDependency(getBundleStr(OSSB_weakcommutative), CGF, FD, Dep, TaskInfo);
31173225
}
31183226
// depend(in :)
31193227
for (const Expr *E : Attr->depIns()) {
3228+
OutlineGlobalVarVisitor.Visit(E);
3229+
31203230
OSSDepDataTy Dep;
31213231
Dep.OSSSyntax = false;
31223232
Dep.E = E;
31233233
EmitDependency(getBundleStr(OSSB_in), CGF, FD, Dep, TaskInfo);
31243234
}
31253235
for (const Expr *E : Attr->depOuts()) {
3236+
OutlineGlobalVarVisitor.Visit(E);
3237+
31263238
OSSDepDataTy Dep;
31273239
Dep.OSSSyntax = false;
31283240
Dep.E = E;
31293241
EmitDependency(getBundleStr(OSSB_out), CGF, FD, Dep, TaskInfo);
31303242
}
31313243
for (const Expr *E : Attr->depInouts()) {
3244+
OutlineGlobalVarVisitor.Visit(E);
3245+
31323246
OSSDepDataTy Dep;
31333247
Dep.OSSSyntax = false;
31343248
Dep.E = E;
31353249
EmitDependency(getBundleStr(OSSB_inout), CGF, FD, Dep, TaskInfo);
31363250
}
31373251
for (const Expr *E : Attr->depConcurrents()) {
3252+
OutlineGlobalVarVisitor.Visit(E);
3253+
31383254
OSSDepDataTy Dep;
31393255
Dep.OSSSyntax = false;
31403256
Dep.E = E;
31413257
EmitDependency(getBundleStr(OSSB_concurrent), CGF, FD, Dep, TaskInfo);
31423258
}
31433259
for (const Expr *E : Attr->depCommutatives()) {
3260+
OutlineGlobalVarVisitor.Visit(E);
3261+
31443262
OSSDepDataTy Dep;
31453263
Dep.OSSSyntax = false;
31463264
Dep.E = E;
31473265
EmitDependency(getBundleStr(OSSB_commutative), CGF, FD, Dep, TaskInfo);
31483266
}
31493267
for (const Expr *E : Attr->depWeakIns()) {
3268+
OutlineGlobalVarVisitor.Visit(E);
3269+
31503270
OSSDepDataTy Dep;
31513271
Dep.OSSSyntax = false;
31523272
Dep.E = E;
31533273
EmitDependency(getBundleStr(OSSB_weakin), CGF, FD, Dep, TaskInfo);
31543274
}
31553275
for (const Expr *E : Attr->depWeakOuts()) {
3276+
OutlineGlobalVarVisitor.Visit(E);
3277+
31563278
OSSDepDataTy Dep;
31573279
Dep.OSSSyntax = false;
31583280
Dep.E = E;
31593281
EmitDependency(getBundleStr(OSSB_weakout), CGF, FD, Dep, TaskInfo);
31603282
}
31613283
for (const Expr *E : Attr->depWeakInouts()) {
3284+
OutlineGlobalVarVisitor.Visit(E);
3285+
31623286
OSSDepDataTy Dep;
31633287
Dep.OSSSyntax = false;
31643288
Dep.E = E;
31653289
EmitDependency(getBundleStr(OSSB_weakinout), CGF, FD, Dep, TaskInfo);
31663290
}
31673291
for (const Expr *E : Attr->depWeakConcurrents()) {
3292+
OutlineGlobalVarVisitor.Visit(E);
3293+
31683294
OSSDepDataTy Dep;
31693295
Dep.OSSSyntax = false;
31703296
Dep.E = E;
31713297
EmitDependency(getBundleStr(OSSB_weakconcurrent), CGF, FD, Dep, TaskInfo);
31723298
}
31733299
for (const Expr *E : Attr->depWeakCommutatives()) {
3300+
OutlineGlobalVarVisitor.Visit(E);
3301+
31743302
OSSDepDataTy Dep;
31753303
Dep.OSSSyntax = false;
31763304
Dep.E = E;
31773305
EmitDependency(getBundleStr(OSSB_weakcommutative), CGF, FD, Dep, TaskInfo);
31783306
}
3307+
3308+
for (const Expr *E : OutlineGlobalVarVisitor.getSharedGlobals()) {
3309+
SmallVector<llvm::Value *> DSABundleList;
3310+
llvm::Value *DSAValue = CGF.EmitLValue(E).getPointer(CGF);
3311+
DSABundleList.push_back(DSAValue);
3312+
DSABundleList.push_back(llvm::UndefValue::get(CGF.ConvertType(E->getType())));
3313+
TaskInfo.emplace_back(getBundleStr(OSSB_shared), DSABundleList);
3314+
}
3315+
31793316
assert(Attr->reductions_size() == Attr->reductionLHSs_size() &&
31803317
Attr->reductions_size() == Attr->reductionRHSs_size() &&
31813318
Attr->reductions_size() == Attr->reductionOps_size() &&

0 commit comments

Comments
 (0)