Skip to content

Commit 1d9e13b

Browse files
committed
CodeGen: Support OSSArraySections. Now last dimension matches array size instead of being 1
1 parent b17ba30 commit 1d9e13b

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

clang/lib/CodeGen/CGOmpSsRuntime.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,112 @@ class OSSDependVisitor
116116
FillBaseExprDimsAndType(E);
117117
}
118118

119+
void VisitOSSArraySectionExpr(const OSSArraySectionExpr *E) {
120+
// Get Base Type
121+
// An array section is considered a built-in type
122+
BaseElementTy = OSSArraySectionExpr::getBaseOriginalType(
123+
E->getBase()->IgnoreParenImpCasts())
124+
.getCanonicalType();
125+
if (BaseElementTy->isArrayType()) {
126+
if (const ConstantArrayType *BaseArrayTy = CGF.getContext().getAsConstantArrayType(BaseElementTy)){
127+
BaseElementTy = CGF.getContext().getBaseElementType(BaseElementTy);
128+
} else if (const VariableArrayType *BaseArrayTy = CGF.getContext().getAsVariableArrayType(BaseElementTy)){
129+
BaseElementTy = CGF.getContext().getBaseElementType(BaseElementTy);
130+
} else {
131+
llvm_unreachable("Unhandled array type");
132+
}
133+
}
134+
// Get the inner expr
135+
const Expr *TmpE = E;
136+
// First come OSSArraySection
137+
while (const OSSArraySectionExpr *ASE = dyn_cast<OSSArraySectionExpr>(TmpE->IgnoreParenImpCasts())) {
138+
TmpE = ASE->getBase();
139+
// Add indexes
140+
llvm::Value *Idx, *IdxEnd;
141+
142+
const Expr *LowerB = ASE->getLowerBound();
143+
if (LowerB)
144+
Idx = CGF.EmitScalarExpr(LowerB);
145+
else
146+
// OpenMP 5.0 2.1.5 When the lower-bound is absent it defaults to 0.
147+
Idx = llvm::ConstantInt::getSigned(OSSArgTy, 0);
148+
Idx = CGF.Builder.CreateSExt(Idx, OSSArgTy);
149+
150+
const Expr *Size = ASE->getLength();
151+
if (Size) {
152+
IdxEnd = CGF.EmitScalarExpr(Size);
153+
IdxEnd = CGF.Builder.CreateSExt(IdxEnd, OSSArgTy);
154+
IdxEnd = CGF.Builder.CreateAdd(Idx, IdxEnd);
155+
} else if (ASE->getColonLoc().isInvalid()) {
156+
assert(!Size);
157+
// OSSArraySection without ':' are regular array subscripts
158+
IdxEnd = CGF.Builder.CreateAdd(Idx, llvm::ConstantInt::getSigned(OSSArgTy, 1));
159+
} else {
160+
// array[lower : ] -> [lower, dimsize)
161+
162+
// OpenMP 5.0 2.1.5
163+
// When the length is absent it defaults to ⌈(size - lowerbound)∕stride⌉,
164+
// where size is the size of the array dimension.
165+
QualType BaseOriginalTy =
166+
OSSArraySectionExpr::getBaseOriginalType(ASE->getBase());
167+
168+
if (const ConstantArrayType *BaseArrayTy = CGF.getContext().getAsConstantArrayType(BaseOriginalTy)) {
169+
uint64_t DimSize = BaseArrayTy->getSize().getSExtValue();
170+
IdxEnd = llvm::ConstantInt::getSigned(OSSArgTy, DimSize);
171+
} else if (const VariableArrayType *BaseArrayTy = CGF.getContext().getAsVariableArrayType(BaseOriginalTy)) {
172+
auto VlaSize = CGF.getVLAElements1D(BaseArrayTy);
173+
IdxEnd = CGF.Builder.CreateSExt(VlaSize.NumElts, OSSArgTy);
174+
} else {
175+
llvm_unreachable("Unhandled array type");
176+
}
177+
}
178+
IdxEnd = CGF.Builder.CreateSExt(IdxEnd, OSSArgTy);
179+
180+
Starts.push_back(Idx);
181+
Ends.push_back(IdxEnd);
182+
if (TmpE->IgnoreParenImpCasts()->getType()->isPointerType())
183+
break;
184+
}
185+
while (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(TmpE->IgnoreParenImpCasts())) {
186+
TmpE = ASE->getBase();
187+
// Add indexes
188+
llvm::Value *Idx = CGF.EmitScalarExpr(ASE->getIdx());
189+
Idx = CGF.Builder.CreateSExt(Idx, OSSArgTy);
190+
llvm::Value *IdxEnd = CGF.Builder.CreateAdd(Idx, llvm::ConstantInt::getSigned(OSSArgTy, 1));
191+
Starts.push_back(Idx);
192+
Ends.push_back(IdxEnd);
193+
if (TmpE->IgnoreParenImpCasts()->getType()->isPointerType())
194+
break;
195+
}
196+
197+
Ptr = CGF.EmitScalarExpr(TmpE);
198+
199+
TmpE = TmpE->IgnoreParenImpCasts();
200+
201+
QualType TmpTy = TmpE->getType();
202+
// Add Dimensions
203+
if (TmpTy->isPointerType()) {
204+
// T *
205+
Dims.push_back(llvm::ConstantInt::getSigned(OSSArgTy, 1));
206+
} else {
207+
while (TmpTy->isArrayType()) {
208+
// T []
209+
if (const ConstantArrayType *BaseArrayTy = CGF.getContext().getAsConstantArrayType(TmpTy)) {
210+
uint64_t DimSize = BaseArrayTy->getSize().getSExtValue();
211+
Dims.push_back(llvm::ConstantInt::getSigned(OSSArgTy, DimSize));
212+
TmpTy = BaseArrayTy->getElementType();
213+
} else if (const VariableArrayType *BaseArrayTy = CGF.getContext().getAsVariableArrayType(TmpTy)) {
214+
auto VlaSize = CGF.getVLAElements1D(BaseArrayTy);
215+
llvm::Value *DimExpr = CGF.Builder.CreateSExt(VlaSize.NumElts, OSSArgTy);
216+
Dims.push_back(DimExpr);
217+
TmpTy = BaseArrayTy->getElementType();
218+
} else {
219+
llvm_unreachable("Unhandled array type");
220+
}
221+
}
222+
}
223+
}
224+
119225
void VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
120226
// Get Base Type
121227
BaseElementTy = E->getType();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %clang_cc1 -verify -fompss-2 -disable-llvm-passes -ferror-limit 100 %s -S -emit-llvm -o - | FileCheck %s
2+
// expected-no-diagnostics
3+
4+
typedef int* Pointer;
5+
6+
void foo(int x) {
7+
Pointer aFix[10][10];
8+
int aVLA[x][7];
9+
#pragma oss task depend(in : aFix[:], aFix[1:], aFix[: 2], aFix[3 : 4])
10+
#pragma oss task depend(in : aFix[5][:], aFix[5][1:], aFix[5][: 2], aFix[5][3 : 4])
11+
#pragma oss task depend(in : aVLA[:], aVLA[1:], aVLA[: 2], aVLA[3 : 4])
12+
#pragma oss task depend(in : aVLA[5][:], aVLA[5][1:], aVLA[5][: 2], aVLA[5][3 : 4])
13+
{}
14+
}
15+
16+
// CHECK: %x.addr = alloca i32, align 4
17+
// CHECK-NEXT: %aFix = alloca [10 x [10 x i32*]], align 16
18+
// CHECK-NEXT: %saved_stack = alloca i8*, align 8
19+
// CHECK-NEXT: %__vla_expr0 = alloca i64, align 8
20+
// CHECK-NEXT: store i32 %x, i32* %x.addr, align 4
21+
// CHECK-NEXT: %0 = load i32, i32* %x.addr, align 4
22+
// CHECK-NEXT: %1 = zext i32 %0 to i64
23+
// CHECK-NEXT: %2 = call i8* @llvm.stacksave()
24+
// CHECK-NEXT: store i8* %2, i8** %saved_stack, align 8
25+
// CHECK-NEXT: %vla = alloca [7 x i32], i64 %1, align 16
26+
// CHECK-NEXT: store i64 %1, i64* %__vla_expr0, align 8
27+
// CHECK: %3 = call token @llvm.directive.region.entry() [ "DIR.OSS"([5 x i8] c"TASK\00"), "QUAL.OSS.SHARED"([10 x [10 x i32*]]* %aFix), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay, i64 80, i64 0, i64 80, i64 10, i64 0, i64 10), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay1, i64 80, i64 0, i64 80, i64 10, i64 1, i64 10), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay2, i64 80, i64 0, i64 80, i64 10, i64 0, i64 2), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay3, i64 80, i64 0, i64 80, i64 10, i64 3, i64 7) ]
28+
29+
// CHECK: %4 = call token @llvm.directive.region.entry() [ "DIR.OSS"([5 x i8] c"TASK\00"), "QUAL.OSS.SHARED"([10 x [10 x i32*]]* %aFix), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay4, i64 80, i64 0, i64 80, i64 10, i64 5, i64 6), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay5, i64 80, i64 8, i64 80, i64 10, i64 5, i64 6), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay6, i64 80, i64 0, i64 16, i64 10, i64 5, i64 6), "QUAL.OSS.DEP.IN"([10 x i32*]* %arraydecay7, i64 80, i64 24, i64 56, i64 10, i64 5, i64 6) ]
30+
// CHECK-NEXT: %5 = call token @llvm.directive.region.entry() [ "DIR.OSS"([5 x i8] c"TASK\00"), "QUAL.OSS.SHARED.VLA"([7 x i32]* %vla, i64 %1, i64 7), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 28, i64 %1, i64 0, i64 %1), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 28, i64 %1, i64 1, i64 %1), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 28, i64 %1, i64 0, i64 2), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 28, i64 %1, i64 3, i64 7) ]
31+
// CHECK-NEXT: %6 = call token @llvm.directive.region.entry() [ "DIR.OSS"([5 x i8] c"TASK\00"), "QUAL.OSS.SHARED.VLA"([7 x i32]* %vla, i64 %1, i64 7), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 28, i64 %1, i64 5, i64 6), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 4, i64 28, i64 %1, i64 5, i64 6), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 0, i64 8, i64 %1, i64 5, i64 6), "QUAL.OSS.DEP.IN"([7 x i32]* %vla, i64 28, i64 12, i64 28, i64 %1, i64 5, i64 6) ]
32+

0 commit comments

Comments
 (0)