Skip to content

Commit

Permalink
[OpenMP][SPIRV] Implementation for barrier and master added.
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-schuermann committed Dec 5, 2017
1 parent a243679 commit 7ddcfa4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
37 changes: 37 additions & 0 deletions lib/CodeGen/CGOpenMPRuntimeSPIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ llvm::Constant * CGOpenMPRuntimeSPIR::createRuntimeFunction(OpenMPRTLFunctionSPI
case get_group_id: {
return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.SizeTy, param, false), "_Z12get_group_idj");
}
case work_group_barrier: {
//CLK_GLOBAL_MEM_FENCE 0x02
return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, param, false), "_Z18work_group_barrierj");
}
default:
return nullptr;
}
Expand Down Expand Up @@ -459,6 +463,39 @@ void CGOpenMPRuntimeSPIR::emitTeamsCall(CodeGenFunction &CGF,
CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
}

void CGOpenMPRuntimeSPIR::emitMasterRegion(CodeGenFunction &CGF,
const RegionCodeGenTy &MasterOpGen,
SourceLocation Loc) {
if (!CGF.HaveInsertPoint())
return;

// principle: if(threadID == 0):
llvm::Value * arg[] = { CGF.Builder.getInt32(0) };
llvm::CallInst * ltid = CGF.EmitRuntimeCall(createRuntimeFunction(get_local_id), arg);
llvm::Value * ltid_casted = CGF.Builder.CreateTruncOrBitCast(ltid, CGF.Int32Ty);
llvm::Value * cond = CGF.Builder.CreateIsNull(ltid_casted);
llvm::BasicBlock * ThenBlock = CGF.createBasicBlock("omp_if.then");
llvm::BasicBlock * ContBlock = CGF.createBasicBlock("omp_if.end");
// Generate the branch (If-stmt)
CGF.Builder.CreateCondBr(cond, ThenBlock, ContBlock);
CGF.EmitBlock(ThenBlock);

emitInlinedDirective(CGF, OMPD_master, MasterOpGen);
CGF.EmitBranch(ContBlock);
CGF.EmitBlock(ContBlock, true);
}

void CGOpenMPRuntimeSPIR::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
OpenMPDirectiveKind Kind, bool EmitChecks,
bool ForceSimpleCall) {
if (!CGF.HaveInsertPoint())
return;

// call opencl work group barrier
llvm::Value * arg[] = { CGF.Builder.getInt32(1 << 1) }; //CLK_GLOBAL_MEM_FENCE 0x02
CGF.EmitRuntimeCall(createRuntimeFunction(work_group_barrier), arg);
}

void CGOpenMPRuntimeSPIR::emitForStaticInit(CodeGenFunction &CGF,
SourceLocation Loc,
const OpenMPScheduleTy &ScheduleKind,
Expand Down
23 changes: 22 additions & 1 deletion lib/CodeGen/CGOpenMPRuntimeSPIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class CGOpenMPRuntimeSPIR : public CGOpenMPRuntime {
get_local_id,
get_local_size,
get_num_groups,
get_group_id
get_group_id,
work_group_barrier
};
QualType getAddrSpaceType(QualType T, LangAS::ID AddrSpace);
llvm::Constant *createRuntimeFunction(OpenMPRTLFunctionSPIR Function);
Expand Down Expand Up @@ -107,6 +108,26 @@ class CGOpenMPRuntimeSPIR : public CGOpenMPRuntime {
SourceLocation Loc, llvm::Value *OutlinedFn,
ArrayRef<llvm::Value *> CapturedVars) override;

/// \brief Emits a master region.
/// \param MasterOpGen Generator for the statement associated with the given
/// master region.
virtual void emitMasterRegion(CodeGenFunction &CGF,
const RegionCodeGenTy &MasterOpGen,
SourceLocation Loc);

/// \brief Emit an implicit/explicit barrier for OpenMP threads.
/// \param Kind Directive for which this implicit barrier call must be
/// generated. Must be OMPD_barrier for explicit barrier generation.
/// \param EmitChecks true if need to emit checks for cancellation barriers.
/// \param ForceSimpleCall true simple barrier call must be emitted, false if
/// runtime class decides which one to emit (simple or with cancellation
/// checks).
///
virtual void emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
OpenMPDirectiveKind Kind,
bool EmitChecks = true,
bool ForceSimpleCall = false);

/// \brief Call the appropriate runtime routine to initialize it before start
/// of loop.
///
Expand Down

0 comments on commit 7ddcfa4

Please sign in to comment.