2016-02-08 23:59:20 +08:00
|
|
|
//===---- CGOpenMPRuntimeNVPTX.cpp - Interface to OpenMP NVPTX Runtimes ---===//
|
|
|
|
//
|
|
|
|
// 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 specialized to NVPTX
|
|
|
|
// targets.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CGOpenMPRuntimeNVPTX.h"
|
2016-04-04 23:55:02 +08:00
|
|
|
#include "CodeGenFunction.h"
|
2018-03-14 22:17:45 +08:00
|
|
|
#include "clang/AST/DeclOpenMP.h"
|
2016-04-04 23:55:02 +08:00
|
|
|
#include "clang/AST/StmtOpenMP.h"
|
2018-03-14 22:17:45 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2016-02-08 23:59:20 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
namespace {
|
|
|
|
enum OpenMPRTLFunctionNVPTX {
|
2017-11-22 22:46:49 +08:00
|
|
|
/// \brief Call to void __kmpc_kernel_init(kmp_int32 thread_limit,
|
|
|
|
/// int16_t RequiresOMPRuntime);
|
2017-01-04 04:19:56 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_kernel_init,
|
2017-11-22 22:46:49 +08:00
|
|
|
/// \brief Call to void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized);
|
2017-01-05 23:24:05 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_kernel_deinit,
|
2017-01-19 03:35:00 +08:00
|
|
|
/// \brief Call to void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
|
2017-11-22 22:46:49 +08:00
|
|
|
/// int16_t RequiresOMPRuntime, int16_t RequiresDataSharing);
|
2017-01-19 03:35:00 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_spmd_kernel_init,
|
|
|
|
/// \brief Call to void __kmpc_spmd_kernel_deinit();
|
|
|
|
OMPRTL_NVPTX__kmpc_spmd_kernel_deinit,
|
2017-01-10 23:42:51 +08:00
|
|
|
/// \brief Call to void __kmpc_kernel_prepare_parallel(void
|
2018-03-08 05:59:50 +08:00
|
|
|
/// *outlined_function, int16_t
|
2017-12-27 18:39:56 +08:00
|
|
|
/// IsOMPRuntimeInitialized);
|
2017-01-10 23:42:51 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_kernel_prepare_parallel,
|
2018-03-08 05:59:50 +08:00
|
|
|
/// \brief Call to bool __kmpc_kernel_parallel(void **outlined_function,
|
|
|
|
/// int16_t IsOMPRuntimeInitialized);
|
2017-01-10 23:42:51 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_kernel_parallel,
|
|
|
|
/// \brief Call to void __kmpc_kernel_end_parallel();
|
|
|
|
OMPRTL_NVPTX__kmpc_kernel_end_parallel,
|
|
|
|
/// Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
|
|
|
|
/// global_tid);
|
|
|
|
OMPRTL_NVPTX__kmpc_serialized_parallel,
|
|
|
|
/// Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
|
|
|
|
/// global_tid);
|
|
|
|
OMPRTL_NVPTX__kmpc_end_serialized_parallel,
|
2017-02-17 00:20:16 +08:00
|
|
|
/// \brief Call to int32_t __kmpc_shuffle_int32(int32_t element,
|
|
|
|
/// int16_t lane_offset, int16_t warp_size);
|
|
|
|
OMPRTL_NVPTX__kmpc_shuffle_int32,
|
|
|
|
/// \brief Call to int64_t __kmpc_shuffle_int64(int64_t element,
|
|
|
|
/// int16_t lane_offset, int16_t warp_size);
|
|
|
|
OMPRTL_NVPTX__kmpc_shuffle_int64,
|
|
|
|
/// \brief Call to __kmpc_nvptx_parallel_reduce_nowait(kmp_int32
|
|
|
|
/// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
|
|
|
|
/// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
|
|
|
|
/// lane_offset, int16_t shortCircuit),
|
|
|
|
/// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num));
|
|
|
|
OMPRTL_NVPTX__kmpc_parallel_reduce_nowait,
|
2017-02-17 00:48:49 +08:00
|
|
|
/// \brief Call to __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
|
|
|
|
/// int32_t num_vars, size_t reduce_size, void *reduce_data,
|
|
|
|
/// void (*kmp_ShuffleReductFctPtr)(void *rhs, int16_t lane_id, int16_t
|
|
|
|
/// lane_offset, int16_t shortCircuit),
|
|
|
|
/// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
|
|
|
|
/// void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
|
|
|
|
/// int32_t index, int32_t width),
|
|
|
|
/// void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad, int32_t
|
|
|
|
/// index, int32_t width, int32_t reduce))
|
|
|
|
OMPRTL_NVPTX__kmpc_teams_reduce_nowait,
|
2017-02-17 00:20:16 +08:00
|
|
|
/// \brief Call to __kmpc_nvptx_end_reduce_nowait(int32_t global_tid);
|
2018-03-14 22:17:45 +08:00
|
|
|
OMPRTL_NVPTX__kmpc_end_reduce_nowait,
|
|
|
|
/// \brief Call to void __kmpc_data_sharing_init_stack();
|
|
|
|
OMPRTL_NVPTX__kmpc_data_sharing_init_stack,
|
|
|
|
/// \brief Call to void* __kmpc_data_sharing_push_stack(size_t size,
|
|
|
|
/// int16_t UseSharedMemory);
|
|
|
|
OMPRTL_NVPTX__kmpc_data_sharing_push_stack,
|
|
|
|
/// \brief Call to void __kmpc_data_sharing_pop_stack(void *a);
|
|
|
|
OMPRTL_NVPTX__kmpc_data_sharing_pop_stack,
|
|
|
|
/// \brief Call to void __kmpc_begin_sharing_variables(void ***args,
|
|
|
|
/// size_t n_args);
|
|
|
|
OMPRTL_NVPTX__kmpc_begin_sharing_variables,
|
|
|
|
/// \brief Call to void __kmpc_end_sharing_variables();
|
|
|
|
OMPRTL_NVPTX__kmpc_end_sharing_variables,
|
|
|
|
/// \brief Call to void __kmpc_get_shared_variables(void ***GlobalArgs)
|
|
|
|
OMPRTL_NVPTX__kmpc_get_shared_variables,
|
2017-01-04 04:19:56 +08:00
|
|
|
};
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
/// Pre(post)-action for different OpenMP constructs specialized for NVPTX.
|
|
|
|
class NVPTXActionTy final : public PrePostActionTy {
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *EnterCallee = nullptr;
|
2017-01-10 23:42:51 +08:00
|
|
|
ArrayRef<llvm::Value *> EnterArgs;
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ExitCallee = nullptr;
|
2017-01-10 23:42:51 +08:00
|
|
|
ArrayRef<llvm::Value *> ExitArgs;
|
2018-04-17 04:16:21 +08:00
|
|
|
bool Conditional = false;
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::BasicBlock *ContBlock = nullptr;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NVPTXActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs,
|
|
|
|
llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs,
|
|
|
|
bool Conditional = false)
|
|
|
|
: EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
|
|
|
|
ExitArgs(ExitArgs), Conditional(Conditional) {}
|
|
|
|
void Enter(CodeGenFunction &CGF) override {
|
|
|
|
llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
|
|
|
|
if (Conditional) {
|
|
|
|
llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
|
|
|
|
auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
|
|
|
|
ContBlock = CGF.createBasicBlock("omp_if.end");
|
|
|
|
// Generate the branch (If-stmt)
|
|
|
|
CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
|
|
|
|
CGF.EmitBlock(ThenBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Done(CodeGenFunction &CGF) {
|
|
|
|
// Emit the rest of blocks/branches
|
|
|
|
CGF.EmitBranch(ContBlock);
|
|
|
|
CGF.EmitBlock(ContBlock, true);
|
|
|
|
}
|
|
|
|
void Exit(CodeGenFunction &CGF) override {
|
|
|
|
CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
|
|
|
|
}
|
|
|
|
};
|
2017-01-19 03:35:00 +08:00
|
|
|
|
|
|
|
// A class to track the execution mode when codegening directives within
|
|
|
|
// a target region. The appropriate mode (generic/spmd) is set on entry
|
|
|
|
// to the target region and used by containing directives such as 'parallel'
|
|
|
|
// to emit optimized code.
|
|
|
|
class ExecutionModeRAII {
|
|
|
|
private:
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode SavedMode;
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode &Mode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ExecutionModeRAII(CGOpenMPRuntimeNVPTX::ExecutionMode &Mode,
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode NewMode)
|
|
|
|
: Mode(Mode) {
|
|
|
|
SavedMode = Mode;
|
|
|
|
Mode = NewMode;
|
|
|
|
}
|
|
|
|
~ExecutionModeRAII() { Mode = SavedMode; }
|
|
|
|
};
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
/// GPU Configuration: This information can be derived from cuda registers,
|
|
|
|
/// however, providing compile time constants helps generate more efficient
|
|
|
|
/// code. For all practical purposes this is fine because the configuration
|
|
|
|
/// is the same for all known NVPTX architectures.
|
|
|
|
enum MachineConfiguration : unsigned {
|
|
|
|
WarpSize = 32,
|
|
|
|
/// Number of bits required to represent a lane identifier, which is
|
|
|
|
/// computed as log_2(WarpSize).
|
|
|
|
LaneIDBits = 5,
|
|
|
|
LaneIDMask = WarpSize - 1,
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
/// Global memory alignment for performance.
|
|
|
|
GlobalMemoryAlignment = 256,
|
2017-02-17 00:20:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum NamedBarrier : unsigned {
|
|
|
|
/// Synchronize on this barrier #ID using a named barrier primitive.
|
|
|
|
/// Only the subset of active threads in a parallel region arrive at the
|
|
|
|
/// barrier.
|
|
|
|
NB_Parallel = 1,
|
|
|
|
};
|
2018-03-14 22:17:45 +08:00
|
|
|
|
|
|
|
/// Get the list of variables that can escape their declaration context.
|
|
|
|
class CheckVarsEscapingDeclContext final
|
|
|
|
: public ConstStmtVisitor<CheckVarsEscapingDeclContext> {
|
|
|
|
CodeGenFunction &CGF;
|
|
|
|
llvm::SetVector<const ValueDecl *> EscapedDecls;
|
2018-03-20 22:45:59 +08:00
|
|
|
llvm::SetVector<const ValueDecl *> EscapedVariableLengthDecls;
|
2018-03-16 02:10:54 +08:00
|
|
|
llvm::SmallPtrSet<const Decl *, 4> EscapedParameters;
|
2018-03-14 22:17:45 +08:00
|
|
|
bool AllEscaped = false;
|
|
|
|
RecordDecl *GlobalizedRD = nullptr;
|
|
|
|
llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> MappedDeclsFields;
|
|
|
|
|
2018-03-31 02:31:07 +08:00
|
|
|
static llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
|
|
|
|
isDeclareTargetDeclaration(const ValueDecl *VD) {
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Decl *D : VD->redecls()) {
|
2018-03-31 02:31:07 +08:00
|
|
|
if (!D->hasAttrs())
|
|
|
|
continue;
|
|
|
|
if (const auto *Attr = D->getAttr<OMPDeclareTargetDeclAttr>())
|
|
|
|
return Attr->getMapType();
|
|
|
|
}
|
|
|
|
return llvm::None;
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:17:45 +08:00
|
|
|
void markAsEscaped(const ValueDecl *VD) {
|
2018-03-31 02:31:07 +08:00
|
|
|
// Do not globalize declare target variables.
|
|
|
|
if (isDeclareTargetDeclaration(VD))
|
|
|
|
return;
|
2018-03-20 22:45:59 +08:00
|
|
|
VD = cast<ValueDecl>(VD->getCanonicalDecl());
|
2018-03-16 02:10:54 +08:00
|
|
|
// Variables captured by value must be globalized.
|
|
|
|
if (auto *CSI = CGF.CapturedStmtInfo) {
|
2018-03-16 15:27:57 +08:00
|
|
|
if (const FieldDecl *FD = CSI->lookup(cast<VarDecl>(VD))) {
|
2018-03-20 22:45:59 +08:00
|
|
|
if (!FD->hasAttrs())
|
|
|
|
return;
|
|
|
|
const auto *Attr = FD->getAttr<OMPCaptureKindAttr>();
|
|
|
|
if (!Attr)
|
|
|
|
return;
|
|
|
|
if (!isOpenMPPrivate(
|
|
|
|
static_cast<OpenMPClauseKind>(Attr->getCaptureKind())) ||
|
|
|
|
Attr->getCaptureKind() == OMPC_map)
|
|
|
|
return;
|
2018-03-16 02:10:54 +08:00
|
|
|
if (FD->getType()->isReferenceType())
|
|
|
|
return;
|
2018-03-20 22:45:59 +08:00
|
|
|
assert(!VD->getType()->isVariablyModifiedType() &&
|
|
|
|
"Parameter captured by value with variably modified type");
|
2018-03-16 02:10:54 +08:00
|
|
|
EscapedParameters.insert(VD);
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 22:45:59 +08:00
|
|
|
if (VD->getType()->isVariablyModifiedType())
|
|
|
|
EscapedVariableLengthDecls.insert(VD);
|
|
|
|
else
|
|
|
|
EscapedDecls.insert(VD);
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VisitValueDecl(const ValueDecl *VD) {
|
2018-03-20 22:45:59 +08:00
|
|
|
if (VD->getType()->isLValueReferenceType())
|
2018-03-14 22:17:45 +08:00
|
|
|
markAsEscaped(VD);
|
2018-03-20 22:45:59 +08:00
|
|
|
if (const auto *VarD = dyn_cast<VarDecl>(VD)) {
|
|
|
|
if (!isa<ParmVarDecl>(VarD) && VarD->hasInit()) {
|
|
|
|
const bool SavedAllEscaped = AllEscaped;
|
|
|
|
AllEscaped = VD->getType()->isLValueReferenceType();
|
|
|
|
Visit(VarD->getInit());
|
|
|
|
AllEscaped = SavedAllEscaped;
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitOpenMPCapturedStmt(const CapturedStmt *S) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const CapturedStmt::Capture &C : S->captures()) {
|
2018-03-14 22:17:45 +08:00
|
|
|
if (C.capturesVariable() && !C.capturesVariableByCopy()) {
|
|
|
|
const ValueDecl *VD = C.getCapturedVar();
|
|
|
|
markAsEscaped(VD);
|
|
|
|
if (isa<OMPCapturedExprDecl>(VD))
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<CharUnits /*Align*/, const ValueDecl *> VarsDataTy;
|
|
|
|
static bool stable_sort_comparator(const VarsDataTy P1, const VarsDataTy P2) {
|
|
|
|
return P1.first > P2.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
void buildRecordForGlobalizedVars() {
|
|
|
|
assert(!GlobalizedRD &&
|
|
|
|
"Record for globalized variables is built already.");
|
|
|
|
if (EscapedDecls.empty())
|
|
|
|
return;
|
|
|
|
ASTContext &C = CGF.getContext();
|
|
|
|
SmallVector<VarsDataTy, 4> GlobalizedVars;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const ValueDecl *D : EscapedDecls)
|
2018-03-14 22:17:45 +08:00
|
|
|
GlobalizedVars.emplace_back(C.getDeclAlign(D), D);
|
|
|
|
std::stable_sort(GlobalizedVars.begin(), GlobalizedVars.end(),
|
|
|
|
stable_sort_comparator);
|
|
|
|
// Build struct _globalized_locals_ty {
|
|
|
|
// /* globalized vars */
|
|
|
|
// };
|
|
|
|
GlobalizedRD = C.buildImplicitRecord("_globalized_locals_ty");
|
|
|
|
GlobalizedRD->startDefinition();
|
|
|
|
for (const auto &Pair : GlobalizedVars) {
|
|
|
|
const ValueDecl *VD = Pair.second;
|
|
|
|
QualType Type = VD->getType();
|
|
|
|
if (Type->isLValueReferenceType())
|
|
|
|
Type = C.getPointerType(Type.getNonReferenceType());
|
|
|
|
else
|
|
|
|
Type = Type.getNonReferenceType();
|
|
|
|
SourceLocation Loc = VD->getLocation();
|
|
|
|
auto *Field = FieldDecl::Create(
|
|
|
|
C, GlobalizedRD, Loc, Loc, VD->getIdentifier(), Type,
|
|
|
|
C.getTrivialTypeSourceInfo(Type, SourceLocation()),
|
|
|
|
/*BW=*/nullptr, /*Mutable=*/false,
|
|
|
|
/*InitStyle=*/ICIS_NoInit);
|
|
|
|
Field->setAccess(AS_public);
|
|
|
|
GlobalizedRD->addDecl(Field);
|
|
|
|
if (VD->hasAttrs()) {
|
|
|
|
for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
|
|
|
|
E(VD->getAttrs().end());
|
|
|
|
I != E; ++I)
|
|
|
|
Field->addAttr(*I);
|
|
|
|
}
|
|
|
|
MappedDeclsFields.try_emplace(VD, Field);
|
|
|
|
}
|
|
|
|
GlobalizedRD->completeDefinition();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-03-20 22:45:59 +08:00
|
|
|
CheckVarsEscapingDeclContext(CodeGenFunction &CGF) : CGF(CGF) {}
|
2018-03-14 22:17:45 +08:00
|
|
|
virtual ~CheckVarsEscapingDeclContext() = default;
|
|
|
|
void VisitDeclStmt(const DeclStmt *S) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Decl *D : S->decls())
|
2018-03-14 22:17:45 +08:00
|
|
|
if (const auto *VD = dyn_cast_or_null<ValueDecl>(D))
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
|
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
if (D->hasAssociatedStmt()) {
|
|
|
|
if (const auto *S =
|
|
|
|
dyn_cast_or_null<CapturedStmt>(D->getAssociatedStmt()))
|
|
|
|
VisitOpenMPCapturedStmt(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitCapturedStmt(const CapturedStmt *S) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const CapturedStmt::Capture &C : S->captures()) {
|
2018-03-14 22:17:45 +08:00
|
|
|
if (C.capturesVariable() && !C.capturesVariableByCopy()) {
|
|
|
|
const ValueDecl *VD = C.getCapturedVar();
|
|
|
|
markAsEscaped(VD);
|
|
|
|
if (isa<OMPCapturedExprDecl>(VD))
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitLambdaExpr(const LambdaExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const LambdaCapture &C : E->captures()) {
|
2018-03-14 22:17:45 +08:00
|
|
|
if (C.capturesVariable()) {
|
|
|
|
if (C.getCaptureKind() == LCK_ByRef) {
|
|
|
|
const ValueDecl *VD = C.getCapturedVar();
|
|
|
|
markAsEscaped(VD);
|
|
|
|
if (E->isInitCapture(&C) || isa<OMPCapturedExprDecl>(VD))
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitBlockExpr(const BlockExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const BlockDecl::Capture &C : E->getBlockDecl()->captures()) {
|
2018-03-14 22:17:45 +08:00
|
|
|
if (C.isByRef()) {
|
|
|
|
const VarDecl *VD = C.getVariable();
|
|
|
|
markAsEscaped(VD);
|
|
|
|
if (isa<OMPCapturedExprDecl>(VD) || VD->isInitCapture())
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void VisitCallExpr(const CallExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
for (const Expr *Arg : E->arguments()) {
|
|
|
|
if (!Arg)
|
|
|
|
continue;
|
|
|
|
if (Arg->isLValue()) {
|
|
|
|
const bool SavedAllEscaped = AllEscaped;
|
|
|
|
AllEscaped = true;
|
|
|
|
Visit(Arg);
|
|
|
|
AllEscaped = SavedAllEscaped;
|
2018-04-17 04:16:21 +08:00
|
|
|
} else {
|
2018-03-14 22:17:45 +08:00
|
|
|
Visit(Arg);
|
2018-04-17 04:16:21 +08:00
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
Visit(E->getCallee());
|
|
|
|
}
|
|
|
|
void VisitDeclRefExpr(const DeclRefExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
const ValueDecl *VD = E->getDecl();
|
|
|
|
if (AllEscaped)
|
|
|
|
markAsEscaped(VD);
|
|
|
|
if (isa<OMPCapturedExprDecl>(VD))
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
else if (const auto *VarD = dyn_cast<VarDecl>(VD))
|
|
|
|
if (VarD->isInitCapture())
|
|
|
|
VisitValueDecl(VD);
|
|
|
|
}
|
|
|
|
void VisitUnaryOperator(const UnaryOperator *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
if (E->getOpcode() == UO_AddrOf) {
|
|
|
|
const bool SavedAllEscaped = AllEscaped;
|
|
|
|
AllEscaped = true;
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
AllEscaped = SavedAllEscaped;
|
2018-04-17 04:16:21 +08:00
|
|
|
} else {
|
2018-03-14 22:17:45 +08:00
|
|
|
Visit(E->getSubExpr());
|
2018-04-17 04:16:21 +08:00
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
void VisitImplicitCastExpr(const ImplicitCastExpr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
if (E->getCastKind() == CK_ArrayToPointerDecay) {
|
|
|
|
const bool SavedAllEscaped = AllEscaped;
|
|
|
|
AllEscaped = true;
|
|
|
|
Visit(E->getSubExpr());
|
|
|
|
AllEscaped = SavedAllEscaped;
|
2018-04-17 04:16:21 +08:00
|
|
|
} else {
|
2018-03-14 22:17:45 +08:00
|
|
|
Visit(E->getSubExpr());
|
2018-04-17 04:16:21 +08:00
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
void VisitExpr(const Expr *E) {
|
|
|
|
if (!E)
|
|
|
|
return;
|
|
|
|
bool SavedAllEscaped = AllEscaped;
|
|
|
|
if (!E->isLValue())
|
|
|
|
AllEscaped = false;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Stmt *Child : E->children())
|
2018-03-14 22:17:45 +08:00
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
|
|
|
AllEscaped = SavedAllEscaped;
|
|
|
|
}
|
|
|
|
void VisitStmt(const Stmt *S) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Stmt *Child : S->children())
|
2018-03-14 22:17:45 +08:00
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
|
|
|
}
|
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
/// Returns the record that handles all the escaped local variables and used
|
|
|
|
/// instead of their original storage.
|
2018-03-14 22:17:45 +08:00
|
|
|
const RecordDecl *getGlobalizedRecord() {
|
|
|
|
if (!GlobalizedRD)
|
|
|
|
buildRecordForGlobalizedVars();
|
|
|
|
return GlobalizedRD;
|
|
|
|
}
|
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
/// Returns the field in the globalized record for the escaped variable.
|
2018-03-14 22:17:45 +08:00
|
|
|
const FieldDecl *getFieldForGlobalizedVar(const ValueDecl *VD) const {
|
|
|
|
assert(GlobalizedRD &&
|
|
|
|
"Record for globalized variables must be generated already.");
|
|
|
|
auto I = MappedDeclsFields.find(VD);
|
|
|
|
if (I == MappedDeclsFields.end())
|
|
|
|
return nullptr;
|
|
|
|
return I->getSecond();
|
|
|
|
}
|
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
/// Returns the list of the escaped local variables/parameters.
|
2018-03-14 22:17:45 +08:00
|
|
|
ArrayRef<const ValueDecl *> getEscapedDecls() const {
|
|
|
|
return EscapedDecls.getArrayRef();
|
|
|
|
}
|
2018-03-16 02:10:54 +08:00
|
|
|
|
|
|
|
/// Checks if the escaped local variable is actually a parameter passed by
|
|
|
|
/// value.
|
|
|
|
const llvm::SmallPtrSetImpl<const Decl *> &getEscapedParameters() const {
|
|
|
|
return EscapedParameters;
|
|
|
|
}
|
2018-03-20 22:45:59 +08:00
|
|
|
|
|
|
|
/// Returns the list of the escaped variables with the variably modified
|
|
|
|
/// types.
|
|
|
|
ArrayRef<const ValueDecl *> getEscapedVariableLengthDecls() const {
|
|
|
|
return EscapedVariableLengthDecls.getArrayRef();
|
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
};
|
2017-01-10 23:42:51 +08:00
|
|
|
} // anonymous namespace
|
2017-01-04 04:19:56 +08:00
|
|
|
|
|
|
|
/// Get the GPU warp size.
|
|
|
|
static llvm::Value *getNVPTXWarpSize(CodeGenFunction &CGF) {
|
2017-08-14 23:01:03 +08:00
|
|
|
return CGF.EmitRuntimeCall(
|
2016-03-22 09:48:56 +08:00
|
|
|
llvm::Intrinsic::getDeclaration(
|
2017-01-04 04:19:56 +08:00
|
|
|
&CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize),
|
2017-08-14 23:01:03 +08:00
|
|
|
"nvptx_warp_size");
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
/// Get the id of the current thread on the GPU.
|
|
|
|
static llvm::Value *getNVPTXThreadID(CodeGenFunction &CGF) {
|
2017-08-14 23:01:03 +08:00
|
|
|
return CGF.EmitRuntimeCall(
|
2016-03-22 09:48:56 +08:00
|
|
|
llvm::Intrinsic::getDeclaration(
|
2017-01-04 04:19:56 +08:00
|
|
|
&CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x),
|
2017-08-14 23:01:03 +08:00
|
|
|
"nvptx_tid");
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// Get the id of the warp in the block.
|
|
|
|
/// We assume that the warp size is 32, which is always the case
|
|
|
|
/// on the NVPTX device, to generate more efficient code.
|
|
|
|
static llvm::Value *getNVPTXWarpID(CodeGenFunction &CGF) {
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
return Bld.CreateAShr(getNVPTXThreadID(CGF), LaneIDBits, "nvptx_warp_id");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the id of the current lane in the Warp.
|
|
|
|
/// We assume that the warp size is 32, which is always the case
|
|
|
|
/// on the NVPTX device, to generate more efficient code.
|
|
|
|
static llvm::Value *getNVPTXLaneID(CodeGenFunction &CGF) {
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
return Bld.CreateAnd(getNVPTXThreadID(CGF), Bld.getInt32(LaneIDMask),
|
|
|
|
"nvptx_lane_id");
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
/// Get the maximum number of threads in a block of the GPU.
|
|
|
|
static llvm::Value *getNVPTXNumThreads(CodeGenFunction &CGF) {
|
2017-08-14 23:01:03 +08:00
|
|
|
return CGF.EmitRuntimeCall(
|
2016-03-22 09:48:56 +08:00
|
|
|
llvm::Intrinsic::getDeclaration(
|
2017-01-04 04:19:56 +08:00
|
|
|
&CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x),
|
2017-08-14 23:01:03 +08:00
|
|
|
"nvptx_num_threads");
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
/// Get barrier to synchronize all threads in a block.
|
|
|
|
static void getNVPTXCTABarrier(CodeGenFunction &CGF) {
|
2017-08-14 23:01:03 +08:00
|
|
|
CGF.EmitRuntimeCall(llvm::Intrinsic::getDeclaration(
|
2017-01-04 04:19:56 +08:00
|
|
|
&CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier0));
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// Get barrier #ID to synchronize selected (multiple of warp size) threads in
|
|
|
|
/// a CTA.
|
|
|
|
static void getNVPTXBarrier(CodeGenFunction &CGF, int ID,
|
|
|
|
llvm::Value *NumThreads) {
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
llvm::Value *Args[] = {Bld.getInt32(ID), NumThreads};
|
2017-08-14 23:01:03 +08:00
|
|
|
CGF.EmitRuntimeCall(llvm::Intrinsic::getDeclaration(
|
|
|
|
&CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier),
|
|
|
|
Args);
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
/// Synchronize all GPU threads in a block.
|
|
|
|
static void syncCTAThreads(CodeGenFunction &CGF) { getNVPTXCTABarrier(CGF); }
|
2016-03-22 09:48:56 +08:00
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// Synchronize worker threads in a parallel region.
|
|
|
|
static void syncParallelThreads(CodeGenFunction &CGF, llvm::Value *NumThreads) {
|
|
|
|
return getNVPTXBarrier(CGF, NB_Parallel, NumThreads);
|
|
|
|
}
|
|
|
|
|
2017-01-05 23:24:05 +08:00
|
|
|
/// Get the value of the thread_limit clause in the teams directive.
|
2017-01-19 03:35:00 +08:00
|
|
|
/// For the 'generic' execution mode, the runtime encodes thread_limit in
|
|
|
|
/// the launch parameters, always starting thread_limit+warpSize threads per
|
|
|
|
/// CTA. The threads in the last warp are reserved for master execution.
|
|
|
|
/// For the 'spmd' execution mode, all threads in a CTA are part of the team.
|
|
|
|
static llvm::Value *getThreadLimit(CodeGenFunction &CGF,
|
|
|
|
bool IsInSpmdExecutionMode = false) {
|
2017-01-05 23:24:05 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-01-19 03:35:00 +08:00
|
|
|
return IsInSpmdExecutionMode
|
|
|
|
? getNVPTXNumThreads(CGF)
|
2018-04-07 00:03:36 +08:00
|
|
|
: Bld.CreateNUWSub(getNVPTXNumThreads(CGF), getNVPTXWarpSize(CGF),
|
|
|
|
"thread_limit");
|
2017-01-05 23:24:05 +08:00
|
|
|
}
|
|
|
|
|
2017-01-04 04:19:56 +08:00
|
|
|
/// Get the thread id of the OMP master thread.
|
2016-03-22 09:48:56 +08:00
|
|
|
/// The master thread id is the first thread (lane) of the last warp in the
|
|
|
|
/// GPU block. Warp size is assumed to be some power of 2.
|
|
|
|
/// Thread id is 0 indexed.
|
|
|
|
/// E.g: If NumThreads is 33, master id is 32.
|
|
|
|
/// If NumThreads is 64, master id is 32.
|
|
|
|
/// If NumThreads is 1024, master id is 992.
|
2017-01-04 04:19:56 +08:00
|
|
|
static llvm::Value *getMasterThreadID(CodeGenFunction &CGF) {
|
2016-03-22 09:48:56 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
llvm::Value *NumThreads = getNVPTXNumThreads(CGF);
|
|
|
|
|
|
|
|
// We assume that the warp size is a power of 2.
|
2018-04-07 00:03:36 +08:00
|
|
|
llvm::Value *Mask = Bld.CreateNUWSub(getNVPTXWarpSize(CGF), Bld.getInt32(1));
|
2016-03-22 09:48:56 +08:00
|
|
|
|
2018-04-07 00:03:36 +08:00
|
|
|
return Bld.CreateAnd(Bld.CreateNUWSub(NumThreads, Bld.getInt32(1)),
|
2016-03-22 09:48:56 +08:00
|
|
|
Bld.CreateNot(Mask), "master_tid");
|
|
|
|
}
|
|
|
|
|
|
|
|
CGOpenMPRuntimeNVPTX::WorkerFunctionState::WorkerFunctionState(
|
2018-01-05 03:45:16 +08:00
|
|
|
CodeGenModule &CGM, SourceLocation Loc)
|
2018-04-17 04:16:21 +08:00
|
|
|
: WorkerFn(nullptr), CGFI(CGM.getTypes().arrangeNullaryFunction()),
|
|
|
|
Loc(Loc) {
|
2016-03-22 09:48:56 +08:00
|
|
|
createWorkerFunction(CGM);
|
2016-03-22 18:41:20 +08:00
|
|
|
}
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::WorkerFunctionState::createWorkerFunction(
|
|
|
|
CodeGenModule &CGM) {
|
|
|
|
// Create an worker function with no arguments.
|
|
|
|
|
|
|
|
WorkerFn = llvm::Function::Create(
|
2018-04-17 04:16:21 +08:00
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
2018-01-09 04:09:47 +08:00
|
|
|
/*placeholder=*/"_worker", &CGM.getModule());
|
2018-04-17 04:16:21 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), WorkerFn, CGFI);
|
2018-04-11 04:10:53 +08:00
|
|
|
WorkerFn->setDoesNotRecurse();
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 03:35:00 +08:00
|
|
|
bool CGOpenMPRuntimeNVPTX::isInSpmdExecutionMode() const {
|
|
|
|
return CurrentExecutionMode == CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CGOpenMPRuntimeNVPTX::ExecutionMode
|
2018-03-01 04:48:35 +08:00
|
|
|
getExecutionMode(CodeGenModule &CGM) {
|
|
|
|
return CGM.getLangOpts().OpenMPCUDAMode
|
|
|
|
? CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd
|
|
|
|
: CGOpenMPRuntimeNVPTX::ExecutionMode::Generic;
|
2017-01-19 03:35:00 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 23:24:05 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericKernel(const OMPExecutableDirective &D,
|
|
|
|
StringRef ParentName,
|
|
|
|
llvm::Function *&OutlinedFn,
|
|
|
|
llvm::Constant *&OutlinedFnID,
|
|
|
|
bool IsOffloadEntry,
|
|
|
|
const RegionCodeGenTy &CodeGen) {
|
2017-01-19 03:35:00 +08:00
|
|
|
ExecutionModeRAII ModeRAII(CurrentExecutionMode,
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode::Generic);
|
2017-01-05 23:24:05 +08:00
|
|
|
EntryFunctionState EST;
|
2018-01-05 03:45:16 +08:00
|
|
|
WorkerFunctionState WST(CGM, D.getLocStart());
|
2017-01-10 23:42:51 +08:00
|
|
|
Work.clear();
|
2018-03-14 22:17:45 +08:00
|
|
|
WrapperFunctionsMap.clear();
|
2017-01-05 23:24:05 +08:00
|
|
|
|
|
|
|
// Emit target region as a standalone region.
|
|
|
|
class NVPTXPrePostActionTy : public PrePostActionTy {
|
|
|
|
CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
|
|
|
|
CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST;
|
|
|
|
|
|
|
|
public:
|
2018-01-05 03:45:16 +08:00
|
|
|
NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
|
2017-01-05 23:24:05 +08:00
|
|
|
CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST)
|
2018-01-05 03:45:16 +08:00
|
|
|
: EST(EST), WST(WST) {}
|
2017-01-05 23:24:05 +08:00
|
|
|
void Enter(CodeGenFunction &CGF) override {
|
2018-01-05 03:45:16 +08:00
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericEntryHeader(CGF, EST, WST);
|
2017-01-05 23:24:05 +08:00
|
|
|
}
|
|
|
|
void Exit(CodeGenFunction &CGF) override {
|
2018-01-05 03:45:16 +08:00
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericEntryFooter(CGF, EST);
|
2017-01-05 23:24:05 +08:00
|
|
|
}
|
2018-01-05 03:45:16 +08:00
|
|
|
} Action(EST, WST);
|
2017-01-05 23:24:05 +08:00
|
|
|
CodeGen.setAction(Action);
|
|
|
|
emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
|
|
|
|
IsOffloadEntry, CodeGen);
|
2017-01-05 02:44:50 +08:00
|
|
|
|
2017-01-05 23:24:05 +08:00
|
|
|
// Now change the name of the worker function to correspond to this target
|
|
|
|
// region's entry function.
|
2018-04-17 04:16:21 +08:00
|
|
|
WST.WorkerFn->setName(Twine(OutlinedFn->getName(), "_worker"));
|
2018-01-09 04:09:47 +08:00
|
|
|
|
|
|
|
// Create the worker function
|
|
|
|
emitWorkerFunction(WST);
|
2017-01-05 23:24:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup NVPTX threads for master-worker OpenMP scheme.
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericEntryHeader(CodeGenFunction &CGF,
|
|
|
|
EntryFunctionState &EST,
|
|
|
|
WorkerFunctionState &WST) {
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
|
|
|
|
llvm::BasicBlock *WorkerBB = CGF.createBasicBlock(".worker");
|
|
|
|
llvm::BasicBlock *MasterCheckBB = CGF.createBasicBlock(".mastercheck");
|
|
|
|
llvm::BasicBlock *MasterBB = CGF.createBasicBlock(".master");
|
|
|
|
EST.ExitBB = CGF.createBasicBlock(".exit");
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *IsWorker =
|
2017-01-05 23:24:05 +08:00
|
|
|
Bld.CreateICmpULT(getNVPTXThreadID(CGF), getThreadLimit(CGF));
|
|
|
|
Bld.CreateCondBr(IsWorker, WorkerBB, MasterCheckBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(WorkerBB);
|
2018-03-20 01:04:07 +08:00
|
|
|
emitCall(CGF, WST.Loc, WST.WorkerFn);
|
2017-01-05 23:24:05 +08:00
|
|
|
CGF.EmitBranch(EST.ExitBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(MasterCheckBB);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *IsMaster =
|
2017-01-05 23:24:05 +08:00
|
|
|
Bld.CreateICmpEQ(getNVPTXThreadID(CGF), getMasterThreadID(CGF));
|
|
|
|
Bld.CreateCondBr(IsMaster, MasterBB, EST.ExitBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(MasterBB);
|
2018-03-14 22:17:45 +08:00
|
|
|
// SEQUENTIAL (MASTER) REGION START
|
2017-01-05 23:24:05 +08:00
|
|
|
// First action in sequential region:
|
|
|
|
// Initialize the state of the OpenMP runtime library on the GPU.
|
2017-11-22 22:46:49 +08:00
|
|
|
// TODO: Optimize runtime initialization and pass in correct value.
|
|
|
|
llvm::Value *Args[] = {getThreadLimit(CGF),
|
|
|
|
Bld.getInt16(/*RequiresOMPRuntime=*/1)};
|
2017-01-05 23:24:05 +08:00
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_init), Args);
|
2018-03-14 22:17:45 +08:00
|
|
|
|
|
|
|
// For data sharing, we need to initialize the stack.
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(
|
|
|
|
OMPRTL_NVPTX__kmpc_data_sharing_init_stack));
|
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
emitGenericVarsProlog(CGF, WST.Loc);
|
2017-01-05 23:24:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericEntryFooter(CodeGenFunction &CGF,
|
|
|
|
EntryFunctionState &EST) {
|
2018-03-16 02:10:54 +08:00
|
|
|
emitGenericVarsEpilog(CGF);
|
|
|
|
if (!CGF.HaveInsertPoint())
|
|
|
|
return;
|
2018-03-14 22:17:45 +08:00
|
|
|
|
2017-01-05 23:24:05 +08:00
|
|
|
if (!EST.ExitBB)
|
|
|
|
EST.ExitBB = CGF.createBasicBlock(".exit");
|
|
|
|
|
|
|
|
llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".termination.notifier");
|
|
|
|
CGF.EmitBranch(TerminateBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(TerminateBB);
|
|
|
|
// Signal termination condition.
|
2017-11-22 22:46:49 +08:00
|
|
|
// TODO: Optimize runtime initialization and pass in correct value.
|
|
|
|
llvm::Value *Args[] = {CGF.Builder.getInt16(/*IsOMPRuntimeInitialized=*/1)};
|
2017-01-05 23:24:05 +08:00
|
|
|
CGF.EmitRuntimeCall(
|
2017-11-22 22:46:49 +08:00
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_deinit), Args);
|
2017-01-05 23:24:05 +08:00
|
|
|
// Barrier to terminate worker threads.
|
|
|
|
syncCTAThreads(CGF);
|
|
|
|
// Master thread jumps to exit point.
|
|
|
|
CGF.EmitBranch(EST.ExitBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(EST.ExitBB);
|
|
|
|
EST.ExitBB = nullptr;
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2017-01-19 03:35:00 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitSpmdKernel(const OMPExecutableDirective &D,
|
|
|
|
StringRef ParentName,
|
|
|
|
llvm::Function *&OutlinedFn,
|
|
|
|
llvm::Constant *&OutlinedFnID,
|
|
|
|
bool IsOffloadEntry,
|
|
|
|
const RegionCodeGenTy &CodeGen) {
|
|
|
|
ExecutionModeRAII ModeRAII(CurrentExecutionMode,
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd);
|
|
|
|
EntryFunctionState EST;
|
|
|
|
|
|
|
|
// Emit target region as a standalone region.
|
|
|
|
class NVPTXPrePostActionTy : public PrePostActionTy {
|
|
|
|
CGOpenMPRuntimeNVPTX &RT;
|
|
|
|
CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
|
|
|
|
const OMPExecutableDirective &D;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX &RT,
|
|
|
|
CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
|
|
|
|
const OMPExecutableDirective &D)
|
|
|
|
: RT(RT), EST(EST), D(D) {}
|
|
|
|
void Enter(CodeGenFunction &CGF) override {
|
|
|
|
RT.emitSpmdEntryHeader(CGF, EST, D);
|
|
|
|
}
|
|
|
|
void Exit(CodeGenFunction &CGF) override {
|
|
|
|
RT.emitSpmdEntryFooter(CGF, EST);
|
|
|
|
}
|
|
|
|
} Action(*this, EST, D);
|
|
|
|
CodeGen.setAction(Action);
|
|
|
|
emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
|
|
|
|
IsOffloadEntry, CodeGen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitSpmdEntryHeader(
|
|
|
|
CodeGenFunction &CGF, EntryFunctionState &EST,
|
|
|
|
const OMPExecutableDirective &D) {
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-01-19 03:35:00 +08:00
|
|
|
|
|
|
|
// Setup BBs in entry function.
|
|
|
|
llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute");
|
|
|
|
EST.ExitBB = CGF.createBasicBlock(".exit");
|
|
|
|
|
|
|
|
// Initialize the OMP state in the runtime; called by all active threads.
|
|
|
|
// TODO: Set RequiresOMPRuntime and RequiresDataSharing parameters
|
|
|
|
// based on code analysis of the target region.
|
|
|
|
llvm::Value *Args[] = {getThreadLimit(CGF, /*IsInSpmdExecutionMode=*/true),
|
|
|
|
/*RequiresOMPRuntime=*/Bld.getInt16(1),
|
|
|
|
/*RequiresDataSharing=*/Bld.getInt16(1)};
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_init), Args);
|
|
|
|
CGF.EmitBranch(ExecuteBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ExecuteBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitSpmdEntryFooter(CodeGenFunction &CGF,
|
|
|
|
EntryFunctionState &EST) {
|
|
|
|
if (!EST.ExitBB)
|
|
|
|
EST.ExitBB = CGF.createBasicBlock(".exit");
|
|
|
|
|
|
|
|
llvm::BasicBlock *OMPDeInitBB = CGF.createBasicBlock(".omp.deinit");
|
|
|
|
CGF.EmitBranch(OMPDeInitBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(OMPDeInitBB);
|
|
|
|
// DeInitialize the OMP state in the runtime; called by all active threads.
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_deinit), None);
|
|
|
|
CGF.EmitBranch(EST.ExitBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(EST.ExitBB);
|
|
|
|
EST.ExitBB = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a unique global variable to indicate the execution mode of this target
|
|
|
|
// region. The execution mode is either 'generic', or 'spmd' depending on the
|
|
|
|
// target directive. This variable is picked up by the offload library to setup
|
|
|
|
// the device appropriately before kernel launch. If the execution mode is
|
|
|
|
// 'generic', the runtime reserves one warp for the master, otherwise, all
|
|
|
|
// warps participate in parallel work.
|
|
|
|
static void setPropertyExecutionMode(CodeGenModule &CGM, StringRef Name,
|
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode Mode) {
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *GVMode = new llvm::GlobalVariable(
|
2017-01-19 03:35:00 +08:00
|
|
|
CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
|
|
|
|
llvm::GlobalValue::WeakAnyLinkage,
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::ConstantInt::get(CGM.Int8Ty, Mode), Twine(Name, "_exec_mode"));
|
|
|
|
CGM.addCompilerUsedGlobal(GVMode);
|
2017-01-19 03:35:00 +08:00
|
|
|
}
|
|
|
|
|
2016-03-22 09:48:56 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitWorkerFunction(WorkerFunctionState &WST) {
|
2017-11-21 23:54:54 +08:00
|
|
|
ASTContext &Ctx = CGM.getContext();
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
|
2018-04-17 04:16:21 +08:00
|
|
|
CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, WST.WorkerFn, WST.CGFI, {},
|
2018-01-05 03:45:16 +08:00
|
|
|
WST.Loc, WST.Loc);
|
2016-03-22 09:48:56 +08:00
|
|
|
emitWorkerLoop(CGF, WST);
|
|
|
|
CGF.FinishFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitWorkerLoop(CodeGenFunction &CGF,
|
|
|
|
WorkerFunctionState &WST) {
|
|
|
|
//
|
|
|
|
// The workers enter this loop and wait for parallel work from the master.
|
|
|
|
// When the master encounters a parallel region it sets up the work + variable
|
|
|
|
// arguments, and wakes up the workers. The workers first check to see if
|
|
|
|
// they are required for the parallel region, i.e., within the # of requested
|
|
|
|
// parallel threads. The activated workers load the variable arguments and
|
|
|
|
// execute the parallel work.
|
|
|
|
//
|
|
|
|
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
|
|
|
|
llvm::BasicBlock *AwaitBB = CGF.createBasicBlock(".await.work");
|
|
|
|
llvm::BasicBlock *SelectWorkersBB = CGF.createBasicBlock(".select.workers");
|
|
|
|
llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute.parallel");
|
|
|
|
llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".terminate.parallel");
|
|
|
|
llvm::BasicBlock *BarrierBB = CGF.createBasicBlock(".barrier.parallel");
|
|
|
|
llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit");
|
|
|
|
|
|
|
|
CGF.EmitBranch(AwaitBB);
|
|
|
|
|
|
|
|
// Workers wait for work from master.
|
|
|
|
CGF.EmitBlock(AwaitBB);
|
|
|
|
// Wait for parallel work
|
|
|
|
syncCTAThreads(CGF);
|
2017-01-05 23:24:05 +08:00
|
|
|
|
2018-03-23 01:33:27 +08:00
|
|
|
// For data sharing, we need to initialize the stack for workers.
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(
|
|
|
|
OMPRTL_NVPTX__kmpc_data_sharing_init_stack));
|
|
|
|
|
2017-01-05 23:24:05 +08:00
|
|
|
Address WorkFn =
|
|
|
|
CGF.CreateDefaultAlignTempAlloca(CGF.Int8PtrTy, /*Name=*/"work_fn");
|
|
|
|
Address ExecStatus =
|
|
|
|
CGF.CreateDefaultAlignTempAlloca(CGF.Int8Ty, /*Name=*/"exec_status");
|
|
|
|
CGF.InitTempAlloca(ExecStatus, Bld.getInt8(/*C=*/0));
|
|
|
|
CGF.InitTempAlloca(WorkFn, llvm::Constant::getNullValue(CGF.Int8PtrTy));
|
|
|
|
|
2017-12-27 18:39:56 +08:00
|
|
|
// TODO: Optimize runtime initialization and pass in correct value.
|
2018-03-08 05:59:50 +08:00
|
|
|
llvm::Value *Args[] = {WorkFn.getPointer(),
|
2017-12-27 18:39:56 +08:00
|
|
|
/*RequiresOMPRuntime=*/Bld.getInt16(1)};
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::Value *Ret = CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_parallel), Args);
|
|
|
|
Bld.CreateStore(Bld.CreateZExt(Ret, CGF.Int8Ty), ExecStatus);
|
2017-01-05 23:24:05 +08:00
|
|
|
|
2016-03-22 09:48:56 +08:00
|
|
|
// On termination condition (workid == 0), exit loop.
|
2017-01-05 23:24:05 +08:00
|
|
|
llvm::Value *ShouldTerminate =
|
|
|
|
Bld.CreateIsNull(Bld.CreateLoad(WorkFn), "should_terminate");
|
2016-03-22 09:48:56 +08:00
|
|
|
Bld.CreateCondBr(ShouldTerminate, ExitBB, SelectWorkersBB);
|
|
|
|
|
|
|
|
// Activate requested workers.
|
|
|
|
CGF.EmitBlock(SelectWorkersBB);
|
2017-01-05 23:24:05 +08:00
|
|
|
llvm::Value *IsActive =
|
|
|
|
Bld.CreateIsNotNull(Bld.CreateLoad(ExecStatus), "is_active");
|
|
|
|
Bld.CreateCondBr(IsActive, ExecuteBB, BarrierBB);
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
// Signal start of parallel region.
|
|
|
|
CGF.EmitBlock(ExecuteBB);
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
// Process work items: outlined parallel functions.
|
2018-04-17 04:16:21 +08:00
|
|
|
for (llvm::Function *W : Work) {
|
2017-01-10 23:42:51 +08:00
|
|
|
// Try to match this outlined function.
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ID = Bld.CreatePointerBitCastOrAddrSpaceCast(W, CGM.Int8PtrTy);
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
llvm::Value *WorkFnMatch =
|
|
|
|
Bld.CreateICmpEQ(Bld.CreateLoad(WorkFn), ID, "work_match");
|
|
|
|
|
|
|
|
llvm::BasicBlock *ExecuteFNBB = CGF.createBasicBlock(".execute.fn");
|
|
|
|
llvm::BasicBlock *CheckNextBB = CGF.createBasicBlock(".check.next");
|
|
|
|
Bld.CreateCondBr(WorkFnMatch, ExecuteFNBB, CheckNextBB);
|
|
|
|
|
|
|
|
// Execute this outlined function.
|
|
|
|
CGF.EmitBlock(ExecuteFNBB);
|
|
|
|
|
2018-03-14 22:17:45 +08:00
|
|
|
// Insert call to work function via shared wrapper. The shared
|
|
|
|
// wrapper takes two arguments:
|
|
|
|
// - the parallelism level;
|
2018-03-20 01:04:07 +08:00
|
|
|
// - the thread ID;
|
|
|
|
emitCall(CGF, WST.Loc, W,
|
|
|
|
{Bld.getInt16(/*ParallelLevel=*/0), getThreadID(CGF, WST.Loc)});
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
// Go to end of parallel region.
|
|
|
|
CGF.EmitBranch(TerminateBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(CheckNextBB);
|
|
|
|
}
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
// Signal end of parallel region.
|
|
|
|
CGF.EmitBlock(TerminateBB);
|
2017-01-10 23:42:51 +08:00
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_end_parallel),
|
|
|
|
llvm::None);
|
2016-03-22 09:48:56 +08:00
|
|
|
CGF.EmitBranch(BarrierBB);
|
|
|
|
|
|
|
|
// All active and inactive workers wait at a barrier after parallel region.
|
|
|
|
CGF.EmitBlock(BarrierBB);
|
|
|
|
// Barrier after parallel region.
|
|
|
|
syncCTAThreads(CGF);
|
|
|
|
CGF.EmitBranch(AwaitBB);
|
|
|
|
|
|
|
|
// Exit target region.
|
|
|
|
CGF.EmitBlock(ExitBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns specified OpenMP runtime function for the current OpenMP
|
|
|
|
/// implementation. Specialized for the NVPTX device.
|
|
|
|
/// \param Function OpenMP runtime function.
|
|
|
|
/// \return Specified function.
|
|
|
|
llvm::Constant *
|
|
|
|
CGOpenMPRuntimeNVPTX::createNVPTXRuntimeFunction(unsigned Function) {
|
|
|
|
llvm::Constant *RTLFn = nullptr;
|
|
|
|
switch (static_cast<OpenMPRTLFunctionNVPTX>(Function)) {
|
|
|
|
case OMPRTL_NVPTX__kmpc_kernel_init: {
|
2017-11-22 22:46:49 +08:00
|
|
|
// Build void __kmpc_kernel_init(kmp_int32 thread_limit, int16_t
|
|
|
|
// RequiresOMPRuntime);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2016-03-22 09:48:56 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_init");
|
|
|
|
break;
|
|
|
|
}
|
2017-01-05 23:24:05 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_kernel_deinit: {
|
2017-11-22 22:46:49 +08:00
|
|
|
// Build void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-11-22 22:46:49 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
2017-01-05 23:24:05 +08:00
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_deinit");
|
|
|
|
break;
|
|
|
|
}
|
2017-01-19 03:35:00 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_spmd_kernel_init: {
|
|
|
|
// Build void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
|
2017-11-22 22:46:49 +08:00
|
|
|
// int16_t RequiresOMPRuntime, int16_t RequiresDataSharing);
|
2017-01-19 03:35:00 +08:00
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-19 03:35:00 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_init");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_spmd_kernel_deinit: {
|
|
|
|
// Build void __kmpc_spmd_kernel_deinit();
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-19 03:35:00 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_deinit");
|
|
|
|
break;
|
|
|
|
}
|
2017-01-10 23:42:51 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_kernel_prepare_parallel: {
|
|
|
|
/// Build void __kmpc_kernel_prepare_parallel(
|
2018-03-08 05:59:50 +08:00
|
|
|
/// void *outlined_function, int16_t IsOMPRuntimeInitialized);
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::Type *TypeParams[] = {CGM.Int8PtrTy, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_prepare_parallel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_kernel_parallel: {
|
2018-03-08 05:59:50 +08:00
|
|
|
/// Build bool __kmpc_kernel_parallel(void **outlined_function,
|
|
|
|
/// int16_t IsOMPRuntimeInitialized);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy, CGM.Int16Ty};
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::Type *RetTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::FunctionType::get(RetTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_parallel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_kernel_end_parallel: {
|
|
|
|
/// Build void __kmpc_kernel_end_parallel();
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_end_parallel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_serialized_parallel: {
|
|
|
|
// Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
|
|
|
|
// global_tid);
|
|
|
|
llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_end_serialized_parallel: {
|
|
|
|
// Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
|
|
|
|
// global_tid);
|
|
|
|
llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
|
|
|
|
break;
|
|
|
|
}
|
2017-02-17 00:20:16 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_shuffle_int32: {
|
|
|
|
// Build int32_t __kmpc_shuffle_int32(int32_t element,
|
|
|
|
// int16_t lane_offset, int16_t warp_size);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-02-17 00:20:16 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int32");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_shuffle_int64: {
|
|
|
|
// Build int64_t __kmpc_shuffle_int64(int64_t element,
|
|
|
|
// int16_t lane_offset, int16_t warp_size);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int16Ty, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-02-17 00:20:16 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int64Ty, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int64");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_parallel_reduce_nowait: {
|
|
|
|
// Build int32_t kmpc_nvptx_parallel_reduce_nowait(kmp_int32 global_tid,
|
|
|
|
// kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
|
|
|
|
// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
|
|
|
|
// lane_offset, int16_t Algorithm Version),
|
|
|
|
// void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num));
|
|
|
|
llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
|
|
|
|
CGM.Int16Ty, CGM.Int16Ty};
|
|
|
|
auto *ShuffleReduceFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
|
|
|
|
auto *InterWarpCopyFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty,
|
|
|
|
CGM.Int32Ty,
|
|
|
|
CGM.SizeTy,
|
|
|
|
CGM.VoidPtrTy,
|
|
|
|
ShuffleReduceFnTy->getPointerTo(),
|
|
|
|
InterWarpCopyFnTy->getPointerTo()};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-02-17 00:20:16 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(
|
|
|
|
FnTy, /*Name=*/"__kmpc_nvptx_parallel_reduce_nowait");
|
|
|
|
break;
|
|
|
|
}
|
2017-02-17 00:48:49 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_teams_reduce_nowait: {
|
|
|
|
// Build int32_t __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
|
|
|
|
// int32_t num_vars, size_t reduce_size, void *reduce_data,
|
|
|
|
// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
|
|
|
|
// lane_offset, int16_t shortCircuit),
|
|
|
|
// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
|
|
|
|
// void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
|
|
|
|
// int32_t index, int32_t width),
|
|
|
|
// void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad,
|
|
|
|
// int32_t index, int32_t width, int32_t reduce))
|
|
|
|
llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
|
|
|
|
CGM.Int16Ty, CGM.Int16Ty};
|
|
|
|
auto *ShuffleReduceFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
|
|
|
|
auto *InterWarpCopyFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *CopyToScratchpadTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy,
|
|
|
|
CGM.Int32Ty, CGM.Int32Ty};
|
|
|
|
auto *CopyToScratchpadFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, CopyToScratchpadTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *LoadReduceTypeParams[] = {
|
|
|
|
CGM.VoidPtrTy, CGM.VoidPtrTy, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty};
|
|
|
|
auto *LoadReduceFnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, LoadReduceTypeParams,
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty,
|
|
|
|
CGM.Int32Ty,
|
|
|
|
CGM.SizeTy,
|
|
|
|
CGM.VoidPtrTy,
|
|
|
|
ShuffleReduceFnTy->getPointerTo(),
|
|
|
|
InterWarpCopyFnTy->getPointerTo(),
|
|
|
|
CopyToScratchpadFnTy->getPointerTo(),
|
|
|
|
LoadReduceFnTy->getPointerTo()};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-02-17 00:48:49 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(
|
|
|
|
FnTy, /*Name=*/"__kmpc_nvptx_teams_reduce_nowait");
|
|
|
|
break;
|
|
|
|
}
|
2017-02-17 00:20:16 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_end_reduce_nowait: {
|
|
|
|
// Build __kmpc_end_reduce_nowait(kmp_int32 global_tid);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int32Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2017-02-17 00:20:16 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(
|
|
|
|
FnTy, /*Name=*/"__kmpc_nvptx_end_reduce_nowait");
|
|
|
|
break;
|
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
case OMPRTL_NVPTX__kmpc_data_sharing_init_stack: {
|
|
|
|
/// Build void __kmpc_data_sharing_init_stack();
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_data_sharing_init_stack");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_data_sharing_push_stack: {
|
|
|
|
// Build void *__kmpc_data_sharing_push_stack(size_t size,
|
|
|
|
// int16_t UseSharedMemory);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.SizeTy, CGM.Int16Ty};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(
|
|
|
|
FnTy, /*Name=*/"__kmpc_data_sharing_push_stack");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_data_sharing_pop_stack: {
|
|
|
|
// Build void __kmpc_data_sharing_pop_stack(void *a);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.VoidPtrTy};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy,
|
|
|
|
/*Name=*/"__kmpc_data_sharing_pop_stack");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_begin_sharing_variables: {
|
|
|
|
/// Build void __kmpc_begin_sharing_variables(void ***args,
|
|
|
|
/// size_t n_args);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo(), CGM.SizeTy};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_begin_sharing_variables");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_end_sharing_variables: {
|
|
|
|
/// Build void __kmpc_end_sharing_variables();
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_sharing_variables");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OMPRTL_NVPTX__kmpc_get_shared_variables: {
|
|
|
|
/// Build void __kmpc_get_shared_variables(void ***GlobalArgs);
|
|
|
|
llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo()};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto *FnTy =
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
|
|
|
|
RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_get_shared_variables");
|
|
|
|
break;
|
|
|
|
}
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
return RTLFn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::createOffloadEntry(llvm::Constant *ID,
|
|
|
|
llvm::Constant *Addr,
|
2018-03-31 02:31:07 +08:00
|
|
|
uint64_t Size, int32_t,
|
|
|
|
llvm::GlobalValue::LinkageTypes) {
|
2016-03-22 09:48:56 +08:00
|
|
|
// TODO: Add support for global variables on the device after declare target
|
|
|
|
// support.
|
2018-04-17 04:16:21 +08:00
|
|
|
if (!isa<llvm::Function>(Addr))
|
2016-03-22 09:48:56 +08:00
|
|
|
return;
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Module &M = CGM.getModule();
|
|
|
|
llvm::LLVMContext &Ctx = CGM.getLLVMContext();
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
// Get "nvvm.annotations" metadata node
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("nvvm.annotations");
|
2016-03-22 09:48:56 +08:00
|
|
|
|
|
|
|
llvm::Metadata *MDVals[] = {
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::ConstantAsMetadata::get(Addr), llvm::MDString::get(Ctx, "kernel"),
|
2016-03-22 09:48:56 +08:00
|
|
|
llvm::ConstantAsMetadata::get(
|
|
|
|
llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1))};
|
|
|
|
// Append metadata to nvvm.annotations
|
|
|
|
MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitTargetOutlinedFunction(
|
|
|
|
const OMPExecutableDirective &D, StringRef ParentName,
|
|
|
|
llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
|
2016-03-29 13:34:15 +08:00
|
|
|
bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
|
2016-03-22 09:48:56 +08:00
|
|
|
if (!IsOffloadEntry) // Nothing to do.
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert(!ParentName.empty() && "Invalid target region parent name!");
|
|
|
|
|
2018-03-01 04:48:35 +08:00
|
|
|
CGOpenMPRuntimeNVPTX::ExecutionMode Mode = getExecutionMode(CGM);
|
2017-01-19 03:35:00 +08:00
|
|
|
switch (Mode) {
|
|
|
|
case CGOpenMPRuntimeNVPTX::ExecutionMode::Generic:
|
|
|
|
emitGenericKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
|
|
|
|
CodeGen);
|
|
|
|
break;
|
|
|
|
case CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd:
|
|
|
|
emitSpmdKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
|
|
|
|
CodeGen);
|
|
|
|
break;
|
|
|
|
case CGOpenMPRuntimeNVPTX::ExecutionMode::Unknown:
|
|
|
|
llvm_unreachable(
|
|
|
|
"Unknown programming model for OpenMP directive on NVPTX target.");
|
|
|
|
}
|
|
|
|
|
|
|
|
setPropertyExecutionMode(CGM, OutlinedFn->getName(), Mode);
|
2016-03-22 09:48:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-08 23:59:20 +08:00
|
|
|
CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
|
2017-01-19 03:35:00 +08:00
|
|
|
: CGOpenMPRuntime(CGM), CurrentExecutionMode(ExecutionMode::Unknown) {
|
2016-03-22 09:48:56 +08:00
|
|
|
if (!CGM.getLangOpts().OpenMPIsDevice)
|
|
|
|
llvm_unreachable("OpenMP NVPTX can only handle device code.");
|
|
|
|
}
|
2016-04-04 23:55:02 +08:00
|
|
|
|
2017-01-26 00:55:10 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitProcBindClause(CodeGenFunction &CGF,
|
|
|
|
OpenMPProcBindClauseKind ProcBind,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
// Do nothing in case of Spmd mode and L0 parallel.
|
|
|
|
// TODO: If in Spmd mode and L1 parallel emit the clause.
|
|
|
|
if (isInSpmdExecutionMode())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CGOpenMPRuntime::emitProcBindClause(CGF, ProcBind, Loc);
|
|
|
|
}
|
|
|
|
|
2017-01-25 09:18:34 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitNumThreadsClause(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *NumThreads,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
// Do nothing in case of Spmd mode and L0 parallel.
|
|
|
|
// TODO: If in Spmd mode and L1 parallel emit the clause.
|
|
|
|
if (isInSpmdExecutionMode())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CGOpenMPRuntime::emitNumThreadsClause(CGF, NumThreads, Loc);
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:55:02 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitNumTeamsClause(CodeGenFunction &CGF,
|
|
|
|
const Expr *NumTeams,
|
|
|
|
const Expr *ThreadLimit,
|
|
|
|
SourceLocation Loc) {}
|
|
|
|
|
2017-01-19 02:18:53 +08:00
|
|
|
llvm::Value *CGOpenMPRuntimeNVPTX::emitParallelOutlinedFunction(
|
2016-04-04 23:55:02 +08:00
|
|
|
const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
|
|
|
|
OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
|
2018-03-16 02:10:54 +08:00
|
|
|
SourceLocation Loc = D.getLocStart();
|
|
|
|
|
|
|
|
// Emit target region as a standalone region.
|
|
|
|
class NVPTXPrePostActionTy : public PrePostActionTy {
|
|
|
|
SourceLocation &Loc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NVPTXPrePostActionTy(SourceLocation &Loc) : Loc(Loc) {}
|
|
|
|
void Enter(CodeGenFunction &CGF) override {
|
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericVarsProlog(CGF, Loc);
|
|
|
|
}
|
|
|
|
void Exit(CodeGenFunction &CGF) override {
|
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericVarsEpilog(CGF);
|
|
|
|
}
|
|
|
|
} Action(Loc);
|
|
|
|
CodeGen.setAction(Action);
|
2018-03-14 22:17:45 +08:00
|
|
|
auto *OutlinedFun =
|
|
|
|
cast<llvm::Function>(CGOpenMPRuntime::emitParallelOutlinedFunction(
|
|
|
|
D, ThreadIDVar, InnermostKind, CodeGen));
|
|
|
|
if (!isInSpmdExecutionMode()) {
|
|
|
|
llvm::Function *WrapperFun =
|
|
|
|
createParallelDataSharingWrapper(OutlinedFun, D);
|
|
|
|
WrapperFunctionsMap[OutlinedFun] = WrapperFun;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OutlinedFun;
|
2017-01-19 02:18:53 +08:00
|
|
|
}
|
2016-04-04 23:55:02 +08:00
|
|
|
|
2017-01-19 02:18:53 +08:00
|
|
|
llvm::Value *CGOpenMPRuntimeNVPTX::emitTeamsOutlinedFunction(
|
|
|
|
const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
|
|
|
|
OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
|
2018-03-16 02:10:54 +08:00
|
|
|
SourceLocation Loc = D.getLocStart();
|
2017-01-19 02:18:53 +08:00
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
// Emit target region as a standalone region.
|
|
|
|
class NVPTXPrePostActionTy : public PrePostActionTy {
|
|
|
|
SourceLocation &Loc;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NVPTXPrePostActionTy(SourceLocation &Loc) : Loc(Loc) {}
|
|
|
|
void Enter(CodeGenFunction &CGF) override {
|
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericVarsProlog(CGF, Loc);
|
|
|
|
}
|
|
|
|
void Exit(CodeGenFunction &CGF) override {
|
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericVarsEpilog(CGF);
|
|
|
|
}
|
|
|
|
} Action(Loc);
|
|
|
|
CodeGen.setAction(Action);
|
2017-01-19 02:18:53 +08:00
|
|
|
llvm::Value *OutlinedFunVal = CGOpenMPRuntime::emitTeamsOutlinedFunction(
|
|
|
|
D, ThreadIDVar, InnermostKind, CodeGen);
|
|
|
|
llvm::Function *OutlinedFun = cast<llvm::Function>(OutlinedFunVal);
|
|
|
|
OutlinedFun->removeFnAttr(llvm::Attribute::NoInline);
|
2017-05-29 13:38:20 +08:00
|
|
|
OutlinedFun->removeFnAttr(llvm::Attribute::OptimizeNone);
|
2017-01-19 02:18:53 +08:00
|
|
|
OutlinedFun->addFnAttr(llvm::Attribute::AlwaysInline);
|
2016-04-04 23:55:02 +08:00
|
|
|
|
|
|
|
return OutlinedFun;
|
|
|
|
}
|
|
|
|
|
2018-03-16 02:10:54 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericVarsProlog(CodeGenFunction &CGF,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
|
|
|
|
const auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
|
|
|
|
if (I == FunctionGlobalizedDecls.end())
|
|
|
|
return;
|
2018-03-20 22:45:59 +08:00
|
|
|
if (const RecordDecl *GlobalizedVarsRecord = I->getSecond().GlobalRecord) {
|
|
|
|
QualType RecTy = CGM.getContext().getRecordType(GlobalizedVarsRecord);
|
|
|
|
|
|
|
|
// Recover pointer to this function's global record. The runtime will
|
|
|
|
// handle the specifics of the allocation of the memory.
|
|
|
|
// Use actual memory size of the record including the padding
|
|
|
|
// for alignment purposes.
|
|
|
|
unsigned Alignment =
|
|
|
|
CGM.getContext().getTypeAlignInChars(RecTy).getQuantity();
|
|
|
|
unsigned GlobalRecordSize =
|
|
|
|
CGM.getContext().getTypeSizeInChars(RecTy).getQuantity();
|
|
|
|
GlobalRecordSize = llvm::alignTo(GlobalRecordSize, Alignment);
|
|
|
|
// TODO: allow the usage of shared memory to be controlled by
|
|
|
|
// the user, for now, default to global.
|
|
|
|
llvm::Value *GlobalRecordSizeArg[] = {
|
|
|
|
llvm::ConstantInt::get(CGM.SizeTy, GlobalRecordSize),
|
|
|
|
CGF.Builder.getInt16(/*UseSharedMemory=*/0)};
|
|
|
|
llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_push_stack),
|
|
|
|
GlobalRecordSizeArg);
|
|
|
|
llvm::Value *GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
GlobalRecValue, CGF.ConvertTypeForMem(RecTy)->getPointerTo());
|
|
|
|
LValue Base =
|
|
|
|
CGF.MakeNaturalAlignPointeeAddrLValue(GlobalRecCastAddr, RecTy);
|
|
|
|
I->getSecond().GlobalRecordAddr = GlobalRecValue;
|
|
|
|
|
|
|
|
// Emit the "global alloca" which is a GEP from the global declaration
|
|
|
|
// record using the pointer returned by the runtime.
|
|
|
|
for (auto &Rec : I->getSecond().LocalVarData) {
|
|
|
|
bool EscapedParam = I->getSecond().EscapedParameters.count(Rec.first);
|
|
|
|
llvm::Value *ParValue;
|
|
|
|
if (EscapedParam) {
|
|
|
|
const auto *VD = cast<VarDecl>(Rec.first);
|
|
|
|
LValue ParLVal =
|
|
|
|
CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
|
|
|
|
ParValue = CGF.EmitLoadOfScalar(ParLVal, Loc);
|
|
|
|
}
|
|
|
|
const FieldDecl *FD = Rec.second.first;
|
|
|
|
LValue VarAddr = CGF.EmitLValueForField(Base, FD);
|
|
|
|
Rec.second.second = VarAddr.getAddress();
|
|
|
|
if (EscapedParam) {
|
|
|
|
const auto *VD = cast<VarDecl>(Rec.first);
|
|
|
|
CGF.EmitStoreOfScalar(ParValue, VarAddr);
|
|
|
|
I->getSecond().MappedParams->setVarAddr(CGF, VD, VarAddr.getAddress());
|
|
|
|
}
|
2018-03-16 02:10:54 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-20 22:45:59 +08:00
|
|
|
for (const ValueDecl *VD : I->getSecond().EscapedVariableLengthDecls) {
|
|
|
|
// Recover pointer to this function's global record. The runtime will
|
|
|
|
// handle the specifics of the allocation of the memory.
|
|
|
|
// Use actual memory size of the record including the padding
|
|
|
|
// for alignment purposes.
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2018-03-20 22:45:59 +08:00
|
|
|
llvm::Value *Size = CGF.getTypeSize(VD->getType());
|
|
|
|
CharUnits Align = CGM.getContext().getDeclAlign(VD);
|
|
|
|
Size = Bld.CreateNUWAdd(
|
|
|
|
Size, llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity() - 1));
|
|
|
|
llvm::Value *AlignVal =
|
|
|
|
llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity());
|
|
|
|
Size = Bld.CreateUDiv(Size, AlignVal);
|
|
|
|
Size = Bld.CreateNUWMul(Size, AlignVal);
|
|
|
|
// TODO: allow the usage of shared memory to be controlled by
|
|
|
|
// the user, for now, default to global.
|
|
|
|
llvm::Value *GlobalRecordSizeArg[] = {
|
|
|
|
Size, CGF.Builder.getInt16(/*UseSharedMemory=*/0)};
|
|
|
|
llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_push_stack),
|
|
|
|
GlobalRecordSizeArg);
|
|
|
|
llvm::Value *GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
GlobalRecValue, CGF.ConvertTypeForMem(VD->getType())->getPointerTo());
|
|
|
|
LValue Base = CGF.MakeAddrLValue(GlobalRecCastAddr, VD->getType(),
|
|
|
|
CGM.getContext().getDeclAlign(VD),
|
|
|
|
AlignmentSource::Decl);
|
|
|
|
I->getSecond().MappedParams->setVarAddr(CGF, cast<VarDecl>(VD),
|
|
|
|
Base.getAddress());
|
|
|
|
I->getSecond().EscapedVariableLengthDeclsAddrs.emplace_back(GlobalRecValue);
|
|
|
|
}
|
2018-03-16 02:10:54 +08:00
|
|
|
I->getSecond().MappedParams->apply(CGF);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericVarsEpilog(CodeGenFunction &CGF) {
|
|
|
|
const auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
|
2018-03-20 22:45:59 +08:00
|
|
|
if (I != FunctionGlobalizedDecls.end()) {
|
2018-03-16 02:10:54 +08:00
|
|
|
I->getSecond().MappedParams->restore(CGF);
|
|
|
|
if (!CGF.HaveInsertPoint())
|
|
|
|
return;
|
2018-03-20 22:45:59 +08:00
|
|
|
for (llvm::Value *Addr :
|
|
|
|
llvm::reverse(I->getSecond().EscapedVariableLengthDeclsAddrs)) {
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_pop_stack),
|
|
|
|
Addr);
|
|
|
|
}
|
|
|
|
if (I->getSecond().GlobalRecordAddr) {
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_pop_stack),
|
|
|
|
I->getSecond().GlobalRecordAddr);
|
|
|
|
}
|
2018-03-16 02:10:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 23:55:02 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitTeamsCall(CodeGenFunction &CGF,
|
|
|
|
const OMPExecutableDirective &D,
|
|
|
|
SourceLocation Loc,
|
|
|
|
llvm::Value *OutlinedFn,
|
|
|
|
ArrayRef<llvm::Value *> CapturedVars) {
|
|
|
|
if (!CGF.HaveInsertPoint())
|
|
|
|
return;
|
|
|
|
|
2018-03-20 01:04:07 +08:00
|
|
|
Address ZeroAddr = CGF.CreateMemTemp(
|
|
|
|
CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
|
|
|
|
/*Name*/ ".zero.addr");
|
2016-04-04 23:55:02 +08:00
|
|
|
CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
|
|
|
|
llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
|
2018-03-20 01:04:07 +08:00
|
|
|
OutlinedFnArgs.push_back(emitThreadIDAddress(CGF, Loc).getPointer());
|
2016-04-04 23:55:02 +08:00
|
|
|
OutlinedFnArgs.push_back(ZeroAddr.getPointer());
|
|
|
|
OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
|
2017-08-14 23:01:03 +08:00
|
|
|
emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
|
2016-04-04 23:55:02 +08:00
|
|
|
}
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitParallelCall(
|
|
|
|
CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
|
|
|
|
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
|
|
|
|
if (!CGF.HaveInsertPoint())
|
|
|
|
return;
|
|
|
|
|
2017-01-19 03:35:00 +08:00
|
|
|
if (isInSpmdExecutionMode())
|
|
|
|
emitSpmdParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
|
|
|
|
else
|
|
|
|
emitGenericParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
|
2017-01-10 23:42:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitGenericParallelCall(
|
|
|
|
CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
|
|
|
|
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
|
|
|
|
llvm::Function *Fn = cast<llvm::Function>(OutlinedFn);
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::Function *WFn = WrapperFunctionsMap[Fn];
|
|
|
|
|
|
|
|
assert(WFn && "Wrapper function does not exist!");
|
|
|
|
|
|
|
|
// Force inline this outlined function at its call site.
|
|
|
|
Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
|
2017-01-10 23:42:51 +08:00
|
|
|
|
2018-03-20 01:04:07 +08:00
|
|
|
auto &&L0ParallelGen = [this, WFn, CapturedVars](CodeGenFunction &CGF,
|
2018-03-14 22:17:45 +08:00
|
|
|
PrePostActionTy &) {
|
2017-01-10 23:42:51 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
|
2018-03-14 22:17:45 +08:00
|
|
|
llvm::Value *ID = Bld.CreateBitOrPointerCast(WFn, CGM.Int8PtrTy);
|
|
|
|
|
|
|
|
// Prepare for parallel region. Indicate the outlined function.
|
|
|
|
llvm::Value *Args[] = {ID, /*RequiresOMPRuntime=*/Bld.getInt16(1)};
|
|
|
|
CGF.EmitRuntimeCall(createNVPTXRuntimeFunction(
|
|
|
|
OMPRTL_NVPTX__kmpc_kernel_prepare_parallel),
|
|
|
|
Args);
|
|
|
|
|
|
|
|
// Create a private scope that will globalize the arguments
|
|
|
|
// passed from the outside of the target region.
|
|
|
|
CodeGenFunction::OMPPrivateScope PrivateArgScope(CGF);
|
|
|
|
|
|
|
|
// There's somehting to share.
|
|
|
|
if (!CapturedVars.empty()) {
|
|
|
|
// Prepare for parallel region. Indicate the outlined function.
|
|
|
|
Address SharedArgs =
|
|
|
|
CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "shared_arg_refs");
|
|
|
|
llvm::Value *SharedArgsPtr = SharedArgs.getPointer();
|
|
|
|
|
|
|
|
llvm::Value *DataSharingArgs[] = {
|
|
|
|
SharedArgsPtr,
|
|
|
|
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())};
|
|
|
|
CGF.EmitRuntimeCall(createNVPTXRuntimeFunction(
|
|
|
|
OMPRTL_NVPTX__kmpc_begin_sharing_variables),
|
|
|
|
DataSharingArgs);
|
|
|
|
|
|
|
|
// Store variable address in a list of references to pass to workers.
|
|
|
|
unsigned Idx = 0;
|
|
|
|
ASTContext &Ctx = CGF.getContext();
|
|
|
|
Address SharedArgListAddress = CGF.EmitLoadOfPointer(SharedArgs,
|
|
|
|
Ctx.getPointerType(Ctx.getPointerType(Ctx.VoidPtrTy))
|
|
|
|
.castAs<PointerType>());
|
|
|
|
for (llvm::Value *V : CapturedVars) {
|
|
|
|
Address Dst = Bld.CreateConstInBoundsGEP(
|
|
|
|
SharedArgListAddress, Idx, CGF.getPointerSize());
|
2018-03-20 23:41:05 +08:00
|
|
|
llvm::Value * PtrV;
|
|
|
|
if (V->getType()->isIntegerTy())
|
|
|
|
PtrV = Bld.CreateIntToPtr(V, CGF.VoidPtrTy);
|
|
|
|
else
|
|
|
|
PtrV = Bld.CreatePointerBitCastOrAddrSpaceCast(V, CGF.VoidPtrTy);
|
2018-03-14 22:17:45 +08:00
|
|
|
CGF.EmitStoreOfScalar(PtrV, Dst, /*Volatile=*/false,
|
|
|
|
Ctx.getPointerType(Ctx.VoidPtrTy));
|
2018-03-16 02:10:54 +08:00
|
|
|
++Idx;
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-10 23:42:51 +08:00
|
|
|
|
|
|
|
// Activate workers. This barrier is used by the master to signal
|
|
|
|
// work for the workers.
|
|
|
|
syncCTAThreads(CGF);
|
|
|
|
|
|
|
|
// OpenMP [2.5, Parallel Construct, p.49]
|
|
|
|
// There is an implied barrier at the end of a parallel region. After the
|
|
|
|
// end of a parallel region, only the master thread of the team resumes
|
|
|
|
// execution of the enclosing task region.
|
|
|
|
//
|
|
|
|
// The master waits at this barrier until all workers are done.
|
|
|
|
syncCTAThreads(CGF);
|
|
|
|
|
2018-03-14 22:17:45 +08:00
|
|
|
if (!CapturedVars.empty())
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_sharing_variables));
|
|
|
|
|
2017-01-10 23:42:51 +08:00
|
|
|
// Remember for post-processing in worker loop.
|
2018-03-14 22:17:45 +08:00
|
|
|
Work.emplace_back(WFn);
|
2017-01-10 23:42:51 +08:00
|
|
|
};
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
|
|
|
|
llvm::Value *ThreadID = getThreadID(CGF, Loc);
|
2017-01-10 23:42:51 +08:00
|
|
|
llvm::Value *Args[] = {RTLoc, ThreadID};
|
|
|
|
|
2018-03-20 01:18:13 +08:00
|
|
|
auto &&SeqGen = [this, Fn, CapturedVars, &Args, Loc](CodeGenFunction &CGF,
|
|
|
|
PrePostActionTy &) {
|
2018-03-20 01:04:07 +08:00
|
|
|
auto &&CodeGen = [this, Fn, CapturedVars, Loc](CodeGenFunction &CGF,
|
|
|
|
PrePostActionTy &Action) {
|
2017-01-10 23:42:51 +08:00
|
|
|
Action.Enter(CGF);
|
|
|
|
|
|
|
|
llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
|
2018-03-20 01:04:07 +08:00
|
|
|
Address ZeroAddr =
|
|
|
|
CGF.CreateMemTemp(CGF.getContext().getIntTypeForBitwidth(
|
|
|
|
/*DestWidth=*/32, /*Signed=*/1),
|
|
|
|
".zero.addr");
|
|
|
|
CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
|
|
|
|
OutlinedFnArgs.push_back(emitThreadIDAddress(CGF, Loc).getPointer());
|
|
|
|
OutlinedFnArgs.push_back(ZeroAddr.getPointer());
|
2017-01-10 23:42:51 +08:00
|
|
|
OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
|
2017-08-14 23:01:03 +08:00
|
|
|
emitOutlinedFunctionCall(CGF, Loc, Fn, OutlinedFnArgs);
|
2017-01-10 23:42:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
RegionCodeGenTy RCG(CodeGen);
|
|
|
|
NVPTXActionTy Action(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel),
|
|
|
|
Args,
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel),
|
|
|
|
Args);
|
|
|
|
RCG.setAction(Action);
|
|
|
|
RCG(CGF);
|
|
|
|
};
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
if (IfCond) {
|
2017-01-10 23:42:51 +08:00
|
|
|
emitOMPIfClause(CGF, IfCond, L0ParallelGen, SeqGen);
|
2018-04-17 04:16:21 +08:00
|
|
|
} else {
|
2017-01-10 23:42:51 +08:00
|
|
|
CodeGenFunction::RunCleanupsScope Scope(CGF);
|
|
|
|
RegionCodeGenTy ThenRCG(L0ParallelGen);
|
|
|
|
ThenRCG(CGF);
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 03:35:00 +08:00
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitSpmdParallelCall(
|
|
|
|
CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
|
|
|
|
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
|
|
|
|
// Just call the outlined function to execute the parallel region.
|
|
|
|
// OutlinedFn(>id, &zero, CapturedStruct);
|
|
|
|
//
|
|
|
|
// TODO: Do something with IfCond when support for the 'if' clause
|
|
|
|
// is added on Spmd target directives.
|
|
|
|
llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
|
2018-03-01 04:48:35 +08:00
|
|
|
|
|
|
|
Address ZeroAddr = CGF.CreateMemTemp(
|
|
|
|
CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
|
|
|
|
".zero.addr");
|
|
|
|
CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
|
2018-03-20 01:04:07 +08:00
|
|
|
OutlinedFnArgs.push_back(emitThreadIDAddress(CGF, Loc).getPointer());
|
2018-03-01 04:48:35 +08:00
|
|
|
OutlinedFnArgs.push_back(ZeroAddr.getPointer());
|
2017-01-19 03:35:00 +08:00
|
|
|
OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
|
2017-08-14 23:01:03 +08:00
|
|
|
emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
|
2017-01-19 03:35:00 +08:00
|
|
|
}
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-01-05 04:18:55 +08:00
|
|
|
/// Cast value to the specified type.
|
2018-03-20 01:53:56 +08:00
|
|
|
static llvm::Value *castValueToType(CodeGenFunction &CGF, llvm::Value *Val,
|
|
|
|
QualType ValTy, QualType CastTy,
|
|
|
|
SourceLocation Loc) {
|
|
|
|
assert(!CGF.getContext().getTypeSizeInChars(CastTy).isZero() &&
|
|
|
|
"Cast type must sized.");
|
|
|
|
assert(!CGF.getContext().getTypeSizeInChars(ValTy).isZero() &&
|
|
|
|
"Val type must sized.");
|
|
|
|
llvm::Type *LLVMCastTy = CGF.ConvertTypeForMem(CastTy);
|
|
|
|
if (ValTy == CastTy)
|
2018-01-05 04:18:55 +08:00
|
|
|
return Val;
|
2018-03-20 01:53:56 +08:00
|
|
|
if (CGF.getContext().getTypeSizeInChars(ValTy) ==
|
|
|
|
CGF.getContext().getTypeSizeInChars(CastTy))
|
|
|
|
return CGF.Builder.CreateBitCast(Val, LLVMCastTy);
|
|
|
|
if (CastTy->isIntegerType() && ValTy->isIntegerType())
|
|
|
|
return CGF.Builder.CreateIntCast(Val, LLVMCastTy,
|
|
|
|
CastTy->hasSignedIntegerRepresentation());
|
|
|
|
Address CastItem = CGF.CreateMemTemp(CastTy);
|
2018-01-05 04:18:55 +08:00
|
|
|
Address ValCastItem = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CastItem, Val->getType()->getPointerTo(CastItem.getAddressSpace()));
|
2018-03-20 01:53:56 +08:00
|
|
|
CGF.EmitStoreOfScalar(Val, ValCastItem, /*Volatile=*/false, ValTy);
|
|
|
|
return CGF.EmitLoadOfScalar(CastItem, /*Volatile=*/false, CastTy, Loc);
|
2018-01-05 04:18:55 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// This function creates calls to one of two shuffle functions to copy
|
|
|
|
/// variables between lanes in a warp.
|
|
|
|
static llvm::Value *createRuntimeShuffleFunction(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Elem,
|
2018-03-20 01:53:56 +08:00
|
|
|
QualType ElemType,
|
|
|
|
llvm::Value *Offset,
|
|
|
|
SourceLocation Loc) {
|
2018-04-17 04:16:21 +08:00
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:20:16 +08:00
|
|
|
CGOpenMPRuntimeNVPTX &RT =
|
|
|
|
*(static_cast<CGOpenMPRuntimeNVPTX *>(&CGM.getOpenMPRuntime()));
|
|
|
|
|
2018-03-20 01:53:56 +08:00
|
|
|
CharUnits Size = CGF.getContext().getTypeSizeInChars(ElemType);
|
|
|
|
assert(Size.getQuantity() <= 8 &&
|
|
|
|
"Unsupported bitwidth in shuffle instruction.");
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-03-20 01:53:56 +08:00
|
|
|
OpenMPRTLFunctionNVPTX ShuffleFn = Size.getQuantity() <= 4
|
2017-02-17 00:20:16 +08:00
|
|
|
? OMPRTL_NVPTX__kmpc_shuffle_int32
|
|
|
|
: OMPRTL_NVPTX__kmpc_shuffle_int64;
|
|
|
|
|
|
|
|
// Cast all types to 32- or 64-bit values before calling shuffle routines.
|
2018-03-20 01:53:56 +08:00
|
|
|
QualType CastTy = CGF.getContext().getIntTypeForBitwidth(
|
|
|
|
Size.getQuantity() <= 4 ? 32 : 64, /*Signed=*/1);
|
|
|
|
llvm::Value *ElemCast = castValueToType(CGF, Elem, ElemType, CastTy, Loc);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *WarpSize =
|
2018-01-05 04:18:55 +08:00
|
|
|
Bld.CreateIntCast(getNVPTXWarpSize(CGF), CGM.Int16Ty, /*isSigned=*/true);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ShuffledVal = CGF.EmitRuntimeCall(
|
|
|
|
RT.createNVPTXRuntimeFunction(ShuffleFn), {ElemCast, Offset, WarpSize});
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-03-20 01:53:56 +08:00
|
|
|
return castValueToType(CGF, ShuffledVal, CastTy, ElemType, Loc);
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
enum CopyAction : unsigned {
|
|
|
|
// RemoteLaneToThread: Copy over a Reduce list from a remote lane in
|
|
|
|
// the warp using shuffle instructions.
|
|
|
|
RemoteLaneToThread,
|
|
|
|
// ThreadCopy: Make a copy of a Reduce list on the thread's stack.
|
|
|
|
ThreadCopy,
|
2017-02-17 00:48:49 +08:00
|
|
|
// ThreadToScratchpad: Copy a team-reduced array to the scratchpad.
|
|
|
|
ThreadToScratchpad,
|
|
|
|
// ScratchpadToThread: Copy from a scratchpad array in global memory
|
|
|
|
// containing team-reduced data to a thread's stack.
|
|
|
|
ScratchpadToThread,
|
2017-02-17 00:20:16 +08:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2017-02-17 00:48:49 +08:00
|
|
|
struct CopyOptionsTy {
|
|
|
|
llvm::Value *RemoteLaneOffset;
|
|
|
|
llvm::Value *ScratchpadIndex;
|
|
|
|
llvm::Value *ScratchpadWidth;
|
|
|
|
};
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// Emit instructions to copy a Reduce list, which contains partially
|
|
|
|
/// aggregated values, in the specified direction.
|
2017-02-17 00:48:49 +08:00
|
|
|
static void emitReductionListCopy(
|
|
|
|
CopyAction Action, CodeGenFunction &CGF, QualType ReductionArrayTy,
|
|
|
|
ArrayRef<const Expr *> Privates, Address SrcBase, Address DestBase,
|
|
|
|
CopyOptionsTy CopyOptions = {nullptr, nullptr, nullptr}) {
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
CodeGenModule &CGM = CGF.CGM;
|
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *RemoteLaneOffset = CopyOptions.RemoteLaneOffset;
|
|
|
|
llvm::Value *ScratchpadIndex = CopyOptions.ScratchpadIndex;
|
|
|
|
llvm::Value *ScratchpadWidth = CopyOptions.ScratchpadWidth;
|
2017-02-17 00:48:49 +08:00
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
// Iterates, element-by-element, through the source Reduce list and
|
|
|
|
// make a copy.
|
|
|
|
unsigned Idx = 0;
|
2017-02-17 00:48:49 +08:00
|
|
|
unsigned Size = Privates.size();
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Expr *Private : Privates) {
|
2017-02-17 00:20:16 +08:00
|
|
|
Address SrcElementAddr = Address::invalid();
|
|
|
|
Address DestElementAddr = Address::invalid();
|
|
|
|
Address DestElementPtrAddr = Address::invalid();
|
|
|
|
// Should we shuffle in an element from a remote lane?
|
|
|
|
bool ShuffleInElement = false;
|
|
|
|
// Set to true to update the pointer in the dest Reduce list to a
|
|
|
|
// newly created element.
|
|
|
|
bool UpdateDestListPtr = false;
|
2017-02-17 00:48:49 +08:00
|
|
|
// Increment the src or dest pointer to the scratchpad, for each
|
|
|
|
// new element.
|
|
|
|
bool IncrScratchpadSrc = false;
|
|
|
|
bool IncrScratchpadDest = false;
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
switch (Action) {
|
|
|
|
case RemoteLaneToThread: {
|
|
|
|
// Step 1.1: Get the address for the src element in the Reduce list.
|
|
|
|
Address SrcElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
|
2018-01-05 04:18:55 +08:00
|
|
|
SrcElementAddr = CGF.EmitLoadOfPointer(
|
|
|
|
SrcElementPtrAddr,
|
|
|
|
C.getPointerType(Private->getType())->castAs<PointerType>());
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// Step 1.2: Create a temporary to store the element in the destination
|
|
|
|
// Reduce list.
|
|
|
|
DestElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
|
|
|
|
DestElementAddr =
|
|
|
|
CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
|
|
|
|
ShuffleInElement = true;
|
|
|
|
UpdateDestListPtr = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ThreadCopy: {
|
|
|
|
// Step 1.1: Get the address for the src element in the Reduce list.
|
|
|
|
Address SrcElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
|
2018-01-05 04:18:55 +08:00
|
|
|
SrcElementAddr = CGF.EmitLoadOfPointer(
|
|
|
|
SrcElementPtrAddr,
|
|
|
|
C.getPointerType(Private->getType())->castAs<PointerType>());
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// Step 1.2: Get the address for dest element. The destination
|
|
|
|
// element has already been created on the thread's stack.
|
|
|
|
DestElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
|
2018-01-05 04:18:55 +08:00
|
|
|
DestElementAddr = CGF.EmitLoadOfPointer(
|
|
|
|
DestElementPtrAddr,
|
|
|
|
C.getPointerType(Private->getType())->castAs<PointerType>());
|
2017-02-17 00:20:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-02-17 00:48:49 +08:00
|
|
|
case ThreadToScratchpad: {
|
|
|
|
// Step 1.1: Get the address for the src element in the Reduce list.
|
|
|
|
Address SrcElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
|
2018-01-05 04:18:55 +08:00
|
|
|
SrcElementAddr = CGF.EmitLoadOfPointer(
|
|
|
|
SrcElementPtrAddr,
|
|
|
|
C.getPointerType(Private->getType())->castAs<PointerType>());
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// Step 1.2: Get the address for dest element:
|
|
|
|
// address = base + index * ElementSizeInChars.
|
2018-04-07 00:03:36 +08:00
|
|
|
llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CurrentOffset =
|
2018-04-07 00:03:36 +08:00
|
|
|
Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ScratchPadElemAbsolutePtrVal =
|
2018-04-07 00:03:36 +08:00
|
|
|
Bld.CreateNUWAdd(DestBase.getPointer(), CurrentOffset);
|
2017-02-17 00:48:49 +08:00
|
|
|
ScratchPadElemAbsolutePtrVal =
|
|
|
|
Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
|
2018-01-05 04:18:55 +08:00
|
|
|
DestElementAddr = Address(ScratchPadElemAbsolutePtrVal,
|
|
|
|
C.getTypeAlignInChars(Private->getType()));
|
2017-02-17 00:48:49 +08:00
|
|
|
IncrScratchpadDest = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ScratchpadToThread: {
|
|
|
|
// Step 1.1: Get the address for the src element in the scratchpad.
|
|
|
|
// address = base + index * ElementSizeInChars.
|
2018-04-07 00:03:36 +08:00
|
|
|
llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CurrentOffset =
|
2018-04-07 00:03:36 +08:00
|
|
|
Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ScratchPadElemAbsolutePtrVal =
|
2018-04-07 00:03:36 +08:00
|
|
|
Bld.CreateNUWAdd(SrcBase.getPointer(), CurrentOffset);
|
2017-02-17 00:48:49 +08:00
|
|
|
ScratchPadElemAbsolutePtrVal =
|
|
|
|
Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
|
|
|
|
SrcElementAddr = Address(ScratchPadElemAbsolutePtrVal,
|
|
|
|
C.getTypeAlignInChars(Private->getType()));
|
|
|
|
IncrScratchpadSrc = true;
|
|
|
|
|
|
|
|
// Step 1.2: Create a temporary to store the element in the destination
|
|
|
|
// Reduce list.
|
|
|
|
DestElementPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
|
|
|
|
DestElementAddr =
|
|
|
|
CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
|
|
|
|
UpdateDestListPtr = true;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regardless of src and dest of copy, we emit the load of src
|
|
|
|
// element as this is required in all directions
|
|
|
|
SrcElementAddr = Bld.CreateElementBitCast(
|
|
|
|
SrcElementAddr, CGF.ConvertTypeForMem(Private->getType()));
|
|
|
|
llvm::Value *Elem =
|
|
|
|
CGF.EmitLoadOfScalar(SrcElementAddr, /*Volatile=*/false,
|
2018-01-24 02:12:38 +08:00
|
|
|
Private->getType(), Private->getExprLoc());
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// Now that all active lanes have read the element in the
|
|
|
|
// Reduce list, shuffle over the value from the remote lane.
|
2018-03-20 01:53:56 +08:00
|
|
|
if (ShuffleInElement) {
|
|
|
|
Elem =
|
|
|
|
createRuntimeShuffleFunction(CGF, Elem, Private->getType(),
|
|
|
|
RemoteLaneOffset, Private->getExprLoc());
|
|
|
|
}
|
2018-01-05 04:18:55 +08:00
|
|
|
|
|
|
|
DestElementAddr = Bld.CreateElementBitCast(DestElementAddr,
|
|
|
|
SrcElementAddr.getElementType());
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// Store the source element value to the dest element address.
|
|
|
|
CGF.EmitStoreOfScalar(Elem, DestElementAddr, /*Volatile=*/false,
|
|
|
|
Private->getType());
|
|
|
|
|
|
|
|
// Step 3.1: Modify reference in dest Reduce list as needed.
|
|
|
|
// Modifying the reference in Reduce list to point to the newly
|
|
|
|
// created element. The element is live in the current function
|
|
|
|
// scope and that of functions it invokes (i.e., reduce_function).
|
|
|
|
// RemoteReduceData[i] = (void*)&RemoteElem
|
|
|
|
if (UpdateDestListPtr) {
|
|
|
|
CGF.EmitStoreOfScalar(Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
DestElementAddr.getPointer(), CGF.VoidPtrTy),
|
|
|
|
DestElementPtrAddr, /*Volatile=*/false,
|
|
|
|
C.VoidPtrTy);
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:48:49 +08:00
|
|
|
// Step 4.1: Increment SrcBase/DestBase so that it points to the starting
|
|
|
|
// address of the next element in scratchpad memory, unless we're currently
|
|
|
|
// processing the last one. Memory alignment is also taken care of here.
|
|
|
|
if ((IncrScratchpadDest || IncrScratchpadSrc) && (Idx + 1 < Size)) {
|
|
|
|
llvm::Value *ScratchpadBasePtr =
|
|
|
|
IncrScratchpadDest ? DestBase.getPointer() : SrcBase.getPointer();
|
2018-04-07 00:03:36 +08:00
|
|
|
llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
|
|
|
|
ScratchpadBasePtr = Bld.CreateNUWAdd(
|
2017-02-17 00:48:49 +08:00
|
|
|
ScratchpadBasePtr,
|
2018-04-07 00:03:36 +08:00
|
|
|
Bld.CreateNUWMul(ScratchpadWidth, ElementSizeInChars));
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// Take care of global memory alignment for performance
|
2018-04-07 00:03:36 +08:00
|
|
|
ScratchpadBasePtr = Bld.CreateNUWSub(
|
|
|
|
ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1));
|
|
|
|
ScratchpadBasePtr = Bld.CreateUDiv(
|
2017-02-17 00:48:49 +08:00
|
|
|
ScratchpadBasePtr,
|
|
|
|
llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
|
2018-04-07 00:03:36 +08:00
|
|
|
ScratchpadBasePtr = Bld.CreateNUWAdd(
|
|
|
|
ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1));
|
|
|
|
ScratchpadBasePtr = Bld.CreateNUWMul(
|
2017-02-17 00:48:49 +08:00
|
|
|
ScratchpadBasePtr,
|
|
|
|
llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
|
|
|
|
|
|
|
|
if (IncrScratchpadDest)
|
|
|
|
DestBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
|
|
|
|
else /* IncrScratchpadSrc = true */
|
|
|
|
SrcBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
|
|
|
|
}
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
++Idx;
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:48:49 +08:00
|
|
|
/// This function emits a helper that loads data from the scratchpad array
|
|
|
|
/// and (optionally) reduces it with the input operand.
|
|
|
|
///
|
|
|
|
/// load_and_reduce(local, scratchpad, index, width, should_reduce)
|
|
|
|
/// reduce_data remote;
|
|
|
|
/// for elem in remote:
|
|
|
|
/// remote.elem = Scratchpad[elem_id][index]
|
|
|
|
/// if (should_reduce)
|
|
|
|
/// local = local @ remote
|
|
|
|
/// else
|
|
|
|
/// local = remote
|
2018-01-05 03:45:16 +08:00
|
|
|
static llvm::Value *emitReduceScratchpadFunction(
|
|
|
|
CodeGenModule &CGM, ArrayRef<const Expr *> Privates,
|
|
|
|
QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) {
|
2018-04-17 04:16:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// Destination of the copy.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// Base address of the scratchpad array, with each element storing a
|
|
|
|
// Reduce list per team.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// A source index into the scratchpad array.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// Row width of an element in the scratchpad array, typically
|
|
|
|
// the number of teams.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// If should_reduce == 1, then it's load AND reduce,
|
|
|
|
// If should_reduce == 0 (or otherwise), then it only loads (+ copy).
|
|
|
|
// The latter case is used for initialization.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ShouldReduceArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
Int32Ty, ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
Args.push_back(&ReduceListArg);
|
|
|
|
Args.push_back(&ScratchPadArg);
|
|
|
|
Args.push_back(&IndexArg);
|
|
|
|
Args.push_back(&WidthArg);
|
|
|
|
Args.push_back(&ShouldReduceArg);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
const CGFunctionInfo &CGFI =
|
|
|
|
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
|
2017-02-17 00:48:49 +08:00
|
|
|
auto *Fn = llvm::Function::Create(
|
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
|
|
|
"_omp_reduction_load_and_reduce", &CGM.getModule());
|
2018-03-01 07:46:35 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
|
2018-04-11 04:10:53 +08:00
|
|
|
Fn->setDoesNotRecurse();
|
2017-02-17 00:48:49 +08:00
|
|
|
CodeGenFunction CGF(CGM);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// Get local Reduce list pointer.
|
|
|
|
Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
|
|
|
|
Address ReduceListAddr(
|
|
|
|
Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
|
2018-01-24 02:12:38 +08:00
|
|
|
C.VoidPtrTy, Loc),
|
2017-02-17 00:48:49 +08:00
|
|
|
CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
|
|
|
|
CGF.getPointerAlign());
|
|
|
|
|
|
|
|
Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
|
|
|
|
llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
|
2018-01-24 02:12:38 +08:00
|
|
|
AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
|
2018-01-24 02:12:38 +08:00
|
|
|
llvm::Value *IndexVal = Bld.CreateIntCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc),
|
|
|
|
CGM.SizeTy, /*isSigned=*/true);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
|
2018-01-24 02:12:38 +08:00
|
|
|
llvm::Value *WidthVal = Bld.CreateIntCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false, Int32Ty, Loc),
|
|
|
|
CGM.SizeTy, /*isSigned=*/true);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrShouldReduceArg = CGF.GetAddrOfLocalVar(&ShouldReduceArg);
|
|
|
|
llvm::Value *ShouldReduceVal = CGF.EmitLoadOfScalar(
|
2018-01-24 02:12:38 +08:00
|
|
|
AddrShouldReduceArg, /*Volatile=*/false, Int32Ty, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// The absolute ptr address to the base addr of the next element to copy.
|
|
|
|
llvm::Value *CumulativeElemBasePtr =
|
|
|
|
Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
|
|
|
|
Address SrcDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
|
|
|
|
|
|
|
|
// Create a Remote Reduce list to store the elements read from the
|
|
|
|
// scratchpad array.
|
|
|
|
Address RemoteReduceList =
|
|
|
|
CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_red_list");
|
|
|
|
|
|
|
|
// Assemble remote Reduce list from scratchpad array.
|
|
|
|
emitReductionListCopy(ScratchpadToThread, CGF, ReductionArrayTy, Privates,
|
|
|
|
SrcDataAddr, RemoteReduceList,
|
|
|
|
{/*RemoteLaneOffset=*/nullptr,
|
|
|
|
/*ScratchpadIndex=*/IndexVal,
|
|
|
|
/*ScratchpadWidth=*/WidthVal});
|
|
|
|
|
|
|
|
llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
|
|
|
|
llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
|
|
|
|
llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CondReduce = Bld.CreateIsNotNull(ShouldReduceVal);
|
2017-02-17 00:48:49 +08:00
|
|
|
Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ThenBB);
|
|
|
|
// We should reduce with the local Reduce list.
|
|
|
|
// reduce_function(LocalReduceList, RemoteReduceList)
|
|
|
|
llvm::Value *LocalDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
ReduceListAddr.getPointer(), CGF.VoidPtrTy);
|
|
|
|
llvm::Value *RemoteDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
RemoteReduceList.getPointer(), CGF.VoidPtrTy);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
|
|
|
|
CGF, Loc, ReduceFn, {LocalDataPtr, RemoteDataPtr});
|
2017-02-17 00:48:49 +08:00
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ElseBB);
|
|
|
|
// No reduction; just copy:
|
|
|
|
// Local Reduce list = Remote Reduce list.
|
|
|
|
emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
|
|
|
|
RemoteReduceList, ReduceListAddr);
|
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(MergeBB);
|
|
|
|
|
|
|
|
CGF.FinishFunction();
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function emits a helper that stores reduced data from the team
|
|
|
|
/// master to a scratchpad array in global memory.
|
|
|
|
///
|
|
|
|
/// for elem in Reduce List:
|
|
|
|
/// scratchpad[elem_id][index] = elem
|
|
|
|
///
|
2017-05-27 04:08:24 +08:00
|
|
|
static llvm::Value *emitCopyToScratchpad(CodeGenModule &CGM,
|
|
|
|
ArrayRef<const Expr *> Privates,
|
2018-01-05 03:45:16 +08:00
|
|
|
QualType ReductionArrayTy,
|
|
|
|
SourceLocation Loc) {
|
2017-02-17 00:48:49 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
// Source of the copy.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// Base address of the scratchpad array, with each element storing a
|
|
|
|
// Reduce list per team.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// A destination index into the scratchpad array, typically the team
|
|
|
|
// identifier.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
// Row width of an element in the scratchpad array, typically
|
|
|
|
// the number of teams.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
Args.push_back(&ReduceListArg);
|
|
|
|
Args.push_back(&ScratchPadArg);
|
|
|
|
Args.push_back(&IndexArg);
|
|
|
|
Args.push_back(&WidthArg);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
const CGFunctionInfo &CGFI =
|
|
|
|
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
|
2017-02-17 00:48:49 +08:00
|
|
|
auto *Fn = llvm::Function::Create(
|
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
|
|
|
"_omp_reduction_copy_to_scratchpad", &CGM.getModule());
|
2018-03-01 07:46:35 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
|
2018-04-11 04:10:53 +08:00
|
|
|
Fn->setDoesNotRecurse();
|
2017-02-17 00:48:49 +08:00
|
|
|
CodeGenFunction CGF(CGM);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
|
|
|
|
Address SrcDataAddr(
|
|
|
|
Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
|
2018-01-24 02:12:38 +08:00
|
|
|
C.VoidPtrTy, Loc),
|
2017-02-17 00:48:49 +08:00
|
|
|
CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
|
|
|
|
CGF.getPointerAlign());
|
|
|
|
|
|
|
|
Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
|
|
|
|
llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
|
2018-01-24 02:12:38 +08:00
|
|
|
AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
|
2018-01-24 02:12:38 +08:00
|
|
|
llvm::Value *IndexVal = Bld.CreateIntCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc),
|
|
|
|
CGF.SizeTy, /*isSigned=*/true);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
|
|
|
|
llvm::Value *WidthVal =
|
|
|
|
Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false,
|
|
|
|
Int32Ty, SourceLocation()),
|
|
|
|
CGF.SizeTy, /*isSigned=*/true);
|
|
|
|
|
|
|
|
// The absolute ptr address to the base addr of the next element to copy.
|
|
|
|
llvm::Value *CumulativeElemBasePtr =
|
|
|
|
Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
|
|
|
|
Address DestDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
|
|
|
|
|
|
|
|
emitReductionListCopy(ThreadToScratchpad, CGF, ReductionArrayTy, Privates,
|
|
|
|
SrcDataAddr, DestDataAddr,
|
|
|
|
{/*RemoteLaneOffset=*/nullptr,
|
|
|
|
/*ScratchpadIndex=*/IndexVal,
|
|
|
|
/*ScratchpadWidth=*/WidthVal});
|
|
|
|
|
|
|
|
CGF.FinishFunction();
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
/// This function emits a helper that gathers Reduce lists from the first
|
|
|
|
/// lane of every active warp to lanes in the first warp.
|
|
|
|
///
|
|
|
|
/// void inter_warp_copy_func(void* reduce_data, num_warps)
|
|
|
|
/// shared smem[warp_size];
|
|
|
|
/// For all data entries D in reduce_data:
|
|
|
|
/// If (I am the first lane in each warp)
|
|
|
|
/// Copy my local D to smem[warp_id]
|
|
|
|
/// sync
|
|
|
|
/// if (I am the first warp)
|
|
|
|
/// Copy smem[thread_id] to my local D
|
|
|
|
/// sync
|
|
|
|
static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM,
|
|
|
|
ArrayRef<const Expr *> Privates,
|
2018-01-05 03:45:16 +08:00
|
|
|
QualType ReductionArrayTy,
|
|
|
|
SourceLocation Loc) {
|
2018-04-17 04:16:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
|
|
|
llvm::Module &M = CGM.getModule();
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// ReduceList: thread local Reduce list.
|
|
|
|
// At the stage of the computation when this function is called, partially
|
|
|
|
// aggregated values reside in the first lane of every active warp.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
// NumWarps: number of warps active in the parallel region. This could
|
|
|
|
// be smaller than 32 (max warps in a CTA) for partial block reduction.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl NumWarpsArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
2017-06-09 21:40:18 +08:00
|
|
|
C.getIntTypeForBitwidth(32, /* Signed */ true),
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
FunctionArgList Args;
|
|
|
|
Args.push_back(&ReduceListArg);
|
|
|
|
Args.push_back(&NumWarpsArg);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
const CGFunctionInfo &CGFI =
|
|
|
|
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
|
2017-02-17 00:20:16 +08:00
|
|
|
auto *Fn = llvm::Function::Create(
|
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
|
|
|
"_omp_reduction_inter_warp_copy_func", &CGM.getModule());
|
2018-03-01 07:46:35 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
|
2018-04-11 04:10:53 +08:00
|
|
|
Fn->setDoesNotRecurse();
|
2017-02-17 00:20:16 +08:00
|
|
|
CodeGenFunction CGF(CGM);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// This array is used as a medium to transfer, one reduce element at a time,
|
|
|
|
// the data from the first lane of every warp to lanes in the first warp
|
|
|
|
// in order to perform the final step of a reduction in a parallel region
|
|
|
|
// (reduction across warps). The array is placed in NVPTX __shared__ memory
|
|
|
|
// for reduced latency, as well as to have a distinct copy for concurrently
|
|
|
|
// executing target regions. The array is declared with common linkage so
|
|
|
|
// as to be shared across compilation units.
|
2018-04-17 04:16:21 +08:00
|
|
|
StringRef TransferMediumName =
|
2017-02-17 00:20:16 +08:00
|
|
|
"__openmp_nvptx_data_transfer_temporary_storage";
|
|
|
|
llvm::GlobalVariable *TransferMedium =
|
|
|
|
M.getGlobalVariable(TransferMediumName);
|
|
|
|
if (!TransferMedium) {
|
|
|
|
auto *Ty = llvm::ArrayType::get(CGM.Int64Ty, WarpSize);
|
|
|
|
unsigned SharedAddressSpace = C.getTargetAddressSpace(LangAS::cuda_shared);
|
|
|
|
TransferMedium = new llvm::GlobalVariable(
|
|
|
|
M, Ty,
|
|
|
|
/*isConstant=*/false, llvm::GlobalVariable::CommonLinkage,
|
|
|
|
llvm::Constant::getNullValue(Ty), TransferMediumName,
|
|
|
|
/*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
|
|
|
|
SharedAddressSpace);
|
2018-04-17 04:16:21 +08:00
|
|
|
CGM.addCompilerUsedGlobal(TransferMedium);
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the CUDA thread id of the current OpenMP thread on the GPU.
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ThreadID = getNVPTXThreadID(CGF);
|
2017-02-17 00:20:16 +08:00
|
|
|
// nvptx_lane_id = nvptx_id % warpsize
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *LaneID = getNVPTXLaneID(CGF);
|
2017-02-17 00:20:16 +08:00
|
|
|
// nvptx_warp_id = nvptx_id / warpsize
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *WarpID = getNVPTXWarpID(CGF);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
|
|
|
|
Address LocalReduceList(
|
|
|
|
Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
|
|
|
|
C.VoidPtrTy, SourceLocation()),
|
|
|
|
CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
|
|
|
|
CGF.getPointerAlign());
|
|
|
|
|
|
|
|
unsigned Idx = 0;
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Expr *Private : Privates) {
|
2017-02-17 00:20:16 +08:00
|
|
|
//
|
|
|
|
// Warp master copies reduce element to transfer medium in __shared__
|
|
|
|
// memory.
|
|
|
|
//
|
|
|
|
llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
|
|
|
|
llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
|
|
|
|
llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
|
|
|
|
|
|
|
|
// if (lane_id == 0)
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *IsWarpMaster = Bld.CreateIsNull(LaneID, "warp_master");
|
2017-02-17 00:20:16 +08:00
|
|
|
Bld.CreateCondBr(IsWarpMaster, ThenBB, ElseBB);
|
|
|
|
CGF.EmitBlock(ThenBB);
|
|
|
|
|
|
|
|
// Reduce element = LocalReduceList[i]
|
|
|
|
Address ElemPtrPtrAddr =
|
|
|
|
Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
|
|
|
|
llvm::Value *ElemPtrPtr = CGF.EmitLoadOfScalar(
|
|
|
|
ElemPtrPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
|
|
|
|
// elemptr = (type[i]*)(elemptrptr)
|
|
|
|
Address ElemPtr =
|
|
|
|
Address(ElemPtrPtr, C.getTypeAlignInChars(Private->getType()));
|
|
|
|
ElemPtr = Bld.CreateElementBitCast(
|
|
|
|
ElemPtr, CGF.ConvertTypeForMem(Private->getType()));
|
|
|
|
// elem = *elemptr
|
|
|
|
llvm::Value *Elem = CGF.EmitLoadOfScalar(
|
|
|
|
ElemPtr, /*Volatile=*/false, Private->getType(), SourceLocation());
|
|
|
|
|
|
|
|
// Get pointer to location in transfer medium.
|
|
|
|
// MediumPtr = &medium[warp_id]
|
|
|
|
llvm::Value *MediumPtrVal = Bld.CreateInBoundsGEP(
|
|
|
|
TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), WarpID});
|
|
|
|
Address MediumPtr(MediumPtrVal, C.getTypeAlignInChars(Private->getType()));
|
|
|
|
// Casting to actual data type.
|
|
|
|
// MediumPtr = (type[i]*)MediumPtrAddr;
|
|
|
|
MediumPtr = Bld.CreateElementBitCast(
|
|
|
|
MediumPtr, CGF.ConvertTypeForMem(Private->getType()));
|
|
|
|
|
|
|
|
//*MediumPtr = elem
|
|
|
|
Bld.CreateStore(Elem, MediumPtr);
|
|
|
|
|
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ElseBB);
|
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(MergeBB);
|
|
|
|
|
|
|
|
Address AddrNumWarpsArg = CGF.GetAddrOfLocalVar(&NumWarpsArg);
|
|
|
|
llvm::Value *NumWarpsVal = CGF.EmitLoadOfScalar(
|
|
|
|
AddrNumWarpsArg, /*Volatile=*/false, C.IntTy, SourceLocation());
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *NumActiveThreads = Bld.CreateNSWMul(
|
2017-02-17 00:20:16 +08:00
|
|
|
NumWarpsVal, getNVPTXWarpSize(CGF), "num_active_threads");
|
|
|
|
// named_barrier_sync(ParallelBarrierID, num_active_threads)
|
|
|
|
syncParallelThreads(CGF, NumActiveThreads);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Warp 0 copies reduce element from transfer medium.
|
|
|
|
//
|
|
|
|
llvm::BasicBlock *W0ThenBB = CGF.createBasicBlock("then");
|
|
|
|
llvm::BasicBlock *W0ElseBB = CGF.createBasicBlock("else");
|
|
|
|
llvm::BasicBlock *W0MergeBB = CGF.createBasicBlock("ifcont");
|
|
|
|
|
|
|
|
// Up to 32 threads in warp 0 are active.
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *IsActiveThread =
|
2017-02-17 00:20:16 +08:00
|
|
|
Bld.CreateICmpULT(ThreadID, NumWarpsVal, "is_active_thread");
|
|
|
|
Bld.CreateCondBr(IsActiveThread, W0ThenBB, W0ElseBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(W0ThenBB);
|
|
|
|
|
|
|
|
// SrcMediumPtr = &medium[tid]
|
|
|
|
llvm::Value *SrcMediumPtrVal = Bld.CreateInBoundsGEP(
|
|
|
|
TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), ThreadID});
|
|
|
|
Address SrcMediumPtr(SrcMediumPtrVal,
|
|
|
|
C.getTypeAlignInChars(Private->getType()));
|
|
|
|
// SrcMediumVal = *SrcMediumPtr;
|
|
|
|
SrcMediumPtr = Bld.CreateElementBitCast(
|
|
|
|
SrcMediumPtr, CGF.ConvertTypeForMem(Private->getType()));
|
|
|
|
llvm::Value *SrcMediumValue = CGF.EmitLoadOfScalar(
|
|
|
|
SrcMediumPtr, /*Volatile=*/false, Private->getType(), SourceLocation());
|
|
|
|
|
|
|
|
// TargetElemPtr = (type[i]*)(SrcDataAddr[i])
|
|
|
|
Address TargetElemPtrPtr =
|
|
|
|
Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
|
|
|
|
llvm::Value *TargetElemPtrVal = CGF.EmitLoadOfScalar(
|
|
|
|
TargetElemPtrPtr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
|
|
|
|
Address TargetElemPtr =
|
|
|
|
Address(TargetElemPtrVal, C.getTypeAlignInChars(Private->getType()));
|
|
|
|
TargetElemPtr = Bld.CreateElementBitCast(
|
|
|
|
TargetElemPtr, CGF.ConvertTypeForMem(Private->getType()));
|
|
|
|
|
|
|
|
// *TargetElemPtr = SrcMediumVal;
|
|
|
|
CGF.EmitStoreOfScalar(SrcMediumValue, TargetElemPtr, /*Volatile=*/false,
|
|
|
|
Private->getType());
|
|
|
|
Bld.CreateBr(W0MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(W0ElseBB);
|
|
|
|
Bld.CreateBr(W0MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(W0MergeBB);
|
|
|
|
|
|
|
|
// While warp 0 copies values from transfer medium, all other warps must
|
|
|
|
// wait.
|
|
|
|
syncParallelThreads(CGF, NumActiveThreads);
|
2018-04-17 04:16:21 +08:00
|
|
|
++Idx;
|
2017-02-17 00:20:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CGF.FinishFunction();
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a helper that reduces data across two OpenMP threads (lanes)
|
|
|
|
/// in the same warp. It uses shuffle instructions to copy over data from
|
|
|
|
/// a remote lane's stack. The reduction algorithm performed is specified
|
|
|
|
/// by the fourth parameter.
|
|
|
|
///
|
|
|
|
/// Algorithm Versions.
|
|
|
|
/// Full Warp Reduce (argument value 0):
|
|
|
|
/// This algorithm assumes that all 32 lanes are active and gathers
|
|
|
|
/// data from these 32 lanes, producing a single resultant value.
|
|
|
|
/// Contiguous Partial Warp Reduce (argument value 1):
|
|
|
|
/// This algorithm assumes that only a *contiguous* subset of lanes
|
|
|
|
/// are active. This happens for the last warp in a parallel region
|
|
|
|
/// when the user specified num_threads is not an integer multiple of
|
|
|
|
/// 32. This contiguous subset always starts with the zeroth lane.
|
|
|
|
/// Partial Warp Reduce (argument value 2):
|
|
|
|
/// This algorithm gathers data from any number of lanes at any position.
|
|
|
|
/// All reduced values are stored in the lowest possible lane. The set
|
|
|
|
/// of problems every algorithm addresses is a super set of those
|
|
|
|
/// addressable by algorithms with a lower version number. Overhead
|
|
|
|
/// increases as algorithm version increases.
|
|
|
|
///
|
|
|
|
/// Terminology
|
|
|
|
/// Reduce element:
|
|
|
|
/// Reduce element refers to the individual data field with primitive
|
|
|
|
/// data types to be combined and reduced across threads.
|
|
|
|
/// Reduce list:
|
|
|
|
/// Reduce list refers to a collection of local, thread-private
|
|
|
|
/// reduce elements.
|
|
|
|
/// Remote Reduce list:
|
|
|
|
/// Remote Reduce list refers to a collection of remote (relative to
|
|
|
|
/// the current thread) reduce elements.
|
|
|
|
///
|
|
|
|
/// We distinguish between three states of threads that are important to
|
|
|
|
/// the implementation of this function.
|
|
|
|
/// Alive threads:
|
|
|
|
/// Threads in a warp executing the SIMT instruction, as distinguished from
|
|
|
|
/// threads that are inactive due to divergent control flow.
|
|
|
|
/// Active threads:
|
|
|
|
/// The minimal set of threads that has to be alive upon entry to this
|
|
|
|
/// function. The computation is correct iff active threads are alive.
|
|
|
|
/// Some threads are alive but they are not active because they do not
|
|
|
|
/// contribute to the computation in any useful manner. Turning them off
|
|
|
|
/// may introduce control flow overheads without any tangible benefits.
|
|
|
|
/// Effective threads:
|
|
|
|
/// In order to comply with the argument requirements of the shuffle
|
|
|
|
/// function, we must keep all lanes holding data alive. But at most
|
|
|
|
/// half of them perform value aggregation; we refer to this half of
|
|
|
|
/// threads as effective. The other half is simply handing off their
|
|
|
|
/// data.
|
|
|
|
///
|
|
|
|
/// Procedure
|
|
|
|
/// Value shuffle:
|
|
|
|
/// In this step active threads transfer data from higher lane positions
|
|
|
|
/// in the warp to lower lane positions, creating Remote Reduce list.
|
|
|
|
/// Value aggregation:
|
|
|
|
/// In this step, effective threads combine their thread local Reduce list
|
|
|
|
/// with Remote Reduce list and store the result in the thread local
|
|
|
|
/// Reduce list.
|
|
|
|
/// Value copy:
|
|
|
|
/// In this step, we deal with the assumption made by algorithm 2
|
|
|
|
/// (i.e. contiguity assumption). When we have an odd number of lanes
|
|
|
|
/// active, say 2k+1, only k threads will be effective and therefore k
|
|
|
|
/// new values will be produced. However, the Reduce list owned by the
|
|
|
|
/// (2k+1)th thread is ignored in the value aggregation. Therefore
|
|
|
|
/// we copy the Reduce list from the (2k+1)th lane to (k+1)th lane so
|
|
|
|
/// that the contiguity assumption still holds.
|
2018-01-05 03:45:16 +08:00
|
|
|
static llvm::Value *emitShuffleAndReduceFunction(
|
|
|
|
CodeGenModule &CGM, ArrayRef<const Expr *> Privates,
|
|
|
|
QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) {
|
2018-04-17 04:16:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// Thread local Reduce list used to host the values of data to be reduced.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.VoidPtrTy, ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
// Current lane id; could be logical.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl LaneIDArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.ShortTy,
|
|
|
|
ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
// Offset of the remote source lane relative to the current lane.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl RemoteLaneOffsetArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.ShortTy, ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
// Algorithm version. This is expected to be known at compile time.
|
2018-01-05 03:45:16 +08:00
|
|
|
ImplicitParamDecl AlgoVerArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
|
|
|
|
C.ShortTy, ImplicitParamDecl::Other);
|
2017-02-17 00:20:16 +08:00
|
|
|
FunctionArgList Args;
|
|
|
|
Args.push_back(&ReduceListArg);
|
|
|
|
Args.push_back(&LaneIDArg);
|
|
|
|
Args.push_back(&RemoteLaneOffsetArg);
|
|
|
|
Args.push_back(&AlgoVerArg);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
const CGFunctionInfo &CGFI =
|
|
|
|
CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
|
2017-02-17 00:20:16 +08:00
|
|
|
auto *Fn = llvm::Function::Create(
|
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
|
|
|
"_omp_reduction_shuffle_and_reduce_func", &CGM.getModule());
|
2018-03-01 07:46:35 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
|
2018-04-11 04:10:53 +08:00
|
|
|
Fn->setDoesNotRecurse();
|
2017-02-17 00:20:16 +08:00
|
|
|
CodeGenFunction CGF(CGM);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
|
|
|
|
Address LocalReduceList(
|
|
|
|
Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
|
|
|
|
C.VoidPtrTy, SourceLocation()),
|
|
|
|
CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
|
|
|
|
CGF.getPointerAlign());
|
|
|
|
|
|
|
|
Address AddrLaneIDArg = CGF.GetAddrOfLocalVar(&LaneIDArg);
|
|
|
|
llvm::Value *LaneIDArgVal = CGF.EmitLoadOfScalar(
|
|
|
|
AddrLaneIDArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
|
|
|
|
|
|
|
|
Address AddrRemoteLaneOffsetArg = CGF.GetAddrOfLocalVar(&RemoteLaneOffsetArg);
|
|
|
|
llvm::Value *RemoteLaneOffsetArgVal = CGF.EmitLoadOfScalar(
|
|
|
|
AddrRemoteLaneOffsetArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
|
|
|
|
|
|
|
|
Address AddrAlgoVerArg = CGF.GetAddrOfLocalVar(&AlgoVerArg);
|
|
|
|
llvm::Value *AlgoVerArgVal = CGF.EmitLoadOfScalar(
|
|
|
|
AddrAlgoVerArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
|
|
|
|
|
|
|
|
// Create a local thread-private variable to host the Reduce list
|
|
|
|
// from a remote lane.
|
|
|
|
Address RemoteReduceList =
|
|
|
|
CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_reduce_list");
|
|
|
|
|
|
|
|
// This loop iterates through the list of reduce elements and copies,
|
|
|
|
// element by element, from a remote lane in the warp to RemoteReduceList,
|
|
|
|
// hosted on the thread's stack.
|
|
|
|
emitReductionListCopy(RemoteLaneToThread, CGF, ReductionArrayTy, Privates,
|
|
|
|
LocalReduceList, RemoteReduceList,
|
2017-02-17 00:48:49 +08:00
|
|
|
{/*RemoteLaneOffset=*/RemoteLaneOffsetArgVal,
|
|
|
|
/*ScratchpadIndex=*/nullptr,
|
|
|
|
/*ScratchpadWidth=*/nullptr});
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// The actions to be performed on the Remote Reduce list is dependent
|
|
|
|
// on the algorithm version.
|
|
|
|
//
|
|
|
|
// if (AlgoVer==0) || (AlgoVer==1 && (LaneId < Offset)) || (AlgoVer==2 &&
|
|
|
|
// LaneId % 2 == 0 && Offset > 0):
|
|
|
|
// do the reduction value aggregation
|
|
|
|
//
|
|
|
|
// The thread local variable Reduce list is mutated in place to host the
|
|
|
|
// reduced data, which is the aggregated value produced from local and
|
|
|
|
// remote lanes.
|
|
|
|
//
|
|
|
|
// Note that AlgoVer is expected to be a constant integer known at compile
|
|
|
|
// time.
|
|
|
|
// When AlgoVer==0, the first conjunction evaluates to true, making
|
|
|
|
// the entire predicate true during compile time.
|
|
|
|
// When AlgoVer==1, the second conjunction has only the second part to be
|
|
|
|
// evaluated during runtime. Other conjunctions evaluates to false
|
|
|
|
// during compile time.
|
|
|
|
// When AlgoVer==2, the third conjunction has only the second part to be
|
|
|
|
// evaluated during runtime. Other conjunctions evaluates to false
|
|
|
|
// during compile time.
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CondAlgo0 = Bld.CreateIsNull(AlgoVerArgVal);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
|
|
|
|
llvm::Value *CondAlgo1 = Bld.CreateAnd(
|
2017-02-17 00:20:16 +08:00
|
|
|
Algo1, Bld.CreateICmpULT(LaneIDArgVal, RemoteLaneOffsetArgVal));
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *Algo2 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(2));
|
|
|
|
llvm::Value *CondAlgo2 = Bld.CreateAnd(
|
|
|
|
Algo2, Bld.CreateIsNull(Bld.CreateAnd(LaneIDArgVal, Bld.getInt16(1))));
|
2017-02-17 00:20:16 +08:00
|
|
|
CondAlgo2 = Bld.CreateAnd(
|
|
|
|
CondAlgo2, Bld.CreateICmpSGT(RemoteLaneOffsetArgVal, Bld.getInt16(0)));
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CondReduce = Bld.CreateOr(CondAlgo0, CondAlgo1);
|
2017-02-17 00:20:16 +08:00
|
|
|
CondReduce = Bld.CreateOr(CondReduce, CondAlgo2);
|
|
|
|
|
|
|
|
llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
|
|
|
|
llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
|
|
|
|
llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
|
|
|
|
Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ThenBB);
|
|
|
|
// reduce_function(LocalReduceList, RemoteReduceList)
|
|
|
|
llvm::Value *LocalReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
LocalReduceList.getPointer(), CGF.VoidPtrTy);
|
|
|
|
llvm::Value *RemoteReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
RemoteReduceList.getPointer(), CGF.VoidPtrTy);
|
2018-01-05 03:45:16 +08:00
|
|
|
CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
|
|
|
|
CGF, Loc, ReduceFn, {LocalReduceListPtr, RemoteReduceListPtr});
|
2017-02-17 00:20:16 +08:00
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(ElseBB);
|
|
|
|
Bld.CreateBr(MergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(MergeBB);
|
|
|
|
|
|
|
|
// if (AlgoVer==1 && (LaneId >= Offset)) copy Remote Reduce list to local
|
|
|
|
// Reduce list.
|
|
|
|
Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *CondCopy = Bld.CreateAnd(
|
2017-02-17 00:20:16 +08:00
|
|
|
Algo1, Bld.CreateICmpUGE(LaneIDArgVal, RemoteLaneOffsetArgVal));
|
|
|
|
|
|
|
|
llvm::BasicBlock *CpyThenBB = CGF.createBasicBlock("then");
|
|
|
|
llvm::BasicBlock *CpyElseBB = CGF.createBasicBlock("else");
|
|
|
|
llvm::BasicBlock *CpyMergeBB = CGF.createBasicBlock("ifcont");
|
|
|
|
Bld.CreateCondBr(CondCopy, CpyThenBB, CpyElseBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(CpyThenBB);
|
|
|
|
emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
|
|
|
|
RemoteReduceList, LocalReduceList);
|
|
|
|
Bld.CreateBr(CpyMergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(CpyElseBB);
|
|
|
|
Bld.CreateBr(CpyMergeBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(CpyMergeBB);
|
|
|
|
|
|
|
|
CGF.FinishFunction();
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Design of OpenMP reductions on the GPU
|
|
|
|
///
|
|
|
|
/// Consider a typical OpenMP program with one or more reduction
|
|
|
|
/// clauses:
|
|
|
|
///
|
|
|
|
/// float foo;
|
|
|
|
/// double bar;
|
|
|
|
/// #pragma omp target teams distribute parallel for \
|
|
|
|
/// reduction(+:foo) reduction(*:bar)
|
|
|
|
/// for (int i = 0; i < N; i++) {
|
|
|
|
/// foo += A[i]; bar *= B[i];
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// where 'foo' and 'bar' are reduced across all OpenMP threads in
|
|
|
|
/// all teams. In our OpenMP implementation on the NVPTX device an
|
|
|
|
/// OpenMP team is mapped to a CUDA threadblock and OpenMP threads
|
|
|
|
/// within a team are mapped to CUDA threads within a threadblock.
|
|
|
|
/// Our goal is to efficiently aggregate values across all OpenMP
|
|
|
|
/// threads such that:
|
|
|
|
///
|
|
|
|
/// - the compiler and runtime are logically concise, and
|
|
|
|
/// - the reduction is performed efficiently in a hierarchical
|
|
|
|
/// manner as follows: within OpenMP threads in the same warp,
|
|
|
|
/// across warps in a threadblock, and finally across teams on
|
|
|
|
/// the NVPTX device.
|
|
|
|
///
|
|
|
|
/// Introduction to Decoupling
|
|
|
|
///
|
|
|
|
/// We would like to decouple the compiler and the runtime so that the
|
|
|
|
/// latter is ignorant of the reduction variables (number, data types)
|
|
|
|
/// and the reduction operators. This allows a simpler interface
|
|
|
|
/// and implementation while still attaining good performance.
|
|
|
|
///
|
|
|
|
/// Pseudocode for the aforementioned OpenMP program generated by the
|
|
|
|
/// compiler is as follows:
|
|
|
|
///
|
|
|
|
/// 1. Create private copies of reduction variables on each OpenMP
|
|
|
|
/// thread: 'foo_private', 'bar_private'
|
|
|
|
/// 2. Each OpenMP thread reduces the chunk of 'A' and 'B' assigned
|
|
|
|
/// to it and writes the result in 'foo_private' and 'bar_private'
|
|
|
|
/// respectively.
|
|
|
|
/// 3. Call the OpenMP runtime on the GPU to reduce within a team
|
|
|
|
/// and store the result on the team master:
|
|
|
|
///
|
|
|
|
/// __kmpc_nvptx_parallel_reduce_nowait(...,
|
|
|
|
/// reduceData, shuffleReduceFn, interWarpCpyFn)
|
|
|
|
///
|
|
|
|
/// where:
|
|
|
|
/// struct ReduceData {
|
|
|
|
/// double *foo;
|
|
|
|
/// double *bar;
|
|
|
|
/// } reduceData
|
|
|
|
/// reduceData.foo = &foo_private
|
|
|
|
/// reduceData.bar = &bar_private
|
|
|
|
///
|
|
|
|
/// 'shuffleReduceFn' and 'interWarpCpyFn' are pointers to two
|
|
|
|
/// auxiliary functions generated by the compiler that operate on
|
|
|
|
/// variables of type 'ReduceData'. They aid the runtime perform
|
|
|
|
/// algorithmic steps in a data agnostic manner.
|
|
|
|
///
|
|
|
|
/// 'shuffleReduceFn' is a pointer to a function that reduces data
|
|
|
|
/// of type 'ReduceData' across two OpenMP threads (lanes) in the
|
|
|
|
/// same warp. It takes the following arguments as input:
|
|
|
|
///
|
|
|
|
/// a. variable of type 'ReduceData' on the calling lane,
|
|
|
|
/// b. its lane_id,
|
|
|
|
/// c. an offset relative to the current lane_id to generate a
|
|
|
|
/// remote_lane_id. The remote lane contains the second
|
|
|
|
/// variable of type 'ReduceData' that is to be reduced.
|
|
|
|
/// d. an algorithm version parameter determining which reduction
|
|
|
|
/// algorithm to use.
|
|
|
|
///
|
|
|
|
/// 'shuffleReduceFn' retrieves data from the remote lane using
|
|
|
|
/// efficient GPU shuffle intrinsics and reduces, using the
|
|
|
|
/// algorithm specified by the 4th parameter, the two operands
|
|
|
|
/// element-wise. The result is written to the first operand.
|
|
|
|
///
|
|
|
|
/// Different reduction algorithms are implemented in different
|
|
|
|
/// runtime functions, all calling 'shuffleReduceFn' to perform
|
|
|
|
/// the essential reduction step. Therefore, based on the 4th
|
|
|
|
/// parameter, this function behaves slightly differently to
|
|
|
|
/// cooperate with the runtime to ensure correctness under
|
|
|
|
/// different circumstances.
|
|
|
|
///
|
|
|
|
/// 'InterWarpCpyFn' is a pointer to a function that transfers
|
|
|
|
/// reduced variables across warps. It tunnels, through CUDA
|
|
|
|
/// shared memory, the thread-private data of type 'ReduceData'
|
|
|
|
/// from lane 0 of each warp to a lane in the first warp.
|
2017-02-17 00:48:49 +08:00
|
|
|
/// 4. Call the OpenMP runtime on the GPU to reduce across teams.
|
|
|
|
/// The last team writes the global reduced value to memory.
|
|
|
|
///
|
|
|
|
/// ret = __kmpc_nvptx_teams_reduce_nowait(...,
|
|
|
|
/// reduceData, shuffleReduceFn, interWarpCpyFn,
|
|
|
|
/// scratchpadCopyFn, loadAndReduceFn)
|
|
|
|
///
|
|
|
|
/// 'scratchpadCopyFn' is a helper that stores reduced
|
|
|
|
/// data from the team master to a scratchpad array in
|
|
|
|
/// global memory.
|
|
|
|
///
|
|
|
|
/// 'loadAndReduceFn' is a helper that loads data from
|
|
|
|
/// the scratchpad array and reduces it with the input
|
|
|
|
/// operand.
|
|
|
|
///
|
|
|
|
/// These compiler generated functions hide address
|
|
|
|
/// calculation and alignment information from the runtime.
|
2017-02-17 00:20:16 +08:00
|
|
|
/// 5. if ret == 1:
|
|
|
|
/// The team master of the last team stores the reduced
|
|
|
|
/// result to the globals in memory.
|
|
|
|
/// foo += reduceData.foo; bar *= reduceData.bar
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Warp Reduction Algorithms
|
|
|
|
///
|
|
|
|
/// On the warp level, we have three algorithms implemented in the
|
|
|
|
/// OpenMP runtime depending on the number of active lanes:
|
|
|
|
///
|
|
|
|
/// Full Warp Reduction
|
|
|
|
///
|
|
|
|
/// The reduce algorithm within a warp where all lanes are active
|
|
|
|
/// is implemented in the runtime as follows:
|
|
|
|
///
|
|
|
|
/// full_warp_reduce(void *reduce_data,
|
|
|
|
/// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
|
|
|
|
/// for (int offset = WARPSIZE/2; offset > 0; offset /= 2)
|
|
|
|
/// ShuffleReduceFn(reduce_data, 0, offset, 0);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// The algorithm completes in log(2, WARPSIZE) steps.
|
|
|
|
///
|
|
|
|
/// 'ShuffleReduceFn' is used here with lane_id set to 0 because it is
|
|
|
|
/// not used therefore we save instructions by not retrieving lane_id
|
|
|
|
/// from the corresponding special registers. The 4th parameter, which
|
|
|
|
/// represents the version of the algorithm being used, is set to 0 to
|
|
|
|
/// signify full warp reduction.
|
|
|
|
///
|
|
|
|
/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
|
|
|
|
///
|
|
|
|
/// #reduce_elem refers to an element in the local lane's data structure
|
|
|
|
/// #remote_elem is retrieved from a remote lane
|
|
|
|
/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
|
|
|
|
/// reduce_elem = reduce_elem REDUCE_OP remote_elem;
|
|
|
|
///
|
|
|
|
/// Contiguous Partial Warp Reduction
|
|
|
|
///
|
|
|
|
/// This reduce algorithm is used within a warp where only the first
|
|
|
|
/// 'n' (n <= WARPSIZE) lanes are active. It is typically used when the
|
|
|
|
/// number of OpenMP threads in a parallel region is not a multiple of
|
|
|
|
/// WARPSIZE. The algorithm is implemented in the runtime as follows:
|
|
|
|
///
|
|
|
|
/// void
|
|
|
|
/// contiguous_partial_reduce(void *reduce_data,
|
|
|
|
/// kmp_ShuffleReductFctPtr ShuffleReduceFn,
|
|
|
|
/// int size, int lane_id) {
|
|
|
|
/// int curr_size;
|
|
|
|
/// int offset;
|
|
|
|
/// curr_size = size;
|
|
|
|
/// mask = curr_size/2;
|
|
|
|
/// while (offset>0) {
|
|
|
|
/// ShuffleReduceFn(reduce_data, lane_id, offset, 1);
|
|
|
|
/// curr_size = (curr_size+1)/2;
|
|
|
|
/// offset = curr_size/2;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
|
|
|
|
///
|
|
|
|
/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
|
|
|
|
/// if (lane_id < offset)
|
|
|
|
/// reduce_elem = reduce_elem REDUCE_OP remote_elem
|
|
|
|
/// else
|
|
|
|
/// reduce_elem = remote_elem
|
|
|
|
///
|
|
|
|
/// This algorithm assumes that the data to be reduced are located in a
|
|
|
|
/// contiguous subset of lanes starting from the first. When there is
|
|
|
|
/// an odd number of active lanes, the data in the last lane is not
|
|
|
|
/// aggregated with any other lane's dat but is instead copied over.
|
|
|
|
///
|
|
|
|
/// Dispersed Partial Warp Reduction
|
|
|
|
///
|
|
|
|
/// This algorithm is used within a warp when any discontiguous subset of
|
|
|
|
/// lanes are active. It is used to implement the reduction operation
|
|
|
|
/// across lanes in an OpenMP simd region or in a nested parallel region.
|
|
|
|
///
|
|
|
|
/// void
|
|
|
|
/// dispersed_partial_reduce(void *reduce_data,
|
|
|
|
/// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
|
|
|
|
/// int size, remote_id;
|
|
|
|
/// int logical_lane_id = number_of_active_lanes_before_me() * 2;
|
|
|
|
/// do {
|
|
|
|
/// remote_id = next_active_lane_id_right_after_me();
|
|
|
|
/// # the above function returns 0 of no active lane
|
|
|
|
/// # is present right after the current lane.
|
|
|
|
/// size = number_of_active_lanes_in_this_warp();
|
|
|
|
/// logical_lane_id /= 2;
|
|
|
|
/// ShuffleReduceFn(reduce_data, logical_lane_id,
|
|
|
|
/// remote_id-1-threadIdx.x, 2);
|
|
|
|
/// } while (logical_lane_id % 2 == 0 && size > 1);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// There is no assumption made about the initial state of the reduction.
|
|
|
|
/// Any number of lanes (>=1) could be active at any position. The reduction
|
|
|
|
/// result is returned in the first active lane.
|
|
|
|
///
|
|
|
|
/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
|
|
|
|
///
|
|
|
|
/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
|
|
|
|
/// if (lane_id % 2 == 0 && offset > 0)
|
|
|
|
/// reduce_elem = reduce_elem REDUCE_OP remote_elem
|
|
|
|
/// else
|
|
|
|
/// reduce_elem = remote_elem
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Intra-Team Reduction
|
|
|
|
///
|
|
|
|
/// This function, as implemented in the runtime call
|
|
|
|
/// '__kmpc_nvptx_parallel_reduce_nowait', aggregates data across OpenMP
|
|
|
|
/// threads in a team. It first reduces within a warp using the
|
|
|
|
/// aforementioned algorithms. We then proceed to gather all such
|
|
|
|
/// reduced values at the first warp.
|
|
|
|
///
|
|
|
|
/// The runtime makes use of the function 'InterWarpCpyFn', which copies
|
|
|
|
/// data from each of the "warp master" (zeroth lane of each warp, where
|
|
|
|
/// warp-reduced data is held) to the zeroth warp. This step reduces (in
|
|
|
|
/// a mathematical sense) the problem of reduction across warp masters in
|
|
|
|
/// a block to the problem of warp reduction.
|
|
|
|
///
|
2017-02-17 00:48:49 +08:00
|
|
|
///
|
|
|
|
/// Inter-Team Reduction
|
|
|
|
///
|
|
|
|
/// Once a team has reduced its data to a single value, it is stored in
|
|
|
|
/// a global scratchpad array. Since each team has a distinct slot, this
|
|
|
|
/// can be done without locking.
|
|
|
|
///
|
|
|
|
/// The last team to write to the scratchpad array proceeds to reduce the
|
|
|
|
/// scratchpad array. One or more workers in the last team use the helper
|
|
|
|
/// 'loadAndReduceDataFn' to load and reduce values from the array, i.e.,
|
|
|
|
/// the k'th worker reduces every k'th element.
|
|
|
|
///
|
|
|
|
/// Finally, a call is made to '__kmpc_nvptx_parallel_reduce_nowait' to
|
|
|
|
/// reduce across workers and compute a globally reduced value.
|
|
|
|
///
|
2017-02-17 00:20:16 +08:00
|
|
|
void CGOpenMPRuntimeNVPTX::emitReduction(
|
|
|
|
CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
|
|
|
|
ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
|
|
|
|
ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
|
|
|
|
if (!CGF.HaveInsertPoint())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
|
2017-02-17 00:48:49 +08:00
|
|
|
bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
|
|
|
|
// FIXME: Add support for simd reduction.
|
|
|
|
assert((TeamsReduction || ParallelReduction) &&
|
|
|
|
"Invalid reduction selection in emitReduction.");
|
2017-02-17 00:20:16 +08:00
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
ASTContext &C = CGM.getContext();
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// 1. Build a list of reduction variables.
|
|
|
|
// void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
|
|
|
|
auto Size = RHSExprs.size();
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Expr *E : Privates) {
|
2017-02-17 00:20:16 +08:00
|
|
|
if (E->getType()->isVariablyModifiedType())
|
|
|
|
// Reserve place for array size.
|
|
|
|
++Size;
|
|
|
|
}
|
|
|
|
llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
|
|
|
|
QualType ReductionArrayTy =
|
|
|
|
C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
|
|
|
|
/*IndexTypeQuals=*/0);
|
|
|
|
Address ReductionList =
|
|
|
|
CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
|
|
|
|
auto IPriv = Privates.begin();
|
|
|
|
unsigned Idx = 0;
|
|
|
|
for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
|
|
|
|
Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
|
|
|
|
CGF.getPointerSize());
|
|
|
|
CGF.Builder.CreateStore(
|
|
|
|
CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
|
|
|
|
Elem);
|
|
|
|
if ((*IPriv)->getType()->isVariablyModifiedType()) {
|
|
|
|
// Store array size.
|
|
|
|
++Idx;
|
|
|
|
Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
|
|
|
|
CGF.getPointerSize());
|
|
|
|
llvm::Value *Size = CGF.Builder.CreateIntCast(
|
|
|
|
CGF.getVLASize(
|
|
|
|
CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
|
2018-02-03 21:55:59 +08:00
|
|
|
.NumElts,
|
2017-02-17 00:20:16 +08:00
|
|
|
CGF.SizeTy, /*isSigned=*/false);
|
|
|
|
CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
|
|
|
|
Elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Emit reduce_func().
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ReductionFn = emitReductionFunction(
|
2018-01-05 03:45:16 +08:00
|
|
|
CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(),
|
|
|
|
Privates, LHSExprs, RHSExprs, ReductionOps);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// 4. Build res = __kmpc_reduce{_nowait}(<gtid>, <n>, sizeof(RedList),
|
|
|
|
// RedList, shuffle_reduce_func, interwarp_copy_func);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ThreadId = getThreadID(CGF, Loc);
|
|
|
|
llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
|
|
|
|
llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
2017-02-17 00:20:16 +08:00
|
|
|
ReductionList.getPointer(), CGF.VoidPtrTy);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ShuffleAndReduceFn = emitShuffleAndReduceFunction(
|
2018-01-05 03:45:16 +08:00
|
|
|
CGM, Privates, ReductionArrayTy, ReductionFn, Loc);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *InterWarpCopyFn =
|
2018-01-05 03:45:16 +08:00
|
|
|
emitInterWarpCopyFunction(CGM, Privates, ReductionArrayTy, Loc);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
llvm::Value *Res = nullptr;
|
|
|
|
if (ParallelReduction) {
|
|
|
|
llvm::Value *Args[] = {ThreadId,
|
|
|
|
CGF.Builder.getInt32(RHSExprs.size()),
|
|
|
|
ReductionArrayTySize,
|
|
|
|
RL,
|
|
|
|
ShuffleAndReduceFn,
|
|
|
|
InterWarpCopyFn};
|
|
|
|
|
|
|
|
Res = CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_reduce_nowait),
|
|
|
|
Args);
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:48:49 +08:00
|
|
|
if (TeamsReduction) {
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *ScratchPadCopyFn =
|
2018-01-05 03:45:16 +08:00
|
|
|
emitCopyToScratchpad(CGM, Privates, ReductionArrayTy, Loc);
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::Value *LoadAndReduceFn = emitReduceScratchpadFunction(
|
2018-01-05 03:45:16 +08:00
|
|
|
CGM, Privates, ReductionArrayTy, ReductionFn, Loc);
|
2017-02-17 00:48:49 +08:00
|
|
|
|
|
|
|
llvm::Value *Args[] = {ThreadId,
|
|
|
|
CGF.Builder.getInt32(RHSExprs.size()),
|
|
|
|
ReductionArrayTySize,
|
|
|
|
RL,
|
|
|
|
ShuffleAndReduceFn,
|
|
|
|
InterWarpCopyFn,
|
|
|
|
ScratchPadCopyFn,
|
|
|
|
LoadAndReduceFn};
|
|
|
|
Res = CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_teams_reduce_nowait),
|
|
|
|
Args);
|
|
|
|
}
|
|
|
|
|
2017-02-17 00:20:16 +08:00
|
|
|
// 5. Build switch(res)
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
|
|
|
|
llvm::SwitchInst *SwInst =
|
|
|
|
CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/1);
|
2017-02-17 00:20:16 +08:00
|
|
|
|
|
|
|
// 6. Build case 1: where we have reduced values in the master
|
|
|
|
// thread in each team.
|
|
|
|
// __kmpc_end_reduce{_nowait}(<gtid>);
|
|
|
|
// break;
|
2018-04-17 04:16:21 +08:00
|
|
|
llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
|
2017-02-17 00:20:16 +08:00
|
|
|
SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
|
|
|
|
CGF.EmitBlock(Case1BB);
|
|
|
|
|
|
|
|
// Add emission of __kmpc_end_reduce{_nowait}(<gtid>);
|
|
|
|
llvm::Value *EndArgs[] = {ThreadId};
|
2018-04-17 04:16:21 +08:00
|
|
|
auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps,
|
2017-02-17 00:20:16 +08:00
|
|
|
this](CodeGenFunction &CGF, PrePostActionTy &Action) {
|
|
|
|
auto IPriv = Privates.begin();
|
|
|
|
auto ILHS = LHSExprs.begin();
|
|
|
|
auto IRHS = RHSExprs.begin();
|
2018-04-17 04:16:21 +08:00
|
|
|
for (const Expr *E : ReductionOps) {
|
2017-02-17 00:20:16 +08:00
|
|
|
emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
|
|
|
|
cast<DeclRefExpr>(*IRHS));
|
|
|
|
++IPriv;
|
|
|
|
++ILHS;
|
|
|
|
++IRHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
RegionCodeGenTy RCG(CodeGen);
|
|
|
|
NVPTXActionTy Action(
|
|
|
|
nullptr, llvm::None,
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_reduce_nowait),
|
|
|
|
EndArgs);
|
|
|
|
RCG.setAction(Action);
|
|
|
|
RCG(CGF);
|
|
|
|
CGF.EmitBranch(DefaultBB);
|
|
|
|
CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
|
|
|
|
}
|
2017-08-09 02:04:06 +08:00
|
|
|
|
|
|
|
const VarDecl *
|
|
|
|
CGOpenMPRuntimeNVPTX::translateParameter(const FieldDecl *FD,
|
|
|
|
const VarDecl *NativeParam) const {
|
|
|
|
if (!NativeParam->getType()->isReferenceType())
|
|
|
|
return NativeParam;
|
|
|
|
QualType ArgType = NativeParam->getType();
|
|
|
|
QualifierCollector QC;
|
|
|
|
const Type *NonQualTy = QC.strip(ArgType);
|
|
|
|
QualType PointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType();
|
|
|
|
if (const auto *Attr = FD->getAttr<OMPCaptureKindAttr>()) {
|
|
|
|
if (Attr->getCaptureKind() == OMPC_map) {
|
|
|
|
PointeeTy = CGM.getContext().getAddrSpaceQualType(PointeeTy,
|
|
|
|
LangAS::opencl_global);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArgType = CGM.getContext().getPointerType(PointeeTy);
|
|
|
|
QC.addRestrict();
|
|
|
|
enum { NVPTX_local_addr = 5 };
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
QC.addAddressSpace(getLangASFromTargetAS(NVPTX_local_addr));
|
2017-08-09 02:04:06 +08:00
|
|
|
ArgType = QC.apply(CGM.getContext(), ArgType);
|
2018-04-17 04:16:21 +08:00
|
|
|
if (isa<ImplicitParamDecl>(NativeParam))
|
2017-11-23 00:02:03 +08:00
|
|
|
return ImplicitParamDecl::Create(
|
|
|
|
CGM.getContext(), /*DC=*/nullptr, NativeParam->getLocation(),
|
|
|
|
NativeParam->getIdentifier(), ArgType, ImplicitParamDecl::Other);
|
|
|
|
return ParmVarDecl::Create(
|
|
|
|
CGM.getContext(),
|
|
|
|
const_cast<DeclContext *>(NativeParam->getDeclContext()),
|
|
|
|
NativeParam->getLocStart(), NativeParam->getLocation(),
|
|
|
|
NativeParam->getIdentifier(), ArgType,
|
|
|
|
/*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr);
|
2017-08-09 02:04:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Address
|
|
|
|
CGOpenMPRuntimeNVPTX::getParameterAddress(CodeGenFunction &CGF,
|
|
|
|
const VarDecl *NativeParam,
|
|
|
|
const VarDecl *TargetParam) const {
|
|
|
|
assert(NativeParam != TargetParam &&
|
|
|
|
NativeParam->getType()->isReferenceType() &&
|
|
|
|
"Native arg must not be the same as target arg.");
|
|
|
|
Address LocalAddr = CGF.GetAddrOfLocalVar(TargetParam);
|
|
|
|
QualType NativeParamType = NativeParam->getType();
|
|
|
|
QualifierCollector QC;
|
|
|
|
const Type *NonQualTy = QC.strip(NativeParamType);
|
|
|
|
QualType NativePointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType();
|
|
|
|
unsigned NativePointeeAddrSpace =
|
Convert clang::LangAS to a strongly typed enum
Summary:
Convert clang::LangAS to a strongly typed enum
Currently both clang AST address spaces and target specific address spaces
are represented as unsigned which can lead to subtle errors if the wrong
type is passed. It is especially confusing in the CodeGen files as it is
not possible to see what kind of address space should be passed to a
function without looking at the implementation.
I originally made this change for our LLVM fork for the CHERI architecture
where we make extensive use of address spaces to differentiate between
capabilities and pointers. When merging the upstream changes I usually
run into some test failures or runtime crashes because the wrong kind of
address space is passed to a function. By converting the LangAS enum to a
C++11 we can catch these errors at compile time. Additionally, it is now
obvious from the function signature which kind of address space it expects.
I found the following errors while writing this patch:
- ItaniumRecordLayoutBuilder::LayoutField was passing a clang AST address
space to TargetInfo::getPointer{Width,Align}()
- TypePrinter::printAttributedAfter() prints the numeric value of the
clang AST address space instead of the target address space.
However, this code is not used so I kept the current behaviour
- initializeForBlockHeader() in CGBlocks.cpp was passing
LangAS::opencl_generic to TargetInfo::getPointer{Width,Align}()
- CodeGenFunction::EmitBlockLiteral() was passing a AST address space to
TargetInfo::getPointerWidth()
- CGOpenMPRuntimeNVPTX::translateParameter() passed a target address space
to Qualifiers::addAddressSpace()
- CGOpenMPRuntimeNVPTX::getParameterAddress() was using
llvm::Type::getPointerTo() with a AST address space
- clang_getAddressSpace() returns either a LangAS or a target address
space. As this is exposed to C I have kept the current behaviour and
added a comment stating that it is probably not correct.
Other than this the patch should not cause any functional changes.
Reviewers: yaxunl, pcc, bader
Reviewed By: yaxunl, bader
Subscribers: jlebar, jholewinski, nhaehnle, Anastasia, cfe-commits
Differential Revision: https://reviews.llvm.org/D38816
llvm-svn: 315871
2017-10-16 02:48:14 +08:00
|
|
|
CGF.getContext().getTargetAddressSpace(NativePointeeTy);
|
2017-09-14 04:20:59 +08:00
|
|
|
QualType TargetTy = TargetParam->getType();
|
2017-08-09 02:04:06 +08:00
|
|
|
llvm::Value *TargetAddr = CGF.EmitLoadOfScalar(
|
2017-09-14 04:20:59 +08:00
|
|
|
LocalAddr, /*Volatile=*/false, TargetTy, SourceLocation());
|
2017-08-09 02:04:06 +08:00
|
|
|
// First cast to generic.
|
|
|
|
TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo(
|
|
|
|
/*AddrSpace=*/0));
|
|
|
|
// Cast from generic to native address space.
|
|
|
|
TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo(
|
|
|
|
NativePointeeAddrSpace));
|
|
|
|
Address NativeParamAddr = CGF.CreateMemTemp(NativeParamType);
|
|
|
|
CGF.EmitStoreOfScalar(TargetAddr, NativeParamAddr, /*Volatile=*/false,
|
2017-09-14 04:20:59 +08:00
|
|
|
NativeParamType);
|
2017-08-09 02:04:06 +08:00
|
|
|
return NativeParamAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitOutlinedFunctionCall(
|
2017-08-14 23:01:03 +08:00
|
|
|
CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
|
2017-08-09 02:04:06 +08:00
|
|
|
ArrayRef<llvm::Value *> Args) const {
|
|
|
|
SmallVector<llvm::Value *, 4> TargetArgs;
|
2017-08-15 22:34:04 +08:00
|
|
|
TargetArgs.reserve(Args.size());
|
2017-08-09 02:04:06 +08:00
|
|
|
auto *FnType =
|
|
|
|
cast<llvm::FunctionType>(OutlinedFn->getType()->getPointerElementType());
|
|
|
|
for (unsigned I = 0, E = Args.size(); I < E; ++I) {
|
2017-08-15 22:34:04 +08:00
|
|
|
if (FnType->isVarArg() && FnType->getNumParams() <= I) {
|
|
|
|
TargetArgs.append(std::next(Args.begin(), I), Args.end());
|
|
|
|
break;
|
|
|
|
}
|
2017-08-09 02:04:06 +08:00
|
|
|
llvm::Type *TargetType = FnType->getParamType(I);
|
|
|
|
llvm::Value *NativeArg = Args[I];
|
|
|
|
if (!TargetType->isPointerTy()) {
|
|
|
|
TargetArgs.emplace_back(NativeArg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
llvm::Value *TargetArg = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
|
2018-03-16 02:10:54 +08:00
|
|
|
NativeArg,
|
|
|
|
NativeArg->getType()->getPointerElementType()->getPointerTo());
|
2017-08-09 02:04:06 +08:00
|
|
|
TargetArgs.emplace_back(
|
|
|
|
CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TargetArg, TargetType));
|
|
|
|
}
|
2017-08-14 23:01:03 +08:00
|
|
|
CGOpenMPRuntime::emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, TargetArgs);
|
2017-08-09 02:04:06 +08:00
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
|
|
|
|
/// Emit function which wraps the outline parallel region
|
|
|
|
/// and controls the arguments which are passed to this function.
|
|
|
|
/// The wrapper ensures that the outlined function is called
|
|
|
|
/// with the correct arguments when data is shared.
|
|
|
|
llvm::Function *CGOpenMPRuntimeNVPTX::createParallelDataSharingWrapper(
|
|
|
|
llvm::Function *OutlinedParallelFn, const OMPExecutableDirective &D) {
|
|
|
|
ASTContext &Ctx = CGM.getContext();
|
|
|
|
const auto &CS = *D.getCapturedStmt(OMPD_parallel);
|
|
|
|
|
|
|
|
// Create a function that takes as argument the source thread.
|
|
|
|
FunctionArgList WrapperArgs;
|
|
|
|
QualType Int16QTy =
|
|
|
|
Ctx.getIntTypeForBitwidth(/*DestWidth=*/16, /*Signed=*/false);
|
|
|
|
QualType Int32QTy =
|
|
|
|
Ctx.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false);
|
|
|
|
ImplicitParamDecl ParallelLevelArg(Ctx, /*DC=*/nullptr, D.getLocStart(),
|
|
|
|
/*Id=*/nullptr, Int16QTy,
|
|
|
|
ImplicitParamDecl::Other);
|
|
|
|
ImplicitParamDecl WrapperArg(Ctx, /*DC=*/nullptr, D.getLocStart(),
|
|
|
|
/*Id=*/nullptr, Int32QTy,
|
|
|
|
ImplicitParamDecl::Other);
|
|
|
|
WrapperArgs.emplace_back(&ParallelLevelArg);
|
|
|
|
WrapperArgs.emplace_back(&WrapperArg);
|
|
|
|
|
2018-04-17 04:16:21 +08:00
|
|
|
const CGFunctionInfo &CGFI =
|
2018-03-14 22:17:45 +08:00
|
|
|
CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, WrapperArgs);
|
|
|
|
|
|
|
|
auto *Fn = llvm::Function::Create(
|
|
|
|
CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
|
2018-04-17 04:16:21 +08:00
|
|
|
Twine(OutlinedParallelFn->getName(), "_wrapper"), &CGM.getModule());
|
2018-03-16 02:10:54 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
|
2018-03-14 22:17:45 +08:00
|
|
|
Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
|
2018-04-11 04:10:53 +08:00
|
|
|
Fn->setDoesNotRecurse();
|
2018-03-14 22:17:45 +08:00
|
|
|
|
|
|
|
CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
|
|
|
|
CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, Fn, CGFI, WrapperArgs,
|
|
|
|
D.getLocStart(), D.getLocStart());
|
|
|
|
|
|
|
|
const auto *RD = CS.getCapturedRecordDecl();
|
|
|
|
auto CurField = RD->field_begin();
|
|
|
|
|
2018-03-20 01:04:07 +08:00
|
|
|
Address ZeroAddr = CGF.CreateMemTemp(
|
|
|
|
CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
|
|
|
|
/*Name*/ ".zero.addr");
|
|
|
|
CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
|
2018-03-14 22:17:45 +08:00
|
|
|
// Get the array of arguments.
|
|
|
|
SmallVector<llvm::Value *, 8> Args;
|
|
|
|
|
2018-03-20 01:04:07 +08:00
|
|
|
Args.emplace_back(CGF.GetAddrOfLocalVar(&WrapperArg).getPointer());
|
|
|
|
Args.emplace_back(ZeroAddr.getPointer());
|
2018-03-14 22:17:45 +08:00
|
|
|
|
|
|
|
CGBuilderTy &Bld = CGF.Builder;
|
|
|
|
auto CI = CS.capture_begin();
|
|
|
|
|
|
|
|
// Use global memory for data sharing.
|
|
|
|
// Handle passing of global args to workers.
|
|
|
|
Address GlobalArgs =
|
|
|
|
CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "global_args");
|
|
|
|
llvm::Value *GlobalArgsPtr = GlobalArgs.getPointer();
|
|
|
|
llvm::Value *DataSharingArgs[] = {GlobalArgsPtr};
|
|
|
|
CGF.EmitRuntimeCall(
|
|
|
|
createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_get_shared_variables),
|
|
|
|
DataSharingArgs);
|
|
|
|
|
|
|
|
// Retrieve the shared variables from the list of references returned
|
|
|
|
// by the runtime. Pass the variables to the outlined function.
|
2018-03-20 23:41:05 +08:00
|
|
|
Address SharedArgListAddress = Address::invalid();
|
|
|
|
if (CS.capture_size() > 0 ||
|
|
|
|
isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) {
|
|
|
|
SharedArgListAddress = CGF.EmitLoadOfPointer(
|
|
|
|
GlobalArgs, CGF.getContext()
|
|
|
|
.getPointerType(CGF.getContext().getPointerType(
|
|
|
|
CGF.getContext().VoidPtrTy))
|
|
|
|
.castAs<PointerType>());
|
|
|
|
}
|
|
|
|
unsigned Idx = 0;
|
|
|
|
if (isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) {
|
|
|
|
Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx,
|
|
|
|
CGF.getPointerSize());
|
|
|
|
Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
Src, CGF.SizeTy->getPointerTo());
|
|
|
|
llvm::Value *LB = CGF.EmitLoadOfScalar(
|
|
|
|
TypedAddress,
|
|
|
|
/*Volatile=*/false,
|
|
|
|
CGF.getContext().getPointerType(CGF.getContext().getSizeType()),
|
|
|
|
cast<OMPLoopDirective>(D).getLowerBoundVariable()->getExprLoc());
|
|
|
|
Args.emplace_back(LB);
|
|
|
|
++Idx;
|
|
|
|
Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx,
|
|
|
|
CGF.getPointerSize());
|
|
|
|
TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
|
|
|
Src, CGF.SizeTy->getPointerTo());
|
|
|
|
llvm::Value *UB = CGF.EmitLoadOfScalar(
|
|
|
|
TypedAddress,
|
|
|
|
/*Volatile=*/false,
|
|
|
|
CGF.getContext().getPointerType(CGF.getContext().getSizeType()),
|
|
|
|
cast<OMPLoopDirective>(D).getUpperBoundVariable()->getExprLoc());
|
|
|
|
Args.emplace_back(UB);
|
|
|
|
++Idx;
|
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
if (CS.capture_size() > 0) {
|
|
|
|
ASTContext &CGFContext = CGF.getContext();
|
|
|
|
for (unsigned I = 0, E = CS.capture_size(); I < E; ++I, ++CI, ++CurField) {
|
|
|
|
QualType ElemTy = CurField->getType();
|
2018-03-20 23:41:05 +08:00
|
|
|
Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, I + Idx,
|
|
|
|
CGF.getPointerSize());
|
|
|
|
Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
|
2018-03-14 22:17:45 +08:00
|
|
|
Src, CGF.ConvertTypeForMem(CGFContext.getPointerType(ElemTy)));
|
|
|
|
llvm::Value *Arg = CGF.EmitLoadOfScalar(TypedAddress,
|
|
|
|
/*Volatile=*/false,
|
|
|
|
CGFContext.getPointerType(ElemTy),
|
|
|
|
CI->getLocation());
|
2018-03-20 23:41:05 +08:00
|
|
|
if (CI->capturesVariableByCopy()) {
|
|
|
|
Arg = castValueToType(CGF, Arg, ElemTy, CGFContext.getUIntPtrType(),
|
|
|
|
CI->getLocation());
|
|
|
|
}
|
2018-03-14 22:17:45 +08:00
|
|
|
Args.emplace_back(Arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emitOutlinedFunctionCall(CGF, D.getLocStart(), OutlinedParallelFn, Args);
|
|
|
|
CGF.FinishFunction();
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::emitFunctionProlog(CodeGenFunction &CGF,
|
|
|
|
const Decl *D) {
|
|
|
|
assert(D && "Expected function or captured|block decl.");
|
|
|
|
assert(FunctionGlobalizedDecls.count(CGF.CurFn) == 0 &&
|
|
|
|
"Function is registered already.");
|
|
|
|
const Stmt *Body = nullptr;
|
2018-03-16 02:10:54 +08:00
|
|
|
bool NeedToDelayGlobalization = false;
|
2018-03-14 22:17:45 +08:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
Body = FD->getBody();
|
|
|
|
} else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
|
|
|
|
Body = BD->getBody();
|
|
|
|
} else if (const auto *CD = dyn_cast<CapturedDecl>(D)) {
|
|
|
|
Body = CD->getBody();
|
2018-03-20 22:45:59 +08:00
|
|
|
NeedToDelayGlobalization = CGF.CapturedStmtInfo->getKind() == CR_OpenMP;
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
if (!Body)
|
|
|
|
return;
|
2018-03-20 22:45:59 +08:00
|
|
|
CheckVarsEscapingDeclContext VarChecker(CGF);
|
2018-03-14 22:17:45 +08:00
|
|
|
VarChecker.Visit(Body);
|
|
|
|
const RecordDecl *GlobalizedVarsRecord = VarChecker.getGlobalizedRecord();
|
2018-03-20 22:45:59 +08:00
|
|
|
ArrayRef<const ValueDecl *> EscapedVariableLengthDecls =
|
|
|
|
VarChecker.getEscapedVariableLengthDecls();
|
|
|
|
if (!GlobalizedVarsRecord && EscapedVariableLengthDecls.empty())
|
2018-03-14 22:17:45 +08:00
|
|
|
return;
|
2018-03-16 02:10:54 +08:00
|
|
|
auto I = FunctionGlobalizedDecls.try_emplace(CGF.CurFn).first;
|
|
|
|
I->getSecond().MappedParams =
|
|
|
|
llvm::make_unique<CodeGenFunction::OMPMapVars>();
|
|
|
|
I->getSecond().GlobalRecord = GlobalizedVarsRecord;
|
|
|
|
I->getSecond().EscapedParameters.insert(
|
|
|
|
VarChecker.getEscapedParameters().begin(),
|
|
|
|
VarChecker.getEscapedParameters().end());
|
2018-03-20 22:45:59 +08:00
|
|
|
I->getSecond().EscapedVariableLengthDecls.append(
|
|
|
|
EscapedVariableLengthDecls.begin(), EscapedVariableLengthDecls.end());
|
2018-03-16 02:10:54 +08:00
|
|
|
DeclToAddrMapTy &Data = I->getSecond().LocalVarData;
|
2018-03-14 22:17:45 +08:00
|
|
|
for (const ValueDecl *VD : VarChecker.getEscapedDecls()) {
|
2018-03-20 22:45:59 +08:00
|
|
|
assert(VD->isCanonicalDecl() && "Expected canonical declaration");
|
2018-03-14 22:17:45 +08:00
|
|
|
const FieldDecl *FD = VarChecker.getFieldForGlobalizedVar(VD);
|
2018-03-16 02:10:54 +08:00
|
|
|
Data.insert(std::make_pair(VD, std::make_pair(FD, Address::invalid())));
|
|
|
|
}
|
|
|
|
if (!NeedToDelayGlobalization) {
|
|
|
|
emitGenericVarsProlog(CGF, D->getLocStart());
|
|
|
|
struct GlobalizationScope final : EHScopeStack::Cleanup {
|
|
|
|
GlobalizationScope() = default;
|
|
|
|
|
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
|
|
|
static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
|
|
|
|
.emitGenericVarsEpilog(CGF);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
CGF.EHStack.pushCleanup<GlobalizationScope>(NormalAndEHCleanup);
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Address CGOpenMPRuntimeNVPTX::getAddressOfLocalVariable(CodeGenFunction &CGF,
|
|
|
|
const VarDecl *VD) {
|
2018-03-20 22:45:59 +08:00
|
|
|
VD = VD->getCanonicalDecl();
|
2018-03-14 22:17:45 +08:00
|
|
|
auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
|
|
|
|
if (I == FunctionGlobalizedDecls.end())
|
|
|
|
return Address::invalid();
|
2018-03-16 02:10:54 +08:00
|
|
|
auto VDI = I->getSecond().LocalVarData.find(VD);
|
2018-03-20 22:45:59 +08:00
|
|
|
if (VDI != I->getSecond().LocalVarData.end())
|
|
|
|
return VDI->second.second;
|
|
|
|
if (VD->hasAttrs()) {
|
|
|
|
for (specific_attr_iterator<OMPReferencedVarAttr> IT(VD->attr_begin()),
|
|
|
|
E(VD->attr_end());
|
|
|
|
IT != E; ++IT) {
|
|
|
|
auto VDI = I->getSecond().LocalVarData.find(
|
|
|
|
cast<VarDecl>(cast<DeclRefExpr>(IT->getRef())->getDecl())
|
|
|
|
->getCanonicalDecl());
|
|
|
|
if (VDI != I->getSecond().LocalVarData.end())
|
|
|
|
return VDI->second.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Address::invalid();
|
2018-03-14 22:17:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CGOpenMPRuntimeNVPTX::functionFinished(CodeGenFunction &CGF) {
|
|
|
|
FunctionGlobalizedDecls.erase(CGF.CurFn);
|
|
|
|
CGOpenMPRuntime::functionFinished(CGF);
|
|
|
|
}
|