clang 24.0.0git
CIRGenStmtOpenMP.cpp File Reference
#include "CIRGenBuilder.h"
#include "CIRGenFunction.h"
#include "CIRGenOpenMPClause.h"
#include "CIRGenOpenMPConstructDecomposition.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "clang/AST/OpenMPClause.h"
#include "clang/AST/StmtOpenMP.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "llvm/Frontend/OpenMP/OMPConstants.h"
#include "llvm/Support/SaveAndRestore.h"
#include <type_traits>

Go to the source code of this file.

Functions

static mlir::LogicalResult checkSynthesizedClauses (CIRGenFunction &cgf, const OMPExecutableDirective &s, omp::ConstructQueue::const_iterator item)
 Report item's synthesized clauses as not-yet-implemented: having no AST node, they cannot go through the clause emitters.
static const OMPExecutableDirective * getSingleNestedOMPDirective (const OMPExecutableDirective &s)
 Returns s's nested OpenMP directive – its body after unwrapping any single-statement compounds – or null if the body isn't itself a single OpenMP directive.
static bool isCombinableLeaf (llvm::omp::Directive dir)
 Returns true if dir is a leaf construct kind that participates in CIR's "combined" op marking (see hasCombinableNestedLeaf).
static bool hasCombinableNestedLeaf (const OMPExecutableDirective &s)
 Returns true if s's body is itself an OpenMP directive that combines with s for codegen purposes, e.g.
static const OMPLoopDirective * findNestedOMPLoopDirective (const OMPExecutableDirective &s)
 Finds the loop directive nested (possibly transitively) inside s.
static bool isTargetSPMD (const OMPExecutableDirective &s)
 Returns true if s is a target SPMD construct: a combined target parallel for, or the same construct written with explicitly nested directives, e.g.
static mlir::LogicalResult emitParallelClauses (CIRGenFunction &cgf, CIRGenModule &cgm, CIRGenBuilderTy &builder, mlir::Location loc, llvm::ArrayRef< const OMPClause * > clauses, mlir::omp::ParallelOperands &clauseOps)
template<typename TermOp>
static mlir::LogicalResult emitOMPRegionBody (CIRGenFunction &cgf, mlir::Block &block, mlir::Location begin, mlir::Location end, llvm::function_ref< mlir::LogicalResult()> emitBody)
 Emits the contents of an OpenMP region op's block in a new lexical scope and terminates the block with a TermOp.
template<typename DirectiveTy>
static mlir::LogicalResult emitParallelOp (CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue, omp::ConstructQueue::const_iterator item, mlir::Location begin, mlir::Location end, const mlir::omp::ParallelOperands &clauseOps, llvm::function_ref< mlir::LogicalResult()> emitBody)
template<typename DirectiveTy>
static mlir::LogicalResult emitParallel (CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue, omp::ConstructQueue::const_iterator parallelItem, mlir::Location begin, mlir::Location end, llvm::function_ref< mlir::LogicalResult()> emitBody)
 Checks, emits clauses for, and emits an omp.parallel for the parallel directive at parallelItem, deferring to emitBody for the region's contents.
static mlir::LogicalResult emitLoopPreInit (CIRGenFunction &cgf, const Stmt *preInit)
 Emits a single Sema-generated pre-init statement for an OpenMP loop directive: a VarDecl's DeclStmt is lowered directly (it has no initializer CIRGenFunction::emitStmt can handle on its own), everything else goes through the general statement emitter.
static mlir::LogicalResult emitLoopPreInits (CIRGenFunction &cgf, const Stmt *preInits)
 Emits the Sema-generated pre-init statements for an OpenMP loop directive.
static llvm::SmallVector< const VarDecl * > getLoopPreInitVars (const Stmt *preInits)
 Returns the variables declared by the Sema-generated pre-init statements of an OpenMP loop directive (see emitLoopPreInits).
static const ContinueStmt * findLoopBodyContinue (const Stmt *s)
 Returns the first continue in s that is not nested in an inner loop, i.e.
