171 changes: 171 additions & 0 deletions clang/lib/CodeGen/CGOpenMPRuntime.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//===----- CGOpenMPRuntime.h - Interface to OpenMP Runtimes -----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This provides a class for OpenMP runtime code generation.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CODEGEN_OPENMPRUNTIME_H
#define CLANG_CODEGEN_OPENMPRUNTIME_H

#include "clang/AST/Type.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"

namespace llvm {
class AllocaInst;
class CallInst;
class GlobalVariable;
class Constant;
class Function;
class Module;
class StructLayout;
class FunctionType;
class StructType;
class Type;
class Value;
}

namespace clang {

namespace CodeGen {

class CodeGenFunction;
class CodeGenModule;

class CGOpenMPRuntime {
public:
/// \brief Values for bit flags used in the ident_t to describe the fields.
/// All enumeric elements are named and described in accordance with the code
/// from http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
enum OpenMPLocationFlags {
/// \brief Use trampoline for internal microtask.
OMP_IDENT_IMD = 0x01,
/// \brief Use c-style ident structure.
OMP_IDENT_KMPC = 0x02,
/// \brief Atomic reduction option for kmpc_reduce.
OMP_ATOMIC_REDUCE = 0x10,
/// \brief Explicit 'barrier' directive.
OMP_IDENT_BARRIER_EXPL = 0x20,
/// \brief Implicit barrier in code.
OMP_IDENT_BARRIER_IMPL = 0x40,
/// \brief Implicit barrier in 'for' directive.
OMP_IDENT_BARRIER_IMPL_FOR = 0x40,
/// \brief Implicit barrier in 'sections' directive.
OMP_IDENT_BARRIER_IMPL_SECTIONS = 0xC0,
/// \brief Implicit barrier in 'single' directive.
OMP_IDENT_BARRIER_IMPL_SINGLE = 0x140
};
enum OpenMPRTLFunction {
// Call to void __kmpc_fork_call(ident_t *loc, kmp_int32 argc, kmpc_micro
// microtask, ...);
OMPRTL__kmpc_fork_call,
// Call to kmp_int32 kmpc_global_thread_num(ident_t *loc);
OMPRTL__kmpc_global_thread_num
};

private:
CodeGenModule &CGM;
/// \brief Default const ident_t object used for initialization of all other
/// ident_t objects.
llvm::Constant *DefaultOpenMPPSource;
llvm::Value *GetOrCreateDefaultOpenMPLocation(OpenMPLocationFlags Flags);
/// \brief Describes ident structure that describes a source location.
/// All descriptions are taken from
/// http://llvm.org/svn/llvm-project/openmp/trunk/runtime/src/kmp.h
/// Original structure:
/// typedef struct ident {
/// kmp_int32 reserved_1; /**< might be used in Fortran;
/// see above */
/// kmp_int32 flags; /**< also f.flags; KMP_IDENT_xxx flags;
/// KMP_IDENT_KMPC identifies this union
/// member */
/// kmp_int32 reserved_2; /**< not really used in Fortran any more;
/// see above */
///#if USE_ITT_BUILD
/// /* but currently used for storing
/// region-specific ITT */
/// /* contextual information. */
///#endif /* USE_ITT_BUILD */
/// kmp_int32 reserved_3; /**< source[4] in Fortran, do not use for
/// C++ */
/// char const *psource; /**< String describing the source location.
/// The string is composed of semi-colon separated
// fields which describe the source file,
/// the function and a pair of line numbers that
/// delimit the construct.
/// */
/// } ident_t;
enum IdentFieldIndex {
/// \brief might be used in Fortran
IdentField_Reserved_1,
/// \brief OMP_IDENT_xxx flags; OMP_IDENT_KMPC identifies this union member.
IdentField_Flags,
/// \brief Not really used in Fortran any more
IdentField_Reserved_2,
/// \brief Source[4] in Fortran, do not use for C++
IdentField_Reserved_3,
/// \brief String describing the source location. The string is composed of
/// semi-colon separated fields which describe the source file, the function
/// and a pair of line numbers that delimit the construct.
IdentField_PSource
};
llvm::StructType *IdentTy;
/// \brief The type for a microtask which gets passed to __kmpc_fork_call().
/// Original representation is:
/// typedef void (kmpc_micro)(kmp_int32 global_tid, kmp_int32 bound_tid,...);
llvm::FunctionType *Kmpc_MicroTy;
/// \brief Map of local debug location and functions.
typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPLocMapTy;
OpenMPLocMapTy OpenMPLocMap;
/// \brief Map of local gtid and functions.
typedef llvm::DenseMap<llvm::Function *, llvm::Value *> OpenMPGtidMapTy;
OpenMPGtidMapTy OpenMPGtidMap;

public:
CGOpenMPRuntime(CodeGenModule &CGM);
~CGOpenMPRuntime() {}

/// \brief Cleans up references to the objects in finished function.
/// \param CGF Reference to finished CodeGenFunction.
///
void FunctionFinished(CodeGenFunction &CGF);

/// \brief Emits object of ident_t type with info for source location.
/// \param CGF Reference to current CodeGenFunction.
/// \param Loc Clang source location.
/// \param Flags Flags for OpenMP location.
///
llvm::Value *
EmitOpenMPUpdateLocation(CodeGenFunction &CGF, SourceLocation Loc,
OpenMPLocationFlags Flags = OMP_IDENT_KMPC);

/// \brief Generates global thread number value.
/// \param CGF Reference to current CodeGenFunction.
/// \param Loc Clang source location.
///
llvm::Value *GetOpenMPGlobalThreadNum(CodeGenFunction &CGF,
SourceLocation Loc);

/// \brief Returns pointer to ident_t type;
llvm::Type *getIdentTyPointerTy();

/// \brief Returns pointer to kmpc_micro type;
llvm::Type *getKmpc_MicroPointerTy();

/// \brief Returns specified OpenMP runtime function.
/// \param Function OpenMP runtime function.
/// \return Specified function.
llvm::Constant *CreateRuntimeFunction(OpenMPRTLFunction Function);
};
}
}

