2014-05-06 18:08:46 +08:00
|
|
|
//===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code to emit OpenMP nodes as LLVM code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGOpenMPRuntime.h"
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
|
|
|
#include "clang/AST/Stmt.h"
|
|
|
|
#include "clang/AST/StmtOpenMP.h"
|
2014-09-30 13:29:28 +08:00
|
|
|
#include "TargetInfo.h"
|
2014-05-06 18:08:46 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// OpenMP Directive Emission
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
|
|
|
|
const CapturedStmt *CS = cast<CapturedStmt>(S.getAssociatedStmt());
|
|
|
|
llvm::Value *CapturedStruct = GenerateCapturedStmtArgument(*CS);
|
|
|
|
|
|
|
|
llvm::Value *OutlinedFn;
|
|
|
|
{
|
|
|
|
CodeGenFunction CGF(CGM, true);
|
|
|
|
CGCapturedStmtInfo CGInfo(*CS, CS->getCapturedRegionKind());
|
|
|
|
CGF.CapturedStmtInfo = &CGInfo;
|
2014-06-30 10:55:54 +08:00
|
|
|
OutlinedFn = CGF.GenerateCapturedStmtFunction(*CS);
|
2014-05-06 18:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
|
|
|
|
llvm::Value *Args[] = {
|
2014-06-18 15:08:49 +08:00
|
|
|
CGM.getOpenMPRuntime().EmitOpenMPUpdateLocation(*this, S.getLocStart()),
|
|
|
|
Builder.getInt32(1), // Number of arguments after 'microtask' argument
|
|
|
|
// (there is only one additional argument - 'context')
|
|
|
|
Builder.CreateBitCast(OutlinedFn,
|
|
|
|
CGM.getOpenMPRuntime().getKmpc_MicroPointerTy()),
|
|
|
|
EmitCastToVoidPtr(CapturedStruct)};
|
2014-05-06 18:08:46 +08:00
|
|
|
llvm::Constant *RTLFn = CGM.getOpenMPRuntime().CreateRuntimeFunction(
|
|
|
|
CGOpenMPRuntime::OMPRTL__kmpc_fork_call);
|
|
|
|
EmitRuntimeCall(RTLFn, Args);
|
|
|
|
}
|
2014-05-22 16:54:05 +08:00
|
|
|
|
2014-09-30 13:29:28 +08:00
|
|
|
static void EmitOMPAlignedClause(CodeGenFunction &CGF, CodeGenModule &CGM,
|
|
|
|
const OMPAlignedClause &Clause) {
|
|
|
|
unsigned ClauseAlignment = 0;
|
|
|
|
if (auto AlignmentExpr = Clause.getAlignment()) {
|
|
|
|
auto AlignmentCI =
|
|
|
|
cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr));
|
|
|
|
ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue());
|
|
|
|
}
|
|
|
|
for (auto E : Clause.varlists()) {
|
|
|
|
unsigned Alignment = ClauseAlignment;
|
|
|
|
if (Alignment == 0) {
|
|
|
|
// OpenMP [2.8.1, Description]
|
|
|
|
// If no optional parameter isspecified, implementation-defined default
|
|
|
|
// alignments for SIMD instructions on the target platforms are assumed.
|
|
|
|
Alignment = CGM.getTargetCodeGenInfo().getOpenMPSimdDefaultAlignment(
|
|
|
|
E->getType());
|
|
|
|
}
|
|
|
|
assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) &&
|
|
|
|
"alignment is not power of 2");
|
|
|
|
if (Alignment != 0) {
|
|
|
|
llvm::Value *PtrValue = CGF.EmitScalarExpr(E);
|
|
|
|
CGF.EmitAlignmentAssumption(PtrValue, Alignment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:54:05 +08:00
|
|
|
void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) {
|
|
|
|
const CapturedStmt *CS = cast<CapturedStmt>(S.getAssociatedStmt());
|
|
|
|
const Stmt *Body = CS->getCapturedStmt();
|
|
|
|
LoopStack.setParallel();
|
|
|
|
LoopStack.setVectorizerEnable(true);
|
|
|
|
for (auto C : S.clauses()) {
|
|
|
|
switch (C->getClauseKind()) {
|
|
|
|
case OMPC_safelen: {
|
|
|
|
RValue Len = EmitAnyExpr(cast<OMPSafelenClause>(C)->getSafelen(),
|
|
|
|
AggValueSlot::ignored(), true);
|
|
|
|
llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal());
|
|
|
|
LoopStack.setVectorizerWidth(Val->getZExtValue());
|
|
|
|
// In presence of finite 'safelen', it may be unsafe to mark all
|
|
|
|
// the memory instructions parallel, because loop-carried
|
|
|
|
// dependences of 'safelen' iterations are possible.
|
|
|
|
LoopStack.setParallel(false);
|
|
|
|
break;
|
|
|
|
}
|
2014-09-30 13:29:28 +08:00
|
|
|
case OMPC_aligned:
|
|
|
|
EmitOMPAlignedClause(*this, CGM, cast<OMPAlignedClause>(*C));
|
|
|
|
break;
|
2014-05-22 16:54:05 +08:00
|
|
|
default:
|
|
|
|
// Not handled yet
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EmitStmt(Body);
|
|
|
|
}
|
|
|
|
|
2014-06-18 12:14:57 +08:00
|
|
|
void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &) {
|
2014-06-25 19:44:49 +08:00
|
|
|
llvm_unreachable("CodeGen for 'omp for' is not supported yet.");
|
2014-06-18 12:14:57 +08:00
|
|
|
}
|
2014-06-25 19:44:49 +08:00
|
|
|
|
2014-09-18 13:12:34 +08:00
|
|
|
void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp for simd' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-25 19:44:49 +08:00
|
|
|
void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp sections' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-26 16:21:58 +08:00
|
|
|
void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp section' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-06-26 20:05:45 +08:00
|
|
|
void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp single' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-17 16:54:58 +08:00
|
|
|
void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp master' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-09-22 18:01:53 +08:00
|
|
|
void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
|
|
|
|
// __kmpc_critical();
|
|
|
|
// <captured_body>
|
|
|
|
// __kmpc_end_critical();
|
|
|
|
//
|
|
|
|
|
|
|
|
auto Lock = CGM.getOpenMPRuntime().GetCriticalRegionLock(
|
|
|
|
S.getDirectiveName().getAsString());
|
|
|
|
CGM.getOpenMPRuntime().EmitOMPCriticalRegionStart(*this, Lock,
|
|
|
|
S.getLocStart());
|
|
|
|
{
|
|
|
|
RunCleanupsScope Scope(*this);
|
|
|
|
EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt());
|
|
|
|
EnsureInsertPoint();
|
|
|
|
}
|
|
|
|
CGM.getOpenMPRuntime().EmitOMPCriticalRegionEnd(*this, Lock, S.getLocEnd());
|
2014-07-21 17:42:05 +08:00
|
|
|
}
|
|
|
|
|
2014-07-07 21:01:15 +08:00
|
|
|
void
|
|
|
|
CodeGenFunction::EmitOMPParallelForDirective(const OMPParallelForDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel for' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-09-23 17:33:00 +08:00
|
|
|
void CodeGenFunction::EmitOMPParallelForSimdDirective(
|
|
|
|
const OMPParallelForSimdDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel for simd' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-08 16:12:03 +08:00
|
|
|
void CodeGenFunction::EmitOMPParallelSectionsDirective(
|
|
|
|
const OMPParallelSectionsDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp parallel sections' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-11 19:25:16 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp task' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 15:47:19 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp taskyield' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 17:11:51 +08:00
|
|
|
void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp barrier' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-18 18:17:07 +08:00
|
|
|
void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp taskwait' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-21 19:26:11 +08:00
|
|
|
void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp flush' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-22 14:45:04 +08:00
|
|
|
void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp ordered' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-07-22 18:10:35 +08:00
|
|
|
void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp atomic' is not supported yet.");
|
|
|
|
}
|
|
|
|
|
2014-09-19 16:19:49 +08:00
|
|
|
void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) {
|
|
|
|
llvm_unreachable("CodeGen for 'omp target' is not supported yet.");
|
|
|
|
}
|
|
|
|
|