static mlir::LogicalResult emitOMPLoopNest (CIRGenFunction &cgf, const ForStmt &forStmt, mlir::Value lb, mlir::Value ub, mlir::Value step, const VarDecl *ivDecl, const Expr *update)
 Emits an omp.loop_nest that iterates a normalized [0, tripCount) range and recomputes the real induction variable each iteration using the given update expression.
static mlir::LogicalResult emitWsloopClauses (CIRGenFunction &cgf, CIRGenModule &cgm, CIRGenBuilderTy &builder, mlir::Location loc, llvm::ArrayRef< const OMPClause * > clauses, mlir::omp::WsloopOperands &clauseOps)
 Evaluates the clauses allowed on the for directive; none are supported yet, so every eligible clause is reported as NYI.
static mlir::omp::PrivateClauseOp emitScalarPrivatizer (CIRGenModule &cgm, mlir::Location loc, llvm::StringRef baseName, mlir::Type elemTy)
 Creates a module-level omp.private op (a "privatizer") for a scalar of CIR type elemTy.
static Address emitLoopPrivateCounter (CIRGenFunction &cgf, CIRGenModule &cgm, mlir::Location loc, const VarDecl *privateVd, mlir::omp::PrivateClauseOps &clauseOps)
 Registers a loop counter as predetermined-private on the associated loop wrapper op.
static const ForStmt * getSupportedLoopForStmt (CIRGenFunction &cgf, const OMPLoopDirective &s)
 Returns the for loop associated with s, or reports NYI and returns null if the loop can't be emitted yet.
static mlir::Value emitLoopTripCount (CIRGenFunction &cgf, const OMPLoopDirective &s)
 Emits the trip count of the loop associated with s: Sema's iteration count if the loop's precondition holds, and zero otherwise.
static bool canHostEvalTripCount (CIRGenFunction &cgf, const OMPLoopDirective &s)
 Returns true if the trip count of the loop associated with s can be computed on the host and forwarded via host_eval (see emitHostEvalLoopBounds).
static std::optional< CIRGenFunction::OMPHostEvalBounds > emitHostEvalLoopBounds (CIRGenFunction &cgf, const OMPLoopDirective &s)
 Computes a target SPMD loop's normalized [0, tripCount) bounds at the host insertion point, as builtin-integer values for the enclosing omp.target's host_eval operands.
static mlir::LogicalResult emitOMPWorksharingLoop (CIRGenFunction &cgf, const OMPLoopDirective &s, omp::ConstructQueue::const_iterator item)
 Lowers an OMPLoopDirective's for directive to an omp.wsloop + omp.loop_nest.
static mlir::LogicalResult emitSingleClauses (CIRGenFunction &cgf, CIRGenModule &cgm, CIRGenBuilderTy &builder, mlir::Location loc, llvm::ArrayRef< const OMPClause * > clauses, mlir::omp::SingleOperands &clauseOps)
static mlir::LogicalResult emitMasterOp (CIRGenFunction &cgf, mlir::Location begin, mlir::Location end, const Stmt *body)
static mlir::omp::CriticalDeclareOp getOrCreateCriticalDeclare (CIRGenFunction &cgf, const OMPCriticalDirective &s, llvm::StringRef name, mlir::Location loc)
 Find or create the module-level declaration of the critical section name.
static mlir::LogicalResult emitOMPTargetImplicitCaptures (CIRGenFunction &cgf, const OMPExecutableDirective &s, llvm::ArrayRef< const VarDecl * > mapSyms)
 Check for unsupported implicit captures in a target region.
static mlir::LogicalResult emitTargetClauses (CIRGenFunction &cgf, CIRGenModule &cgm, CIRGenBuilderTy &builder, mlir::Location loc, llvm::ArrayRef< const OMPClause * > clauses, mlir::omp::TargetExtOperands &clauseOps, llvm::SmallVectorImpl< const VarDecl * > &mapSyms)
template<typename DirectiveTy>
static mlir::LogicalResult emitTargetOp (CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue, omp::ConstructQueue::const_iterator item, mlir::Location begin, mlir::Location end, mlir::omp::TargetExtOperands &clauseOps, llvm::ArrayRef< const VarDecl * > mapSyms, llvm::function_ref< mlir::LogicalResult()> emitBody)
template<typename DirectiveTy>
static mlir::LogicalResult emitTarget (CIRGenFunction &cgf, const DirectiveTy &s, const omp::ConstructQueue &queue, omp::ConstructQueue::const_iterator targetItem, mlir::Location begin, mlir::Location end, llvm::function_ref< mlir::LogicalResult()> emitBody)
 Checks, emits clauses for, and emits an omp.target for the target directive at targetItem, deferring to emitBody for the region's contents.

