19#include "llvm/IR/LegacyPassManager.h"
20#include "llvm/IR/Module.h"
21#include "llvm/MC/TargetRegistry.h"
22#include "llvm/Target/TargetMachine.h"
27 Interpreter &Interp, std::unique_ptr<CompilerInstance> Instance,
32 HostParser(HostParser), VFS(FS) {
35 StringRef Arch =
CI->getTargetOpts().CPU;
36 if (!Arch.starts_with(
"sm_") || Arch.substr(3).getAsInteger(10,
SMVersion)) {
37 Err = llvm::joinErrors(std::move(Err), llvm::make_error<llvm::StringError>(
38 "Invalid CUDA architecture",
39 llvm::inconvertibleErrorCode()));
48 return PTU.takeError();
52 return PTX.takeError();
56 return std::move(Err);
58 std::string FatbinFileName =
59 "/incr_module_" + std::to_string(
PTUs.size()) +
".fatbin";
60 VFS->addFile(FatbinFileName, 0,
61 llvm::MemoryBuffer::getMemBuffer(
73 auto &PTU =
PTUs.back();
76 const llvm::Target *
Target = llvm::TargetRegistry::lookupTarget(
77 PTU.TheModule->getTargetTriple(), Error);
79 return llvm::make_error<llvm::StringError>(std::move(Error),
81 llvm::TargetOptions TO = llvm::TargetOptions();
82 llvm::TargetMachine *TargetMachine =
Target->createTargetMachine(
83 PTU.TheModule->getTargetTriple(),
getCI()->getTargetOpts().CPU,
"", TO,
84 llvm::Reloc::Model::PIC_);
85 PTU.TheModule->setDataLayout(TargetMachine->createDataLayout());
88 llvm::raw_svector_ostream dest(
PTXCode);
90 llvm::legacy::PassManager PM;
91 if (TargetMachine->addPassesToEmitFile(PM, dest,
nullptr,
92 llvm::CodeGenFileType::AssemblyFile)) {
93 return llvm::make_error<llvm::StringError>(
94 "NVPTX backend cannot produce PTX code.",
95 llvm::inconvertibleErrorCode());
98 if (!PM.run(*PTU.TheModule))
99 return llvm::make_error<llvm::StringError>(
"Failed to emit PTX code.",
100 llvm::inconvertibleErrorCode());
110 AddressSize64 = 0x01,
118 struct FatBinInnerHeader {
124 uint32_t CompressedSize;
125 uint32_t SubHeaderSize;
126 uint16_t VersionMinor;
127 uint16_t VersionMajor;
135 uint32_t UncompressedSize;
139 FatBinInnerHeader(uint32_t DataSize, uint32_t CudaArch, uint32_t Flags)
140 :
Kind(1 ), unknown02(0x0101), HeaderSize(
sizeof(*
this)),
141 DataSize(DataSize), unknown0c(0), CompressedSize(0),
142 SubHeaderSize(HeaderSize - 8), VersionMinor(2), VersionMajor(4),
143 CudaArch(CudaArch), unknown20(0), unknown24(0), Flags(Flags),
144 unknown2c(0), unknown30(0), unknown34(0), UncompressedSize(0),
145 unknown3c(0), unknown40(0), unknown44(0) {}
148 struct FatBinHeader {
155 FatBinHeader(uint32_t DataSize)
156 : Magic(0xba55ed50), Version(1), HeaderSize(
sizeof(*
this)),
157 DataSize(DataSize), unknown0c(0) {}
160 FatBinHeader OuterHeader(
sizeof(FatBinInnerHeader) +
PTXCode.size());
162 ((
char *)&OuterHeader) + OuterHeader.HeaderSize);
165 FatBinFlags::AddressSize64 |
166 FatBinFlags::HostLinux);
168 ((
char *)&InnerHeader) + InnerHeader.HeaderSize);
172 return llvm::Error::success();
enum clang::sema::@1655::IndirectLocalPathEntry::EntryKind Kind
llvm::MachO::Target Target
Defines the clang::TargetOptions class.
std::string CudaGpuBinaryFileName
Name of file passed with -fcuda-include-gpubinary option to forward to CUDA runtime back-end for inco...
CodeGenOptions & getCodeGenOpts()
llvm::SmallVector< char, 1024 > FatbinContent
llvm::SmallString< 1024 > PTXCode
IncrementalCUDADeviceParser(Interpreter &Interp, std::unique_ptr< CompilerInstance > Instance, IncrementalParser &HostParser, llvm::LLVMContext &LLVMCtx, llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > VFS, llvm::Error &Err)
IncrementalParser & HostParser
llvm::Expected< PartialTranslationUnit & > Parse(llvm::StringRef Input) override
Parses incremental input by creating an in-memory file.
llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > VFS
llvm::Expected< llvm::StringRef > GeneratePTX()
~IncrementalCUDADeviceParser()
llvm::Error GenerateFatbinary()
Provides support for incremental compilation.
std::list< PartialTranslationUnit > PTUs
List containing every information about every incrementally parsed piece of code.
virtual llvm::Expected< PartialTranslationUnit & > Parse(llvm::StringRef Input)
Parses incremental input by creating an in-memory file.
CompilerInstance * getCI()
std::unique_ptr< CompilerInstance > CI
Compiler instance performing the incremental compilation.
Provides top-level interfaces for incremental compilation and execution.
The JSON file list parser is used to communicate input to InstallAPI.