1//===----------- DeviceOffload.h - Device Offloading ------------*- C++ -*-===//
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 file defines the classes required for offloading to devices.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_INTERPRETER_DEVICE_OFFLOAD_H
14#define LLVM_CLANG_LIB_INTERPRETER_DEVICE_OFFLOAD_H
15
16#include "IncrementalParser.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/VirtualFileSystem.h"
20
21namespace clang {
22struct PartialTranslationUnit;
23class CompilerInstance;
24class CodeGenOptions;
25class TargetOptions;
26class IncrementalAction;
27
28class IncrementalDeviceParser : public IncrementalParser {
29
30public:
31 IncrementalDeviceParser(
32 CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
33 IncrementalAction *DeviceAct,
34 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
35 llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
36
37 virtual llvm::Error GenerateOffloadBinary() = 0;
38
39 ~IncrementalDeviceParser() override;
40
41protected:
42 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS;
43 CodeGenOptions &CodeGenOpts;
44 const TargetOptions &TargetOpts;
45};
46
47class IncrementalHIPDeviceParser : public IncrementalDeviceParser {
48 CompilerInstance &DeviceCI;
49 llvm::SmallVector<char, 0> ObjectContent;
50
51 // Generate the AMDGPU object file for the last PTU.
52 llvm::Expected<llvm::StringRef> GenerateDeviceObject();
53
54public:
55 IncrementalHIPDeviceParser(
56 CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
57 IncrementalAction *DeviceAct,
58 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
59 llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
60
61 llvm::Error GenerateOffloadBinary() override;
62
63 ~IncrementalHIPDeviceParser() override;
64};
65
66class IncrementalCUDADeviceParser : public IncrementalDeviceParser {
67 // Generate PTX for the last PTU.
68 llvm::Expected<llvm::StringRef> GeneratePTX();
69
70 // Generate fatbinary contents in memory
71 llvm::Error GenerateFatbinary();
72
73 int SMVersion;
74 llvm::SmallString<1024> PTXCode;
75 llvm::SmallVector<char, 1024> FatbinContent;
76
77public:
78 IncrementalCUDADeviceParser(
79 CompilerInstance &DeviceInstance, CompilerInstance &HostInstance,
80 IncrementalAction *DeviceAct,
81 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> VFS,
82 llvm::Error &Err, std::list<PartialTranslationUnit> &PTUs);
83
84 llvm::Error GenerateOffloadBinary() override;
85
86 ~IncrementalCUDADeviceParser() override;
87};
88
89} // namespace clang
90
91#endif // LLVM_CLANG_LIB_INTERPRETER_DEVICE_OFFLOAD_H
92