Function Documentation

◆ canHostEvalTripCount()

bool canHostEvalTripCount ( CIRGenFunction & cgf,
const OMPLoopDirective & s )
static

Returns true if the trip count of the loop associated with s can be computed on the host and forwarded via host_eval (see emitHostEvalLoopBounds).

The host evaluates the loop's bounds and step outside the omp.target, while the loop evaluates them again in its own context, e.g. inside a nested parallel region. Both only agree if none of these expressions has side effects; e.g. a step of omp_get_num_threads() differs between the two.

Definition at line 456 of file CIRGenStmtOpenMP.cpp.

References clang::CIRGen::CIRGenFunction::getContext(), getLoopPreInitVars(), OMPLoopDirective::getNumIterations(), OMPLoopDirective::getPreCond(), OMPLoopDirective::getPreInits(), and clang::Expr::HasSideEffects().

Referenced by emitTargetOp().

◆ checkSynthesizedClauses()

mlir::LogicalResult checkSynthesizedClauses ( CIRGenFunction & cgf,
const OMPExecutableDirective & s,
omp::ConstructQueue::const_iterator item )
static

◆ emitHostEvalLoopBounds()

std::optional< CIRGenFunction::OMPHostEvalBounds > emitHostEvalLoopBounds ( CIRGenFunction & cgf,
const OMPLoopDirective & s )
static

Computes a target SPMD loop's normalized [0, tripCount) bounds at the host insertion point, as builtin-integer values for the enclosing omp.target's host_eval operands.

All three are forwarded even though only the trip count is runtime-dependent, since the dialect requires an SPMD loop_nest's bounds to be host-evaluated in full.

The pre-init variables emitted here are only visible to the trip count computation. The loop inside the omp.target emits them again, since its isolated region can't use values defined outside of it.

Definition at line 476 of file CIRGenStmtOpenMP.cpp.

References cir::CIRBaseBuilderTy::createBuiltinIntCast(), emitLoopPreInits(), emitLoopTripCount(), clang::CIRGen::CIRGenFunction::getBuilder(), clang::CIRGen::CIRGenBuilderTy::getConstInt(), clang::CIRGen::CIRGenFunction::getLoc(), getLoopPreInitVars(), OMPLoopDirective::getPreInits(), clang::CIRGen::CIRGenFunction::OMPHostEvalBounds::one, clang::CIRGen::CIRGenFunction::symbolTable, clang::CIRGen::CIRGenFunction::OMPHostEvalBounds::tripCount, and clang::CIRGen::CIRGenFunction::OMPHostEvalBounds::zero.

Referenced by emitTargetOp().

◆ emitLoopPreInit()

mlir::LogicalResult emitLoopPreInit ( CIRGenFunction & cgf,
const Stmt * preInit )
static

Emits a single Sema-generated pre-init statement for an OpenMP loop directive: a VarDecl's DeclStmt is lowered directly (it has no initializer CIRGenFunction::emitStmt can handle on its own), everything else goes through the general statement emitter.

Definition at line 251 of file CIRGenStmtOpenMP.cpp.

References clang::cast(), clang::ast_matchers::declStmt, clang::CIRGen::CIRGenFunction::emitOMPNoLifetimeVarDecl(), and clang::CIRGen::CIRGenFunction::emitStmt().

Referenced by emitLoopPreInits().

◆ emitLoopPreInits()

mlir::LogicalResult emitLoopPreInits ( CIRGenFunction & cgf,
const Stmt * preInits )
static

Emits the Sema-generated pre-init statements for an OpenMP loop directive.

Definition at line 262 of file CIRGenStmtOpenMP.cpp.

References emitLoopPreInit(), and clang::ast_matchers::stmt.

Referenced by emitHostEvalLoopBounds(), and emitOMPWorksharingLoop().

