Skip to content

[OpenMPIRBuilder] Introduce OMPRegionInfo managing the stack of OpenMP region constructs. #130135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 52 commits into
base: users/meinersbur/irbuilder-extract
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
5d1b937
OMPRegionInfo
Meinersbur Apr 27, 2022
bac85df
Fix check-mlir
Meinersbur Apr 27, 2022
51296c7
sections
Meinersbur Apr 28, 2022
fa7166e
WIP
Meinersbur Apr 28, 2022
39fad43
try backport non-irbuilder region solution
Meinersbur Apr 28, 2022
ca1700e
try fix unittest
Meinersbur Apr 28, 2022
2ffb6c3
WIP
Meinersbur Apr 28, 2022
bb5c544
clang-format
Meinersbur Apr 28, 2022
c1ad3f1
OMPRegion with breaks
Meinersbur Apr 28, 2022
db7ea75
part fix sections
Meinersbur Apr 28, 2022
d625b12
combine joining finalizations
Meinersbur Apr 28, 2022
c38dea4
unittest fix
Meinersbur Apr 28, 2022
12d4334
avoid warning
Meinersbur Apr 28, 2022
c004d84
fix clang crash
Meinersbur Apr 28, 2022
23627ec
fix some tests
Meinersbur Apr 29, 2022
5f53403
fix other tests
Meinersbur Apr 29, 2022
45d91a2
clang-format
Meinersbur Apr 29, 2022
3f36680
simplify callback
Meinersbur Apr 29, 2022
87ef1ec
undo callback rename
Meinersbur Apr 29, 2022
3bfa8b4
some comments
Meinersbur Apr 29, 2022
feccee7
push/pop all regions
Meinersbur Apr 29, 2022
2e77b00
yerstay's work
Meinersbur May 1, 2022
47635fc
fixin cancellation
Meinersbur May 1, 2022
53e06f0
regen cancel
Meinersbur May 1, 2022
746fc7e
cleanup
Meinersbur May 1, 2022
2ac4ea2
clang-format
Meinersbur May 1, 2022
7be77dc
reformat
Meinersbur May 1, 2022
38ee9a4
WIP to fix polly
Meinersbur May 1, 2022
70cf8d7
Merge branch 'irbuilder-ompregion' into HEAD
Meinersbur May 1, 2022
06ec552
Try fix cancel
Meinersbur May 2, 2022
1bb0a35
cleanup cancel
Meinersbur May 2, 2022
7f7620d
fix inline region
Meinersbur May 2, 2022
76d309d
try fixing sections
Meinersbur May 2, 2022
d13d3ee
mlir test fix
Meinersbur May 2, 2022
45bccf8
codegen_master fix
Meinersbur May 2, 2022
b1367be
codegen_masted fix WIP
Meinersbur May 2, 2022
053a543
codegen_ordered fix WIP
Meinersbur May 2, 2022
1817688
codegen_critical fix
Meinersbur May 3, 2022
3537e89
codegen_cancel fix
Meinersbur May 3, 2022
1710516
clang-format
Meinersbur May 3, 2022
02f03f2
cleanup
Meinersbur May 3, 2022
b240549
cleanup
Meinersbur May 3, 2022
bcc9d0e
cleanup
Meinersbur May 3, 2022
317c40c
clang cleanup
Meinersbur May 3, 2022
0fce69b
remove PushAndPopStackRAII
Meinersbur May 3, 2022
4096a6e
remove createIRBuilderParallel
Meinersbur May 3, 2022
aef32c5
unify CodeGenFunction:: NonOpenMPIRBuilderRegion NonOmpBuilderScope(C…
Meinersbur May 3, 2022
f72c8dc
clang-format
Meinersbur May 3, 2022
f8198f7
remote OpenMPIrBuilder captureStmt type
Meinersbur May 3, 2022
482256d
cleanup
Meinersbur May 3, 2022
fdb6ddc
remove viewCFG
Meinersbur May 3, 2022
d3460d2
Merge branch 'main' into irbuilder-ompregion
Meinersbur May 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,51 +1184,6 @@ CGOpenMPRuntime::getUserDefinedReduction(const OMPDeclareReductionDecl *D) {
return UDRMap.lookup(D);
}

namespace {
// Temporary RAII solution to perform a push/pop stack event on the OpenMP IR
// Builder if one is present.
struct PushAndPopStackRAII {
PushAndPopStackRAII(llvm::OpenMPIRBuilder *OMPBuilder, CodeGenFunction &CGF,
bool HasCancel, llvm::omp::Directive Kind)
: OMPBuilder(OMPBuilder) {
if (!OMPBuilder)
return;

// The following callback is the crucial part of clangs cleanup process.
//
// NOTE:
// Once the OpenMPIRBuilder is used to create parallel regions (and
// similar), the cancellation destination (Dest below) is determined via
// IP. That means if we have variables to finalize we split the block at IP,
// use the new block (=BB) as destination to build a JumpDest (via
// getJumpDestInCurrentScope(BB)) which then is fed to
// EmitBranchThroughCleanup. Furthermore, there will not be the need
// to push & pop an FinalizationInfo object.
// The FiniCB will still be needed but at the point where the
// OpenMPIRBuilder is asked to construct a parallel (or similar) construct.
auto FiniCB = [&CGF](llvm::OpenMPIRBuilder::InsertPointTy IP) {
assert(IP.getBlock()->end() == IP.getPoint() &&
"Clang CG should cause non-terminated block!");
CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
CGF.Builder.restoreIP(IP);
CodeGenFunction::JumpDest Dest =
CGF.getOMPCancelDestination(OMPD_parallel);
CGF.EmitBranchThroughCleanup(Dest);
};

// TODO: Remove this once we emit parallel regions through the
// OpenMPIRBuilder as it can do this setup internally.
llvm::OpenMPIRBuilder::FinalizationInfo FI({FiniCB, Kind, HasCancel});
OMPBuilder->pushFinalizationCB(std::move(FI));
}
~PushAndPopStackRAII() {
if (OMPBuilder)
OMPBuilder->popFinalizationCB();
}
llvm::OpenMPIRBuilder *OMPBuilder;
};
} // namespace

static llvm::Function *emitParallelOrTeamsOutlinedFunction(
CodeGenModule &CGM, const OMPExecutableDirective &D, const CapturedStmt *CS,
const VarDecl *ThreadIDVar, OpenMPDirectiveKind InnermostKind,
Expand Down Expand Up @@ -1256,10 +1211,6 @@ static llvm::Function *emitParallelOrTeamsOutlinedFunction(
dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&D))
HasCancel = OPFD->hasCancel();

// TODO: Temporarily inform the OpenMPIRBuilder, if any, about the new
// parallel region to make cancellation barriers work properly.
llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
PushAndPopStackRAII PSR(&OMPBuilder, CGF, HasCancel, InnermostKind);
CGOpenMPOutlinedRegionInfo CGInfo(*CS, ThreadIDVar, CodeGen, InnermostKind,
HasCancel, OutlinedHelperName);
CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(CGF, &CGInfo);
Expand Down
47 changes: 30 additions & 17 deletions clang/lib/CodeGen/CGStmtOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,8 @@ static void emitCommonOMPParallelDirective(
CodeGenFunction &CGF, const OMPExecutableDirective &S,
OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen,
const CodeGenBoundParametersTy &CodeGenBoundParameters) {
CodeGenFunction::NonOpenMPIRBuilderRegion NonBuilderScope(CGF);

const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel);
llvm::Value *NumThreads = nullptr;
llvm::Function *OutlinedFn =
Expand Down Expand Up @@ -1713,7 +1715,8 @@ void CodeGenFunction::OMPBuilderCBHelpers::EmitOMPOutlinedRegionBody(
}

void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
if (CGM.getLangOpts().OpenMPIRBuilder) {
if (CGM.getLangOpts().OpenMPIRBuilder &&
!IsInsideNonOpenMPIRBuilderHandledRegion) {
llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
// Check if we have any if clause associated with the directive.
llvm::Value *IfCond = nullptr;
Expand Down Expand Up @@ -3729,6 +3732,8 @@ static void emitScanBasedDirective(
static bool emitWorksharingDirective(CodeGenFunction &CGF,
const OMPLoopDirective &S,
bool HasCancel) {
CodeGenFunction::NonOpenMPIRBuilderRegion NonOmpBuilderScope(CGF);

bool HasLastprivates;
if (llvm::any_of(S.getClausesOfKind<OMPReductionClause>(),
[](const OMPReductionClause *C) {
Expand Down Expand Up @@ -3906,6 +3911,8 @@ static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty,
}

void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) {
NonOpenMPIRBuilderRegion NonOmpBuilderScope(*this);

const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt();
const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt);
bool HasLastprivates = false;
Expand Down Expand Up @@ -4121,7 +4128,8 @@ void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
}

void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
if (CGM.getLangOpts().OpenMPIRBuilder) {
if (CGM.getLangOpts().OpenMPIRBuilder &&
!IsInsideNonOpenMPIRBuilderHandledRegion) {
llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;

Expand All @@ -4132,8 +4140,14 @@ void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {

auto BodyGenCB = [SectionRegionBodyStmt, this](InsertPointTy AllocaIP,
InsertPointTy CodeGenIP) {
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
*this, SectionRegionBodyStmt, AllocaIP, CodeGenIP, "section");
Builder.restoreIP(CodeGenIP);
llvm::BasicBlock *FiniBB =
splitBBWithSuffix(Builder, false, ".sectionfini");

OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, *FiniBB);
EmitStmt(SectionRegionBodyStmt);

Builder.CreateBr(FiniBB);
};

LexicalScope Scope(*this, S.getSourceRange());
Expand Down Expand Up @@ -4485,6 +4499,8 @@ void CodeGenFunction::EmitOMPTaskBasedDirective(
const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion,
const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen,
OMPTaskDataTy &Data) {
NonOpenMPIRBuilderRegion NonOmpBuilderScope(*this);

// Emit outlined function for task construct.
const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion);
auto I = CS->getCapturedDecl()->param_begin();
Expand Down Expand Up @@ -5027,6 +5043,8 @@ void CodeGenFunction::EmitOMPTargetTaskBasedDirective(
}

void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
NonOpenMPIRBuilderRegion NonOmpBuilderScope(*this);

// Emit outlined function for task construct.
const CapturedStmt *CS = S.getCapturedStmt(OMPD_task);
Address CapturedStruct = GenerateCapturedStmtArgument(*CS);
Expand All @@ -5044,6 +5062,7 @@ void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) {
// Check if we should emit tied or untied task.
Data.Tied = !S.getSingleClause<OMPUntiedClause>();
auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) {
NonOpenMPIRBuilderRegion NonOmpBuilderScope(CGF);
CGF.EmitStmt(CS->getCapturedStmt());
};
auto &&TaskGen = [&S, SharedsTy, CapturedStruct,
Expand Down Expand Up @@ -6928,20 +6947,14 @@ void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
break;
}
}
if (CGM.getLangOpts().OpenMPIRBuilder) {
if (CGM.getLangOpts().OpenMPIRBuilder &&
!IsInsideNonOpenMPIRBuilderHandledRegion) {
llvm::OpenMPIRBuilder &OMPBuilder = CGM.getOpenMPRuntime().getOMPBuilder();
// TODO: This check is necessary as we only generate `omp parallel` through
// the OpenMPIRBuilder for now.
if (S.getCancelRegion() == OMPD_parallel ||
S.getCancelRegion() == OMPD_sections ||
S.getCancelRegion() == OMPD_section) {
llvm::Value *IfCondition = nullptr;
if (IfCond)
IfCondition = EmitScalarExpr(IfCond,
/*IgnoreResultAssign=*/true);
return Builder.restoreIP(
OMPBuilder.createCancel(Builder, IfCondition, S.getCancelRegion()));
}
llvm::Value *IfCondition = nullptr;
if (IfCond)
IfCondition = EvaluateExprAsBool(IfCond);
return Builder.restoreIP(
OMPBuilder.createCancel(Builder, IfCondition, S.getCancelRegion()));
}

CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond,
Expand Down
37 changes: 29 additions & 8 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,32 @@ class CodeGenFunction : public CodeGenTypeCache {
~CGCapturedStmtRAII() { CGF.CapturedStmtInfo = PrevCapturedStmtInfo; }
};

/// While in a region handled by Clang's CGOpenMPRuntime, do not use the
/// OpenMPIRBuilder which requires all surrounding regions to be handled by
/// OpenMPIRBuilder as well.
///
/// Required until everything can be handled by OpenMPIRBuilder.
/// Isn't the ultimate solution to mixing OpenMPIRBuilder and
/// non-OpenMPIRBuilder codegen either, but works with the current regression
/// tests so far.
bool IsInsideNonOpenMPIRBuilderHandledRegion = false;
class NonOpenMPIRBuilderRegion {
private:
CodeGenFunction &CGF;
bool PreviousIsInsideNonOpenMPIRBuilderHandledRegion;

public:
NonOpenMPIRBuilderRegion(CodeGenFunction &CGF)
: CGF(CGF), PreviousIsInsideNonOpenMPIRBuilderHandledRegion(
CGF.IsInsideNonOpenMPIRBuilderHandledRegion) {
CGF.IsInsideNonOpenMPIRBuilderHandledRegion = true;
}
~NonOpenMPIRBuilderRegion() {
CGF.IsInsideNonOpenMPIRBuilderHandledRegion =
PreviousIsInsideNonOpenMPIRBuilderHandledRegion;
}
};

/// An abstract representation of regular/ObjC call/message targets.
class AbstractCallee {
/// The function declaration of the callee.
Expand Down Expand Up @@ -1776,16 +1802,11 @@ class CodeGenFunction : public CodeGenTypeCache {
/// \param IP Insertion point for generating the finalization code.
static void FinalizeOMPRegion(CodeGenFunction &CGF, InsertPointTy IP) {
CGBuilderTy::InsertPointGuard IPG(CGF.Builder);
assert(IP.getBlock()->end() != IP.getPoint() &&
"OpenMP IR Builder should cause terminated block!");

llvm::BasicBlock *IPBB = IP.getBlock();
llvm::BasicBlock *DestBB = IPBB->getUniqueSuccessor();
assert(DestBB && "Finalization block should have one successor!");
CGF.Builder.restoreIP(IP);
llvm::BasicBlock *DestBB =
llvm::splitBB(CGF.Builder, /*CreateBranch*/ false, ".ompfinalize");

// erase and replace with cleanup branch.
IPBB->getTerminator()->eraseFromParent();
CGF.Builder.SetInsertPoint(IPBB);
CodeGenFunction::JumpDest Dest = CGF.getJumpDestInCurrentScope(DestBB);
CGF.EmitBranchThroughCleanup(Dest);
}
Expand Down
Loading