#endif
10 changes: 9 additions & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ void CodeGenFunction::EmitStmt(const Stmt *S) {
case Stmt::SEHExceptStmtClass:
case Stmt::SEHFinallyStmtClass:
case Stmt::MSDependentExistsStmtClass:
case Stmt::OMPParallelDirectiveClass:
case Stmt::OMPSimdDirectiveClass:
llvm_unreachable("invalid statement class to emit generically");
case Stmt::NullStmtClass:
Expand Down Expand Up @@ -174,6 +173,9 @@ void CodeGenFunction::EmitStmt(const Stmt *S) {
case Stmt::SEHTryStmtClass:
EmitSEHTryStmt(cast<SEHTryStmt>(*S));
break;
case Stmt::OMPParallelDirectiveClass:
EmitOMPParallelDirective(cast<OMPParallelDirective>(*S));
break;
}
}

Expand Down Expand Up @@ -1921,6 +1923,12 @@ CodeGenFunction::EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K) {
return F;
}

llvm::Value *
CodeGenFunction::GenerateCapturedStmtArgument(const CapturedStmt &S) {
LValue CapStruct = InitCapturedStruct(*this, S);
return CapStruct.getAddress();
}

/// Creates the outlined function for a CapturedStmt.
llvm::Function *
CodeGenFunction::GenerateCapturedStmtFunction(const CapturedDecl *CD,
Expand Down
51 changes: 51 additions & 0 deletions clang/lib/CodeGen/CGStmtOpenMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//===--- 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"
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;
OutlinedFn = CGF.GenerateCapturedStmtFunction(
CS->getCapturedDecl(), CS->getCapturedRecordDecl(), CS->getLocStart());
}

// Build call __kmpc_fork_call(loc, 1, microtask, captured_struct/*context*/)
llvm::Value *Args[] = {
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)
};
llvm::Constant *RTLFn = CGM.getOpenMPRuntime().CreateRuntimeFunction(
CGOpenMPRuntime::OMPRTL__kmpc_fork_call);
EmitRuntimeCall(RTLFn, Args);
}
2 changes: 2 additions & 0 deletions clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ add_clang_library(clangCodeGen
CGObjCMac.cpp
CGObjCRuntime.cpp
CGOpenCLRuntime.cpp
CGOpenMPRuntime.cpp
CGRTTI.cpp
CGRecordLayoutBuilder.cpp
CGStmt.cpp
CGStmtOpenMP.cpp
CGVTT.cpp
CGVTables.cpp
CodeGenABITypes.cpp
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "CGCUDARuntime.h"
#include "CGCXXABI.h"
#include "CGDebugInfo.h"
#include "CGOpenMPRuntime.h"
#include "CodeGenModule.h"
#include "CodeGenPGO.h"
#include "TargetInfo.h"
Expand Down Expand Up @@ -72,6 +73,10 @@ CodeGenFunction::~CodeGenFunction() {
// something.
if (FirstBlockInfo)
destroyBlockInfos(FirstBlockInfo);

if (getLangOpts().OpenMP) {
CGM.getOpenMPRuntime().FunctionFinished(*this);
}
}


Expand Down
3 changes: 3 additions & 0 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,9 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Function *GenerateCapturedStmtFunction(const CapturedDecl *CD,
const RecordDecl *RD,
SourceLocation Loc);
llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);