◆ emitLoopPrivateCounter()

Address emitLoopPrivateCounter ( CIRGenFunction & cgf,
CIRGenModule & cgm,
mlir::Location loc,
const VarDecl * privateVd,
mlir::omp::PrivateClauseOps & clauseOps )
static

◆ emitLoopTripCount()

mlir::Value emitLoopTripCount ( CIRGenFunction & cgf,
const OMPLoopDirective & s )
static

Emits the trip count of the loop associated with s: Sema's iteration count if the loop's precondition holds, and zero otherwise.

The iteration count is only meaningful when the loop runs at least once; e.g. for for (unsigned i = 10; i < 0; i += 2) its arithmetic wraps to a positive value.

Definition at line 438 of file CIRGenStmtOpenMP.cpp.

References cir::CIRBaseBuilderTy::createSelect(), clang::CIRGen::CIRGenFunction::emitScalarExpr(), clang::CIRGen::CIRGenFunction::evaluateExprAsBool(), clang::CIRGen::CIRGenFunction::getBuilder(), clang::CIRGen::CIRGenBuilderTy::getConstInt(), clang::CIRGen::CIRGenFunction::getLoc(), OMPLoopDirective::getNumIterations(), and OMPLoopDirective::getPreCond().

Referenced by emitHostEvalLoopBounds(), and emitOMPWorksharingLoop().

◆ emitMasterOp()

mlir::LogicalResult emitMasterOp ( CIRGenFunction & cgf,
mlir::Location begin,
mlir::Location end,
const Stmt * body )
static

◆ emitOMPLoopNest()

mlir::LogicalResult emitOMPLoopNest ( CIRGenFunction & cgf,
const ForStmt & forStmt,
mlir::Value lb,
mlir::Value ub,
mlir::Value step,
const VarDecl * ivDecl,
const Expr * update )
static

Emits an omp.loop_nest that iterates a normalized [0, tripCount) range and recomputes the real induction variable each iteration using the given update expression.

Definition at line 313 of file CIRGenStmtOpenMP.cpp.

References emitOMPRegionBody(), clang::ast_matchers::forStmt, clang::CIRGen::CIRGenFunction::getBuilder(), clang::CIRGen::CIRGenFunction::getLoc(), and step().

Referenced by emitOMPWorksharingLoop().

◆ emitOMPRegionBody()

template<typename TermOp>
mlir::LogicalResult emitOMPRegionBody ( CIRGenFunction & cgf,
mlir::Block & block,
mlir::Location begin,
mlir::Location end,
llvm::function_ref< mlir::LogicalResult()> emitBody )
static

Emits the contents of an OpenMP region op's block in a new lexical scope and terminates the block with a TermOp.

The scope's cleanups (e.g. lifetime markers of locals declared in the region) are emitted before the terminator; destroying the scope instead would make it terminate the block with a cir.yield.

Definition at line 150 of file CIRGenStmtOpenMP.cpp.

References emitBody(), clang::CIRGen::CIRGenFunction::RunCleanupsScope::forceCleanup(), and clang::CIRGen::CIRGenFunction::getBuilder().

Referenced by emitMasterOp(), emitOMPLoopNest(), clang::CIRGen::CIRGenFunction::emitOMPSingleDirective(), emitParallelOp(), and emitTargetOp().

◆ emitOMPTargetImplicitCaptures()

mlir::LogicalResult emitOMPTargetImplicitCaptures ( CIRGenFunction & cgf,
const OMPExecutableDirective & s,
llvm::ArrayRef< const VarDecl * > mapSyms )
static

Check for unsupported implicit captures in a target region.

Definition at line 904 of file CIRGenStmtOpenMP.cpp.

References clang::CapturedStmt::captures(), clang::CIRGen::CIRGenModule::errorNYI(), and clang::CIRGen::CIRGenFunction::getCIRGenModule().

Referenced by emitTargetOp().

◆ emitOMPWorksharingLoop()

◆ emitParallel()

template<typename DirectiveTy>
mlir::LogicalResult emitParallel ( CIRGenFunction & cgf,
const DirectiveTy & s,
const omp::ConstructQueue & queue,
omp::ConstructQueue::const_iterator parallelItem,
mlir::Location begin,
mlir::Location end,
llvm::function_ref< mlir::LogicalResult()> emitBody )
static

