Skip to content

Commit 02337c6

Browse files
committed
Add spirv dump option
1 parent 472ef9c commit 02337c6

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

include/gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ struct OclModule {
226226
struct OclModuleBuilderOpts {
227227
StringRef funcName = {};
228228
bool printIr = false;
229+
bool spirvDump = false;
229230
bool enableObjectDump = false;
230231
ArrayRef<StringRef> sharedLibPaths = {};
231232
void (*pipeline)(OpPassManager &) = nullptr;
@@ -254,6 +255,7 @@ struct OclModuleBuilder {
254255
private:
255256
ModuleOp mlirModule;
256257
const bool printIr;
258+
const bool spirvDump;
257259
const bool enableObjectDump;
258260
const ArrayRef<StringRef> sharedLibPaths;
259261
void (*const pipeline)(OpPassManager &);

lib/gc/ExecutionEngine/GPURuntime/ocl/GpuOclRuntime.cpp

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,9 @@ struct Kernel {
128128

129129
explicit Kernel(cl_program program, cl_kernel kernel, const size_t *gridSize,
130130
const size_t *blockSize, size_t argNum, const size_t *argSize)
131-
: program(program),
132-
kernel(kernel), globalSize{gridSize[0] * blockSize[0],
133-
gridSize[1] * blockSize[1],
134-
gridSize[2] * blockSize[2]},
131+
: program(program), kernel(kernel),
132+
globalSize{gridSize[0] * blockSize[0], gridSize[1] * blockSize[1],
133+
gridSize[2] * blockSize[2]},
135134
localSize{blockSize[0], blockSize[1], blockSize[2]},
136135
argSize(argSize, argSize + argNum) {
137136
#ifndef NDEBUG
@@ -817,7 +816,7 @@ ArrayRef<Type> getArgTypes(const StringRef &funcName, ModuleOp &mod) {
817816

818817
OclModuleBuilder::OclModuleBuilder(ModuleOp module,
819818
const OclModuleBuilderOpts &opts)
820-
: mlirModule(module), printIr(opts.printIr),
819+
: mlirModule(module), printIr(opts.printIr), spirvDump(opts.spirvDump),
821820
enableObjectDump(opts.enableObjectDump),
822821
sharedLibPaths(opts.sharedLibPaths),
823822
pipeline(opts.pipeline
@@ -993,6 +992,43 @@ OclModuleBuilder::build(const OclRuntime::Ext &ext) {
993992
mod.dump();
994993
}
995994

995+
if (spirvDump) {
996+
mod->walk([&](LLVM::GlobalOp global) {
997+
auto isaKernel = [&](LLVM::GlobalOp op) {
998+
return op.getName().starts_with("gcGpuOclKernel_") &&
999+
op.getName().ends_with("SPIRV");
1000+
};
1001+
1002+
if (!isaKernel(global))
1003+
return WalkResult::skip();
1004+
1005+
auto name = global.getName();
1006+
gcLogD("Found a kernel to dump (", name.str(), ")");
1007+
1008+
std::error_code ec;
1009+
std::string filename = "GC_" + name.str() + ".spv";
1010+
llvm::raw_fd_ostream spvStream(filename, ec);
1011+
if (ec) {
1012+
gcLogE("Failed to create a file `", filename,
1013+
"`, error message: ", ec.message());
1014+
return WalkResult::skip();
1015+
}
1016+
1017+
auto val = global.getValue();
1018+
assert(val && "unexpected empty kernel");
1019+
auto string = llvm::cast<mlir::StringAttr>(*val);
1020+
spvStream.write(string.data(), string.size());
1021+
1022+
if (spvStream.has_error()) {
1023+
gcLogE("An error occured while writing to `", filename, "`.");
1024+
return WalkResult::skip();
1025+
}
1026+
1027+
spvStream.flush();
1028+
return WalkResult::skip();
1029+
});
1030+
}
1031+
9961032
OclModule::MainFunc main = {nullptr};
9971033

9981034
if (staticMain.empty()) {

src/gc-gpu-runner/GpuRunner.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ struct Options {
6161
"print-ir",
6262
llvm::cl::desc("Print the resulting IR before the execution."),
6363
llvm::cl::init(false), llvm::cl::cat(runnerCategory)};
64+
llvm::cl::opt<bool> dumpSpirv{
65+
"dump-spirv", llvm::cl::desc("Dump spirv for generated kernels."),
66+
llvm::cl::init(false), llvm::cl::cat(runnerCategory)};
6467
llvm::cl::opt<std::string> objDumpFile{
6568
"obj-dump-file",
6669
llvm::cl::desc("Dump the compiled object to the specified file."),
@@ -143,6 +146,7 @@ int main(int argc, char **argv) {
143146
opts.sharedLibs.end());
144147
builderOpts.funcName = opts.mainFuncName;
145148
builderOpts.printIr = opts.printIr;
149+
builderOpts.spirvDump = opts.dumpSpirv;
146150
builderOpts.enableObjectDump = !opts.objDumpFile.getValue().empty();
147151
builderOpts.sharedLibPaths = sharedLibs;
148152
builderOpts.pipeline =

0 commit comments

Comments
 (0)