Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 11 additions & 1 deletion mlir/include/mlir/Target/SPIRV/Serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ struct SerializationOptions {
/// tool. This saved file is later used for validation.
bool saveModuleForValidation = false;
/// A prefix prepended to the file used when `saveModuleForValidation`
/// is set to `true`.
/// is set to `true`. This can either be a file prefix, or a relative or
/// or an absolute path followed by the prefix. For example:
///
/// * "foo" - Create files with a `foo` prefix in the current working
/// directory. For example: `foo0`, `foo1` ... `fooN`.
///
/// * "my/dir/foo" - Create files in `my/dir` with a `foo` prefix. The
/// `my/dir` need to exists. For example: `foo0`, `foo1` ... `fooN`
/// will be created and stored in `/my/dir`.
///
/// * "/home/user/my/dir" - Same as above but using an absolute path.
std::string validationFilePrefix = "";
};

Expand Down
31 changes: 15 additions & 16 deletions mlir/lib/Target/SPIRV/TranslateRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ void registerFromSPIRVTranslation() {
// Serialization registration
//===----------------------------------------------------------------------===//

// Static variable is probably not ideal, but it lets us have unique files names
// without taking additional parameters from `mlir-translate`.
static size_t validationFileCounter = 0;

static LogicalResult
serializeModule(spirv::ModuleOp moduleOp, raw_ostream &output,
const spirv::SerializationOptions &options) {
Expand All @@ -106,18 +102,22 @@ serializeModule(spirv::ModuleOp moduleOp, raw_ostream &output,
return failure();
}
}
std::string errorMessage;
std::string filename =
options.validationFilePrefix + std::to_string(validationFileCounter++);
std::unique_ptr<llvm::ToolOutputFile> validationOutput =
openOutputFile(filename, &errorMessage);
if (!validationOutput) {
llvm::errs() << errorMessage << "\n";

SmallString<128> filename;
int fd;

std::error_code errorCode = llvm::sys::fs::createUniqueFile(
options.validationFilePrefix + "%%%%%%", fd, filename);
if (errorCode) {
llvm::errs() << "error creating validation output file: "
<< errorCode.message() << "\n";
return failure();
}
validationOutput->os().write(reinterpret_cast<char *>(binary.data()),
sizeInBytes);
validationOutput->keep();

llvm::raw_fd_ostream validationOutput(fd, /*shouldClose=*/true);
validationOutput.write(reinterpret_cast<char *>(binary.data()),
sizeInBytes);
validationOutput.flush();
}

return mlir::success();
Expand All @@ -133,8 +133,7 @@ void registerToSPIRVTranslation() {
"is used to generate separate binaries for validation, where "
"`--split-input-file` normally combines all outputs into one. The "
"one combined output (`-o`) is still written. Created files need to "
"be "
"removed manually once processed."),
"be removed manually once processed."),
llvm::cl::init(""));

TranslateFromMLIRRegistration toBinary(
Expand Down
29 changes: 29 additions & 0 deletions mlir/test/Target/SPIRV/mlir-translate.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// COM: Check that `--spirv-save-validation-files-with-prefix` generates
// COM: a correct number of files.

// REQUIRES: shell
// RUN: rm -rf %t
// RUN: mkdir %t
// RUN: mlir-translate --serialize-spirv --no-implicit-module --split-input-file --spirv-save-validation-files-with-prefix=%t/foo %s
// RUN: ls %t | wc -l | FileCheck %s
// RUN: rm -rf %t

// CHECK: 4

spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
}

// -----

spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
}

// -----

spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
}

// -----

spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
}