◆ emitParallelClauses()

mlir::LogicalResult emitParallelClauses ( CIRGenFunction & cgf,
CIRGenModule & cgm,
CIRGenBuilderTy & builder,
mlir::Location loc,
llvm::ArrayRef< const OMPClause * > clauses,
mlir::omp::ParallelOperands & clauseOps )
static

◆ emitParallelOp()

template<typename DirectiveTy>
mlir::LogicalResult emitParallelOp ( CIRGenFunction & cgf,
const DirectiveTy & s,
const omp::ConstructQueue & queue,
omp::ConstructQueue::const_iterator item,
mlir::Location begin,
mlir::Location end,
const mlir::omp::ParallelOperands & clauseOps,
llvm::function_ref< mlir::LogicalResult()> emitBody )
static

◆ emitScalarPrivatizer()

mlir::omp::PrivateClauseOp emitScalarPrivatizer ( CIRGenModule & cgm,
mlir::Location loc,
llvm::StringRef baseName,
mlir::Type elemTy )
static

Creates a module-level omp.private op (a "privatizer") for a scalar of CIR type elemTy.

Definition at line 363 of file CIRGenStmtOpenMP.cpp.

References clang::CIRGen::CIRGenModule::getGlobalValue(), clang::CIRGen::CIRGenModule::getModule(), and clang::CIRGen::CIRGenModule::insertGlobalSymbol().

Referenced by emitLoopPrivateCounter().

◆ emitSingleClauses()

mlir::LogicalResult emitSingleClauses ( CIRGenFunction & cgf,
CIRGenModule & cgm,
CIRGenBuilderTy & builder,
mlir::Location loc,
llvm::ArrayRef< const OMPClause * > clauses,
mlir::omp::SingleOperands & clauseOps )
static

◆ emitTarget()

template<typename DirectiveTy>
mlir::LogicalResult emitTarget ( CIRGenFunction & cgf,
const DirectiveTy & s,
const omp::ConstructQueue & queue,
omp::ConstructQueue::const_iterator targetItem,
mlir::Location begin,
mlir::Location end,
llvm::function_ref< mlir::LogicalResult()> emitBody )
static

◆ emitTargetClauses()

mlir::LogicalResult emitTargetClauses ( CIRGenFunction & cgf,
CIRGenModule & cgm,
CIRGenBuilderTy & builder,
mlir::Location loc,
llvm::ArrayRef< const OMPClause * > clauses,
mlir::omp::TargetExtOperands & clauseOps,
llvm::SmallVectorImpl< const VarDecl * > & mapSyms )
static

◆ emitTargetOp()

◆ emitWsloopClauses()

mlir::LogicalResult emitWsloopClauses ( CIRGenFunction & cgf,
CIRGenModule & cgm,
CIRGenBuilderTy & builder,
mlir::Location loc,
llvm::ArrayRef< const OMPClause * > clauses,
mlir::omp::WsloopOperands & clauseOps )
static

Evaluates the clauses allowed on the for directive; none are supported yet, so every eligible clause is reported as NYI.

Definition at line 347 of file CIRGenStmtOpenMP.cpp.

References clang::CIRGen::OpenMPClauseEmitter::emitNYI().

Referenced by emitOMPWorksharingLoop().

◆ findLoopBodyContinue()

const ContinueStmt * findLoopBodyContinue ( const Stmt * s)
static

Returns the first continue in s that is not nested in an inner loop, i.e.

one that targets the loop whose body is s.

Definition at line 298 of file CIRGenStmtOpenMP.cpp.

References clang::Stmt::children(), clang::ast_matchers::continueStmt, findLoopBodyContinue(), and clang::isa().

Referenced by findLoopBodyContinue(), and getSupportedLoopForStmt().

◆ findNestedOMPLoopDirective()

const OMPLoopDirective * findNestedOMPLoopDirective ( const OMPExecutableDirective & s)
static

Finds the loop directive nested (possibly transitively) inside s.

Definition at line 93 of file CIRGenStmtOpenMP.cpp.

