Skip to content

[spirv-to-ir-wrapper] Prevent non-IR from being passed along #5688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 10, 2022
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
9 changes: 9 additions & 0 deletions llvm/test/tools/spirv-to-ir-wrapper/skip-unknown-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; RUN: spirv-to-ir-wrapper %s -o %t.out -skip-unknown-input
; RUN: FileCheck %s --input-file %t.out --allow-empty --check-prefix=CHECK_EMPTY
; CHECK_EMPTY-NOT: JUST SOME TEXT

; RUN: spirv-to-ir-wrapper %s -o %t2.out
; RUN: FileCheck %s --input-file %t2.out --check-prefix=CHECK_PASSTHROUGH
; CHECK_PASSTHROUGH: JUST SOME TEXT

JUST SOME TEXT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; Check for passthrough abilities
; RUN: llvm-as %s -o %t.bc
; RUN: spirv-to-ir-wrapper %t.bc -o %t_1.bc
; RUN: spirv-to-ir-wrapper %t.bc -o %t_1.bc -skip-unknown-input
; RUN: llvm-dis %t_1.bc -o %t_1.ll
; RUN: FileCheck %s --input-file %t_1.ll

Expand Down
18 changes: 18 additions & 0 deletions llvm/tools/spirv-to-ir-wrapper/spirv-to-ir-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/StringSaver.h"
Expand All @@ -46,6 +47,12 @@ static cl::opt<std::string>
LlvmSpirvOpts("llvm-spirv-opts", cl::value_desc("llvm-spirv options"),
cl::desc("options to pass to llvm-spirv"));

// SkipUnknown - Skip unknown files (create empty output instead)
static cl::opt<bool>
SkipUnknown("skip-unknown-input",
cl::desc("Only pass through files that are LLVM-IR or "
"converted from SPIR-V"));

static void error(const Twine &Message) {
llvm::errs() << "spirv-to-ir-wrapper: " << Message << '\n';
exit(1);
Expand Down Expand Up @@ -82,6 +89,14 @@ static int copyInputToOutput() {
return llvm::sys::fs::copy_file(InputFilename, Output).value();
}

static int createEmptyOutput() {
int FD;
if (std::error_code EC = openFileForWrite(
Output, FD, sys::fs::CD_CreateAlways, sys::fs::OF_None))
return EC.value();
return llvm::sys::Process::SafelyCloseFileDescriptor(FD).value();
}

static bool isSPIRVBinary(const std::string &File) {
auto FileOrError = MemoryBuffer::getFile(File, /*IsText=*/false,
/*RequiresNullTerminator=*/false);
Expand Down Expand Up @@ -114,6 +129,9 @@ static int checkInputFileIsAlreadyLLVM(const char *Argv0) {
if (Ext == "spv" || isSPIRVBinary(InputFilename))
return convertSPIRVToLLVMIR(Argv0);

if (SkipUnknown)
return createEmptyOutput();

// We could not directly determine the input file, so we just copy it
// to the output file.
return copyInputToOutput();
Expand Down