void EmitOMPParallelDirective(const OMPParallelDirective &S);

//===--------------------------------------------------------------------===//
// LValue Expression Emission
Expand Down
12 changes: 10 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "CGDebugInfo.h"
#include "CGObjCRuntime.h"
#include "CGOpenCLRuntime.h"
#include "CGOpenMPRuntime.h"
#include "CodeGenFunction.h"
#include "CodeGenPGO.h"
#include "CodeGenTBAA.h"
Expand Down Expand Up @@ -78,8 +79,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
Diags(diags), TheDataLayout(TD), Target(C.getTargetInfo()),
ABI(createCXXABI(*this)), VMContext(M.getContext()), TBAA(0),
TheTargetCodeGenInfo(0), Types(*this), VTables(*this), ObjCRuntime(0),
OpenCLRuntime(0), CUDARuntime(0), DebugInfo(0), ARCData(0),
NoObjCARCExceptionsMetadata(0), RRData(0), PGOReader(nullptr),
OpenCLRuntime(0), OpenMPRuntime(nullptr), CUDARuntime(0), DebugInfo(0),
ARCData(0), NoObjCARCExceptionsMetadata(0), RRData(0), PGOReader(nullptr),
CFConstantStringClassRef(0),
ConstantStringClassRef(0), NSConstantStringType(0),
NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockObjectAssign(0),
Expand Down Expand Up @@ -113,6 +114,8 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
createObjCRuntime();
if (LangOpts.OpenCL)
createOpenCLRuntime();
if (LangOpts.OpenMP)
createOpenMPRuntime();
if (LangOpts.CUDA)
createCUDARuntime();

