Skip to content

Commit a42380c

Browse files
committed
[OMPIRBuilder] Add ordered directive to OMPBuilder
Add support for ordered directive in the OpenMPIRBuilder. This patch also modidies clang to use the ordered directive when the option -fopenmp-enable-irbuilder is enabled. Also fix one ICE when parsing one canonical for loop with the relational operator LE or GE in openmp region by replacing unary increment operation of the expression of the variable "Expr A" minus the variable "Expr B" (++(Expr A - Expr B)) with binary addition operation of the experssion of the variable "Expr A" minus the variable "Expr B" and the expression with constant value "1" (Expr A - Expr B + "1"). Reviewed By: Meinersbur, kiranchandramohan Differential Revision: https://reviews.llvm.org/D107430
1 parent 91eda9c commit a42380c

File tree

9 files changed

+788
-110
lines changed

9 files changed

+788
-110
lines changed

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5312,6 +5312,74 @@ static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM,
53125312
}
53135313

53145314
void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
5315+
if (CGM.getLangOpts().OpenMPIRBuilder) {
5316+
llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
5317+
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
5318+
5319+
if (S.hasClausesOfKind<OMPDependClause>()) {
5320+
// The ordered directive with depend clause.
5321+
assert(!S.hasAssociatedStmt() &&
5322+
"No associated statement must be in ordered depend construct.");
5323+
InsertPointTy AllocaIP(AllocaInsertPt->getParent(),
5324+
AllocaInsertPt->getIterator());
5325+
for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) {
5326+
unsigned NumLoops = DC->getNumLoops();
5327+
QualType Int64Ty = CGM.getContext().getIntTypeForBitwidth(
5328+
/*DestWidth=*/64, /*Signed=*/1);
5329+
llvm::SmallVector<llvm::Value *> StoreValues;
5330+
for (unsigned I = 0; I < NumLoops; I++) {
5331+
const Expr *CounterVal = DC->getLoopData(I);
5332+
assert(CounterVal);
5333+
llvm::Value *StoreValue = EmitScalarConversion(
5334+
EmitScalarExpr(CounterVal), CounterVal->getType(), Int64Ty,
5335+
CounterVal->getExprLoc());
5336+
StoreValues.emplace_back(StoreValue);
5337+
}
5338+
bool IsDependSource = false;
5339+
if (DC->getDependencyKind() == OMPC_DEPEND_source)
5340+
IsDependSource = true;
5341+
Builder.restoreIP(OMPBuilder.createOrderedDepend(
5342+
Builder, AllocaIP, NumLoops, StoreValues, ".cnt.addr",
5343+
IsDependSource));
5344+
}
5345+
} else {
5346+
// The ordered directive with threads or simd clause, or without clause.
5347+
// Without clause, it behaves as if the threads clause is specified.
5348+
const auto *C = S.getSingleClause<OMPSIMDClause>();
5349+
5350+
auto FiniCB = [this](InsertPointTy IP) {
5351+
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
5352+
};
5353+
5354+
auto BodyGenCB = [&S, C, this](InsertPointTy AllocaIP,
5355+
InsertPointTy CodeGenIP,
5356+
llvm::BasicBlock &FiniBB) {
5357+
const CapturedStmt *CS = S.getInnermostCapturedStmt();
5358+
if (C) {
5359+
llvm::SmallVector<llvm::Value *, 16> CapturedVars;
5360+
GenerateOpenMPCapturedVars(*CS, CapturedVars);
5361+
llvm::Function *OutlinedFn =
5362+
emitOutlinedOrderedFunction(CGM, CS, S.getBeginLoc());
5363+
assert(S.getBeginLoc().isValid() &&
5364+
"Outlined function call location must be valid.");
5365+
ApplyDebugLocation::CreateDefaultArtificial(*this, S.getBeginLoc());
5366+
OMPBuilderCBHelpers::EmitCaptureStmt(*this, CodeGenIP, FiniBB,
5367+
OutlinedFn, CapturedVars);
5368+
} else {
5369+
OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP,
5370+
FiniBB);
5371+
OMPBuilderCBHelpers::EmitOMPRegionBody(*this, CS->getCapturedStmt(),
5372+
CodeGenIP, FiniBB);
5373+
}
5374+
};
5375+
5376+
OMPLexicalScope Scope(*this, S, OMPD_unknown);
5377+
Builder.restoreIP(
5378+
OMPBuilder.createOrderedThreadsSimd(Builder, BodyGenCB, FiniCB, !C));
5379+
}
5380+
return;
5381+
}
5382+
53155383
if (S.hasClausesOfKind<OMPDependClause>()) {
53165384
assert(!S.hasAssociatedStmt() &&
53175385
"No associated statement must be in ordered depend construct.");

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,24 @@ class CodeGenFunction : public CodeGenTypeCache {
17751775
CGF.Builder.CreateBr(&FiniBB);
17761776
}
17771777

1778+
static void EmitCaptureStmt(CodeGenFunction &CGF, InsertPointTy CodeGenIP,
1779+
llvm::BasicBlock &FiniBB, llvm::Function *Fn,
1780+
ArrayRef<llvm::Value *> Args) {
1781+
llvm::BasicBlock *CodeGenIPBB = CodeGenIP.getBlock();
1782+
if (llvm::Instruction *CodeGenIPBBTI = CodeGenIPBB->getTerminator())
1783+
CodeGenIPBBTI->eraseFromParent();
1784+
1785+
CGF.Builder.SetInsertPoint(CodeGenIPBB);
1786+
1787+
if (Fn->doesNotThrow())
1788+
CGF.EmitNounwindRuntimeCall(Fn, Args);
1789+
else
1790+
CGF.EmitRuntimeCall(Fn, Args);
1791+
1792+
if (CGF.Builder.saveIP().isSet())
1793+
CGF.Builder.CreateBr(&FiniBB);
1794+
}
1795+
17781796
/// RAII for preserving necessary info during Outlined region body codegen.
17791797
class OutlinedRegionBodyRAII {
17801798

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5320,8 +5320,9 @@ static CapturedStmt *buildDistanceFunc(Sema &Actions, QualType LogicalTy,
53205320

53215321
if (Rel == BO_LE || Rel == BO_GE) {
53225322
// Add one to the range if the relational operator is inclusive.
5323-
Range =
5324-
AssertSuccess(Actions.BuildUnaryOp(nullptr, {}, UO_PreInc, Range));
5323+
Range = AssertSuccess(Actions.BuildBinOp(
5324+
nullptr, {}, BO_Add, Range,
5325+
Actions.ActOnIntegerConstant(SourceLocation(), 1).get()));
53255326
}
53265327

53275328
// Divide by the absolute step amount.

0 commit comments

Comments
 (0)