1 //===---- CGOpenMPRuntimeNVPTX.cpp - Interface to OpenMP NVPTX Runtimes ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This provides a class for OpenMP runtime code generation specialized to NVPTX
10 // targets from generalized CGOpenMPRuntimeGPU class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "CGOpenMPRuntimeNVPTX.h"
15 #include "CGOpenMPRuntimeGPU.h"
16 #include "CodeGenFunction.h"
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/DeclOpenMP.h"
19 #include "clang/AST/StmtOpenMP.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/Basic/Cuda.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/IR/IntrinsicsNVPTX.h"
24 
25 using namespace clang;
26 using namespace CodeGen;
27 using namespace llvm::omp;
28 
CGOpenMPRuntimeNVPTX(CodeGenModule & CGM)29 CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
30     : CGOpenMPRuntimeGPU(CGM) {
31   if (!CGM.getLangOpts().OpenMPIsDevice)
32     llvm_unreachable("OpenMP NVPTX can only handle device code.");
33 }
34 
getGPUWarpSize(CodeGenFunction & CGF)35 llvm::Value *CGOpenMPRuntimeNVPTX::getGPUWarpSize(CodeGenFunction &CGF) {
36   return CGF.EmitRuntimeCall(
37       llvm::Intrinsic::getDeclaration(
38           &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize),
39       "nvptx_warp_size");
40 }
41 
getGPUThreadID(CodeGenFunction & CGF)42 llvm::Value *CGOpenMPRuntimeNVPTX::getGPUThreadID(CodeGenFunction &CGF) {
43   CGBuilderTy &Bld = CGF.Builder;
44   llvm::Function *F;
45   F = llvm::Intrinsic::getDeclaration(
46       &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x);
47   return Bld.CreateCall(F, llvm::None, "nvptx_tid");
48 }
49 
getGPUNumThreads(CodeGenFunction & CGF)50 llvm::Value *CGOpenMPRuntimeNVPTX::getGPUNumThreads(CodeGenFunction &CGF) {
51   CGBuilderTy &Bld = CGF.Builder;
52   llvm::Function *F;
53   F = llvm::Intrinsic::getDeclaration(
54       &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x);
55   return Bld.CreateCall(F, llvm::None, "nvptx_num_threads");
56 }
57