Expand Down Expand Up @@ -148,6 +151,7 @@ CodeGenModule::CodeGenModule(ASTContext &C, const CodeGenOptions &CGO,
CodeGenModule::~CodeGenModule() {
delete ObjCRuntime;
delete OpenCLRuntime;
delete OpenMPRuntime;
delete CUDARuntime;
delete TheTargetCodeGenInfo;
delete TBAA;
Expand Down Expand Up @@ -179,6 +183,10 @@ void CodeGenModule::createOpenCLRuntime() {
OpenCLRuntime = new CGOpenCLRuntime(*this);
}

void CodeGenModule::createOpenMPRuntime() {
OpenMPRuntime = new CGOpenMPRuntime(*this);
}

void CodeGenModule::createCUDARuntime() {
CUDARuntime = CreateNVCUDARuntime(*this);
}
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ namespace CodeGen {
class CGDebugInfo;
class CGObjCRuntime;
class CGOpenCLRuntime;
class CGOpenMPRuntime;
class CGCUDARuntime;
class BlockFieldFlags;
class FunctionArgList;
Expand Down Expand Up @@ -261,6 +262,7 @@ class CodeGenModule : public CodeGenTypeCache {

CGObjCRuntime* ObjCRuntime;
CGOpenCLRuntime* OpenCLRuntime;
CGOpenMPRuntime* OpenMPRuntime;
CGCUDARuntime* CUDARuntime;
CGDebugInfo* DebugInfo;
ARCEntrypoints *ARCData;
Expand Down Expand Up @@ -414,6 +416,7 @@ class CodeGenModule : public CodeGenTypeCache {
void createObjCRuntime();

void createOpenCLRuntime();
void createOpenMPRuntime();
void createCUDARuntime();

bool isTriviallyRecursive(const FunctionDecl *F);
Expand Down Expand Up @@ -477,6 +480,12 @@ class CodeGenModule : public CodeGenTypeCache {
return *OpenCLRuntime;
}

/// getOpenMPRuntime() - Return a reference to the configured OpenMP runtime.
CGOpenMPRuntime &getOpenMPRuntime() {
assert(OpenMPRuntime != nullptr);
return *OpenMPRuntime;
}

/// getCUDARuntime() - Return a reference to the configured CUDA runtime.
CGCUDARuntime &getCUDARuntime() {
assert(CUDARuntime != 0);
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Parse/ParseOpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
///
//===----------------------------------------------------------------------===//

#include "clang/AST/ASTConsumer.h"
#include "RAIIObjectsForParser.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/StmtOpenMP.h"
#include "clang/Parse/ParseDiagnostic.h"
#include "clang/Parse/Parser.h"
Expand Down Expand Up @@ -148,7 +149,7 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective() {
{
// The body is a block scope like in Lambdas and Blocks.
Sema::CompoundScopeRAII CompoundScope(Actions);
Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_OpenMP, 1);
Actions.ActOnOpenMPRegionStart(DKind, Loc, getCurScope());
Actions.ActOnStartOfCompoundStmt();
// Parse statement
AssociatedStmt = ParseStatement();
Expand Down
45 changes: 43 additions & 2 deletions clang/lib/Sema/SemaOpenMP.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ----------===//
//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
//
// The LLVM Compiler Infrastructure
//
Expand All @@ -12,13 +12,15 @@
///
//===----------------------------------------------------------------------===//

#include "clang/Basic/OpenMPKinds.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclOpenMP.h"
#include "clang/AST/StmtCXX.h"
#include "clang/AST/StmtOpenMP.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/Basic/OpenMPKinds.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Initialization.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Scope.h"
Expand Down Expand Up @@ -669,6 +671,36 @@ class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
};
}

void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, SourceLocation Loc,
Scope *CurScope) {
switch (DKind) {
case OMPD_parallel: {
QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Sema::CapturedParamNameType Params[3] = {
std::make_pair(".global_tid.", KmpInt32PtrTy),
std::make_pair(".bound_tid.", KmpInt32PtrTy),
std::make_pair(StringRef(), QualType()) // __context with shared vars
};
ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
break;
}
case OMPD_simd: {
Sema::CapturedParamNameType Params[1] = {
std::make_pair(StringRef(), QualType()) // __context with shared vars
};
ActOnCapturedRegionStart(Loc, CurScope, CR_OpenMP, Params);
break;
}
case OMPD_threadprivate:
case OMPD_task:
llvm_unreachable("OpenMP Directive is not allowed");
case OMPD_unknown:
case NUM_OPENMP_DIRECTIVES:
llvm_unreachable("Unknown OpenMP directive");
}
}

StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
ArrayRef<OMPClause *> Clauses,
Stmt *AStmt,
Expand Down Expand Up @@ -725,6 +757,15 @@ StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
Stmt *AStmt,
SourceLocation StartLoc,
SourceLocation EndLoc) {
assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
CapturedStmt *CS = cast<CapturedStmt>(AStmt);
// 1.2.2 OpenMP Language Terminology
// Structured block - An executable statement with a single entry at the
// top and a single exit at the bottom.
// The point of exit cannot be a branch out of the structured block.
// longjmp() and throw() must not violate the entry/exit criteria.
CS->getCapturedDecl()->setNothrow();

getCurFunction()->setHasBranchProtectedScope();

return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
Expand Down
72 changes: 57 additions & 15 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3322,20 +3322,9 @@ Sema::CreateCapturedStmtRecordDecl(CapturedDecl *&CD, SourceLocation Loc,
RD->setImplicit();
RD->startDefinition();

assert(NumParams > 0 && "CapturedStmt requires context parameter");
CD = CapturedDecl::Create(Context, CurContext, NumParams);
DC->addDecl(CD);

// Build the context parameter
assert(NumParams > 0 && "CapturedStmt requires context parameter");
DC = CapturedDecl::castToDeclContext(CD);
IdentifierInfo *VarName = &Context.Idents.get("__context");
QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD));
ImplicitParamDecl *Param
= ImplicitParamDecl::Create(Context, DC, Loc, VarName, ParamType);
DC->addDecl(Param);

CD->setContextParam(Param);

return RD;
}

Expand Down Expand Up @@ -3367,9 +3356,62 @@ static void buildCapturedStmtCaptureList(
void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope,
CapturedRegionKind Kind,
unsigned NumParams) {
CapturedDecl *CD = 0;
CapturedDecl *CD = nullptr;
RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, NumParams);

// Build the context parameter
DeclContext *DC = CapturedDecl::castToDeclContext(CD);
IdentifierInfo *ParamName = &Context.Idents.get("__context");
QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD));
ImplicitParamDecl *Param
= ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType);
DC->addDecl(Param);

CD->setContextParam(0, Param);

// Enter the capturing scope for this captured region.
PushCapturedRegionScope(CurScope, CD, RD, Kind);

if (CurScope)
PushDeclContext(CurScope, CD);
else
CurContext = CD;

PushExpressionEvaluationContext(PotentiallyEvaluated);
}

