-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathdcomputecodegenerator.cpp
109 lines (92 loc) · 3.12 KB
/
dcomputecodegenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//===-- driver/dcomputecodegenerator.cpp ----------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "driver/dcomputecodegenerator.h"
#include "driver/cl_options.h"
#include "driver/cl_helpers.h"
#include "dmd/errors.h"
#include "ir/irdsymbol.h"
#include "llvm/Support/CommandLine.h"
#include <array>
#include <string>
#include <algorithm>
#if !(LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX)
DComputeCodeGenManager::DComputeCodeGenManager(llvm::LLVMContext &c) : ctx(c) {}
void DComputeCodeGenManager::emit(Module *) {}
void DComputeCodeGenManager::writeModules() {}
DComputeCodeGenManager::~DComputeCodeGenManager() {}
#else
DComputeTarget *
DComputeCodeGenManager::createComputeTarget(const std::string &s) {
if (s.substr(0, 4) == "ocl-") {
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV
#define OCL_VALID_VER_INIT 100, 110, 120, 200, 210, 220
const std::array<int, 6> valid_ocl_versions = {{OCL_VALID_VER_INIT}};
const int v = atoi(s.c_str() + 4);
if (std::find(valid_ocl_versions.begin(), valid_ocl_versions.end(), v) !=
valid_ocl_versions.end()) {
return createOCLTarget(ctx, v);
}
#else
error(Loc(), "LDC was not built with OpenCl DCompute support.");
#endif
}
if (s.substr(0, 5) == "cuda-") {
#if LDC_LLVM_SUPPORTED_TARGET_NVPTX
#define CUDA_VALID_VER_INIT 100, 110, 120, 130, 200, 210, 300, 350, 370,\
500, 520, 600, 610, 620, 700, 720, 750, 800
const std::array<int, 18> valid_cuda_versions = {{CUDA_VALID_VER_INIT}};
const int v = atoi(s.c_str() + 5);
if (std::find(valid_cuda_versions.begin(), valid_cuda_versions.end(), v) !=
valid_cuda_versions.end()) {
return createCUDATarget(ctx, v);
}
#else
error(Loc(), "LDC was not built with CUDA DCompute support.");
#endif
}
#define STR(...) #__VA_ARGS__
#define XSTR(x) STR(x)
error(Loc(),
"Unrecognised or invalid DCompute targets: the format is ocl-xy0 "
"for OpenCl x.y and cuda-xy0 for CUDA CC x.y."
#if LDC_LLVM_SUPPORTED_TARGET_SPIRV
" Valid version strings for OpenCl are ocl-{" XSTR(OCL_VALID_VER_INIT) "}."
#endif
#if LDC_LLVM_SUPPORTED_TARGET_NVPTX
" Valid version strings for CUDA are cuda-{" XSTR(CUDA_VALID_VER_INIT) "}."
#endif
);
#undef XSTR
#undef STR
fatal();
return nullptr;
}
DComputeCodeGenManager::DComputeCodeGenManager(llvm::LLVMContext &c) : ctx(c) {
for (auto &option : opts::dcomputeTargets) {
targets.push_back(createComputeTarget(option));
}
oldGIR = gIR;
oldGTargetMachine = gTargetMachine;
}
void DComputeCodeGenManager::emit(Module *m) {
for (auto &target : targets) {
target->emit(m);
IrDsymbol::resetAll();
}
}
void DComputeCodeGenManager::writeModules() {
for (auto &target : targets) {
target->writeModule();
}
}
DComputeCodeGenManager::~DComputeCodeGenManager() {
gIR = oldGIR;
gTargetMachine = oldGTargetMachine;
}
#endif // LDC_LLVM_SUPPORTED_TARGET_SPIRV || LDC_LLVM_SUPPORTED_TARGET_NVPTX