Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ struct OclModule {
struct OclModuleBuilderOpts {
StringRef funcName = {};
bool printIr = false;
bool spirvDump = false;
bool enableObjectDump = false;
ArrayRef<StringRef> sharedLibPaths = {};
void (*pipeline)(OpPassManager &) = nullptr;
Expand Down Expand Up @@ -254,6 +255,7 @@ struct OclModuleBuilder {
private:
ModuleOp mlirModule;
const bool printIr;
const bool spirvDump;
const bool enableObjectDump;
const ArrayRef<StringRef> sharedLibPaths;
void (*const pipeline)(OpPassManager &);
Expand Down
39 changes: 38 additions & 1 deletion lib/gc/ExecutionEngine/GPURuntime/ocl/GpuOclRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ ArrayRef<Type> getArgTypes(const StringRef &funcName, ModuleOp &mod) {

OclModuleBuilder::OclModuleBuilder(ModuleOp module,
const OclModuleBuilderOpts &opts)
: mlirModule(module), printIr(opts.printIr),
: mlirModule(module), printIr(opts.printIr), spirvDump(opts.spirvDump),
enableObjectDump(opts.enableObjectDump),
sharedLibPaths(opts.sharedLibPaths),
pipeline(opts.pipeline
Expand Down Expand Up @@ -993,6 +993,43 @@ OclModuleBuilder::build(const OclRuntime::Ext &ext) {
mod.dump();
}

if (spirvDump) {
mod->walk([&](LLVM::GlobalOp global) {
auto isaKernel = [&](LLVM::GlobalOp op) {
return op.getName().starts_with("gcGpuOclKernel_") &&
op.getName().ends_with("SPIRV");
};

if (!isaKernel(global))
return WalkResult::skip();

auto name = global.getName();
gcLogD("Found a kernel to dump (", name.str(), ")");

std::error_code ec;
std::string filename = "GC_" + name.str() + ".spv";
llvm::raw_fd_ostream spvStream(filename, ec);
if (ec) {
gcLogE("Failed to create a file `", filename,
"`, error message: ", ec.message());
return WalkResult::skip();
}

auto val = global.getValue();
assert(val && "unexpected empty kernel");
auto string = llvm::cast<mlir::StringAttr>(*val);
spvStream.write(string.data(), string.size());

if (spvStream.has_error()) {
gcLogE("An error occured while writing to `", filename, "`.");
return WalkResult::skip();
}

spvStream.flush();
return WalkResult::skip();
});
}

OclModule::MainFunc main = {nullptr};

if (staticMain.empty()) {
Expand Down
4 changes: 4 additions & 0 deletions src/gc-gpu-runner/GpuRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ struct Options {
"print-ir",
llvm::cl::desc("Print the resulting IR before the execution."),
llvm::cl::init(false), llvm::cl::cat(runnerCategory)};
llvm::cl::opt<bool> dumpSpirv{
"dump-spirv", llvm::cl::desc("Dump spirv for generated kernels."),
llvm::cl::init(false), llvm::cl::cat(runnerCategory)};
llvm::cl::opt<std::string> objDumpFile{
"obj-dump-file",
llvm::cl::desc("Dump the compiled object to the specified file."),
Expand Down Expand Up @@ -143,6 +146,7 @@ int main(int argc, char **argv) {
opts.sharedLibs.end());
builderOpts.funcName = opts.mainFuncName;
builderOpts.printIr = opts.printIr;
builderOpts.spirvDump = opts.dumpSpirv;
builderOpts.enableObjectDump = !opts.objDumpFile.getValue().empty();
builderOpts.sharedLibPaths = sharedLibs;
builderOpts.pipeline =
Expand Down
Loading