1104349Sphk//===---- CGOpenMPRuntimeNVPTX.cpp - Interface to OpenMP NVPTX Runtimes ---===//
2104349Sphk//
3104349Sphk// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4104349Sphk// See https://llvm.org/LICENSE.txt for license information.
5178848Scokane// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6178848Scokane//
7104349Sphk//===----------------------------------------------------------------------===//
8104349Sphk//
9104349Sphk// This provides a class for OpenMP runtime code generation specialized to NVPTX
10104349Sphk// targets from generalized CGOpenMPRuntimeGPU class.
11178848Scokane//
12178848Scokane//===----------------------------------------------------------------------===//
13178848Scokane
14178848Scokane#include "CGOpenMPRuntimeNVPTX.h"
15104349Sphk#include "CGOpenMPRuntimeGPU.h"
16178848Scokane#include "CodeGenFunction.h"
17104349Sphk#include "clang/AST/Attr.h"
18178848Scokane#include "clang/AST/DeclOpenMP.h"
19104349Sphk#include "clang/AST/StmtOpenMP.h"
20104349Sphk#include "clang/AST/StmtVisitor.h"
21178848Scokane#include "clang/Basic/Cuda.h"
22104349Sphk#include "llvm/ADT/SmallPtrSet.h"
23104349Sphk#include "llvm/IR/IntrinsicsNVPTX.h"
24104349Sphk
25104349Sphkusing namespace clang;
26104349Sphkusing namespace CodeGen;
27104349Sphkusing namespace llvm::omp;
28104349Sphk
29104349SphkCGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
30104349Sphk    : CGOpenMPRuntimeGPU(CGM) {
31104349Sphk  if (!CGM.getLangOpts().OpenMPIsDevice)
32104349Sphk    llvm_unreachable("OpenMP NVPTX can only handle device code.");
33104349Sphk}
34104349Sphk
35104349Sphkllvm::Value *CGOpenMPRuntimeNVPTX::getGPUWarpSize(CodeGenFunction &CGF) {
36104349Sphk  return CGF.EmitRuntimeCall(
37104349Sphk      llvm::Intrinsic::getDeclaration(
38104349Sphk          &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize),
39104349Sphk      "nvptx_warp_size");
40104349Sphk}
41104349Sphk
42104349Sphkllvm::Value *CGOpenMPRuntimeNVPTX::getGPUThreadID(CodeGenFunction &CGF) {
43104349Sphk  CGBuilderTy &Bld = CGF.Builder;
44104349Sphk  llvm::Function *F;
45104349Sphk  F = llvm::Intrinsic::getDeclaration(
46104349Sphk      &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x);
47104349Sphk  return Bld.CreateCall(F, llvm::None, "nvptx_tid");
48104349Sphk}
49104349Sphk
50104349Sphkllvm::Value *CGOpenMPRuntimeNVPTX::getGPUNumThreads(CodeGenFunction &CGF) {
51104349Sphk  CGBuilderTy &Bld = CGF.Builder;
52104349Sphk  llvm::Function *F;
53104349Sphk  F = llvm::Intrinsic::getDeclaration(
54104349Sphk      &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x);
55104349Sphk  return Bld.CreateCall(F, llvm::None, "nvptx_num_threads");
56104349Sphk}
57104349Sphk