1#ifndef PROTEUS_CORE_LLVM_DEVICE_HPP
2#define PROTEUS_CORE_LLVM_DEVICE_HPP
12#if defined(PROTEUS_ENABLE_HIP) || defined(PROTEUS_ENABLE_CUDA)
14#include <llvm/Analysis/CallGraph.h>
15#include <llvm/Bitcode/BitcodeReader.h>
16#include <llvm/Bitcode/BitcodeWriter.h>
17#include <llvm/IR/Attributes.h>
18#include <llvm/IR/ConstantRange.h>
19#include <llvm/IR/ReplaceConstant.h>
20#include <llvm/IR/Verifier.h>
21#include <llvm/Object/ELFObjectFile.h>
22#include <llvm/Transforms/Utils/Cloning.h>
33inline void setKernelDims(
Module &M,
dim3 &GridDim,
dim3 &BlockDim) {
57 OS <<
"[DimSpec] Replace call to " << F->getName() <<
" with constant "
65 ConstantInt::get(Type::getInt32Ty(M.getContext()),
DimValue);
69 Call->eraseFromParent();
115#if LLVM_VERSION_MAJOR >= 19
118 Call->removeRetAttr(Attribute::Range);
120 Call->getAttributes().addRetAttributes(M.getContext(),
Builder));
126 ConstantAsMetadata::get(ConstantInt::get(
RetTy, 0)),
149 const std::unordered_map<std::string, GlobalVarInfo> &VarNameToGlobalInfo) {
164 ConstantInt::get(Type::getInt64Ty(M.getContext()), 0xDEADBEEFDEADBEEF);
165 auto *
CE = ConstantExpr::getIntToPtr(
Addr,
GV->getType()->getPointerTo());
167 M,
GV->getType()->getPointerTo(),
false, GlobalValue::ExternalLinkage,
168 CE,
GV->getName() +
"$ptr",
nullptr,
GV->getThreadLocalMode(),
169 GV->getAddressSpace(),
true);
175 Worklist.push_back(
GV);
177 while (!Worklist.empty()) {
178 Value *
V = Worklist.pop_back_val();
182 Worklist.push_back(
C);
192 "Expected Instruction or Constant user for Value: " +
toString(*
V) +
229 const std::unordered_map<std::string, GlobalVarInfo> &VarNameToGlobalInfo) {
231 object::ELF64LEObjectFile::create(
Object);
275 " without a concrete device address");
283inline void specializeIR(
286 const SmallVector<std::pair<std::string, StringRef>> LambdaCalleeInfo,
287 bool SpecializeArgs,
bool SpecializeDims,
bool SpecializeDimsRange,
288 bool SpecializeLaunchBounds,
int MinBlocksPerSM) {
290 Function *F = M.getFunction(FnName);
292 assert(F &&
"Expected non-null function!");
298 for (
auto &[FnName,
LambdaType] : LambdaCalleeInfo) {
300 Function *F = M.getFunction(FnName);
312 setKernelDims(M, GridDim, BlockDim);
313 if (SpecializeDimsRange)
315 F->setName(FnName + Suffix);
317 if (SpecializeLaunchBounds) {
318 int BlockSize = BlockDim.x * BlockDim.y * BlockDim.z;
322 OS <<
"[LaunchBoundSpec] MaxThreads " <<
BlockSize <<
" MinBlocksPerSM "
323 << MinBlocksPerSM <<
"\n";
335 <<
"specializeIR " <<
T.elapsed() <<
" ms\n");
const void const char * VarName
Definition CompilerInterfaceDevice.cpp:21
#define PROTEUS_FATAL_ERROR(x)
Definition Error.h:7
#define PROTEUS_TIMER_OUTPUT(x)
Definition TimeTracing.hpp:57
static Config & get()
Definition Config.hpp:298
static LambdaRegistry & instance()
Definition LambdaRegistry.hpp:22
static llvm::raw_ostream & outs(const std::string &Name)
Definition Logger.hpp:25
static void trace(llvm::StringRef Msg)
Definition Logger.hpp:30
const SmallVector< StringRef > & threadIdxXFnName()
Definition CoreLLVMCUDA.hpp:70
const SmallVector< StringRef > & gridDimYFnName()
Definition CoreLLVMCUDA.hpp:30
const SmallVector< StringRef > & threadIdxZFnName()
Definition CoreLLVMCUDA.hpp:80
const SmallVector< StringRef > & blockIdxZFnName()
Definition CoreLLVMCUDA.hpp:65
const SmallVector< StringRef > & gridDimZFnName()
Definition CoreLLVMCUDA.hpp:35
const SmallVector< StringRef > & gridDimXFnName()
Definition CoreLLVMCUDA.hpp:25
const SmallVector< StringRef > & blockIdxXFnName()
Definition CoreLLVMCUDA.hpp:55
const SmallVector< StringRef > & threadIdxYFnName()
Definition CoreLLVMCUDA.hpp:75
const SmallVector< StringRef > & blockIdxYFnName()
Definition CoreLLVMCUDA.hpp:60
const SmallVector< StringRef > & blockDimYFnName()
Definition CoreLLVMCUDA.hpp:45
const SmallVector< StringRef > & blockDimZFnName()
Definition CoreLLVMCUDA.hpp:50
const SmallVector< StringRef > & blockDimXFnName()
Definition CoreLLVMCUDA.hpp:40
Definition StorageCache.cpp:24
void setLaunchBoundsForKernel(Function &F, int MaxThreadsPerSM, int MinBlocksPerSM=0)
Definition CoreLLVMCUDA.hpp:87
T getRuntimeConstantValue(void *Arg)
Definition CompilerInterfaceRuntimeConstantInfo.h:114
std::string toString(CodegenOption Option)
Definition Config.hpp:26
void runCleanupPassPipeline(Module &M)
Definition CoreLLVM.hpp:230