void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope,
CapturedRegionKind Kind,
ArrayRef<CapturedParamNameType> Params) {
CapturedDecl *CD = nullptr;
RecordDecl *RD = CreateCapturedStmtRecordDecl(CD, Loc, Params.size());

// Build the context parameter
DeclContext *DC = CapturedDecl::castToDeclContext(CD);
bool ContextIsFound = false;
unsigned ParamNum = 0;
for (ArrayRef<CapturedParamNameType>::iterator I = Params.begin(),
E = Params.end();
I != E; ++I, ++ParamNum) {
if (I->second.isNull()) {
assert(!ContextIsFound &&
"null type has been found already for '__context' parameter");
IdentifierInfo *ParamName = &Context.Idents.get("__context");
QualType ParamType = Context.getPointerType(Context.getTagDeclType(RD));
ImplicitParamDecl *Param
= ImplicitParamDecl::Create(Context, DC, Loc, ParamName, ParamType);
DC->addDecl(Param);
CD->setContextParam(ParamNum, Param);
ContextIsFound = true;
} else {
IdentifierInfo *ParamName = &Context.Idents.get(I->first);
ImplicitParamDecl *Param
= ImplicitParamDecl::Create(Context, DC, Loc, ParamName, I->second);
DC->addDecl(Param);
CD->setParam(ParamNum, Param);
}
}
assert(ContextIsFound && "no null type for '__context' parameter");
// Enter the capturing scope for this captured region.
PushCapturedRegionScope(CurScope, CD, RD, Kind);

Expand All @@ -3390,8 +3432,8 @@ void Sema::ActOnCapturedRegionError() {
Record->setInvalidDecl();

SmallVector<Decl*, 4> Fields(Record->fields());
ActOnFields(/*Scope=*/0, Record->getLocation(), Record, Fields,
SourceLocation(), SourceLocation(), /*AttributeList=*/0);
ActOnFields(/*Scope=*/nullptr, Record->getLocation(), Record, Fields,
SourceLocation(), SourceLocation(), /*AttributeList=*/nullptr);

PopDeclContext();
PopFunctionScopeInfo();
Expand Down
19 changes: 16 additions & 3 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -6326,7 +6326,7 @@ TreeTransform<Derived>::TransformOMPExecutableDirective(
TClauses.push_back(Clause);
}
else {
TClauses.push_back(0);
TClauses.push_back(nullptr);
}
}
if (!D->getAssociatedStmt()) {
Expand Down Expand Up @@ -9911,9 +9911,22 @@ template<typename Derived>
StmtResult
TreeTransform<Derived>::TransformCapturedStmt(CapturedStmt *S) {
SourceLocation Loc = S->getLocStart();
unsigned NumParams = S->getCapturedDecl()->getNumParams();
CapturedDecl *CD = S->getCapturedDecl();
unsigned NumParams = CD->getNumParams();
unsigned ContextParamPos = CD->getContextParamPosition();
SmallVector<Sema::CapturedParamNameType, 4> Params;
for (unsigned I = 0; I < NumParams; ++I) {
if (I != ContextParamPos) {
Params.push_back(
std::make_pair(
CD->getParam(I)->getName(),
getDerived().TransformType(CD->getParam(I)->getType())));
} else {
Params.push_back(std::make_pair(StringRef(), QualType()));
}
}
getSema().ActOnCapturedRegionStart(Loc, /*CurScope*/0,
S->getCapturedRegionKind(), NumParams);
S->getCapturedRegionKind(), Params);
StmtResult Body = getDerived().TransformStmt(S->getCapturedStmt());

if (Body.isInvalid()) {
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1092,9 +1092,15 @@ void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {

void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
VisitDecl(CD);
unsigned ContextParamPos = Record[Idx++];
CD->setNothrow(Record[Idx++] != 0);
// Body is set by VisitCapturedStmt.
for (unsigned i = 0; i < CD->NumParams; ++i)
CD->setParam(i, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
for (unsigned I = 0; I < CD->NumParams; ++I) {
if (I != ContextParamPos)
CD->setParam(I, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
else
CD->setContextParam(I, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
}
}

void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Serialization/ASTWriterDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,11 @@ void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
Record.push_back(CD->getNumParams());
VisitDecl(CD);
Record.push_back(CD->getContextParamPosition());
Record.push_back(CD->isNothrow() ? 1 : 0);
// Body is stored by VisitCapturedStmt.
for (unsigned i = 0; i < CD->getNumParams(); ++i)
Writer.AddDeclRef(CD->getParam(i), Record);
for (unsigned I = 0; I < CD->getNumParams(); ++I)
Writer.AddDeclRef(CD->getParam(I), Record);
Code = serialization::DECL_CAPTURED;
}

Expand Down
146 changes: 146 additions & 0 deletions clang/test/OpenMP/parallel_codegen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// RUN: %clang_cc1 -verify -fopenmp=libiomp5 -x c++ -emit-llvm %s -fexceptions -fcxx-exceptions -o - | FileCheck %s
// RUN: %clang_cc1 -fopenmp=libiomp5 -x c++ -std=c++11 -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -emit-pch -o %t %s
// RUN: %clang_cc1 -fopenmp=libiomp5 -x c++ -triple x86_64-unknown-unknown -fexceptions -fcxx-exceptions -g -std=c++11 -include-pch %t -verify %s -emit-llvm -o - | FileCheck --check-prefix=CHECK-DEBUG %s
// expected-no-diagnostics

#ifndef HEADER
#define HEADER

// CHECK-DAG: %ident_t = type { i32, i32, i32, i32, i8* }
// CHECK-DAG: %struct.anon = type { i32* }
// CHECK-DAG: %struct.anon.0 = type { i8*** }
// CHECK-DAG: @.str = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00"
// CHECK-DAG: @.kmpc_default_loc_2.addr = private unnamed_addr constant %ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8]* @.str, i32 0, i32 0) }
// CHECK-DEBUG-DAG: %ident_t = type { i32, i32, i32, i32, i8* }
// CHECK-DEBUG-DAG: %struct.anon = type { i32* }
// CHECK-DEBUG-DAG: %struct.anon.0 = type { i8*** }
// CHECK-DEBUG-DAG: @.str = private unnamed_addr constant [23 x i8] c";unknown;unknown;0;0;;\00"
// CHECK-DEBUG-DAG: @.kmpc_default_loc_2.addr = private unnamed_addr constant %ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([23 x i8]* @.str, i32 0, i32 0) }
// CHECK-DEBUG-DAG: [[LOC1:@.+]] = private unnamed_addr constant [{{.+}} x i8] c";{{.*}}parallel_codegen.cpp;main;[[@LINE+14]];9;;\00"
// CHECK-DEBUG-DAG: [[LOC2:@.+]] = private unnamed_addr constant [{{.+}} x i8] c";{{.*}}parallel_codegen.cpp;tmain;[[@LINE+7]];9;;\00"

template <class T>
void foo(T argc) {}

template <typename T>
int tmain(T argc) {
#pragma omp parallel
foo(argc);
return 0;
}

int main (int argc, char **argv) {
#pragma omp parallel
foo(argc);
return tmain(argv);
}

// CHECK-LABEL: define i32 @main(i32 %argc, i8** %argv)
// CHECK: %agg.captured = alloca %struct.anon
// CHECK: [[ARGC_REF:%.+]] = getelementptr inbounds %struct.anon* %agg.captured, i32 0, i32 0
// CHECK-NEXT: store i32* %argc.addr, i32** [[ARGC_REF]]
// CHECK-NEXT: [[BITCAST:%.+]] = bitcast %struct.anon* %agg.captured to i8*
// CHECK-NEXT: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...)* @__kmpc_fork_call(%ident_t* @.kmpc_default_loc_2.addr, i32 1, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, %struct.anon*)* @__captured_stmt to void (i32*, i32*, ...)*), i8* [[BITCAST]])
// CHECK-NEXT: [[ARGV:%.+]] = load i8*** %argv.addr, align 8
// CHECK-NEXT: [[RET:%.+]] = call i32 @_Z5tmainIPPcEiT_(i8** [[ARGV]])
// CHECK-NEXT: ret i32 [[RET]]
// CHECK-NEXT: }
// CHECK-DEBUG-LABEL: define i32 @main(i32 %argc, i8** %argv)
// CHECK-DEBUG-DAG: %agg.captured = alloca %struct.anon
// CHECK-DEBUG-DAG: %.kmpc_loc_2.addr = alloca %ident_t
// CHECK-DEBUG: [[KMPC_LOC_VOIDPTR:%.+]] = bitcast %ident_t* %.kmpc_loc_2.addr to i8*
// CHECK-DEBUG-NEXT: [[KMPC_DEFAULT_LOC_VOIDPTR:%.+]] = bitcast %ident_t* @.kmpc_default_loc_2.addr to i8*
// CHECK-DEBUG-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[KMPC_LOC_VOIDPTR]], i8* [[KMPC_DEFAULT_LOC_VOIDPTR]], i64 ptrtoint (%ident_t* getelementptr (%ident_t* null, i32 1) to i64), i32 8, i1 false)
// CHECK-DEBUG: [[ARGC_REF:%.+]] = getelementptr inbounds %struct.anon* %agg.captured, i32 0, i32 0
// CHECK-DEBUG-NEXT: store i32* %argc.addr, i32** [[ARGC_REF]]
// CHECK-DEBUG-NEXT: [[KMPC_LOC_PSOURCE_REF:%.+]] = getelementptr inbounds %ident_t* %.kmpc_loc_2.addr, i32 0, i32 4
// CHECK-DEBUG-NEXT: store i8* getelementptr inbounds ([{{.+}} x i8]* [[LOC1]], i32 0, i32 0), i8** [[KMPC_LOC_PSOURCE_REF]]
// CHECK-DEBUG-NEXT: [[BITCAST:%.+]] = bitcast %struct.anon* %agg.captured to i8*
// CHECK-DEBUG-NEXT: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...)* @__kmpc_fork_call(%ident_t* %.kmpc_loc_2.addr, i32 1, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, %struct.anon*)* @__captured_stmt to void (i32*, i32*, ...)*), i8* [[BITCAST]])
// CHECK-DEBUG-NEXT: [[ARGV:%.+]] = load i8*** %argv.addr, align 8
// CHECK-DEBUG-NEXT: [[RET:%.+]] = call i32 @_Z5tmainIPPcEiT_(i8** [[ARGV]])
// CHECK-DEBUG-NEXT: ret i32 [[RET]]
// CHECK-DEBUG-NEXT: }

// CHECK-LABEL: define internal void @__captured_stmt(i32* %.global_tid., i32* %.bound_tid., %struct.anon* %__context)
// CHECK: %__context.addr = alloca %struct.anon*
// CHECK: store %struct.anon* %__context, %struct.anon** %__context.addr
// CHECK-NEXT: [[CONTEXT_PTR:%.+]] = load %struct.anon** %__context.addr
// CHECK-NEXT: [[ARGC_PTR_REF:%.+]] = getelementptr inbounds %struct.anon* [[CONTEXT_PTR]], i32 0, i32 0
// CHECK-NEXT: [[ARGC_REF:%.+]] = load i32** [[ARGC_PTR_REF]]
// CHECK-NEXT: [[ARGC:%.+]] = load i32* [[ARGC_REF]]
// CHECK-NEXT: invoke void @_Z3fooIiEvT_(i32 [[ARGC]])
// CHECK: ret void
// CHECK: call void @__clang_call_terminate(i8*
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
// CHECK-DEBUG-LABEL: define internal void @__captured_stmt(i32* %.global_tid., i32* %.bound_tid., %struct.anon* %__context)
// CHECK-DEBUG: %__context.addr = alloca %struct.anon*
// CHECK-DEBUG: store %struct.anon* %__context, %struct.anon** %__context.addr
// CHECK-DEBUG: [[CONTEXT_PTR:%.+]] = load %struct.anon** %__context.addr
// CHECK-DEBUG-NEXT: [[ARGC_PTR_REF:%.+]] = getelementptr inbounds %struct.anon* [[CONTEXT_PTR]], i32 0, i32 0
// CHECK-DEBUG-NEXT: [[ARGC_REF:%.+]] = load i32** [[ARGC_PTR_REF]]
// CHECK-DEBUG-NEXT: [[ARGC:%.+]] = load i32* [[ARGC_REF]]
// CHECK-DEBUG-NEXT: invoke void @_Z3fooIiEvT_(i32 [[ARGC]])
// CHECK-DEBUG: ret void
// CHECK-DEBUG: call void @__clang_call_terminate(i8*
// CHECK-DEBUG-NEXT: unreachable
// CHECK-DEBUG-NEXT: }

// CHECK-DAG: define linkonce_odr void @_Z3fooIiEvT_(i32 %argc)
// CHECK-DAG: declare void @__kmpc_fork_call(%ident_t*, i32, void (i32*, i32*, ...)*, ...)
// CHECK-DEBUG-DAG: define linkonce_odr void @_Z3fooIiEvT_(i32 %argc)
// CHECK-DEBUG-DAG: declare void @__kmpc_fork_call(%ident_t*, i32, void (i32*, i32*, ...)*, ...)

// CHECK-LABEL: define linkonce_odr i32 @_Z5tmainIPPcEiT_(i8** %argc)
// CHECK: %agg.captured = alloca %struct.anon.0
// CHECK: [[ARGC_REF:%.+]] = getelementptr inbounds %struct.anon.0* %agg.captured, i32 0, i32 0
// CHECK-NEXT: store i8*** %argc.addr, i8**** [[ARGC_REF]]
// CHECK-NEXT: [[BITCAST:%.+]] = bitcast %struct.anon.0* %agg.captured to i8*
// CHECK-NEXT: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...)* @__kmpc_fork_call(%ident_t* @.kmpc_default_loc_2.addr, i32 1, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, %struct.anon.0*)* @__captured_stmt1 to void (i32*, i32*, ...)*), i8* [[BITCAST]])
// CHECK-NEXT: ret i32 0
// CHECK-NEXT: }
// CHECK-DEBUG-LABEL: define linkonce_odr i32 @_Z5tmainIPPcEiT_(i8** %argc)
// CHECK-DEBUG-DAG: %agg.captured = alloca %struct.anon.0
// CHECK-DEBUG-DAG: %.kmpc_loc_2.addr = alloca %ident_t
// CHECK-DEBUG: [[KMPC_LOC_VOIDPTR:%.+]] = bitcast %ident_t* %.kmpc_loc_2.addr to i8*
// CHECK-DEBUG-NEXT: [[KMPC_DEFAULT_LOC_VOIDPTR:%.+]] = bitcast %ident_t* @.kmpc_default_loc_2.addr to i8*
// CHECK-DEBUG-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[KMPC_LOC_VOIDPTR]], i8* [[KMPC_DEFAULT_LOC_VOIDPTR]], i64 ptrtoint (%ident_t* getelementptr (%ident_t* null, i32 1) to i64), i32 8, i1 false)
// CHECK-DEBUG: [[ARGC_REF:%.+]] = getelementptr inbounds %struct.anon.0* %agg.captured, i32 0, i32 0
// CHECK-DEBUG-NEXT: store i8*** %argc.addr, i8**** [[ARGC_REF]]
// CHECK-DEBUG-NEXT: [[KMPC_LOC_PSOURCE_REF:%.+]] = getelementptr inbounds %ident_t* %.kmpc_loc_2.addr, i32 0, i32 4
// CHECK-DEBUG-NEXT: store i8* getelementptr inbounds ([{{.+}} x i8]* [[LOC2]], i32 0, i32 0), i8** [[KMPC_LOC_PSOURCE_REF]]
// CHECK-DEBUG-NEXT: [[BITCAST:%.+]] = bitcast %struct.anon.0* %agg.captured to i8*
// CHECK-DEBUG-NEXT: call void (%ident_t*, i32, void (i32*, i32*, ...)*, ...)* @__kmpc_fork_call(%ident_t* %.kmpc_loc_2.addr, i32 1, void (i32*, i32*, ...)* bitcast (void (i32*, i32*, %struct.anon.0*)* @__captured_stmt1 to void (i32*, i32*, ...)*), i8* [[BITCAST]])
// CHECK-DEBUG-NEXT: ret i32 0
// CHECK-DEBUG-NEXT: }

// CHECK-LABEL: define internal void @__captured_stmt1(i32* %.global_tid., i32* %.bound_tid., %struct.anon.0* %__context)
// CHECK: %__context.addr = alloca %struct.anon.0*, align 8
// CHECK: store %struct.anon.0* %__context, %struct.anon.0** %__context.addr, align 8
// CHECK-NEXT: [[CONTEXT_PTR:%.+]] = load %struct.anon.0** %__context.addr
// CHECK-NEXT: [[ARGC_PTR_REF:%.+]] = getelementptr inbounds %struct.anon.0* [[CONTEXT_PTR]], i32 0, i32 0
// CHECK-NEXT: [[ARGC_REF:%.+]] = load i8**** [[ARGC_PTR_REF]]
// CHECK-NEXT: [[ARGC:%.+]] = load i8*** [[ARGC_REF]]
// CHECK-NEXT: invoke void @_Z3fooIPPcEvT_(i8** [[ARGC]])
// CHECK: ret void
// CHECK: call void @__clang_call_terminate(i8*
// CHECK-NEXT: unreachable
// CHECK-NEXT: }
// CHECK-DEBUG-LABEL: define internal void @__captured_stmt1(i32* %.global_tid., i32* %.bound_tid., %struct.anon.0* %__context)
// CHECK-DEBUG: %__context.addr = alloca %struct.anon.0*, align 8
// CHECK-DEBUG: store %struct.anon.0* %__context, %struct.anon.0** %__context.addr, align 8
// CHECK-DEBUG: [[CONTEXT_PTR:%.+]] = load %struct.anon.0** %__context.addr
// CHECK-DEBUG-NEXT: [[ARGC_PTR_REF:%.+]] = getelementptr inbounds %struct.anon.0* [[CONTEXT_PTR]], i32 0, i32 0
// CHECK-DEBUG-NEXT: [[ARGC_REF:%.+]] = load i8**** [[ARGC_PTR_REF]]
// CHECK-DEBUG-NEXT: [[ARGC:%.+]] = load i8*** [[ARGC_REF]]
// CHECK-DEBUG-NEXT: invoke void @_Z3fooIPPcEvT_(i8** [[ARGC]])
// CHECK-DEBUG: ret void
// CHECK-DEBUG: call void @__clang_call_terminate(i8*
// CHECK-DEBUG-NEXT: unreachable
// CHECK-DEBUG-NEXT: }

// CHECK: define linkonce_odr void @_Z3fooIPPcEvT_(i8** %argc)
// CHECK-DEBUG: define linkonce_odr void @_Z3fooIPPcEvT_(i8** %argc)

#endif