References findNestedOMPLoopDirective(), and getSingleNestedOMPDirective().

Referenced by emitTargetOp(), and findNestedOMPLoopDirective().

◆ getLoopPreInitVars()

llvm::SmallVector< const VarDecl * > getLoopPreInitVars ( const Stmt * preInits)
static

Returns the variables declared by the Sema-generated pre-init statements of an OpenMP loop directive (see emitLoopPreInits).

Definition at line 280 of file CIRGenStmtOpenMP.cpp.

References clang::cast(), clang::ast_matchers::declStmt, and clang::ast_matchers::stmt.

Referenced by canHostEvalTripCount(), and emitHostEvalLoopBounds().

◆ getOrCreateCriticalDeclare()

mlir::omp::CriticalDeclareOp getOrCreateCriticalDeclare ( CIRGenFunction & cgf,
const OMPCriticalDirective & s,
llvm::StringRef name,
mlir::Location loc )
static

Find or create the module-level declaration of the critical section name.

Returns null, after emitting an error, if the name is already used by another symbol or the hint cannot be represented.

Definition at line 719 of file CIRGenStmtOpenMP.cpp.

References clang::CIRGen::CIRGenModule::errorNYI(), clang::CIRGen::CIRGenFunction::getCIRGenModule(), clang::CIRGen::CIRGenFunction::getContext(), clang::CIRGen::CIRGenFunction::getMLIRContext(), clang::CIRGen::CIRGenModule::getModule(), and uint64_t.

Referenced by clang::CIRGen::CIRGenFunction::emitOMPCriticalDirective().

◆ getSingleNestedOMPDirective()

const OMPExecutableDirective * getSingleNestedOMPDirective ( const OMPExecutableDirective & s)
static

Returns s's nested OpenMP directive – its body after unwrapping any single-statement compounds – or null if the body isn't itself a single OpenMP directive.

Definition at line 60 of file CIRGenStmtOpenMP.cpp.

Referenced by findNestedOMPLoopDirective(), hasCombinableNestedLeaf(), and isTargetSPMD().

◆ getSupportedLoopForStmt()

const ForStmt * getSupportedLoopForStmt ( CIRGenFunction & cgf,
const OMPLoopDirective & s )
static

Returns the for loop associated with s, or reports NYI and returns null if the loop can't be emitted yet.

Checked before any op is created, so that a failure doesn't leave behind a loop wrapper or SPMD target without its omp.loop_nest.

Definition at line 409 of file CIRGenStmtOpenMP.cpp.

References clang::ast_matchers::continueStmt, clang::CIRGen::CIRGenModule::errorNYI(), findLoopBodyContinue(), clang::ast_matchers::forStmt, clang::CIRGen::CIRGenFunction::getCIRGenModule(), clang::Stmt::getSourceRange(), and clang::isa().

Referenced by emitOMPWorksharingLoop(), and emitTargetOp().

◆ hasCombinableNestedLeaf()

bool hasCombinableNestedLeaf ( const OMPExecutableDirective & s)
static

Returns true if s's body is itself an OpenMP directive that combines with s for codegen purposes, e.g.

explicitly-nested target + parallel written as separate pragmas rather than target parallel.

Definition at line 83 of file CIRGenStmtOpenMP.cpp.

References getSingleNestedOMPDirective(), and isCombinableLeaf().

Referenced by emitParallelOp(), and emitTargetOp().

◆ isCombinableLeaf()

bool isCombinableLeaf ( llvm::omp::Directive dir)
static

Returns true if dir is a leaf construct kind that participates in CIR's "combined" op marking (see hasCombinableNestedLeaf).

Definition at line 69 of file CIRGenStmtOpenMP.cpp.

Referenced by hasCombinableNestedLeaf().

◆ isTargetSPMD()

bool isTargetSPMD ( const OMPExecutableDirective & s)
static

Returns true if s is a target SPMD construct: a combined target parallel for, or the same construct written with explicitly nested directives, e.g.

a target whose body is a parallel whose body is a for.

Definition at line 106 of file CIRGenStmtOpenMP.cpp.

References getSingleNestedOMPDirective().

Referenced by emitTargetOp().