Skip to content

Commit fb51bb4

Browse files
[GpuOclRuntime] Add spirv dump option (#418)
1 parent 472ef9c commit fb51bb4

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
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: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ ArrayRef<Type> getArgTypes(const StringRef &funcName, ModuleOp &mod) {
817817

818818
OclModuleBuilder::OclModuleBuilder(ModuleOp module,
819819
const OclModuleBuilderOpts &opts)
820-
: mlirModule(module), printIr(opts.printIr),
820+
: mlirModule(module), printIr(opts.printIr), spirvDump(opts.spirvDump),
821821
enableObjectDump(opts.enableObjectDump),
822822
sharedLibPaths(opts.sharedLibPaths),
823823
pipeline(opts.pipeline
@@ -993,6 +993,43 @@ OclModuleBuilder::build(const OclRuntime::Ext &ext) {
993993
mod.dump();
994994
}
995995

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

9981035
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)