forked from llvm/offload-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffloader.cpp
More file actions
215 lines (178 loc) · 6.93 KB
/
Copy pathoffloader.cpp
File metadata and controls
215 lines (178 loc) · 6.93 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//===- gpu-exec.cpp - HLSL GPU Execution Tool -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
#include "API/API.h"
#include "API/Device.h"
#include "Config.h"
#include "Image/Image.h"
#include "Support/Check.h"
#include "Support/Pipeline.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/ToolOutputFile.h"
#include <string>
using namespace llvm;
using namespace offloadtest;
static cl::opt<std::string>
InputPipeline(cl::Positional, cl::desc("<input pipeline description>"),
cl::value_desc("filename"));
static cl::list<std::string> InputShader(cl::Positional,
cl::desc("<input compiled shader>"),
cl::value_desc("filename"));
static cl::opt<GPUAPI>
APIToUse("api", cl::desc("GPU API to use"), cl::init(GPUAPI::Unknown),
cl::values(clEnumValN(GPUAPI::DirectX, "dx", "DirectX"),
clEnumValN(GPUAPI::Vulkan, "vk", "Vulkan"),
clEnumValN(GPUAPI::Metal, "mtl", "Metal")));
static cl::opt<std::string> OutputFilename("o", cl::desc("Output filename"),
cl::value_desc("filename"),
cl::init("-"));
static cl::opt<std::string>
ImageOutput("r", cl::desc("Resource name to output as png"),
cl::value_desc("<name>"), cl::init(""));
static cl::opt<bool>
Quiet("quiet", cl::desc("Suppress printing the pipeline as output"));
static cl::opt<bool> Debug("debug-layer",
cl::desc("Enable runtime debug layers"));
static cl::opt<bool> Validation("validation-layer",
cl::desc("Enable runtime validation layers"));
static cl::opt<bool> UseWarp("warp", cl::desc("Use warp"));
static cl::opt<std::string> AdapterRegex(
"adapter-regex",
cl::desc(
"Case-insensitive regular expression to match GPU adapter description"),
cl::value_desc("<regex>"), cl::init(""));
static std::unique_ptr<MemoryBuffer> readFile(const std::string &Path) {
const ExitOnError ExitOnErr("gpu-exec: error: ");
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
MemoryBuffer::getFileOrSTDIN(Path);
ExitOnErr(errorCodeToError(FileOrErr.getError()));
return std::move(FileOrErr.get());
}
static int run();
int main(int ArgC, char **ArgV) {
const InitLLVM X(ArgC, ArgV);
cl::ParseCommandLineOptions(ArgC, ArgV, "GPU Execution Tool");
if (run())
return 1;
return 0;
}
static bool matchesRegexIgnoreCase(StringRef GPUDescription,
StringRef SearchExpr) {
const llvm::Regex R(SearchExpr, llvm::Regex::IgnoreCase);
return R.isValid() && R.match(GPUDescription);
}
static int run() {
const ExitOnError ExitOnErr("gpu-exec: error: ");
const DeviceConfig Config = {Debug, Validation};
auto DevicesOrErr = initializeDevices(Config);
if (!DevicesOrErr) {
logAllUnhandledErrors(DevicesOrErr.takeError(), errs(),
"gpu-exec: error: ");
return 1;
}
auto Devices = std::move(*DevicesOrErr);
const std::unique_ptr<MemoryBuffer> PipelineBuf = readFile(InputPipeline);
Pipeline PipelineDesc;
yaml::Input YIn(PipelineBuf->getBuffer());
YIn >> PipelineDesc;
ExitOnErr(llvm::errorCodeToError(YIn.error()));
// Read in the shaders
for (size_t I = 0; I < InputShader.size(); ++I) {
PipelineDesc.Shaders[I].Shader = readFile(InputShader[I]);
}
if (InputShader.size() != PipelineDesc.Shaders.size())
ExitOnErr(createStringError(
std::errc::invalid_argument,
"Pipeline description expects %d shader(s) %d provided",
PipelineDesc.Shaders.size(), InputShader.size()));
// Try to guess the API by reading the shader binary.
const StringRef Binary = PipelineDesc.Shaders[0].Shader->getBuffer();
if (APIToUse == GPUAPI::Unknown) {
if (Binary.starts_with("DXBC")) {
#ifdef __APPLE__
APIToUse = GPUAPI::Metal;
outs() << "Using Metal API\n";
#else
APIToUse = GPUAPI::DirectX;
outs() << "Using DirectX API\n";
#endif
} else if (*reinterpret_cast<const uint32_t *>(Binary.data()) ==
0x07230203) {
APIToUse = GPUAPI::Vulkan;
outs() << "Using Vulkan API\n";
}
}
if (UseWarp && APIToUse != GPUAPI::DirectX)
ExitOnErr(createStringError(std::errc::executable_format_error,
"WARP required DirectX API"));
if (APIToUse == GPUAPI::Unknown)
ExitOnErr(
createStringError(std::errc::executable_format_error,
"Could not identify API to execute provided shader"));
if (Devices.empty()) {
errs() << "No device available.";
return 1;
}
for (const auto &D : Devices) {
if (D->getAPI() != APIToUse)
continue;
if (UseWarp && D->getDescription() != "Microsoft Basic Render Driver")
continue;
if (!AdapterRegex.empty() &&
!matchesRegexIgnoreCase(D->getDescription(), AdapterRegex))
continue;
ExitOnErr(D->executeProgram(PipelineDesc));
// check the results
llvm::Error ResultErr = Error::success();
for (const auto &R : PipelineDesc.Results)
ResultErr = llvm::joinErrors(std::move(ResultErr), verifyResult(R));
if (ResultErr) {
logAllUnhandledErrors(std::move(ResultErr), errs());
return 1;
}
for (const auto &B : PipelineDesc.Buffers) {
if (B.Name == ImageOutput) {
if (B.ArraySize != 1)
ExitOnErr(
createStringError(std::errc::invalid_argument,
"Cannot output image for buffer '%s' with "
"array size %d, which is greater than 1",
B.Name.c_str(), B.ArraySize));
const ImageRef Img = ImageRef(B);
ExitOnErr(Image::writePNG(Img, OutputFilename));
return 0;
}
}
if (Quiet)
return 0;
llvm::sys::fs::OpenFlags OpenFlags = llvm::sys::fs::OF_None;
if (ImageOutput.empty()) {
std::error_code EC;
OpenFlags |= llvm::sys::fs::OF_Text;
auto Out =
std::make_unique<llvm::ToolOutputFile>(OutputFilename, EC, OpenFlags);
ExitOnErr(llvm::errorCodeToError(EC));
yaml::Output YOut(Out->os());
YOut << PipelineDesc;
Out->keep();
return 0;
}
ExitOnErr(
createStringError(Twine("No descriptor with name ") + ImageOutput));
}
return 1;
}