Home | History | Annotate | Line # | Download | only in CodeGen
      1 //===-- CGOpenMPRuntimeAMDGCN.cpp - Interface to OpenMP AMDGCN 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
     10 // AMDGCN targets from generalized CGOpenMPRuntimeGPU class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "CGOpenMPRuntimeAMDGCN.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/IntrinsicsAMDGPU.h"
     24 
     25 using namespace clang;
     26 using namespace CodeGen;
     27 using namespace llvm::omp;
     28 
     29 CGOpenMPRuntimeAMDGCN::CGOpenMPRuntimeAMDGCN(CodeGenModule &CGM)
     30     : CGOpenMPRuntimeGPU(CGM) {
     31   if (!CGM.getLangOpts().OpenMPIsDevice)
     32     llvm_unreachable("OpenMP AMDGCN can only handle device code.");
     33 }
     34 
     35 llvm::Value *CGOpenMPRuntimeAMDGCN::getGPUWarpSize(CodeGenFunction &CGF) {
     36   CGBuilderTy &Bld = CGF.Builder;
     37   // return constant compile-time target-specific warp size
     38   unsigned WarpSize = CGF.getTarget().getGridValue(llvm::omp::GV_Warp_Size);
     39   return Bld.getInt32(WarpSize);
     40 }
     41 
     42 llvm::Value *CGOpenMPRuntimeAMDGCN::getGPUThreadID(CodeGenFunction &CGF) {
     43   CGBuilderTy &Bld = CGF.Builder;
     44   llvm::Function *F =
     45       CGF.CGM.getIntrinsic(llvm::Intrinsic::amdgcn_workitem_id_x);
     46   return Bld.CreateCall(F, llvm::None, "nvptx_tid");
     47 }
     48 
     49 llvm::Value *CGOpenMPRuntimeAMDGCN::getGPUNumThreads(CodeGenFunction &CGF) {
     50   CGBuilderTy &Bld = CGF.Builder;
     51   llvm::Module *M = &CGF.CGM.getModule();
     52   const char *LocSize = "__kmpc_amdgcn_gpu_num_threads";
     53   llvm::Function *F = M->getFunction(LocSize);
     54   if (!F) {
     55     F = llvm::Function::Create(
     56         llvm::FunctionType::get(CGF.Int32Ty, llvm::None, false),
     57         llvm::GlobalVariable::ExternalLinkage, LocSize, &CGF.CGM.getModule());
     58   }
     59   return Bld.CreateCall(F, llvm::None, "nvptx_num_threads");
     60 }
     61