Skip to content

Commit 45ab7c9

Browse files
authored
[spirv-to-ir-wrapper] Prevent non-IR from being passed along (#5688)
Introduce an option -skip-unknown-input which can be used to prevent unknown files from being passed through the tool. There are usage models involving archives in which the 'fat archives' do not contain all fat objects. In the case where this occurs, provide a way to prevent the non-IR object from the device side to be passed through as it is not valid at the device link step.
1 parent d18e69c commit 45ab7c9

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; RUN: spirv-to-ir-wrapper %s -o %t.out -skip-unknown-input
2+
; RUN: FileCheck %s --input-file %t.out --allow-empty --check-prefix=CHECK_EMPTY
3+
; CHECK_EMPTY-NOT: JUST SOME TEXT
4+
5+
; RUN: spirv-to-ir-wrapper %s -o %t2.out
6+
; RUN: FileCheck %s --input-file %t2.out --check-prefix=CHECK_PASSTHROUGH
7+
; CHECK_PASSTHROUGH: JUST SOME TEXT
8+
9+
JUST SOME TEXT

llvm/test/tools/spirv-to-ir-wrapper/spirv-to-ir-wrapper.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; Check for passthrough abilities
22
; RUN: llvm-as %s -o %t.bc
3-
; RUN: spirv-to-ir-wrapper %t.bc -o %t_1.bc
3+
; RUN: spirv-to-ir-wrapper %t.bc -o %t_1.bc -skip-unknown-input
44
; RUN: llvm-dis %t_1.bc -o %t_1.ll
55
; RUN: FileCheck %s --input-file %t_1.ll
66

llvm/tools/spirv-to-ir-wrapper/spirv-to-ir-wrapper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/FileSystem.h"
2727
#include "llvm/Support/InitLLVM.h"
2828
#include "llvm/Support/Path.h"
29+
#include "llvm/Support/Process.h"
2930
#include "llvm/Support/Program.h"
3031
#include "llvm/Support/SourceMgr.h"
3132
#include "llvm/Support/StringSaver.h"
@@ -46,6 +47,12 @@ static cl::opt<std::string>
4647
LlvmSpirvOpts("llvm-spirv-opts", cl::value_desc("llvm-spirv options"),
4748
cl::desc("options to pass to llvm-spirv"));
4849

50+
// SkipUnknown - Skip unknown files (create empty output instead)
51+
static cl::opt<bool>
52+
SkipUnknown("skip-unknown-input",
53+
cl::desc("Only pass through files that are LLVM-IR or "
54+
"converted from SPIR-V"));
55+
4956
static void error(const Twine &Message) {
5057
llvm::errs() << "spirv-to-ir-wrapper: " << Message << '\n';
5158
exit(1);
@@ -82,6 +89,14 @@ static int copyInputToOutput() {
8289
return llvm::sys::fs::copy_file(InputFilename, Output).value();
8390
}
8491

92+
static int createEmptyOutput() {
93+
int FD;
94+
if (std::error_code EC = openFileForWrite(
95+
Output, FD, sys::fs::CD_CreateAlways, sys::fs::OF_None))
96+
return EC.value();
97+
return llvm::sys::Process::SafelyCloseFileDescriptor(FD).value();
98+
}
99+
85100
static bool isSPIRVBinary(const std::string &File) {
86101
auto FileOrError = MemoryBuffer::getFile(File, /*IsText=*/false,
87102
/*RequiresNullTerminator=*/false);
@@ -114,6 +129,9 @@ static int checkInputFileIsAlreadyLLVM(const char *Argv0) {
114129
if (Ext == "spv" || isSPIRVBinary(InputFilename))
115130
return convertSPIRVToLLVMIR(Argv0);
116131

132+
if (SkipUnknown)
133+
return createEmptyOutput();
134+
117135
// We could not directly determine the input file, so we just copy it
118136
// to the output file.
119137
return copyInputToOutput();

0 commit comments

Comments
 (0)