Skip to content

Commit

Permalink
Add command line options to restrict max SPIR-V version
Browse files Browse the repository at this point in the history
llvm-spirv tool updated to use new translator API.

Signed-off-by: Alexey Sachkov <alexey.sachkov@intel.com>
  • Loading branch information
AlexeySachkov authored and AlexeySotkin committed Aug 12, 2019
1 parent 355a324 commit a0a7f26
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ To translate between LLVM IR and SPIR-V:
* `-spirv-text` - read/write SPIR-V in an internal textual format for debugging purpose. The textual format is not defined by SPIR-V spec.
* `-help` - to see full list of options

### Handling SPIR-V versions generated by the translator

There is one option to control the behavior of the translator with respect to
the version of the SPIR-V file which is being generated/consumed.

* `-spirv-max-version=` - this option allows restricting the
SPIRV-LLVM-Translator **not** to generate a SPIR-V with a version which is
higher than the one specified via this option.

If the `-r` option was also specified, the SPIRV-LLVM-Translator will reject
the input file and emit an error if the SPIR-V version in it is higher than
one specified via this option.

Allowed values are `1.0`/`1.1`.

## Branching strategy

Code on the master branch in this repository is intended to be compatible with master/trunk branch of the [llvm](https://github.com/llvm-mirror/llvm) project. That is, for an OpenCL kernel compiled to llvm bitcode by the latest version(built with the latest git commit or svn revision) of Clang it should be possible to translate it to SPIR-V with the llvm-spirv tool.
Expand Down
25 changes: 18 additions & 7 deletions tools/llvm-spirv/llvm-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ static cl::opt<bool>
IsRegularization("s",
cl::desc("Regularize LLVM to be representable by SPIR-V"));

using SPIRV::VersionNumber;

static cl::opt<VersionNumber> MaxSPIRVVersion(
"spirv-max-version",
cl::desc("Choose maximum SPIR-V version which can be emitted"),
cl::values(clEnumValN(VersionNumber::SPIRV_1_0, "1.0", "SPIR-V 1.0"),
clEnumValN(VersionNumber::SPIRV_1_1, "1.1", "SPIR-V 1.1")),
cl::init(VersionNumber::MaximumVersion));

#ifdef _SPIRV_SUPPORT_TEXT_FMT
namespace SPIRV {
// Use textual format for SPIRV.
Expand All @@ -117,7 +126,7 @@ static std::string removeExt(const std::string &FileName) {

static ExitOnError ExitOnErr;

static int convertLLVMToSPIRV() {
static int convertLLVMToSPIRV(const SPIRV::TranslatorOpts &Opts) {
LLVMContext Context;

std::unique_ptr<MemoryBuffer> MB =
Expand All @@ -140,9 +149,9 @@ static int convertLLVMToSPIRV() {
bool Success = false;
if (OutputFile != "-") {
std::ofstream OutFile(OutputFile, std::ios::binary);
Success = writeSpirv(M.get(), OutFile, Err);
Success = writeSpirv(M.get(), Opts, OutFile, Err);
} else {
Success = writeSpirv(M.get(), std::cout, Err);
Success = writeSpirv(M.get(), Opts, std::cout, Err);
}

if (!Success) {
Expand All @@ -152,13 +161,13 @@ static int convertLLVMToSPIRV() {
return 0;
}

static int convertSPIRVToLLVM() {
static int convertSPIRVToLLVM(const SPIRV::TranslatorOpts &Opts) {
LLVMContext Context;
std::ifstream IFS(InputFile, std::ios::binary);
Module *M;
std::string Err;

if (!readSpirv(Context, IFS, M, Err)) {
if (!readSpirv(Context, Opts, IFS, M, Err)) {
errs() << "Fails to load SPIR-V as LLVM Module: " << Err << '\n';
return -1;
}
Expand Down Expand Up @@ -266,6 +275,8 @@ int main(int Ac, char **Av) {

cl::ParseCommandLineOptions(Ac, Av, "LLVM/SPIR-V translator");

SPIRV::TranslatorOpts Opts(MaxSPIRVVersion);

#ifdef _SPIRV_SUPPORT_TEXT_FMT
if (ToText && (ToBinary || IsReverse || IsRegularization)) {
errs() << "Cannot use -to-text with -to-binary, -r, -s\n";
Expand All @@ -282,14 +293,14 @@ int main(int Ac, char **Av) {
#endif

if (!IsReverse && !IsRegularization)
return convertLLVMToSPIRV();
return convertLLVMToSPIRV(Opts);

if (IsReverse && IsRegularization) {
errs() << "Cannot have both -r and -s options\n";
return -1;
}
if (IsReverse)
return convertSPIRVToLLVM();
return convertSPIRVToLLVM(Opts);

if (IsRegularization)
return regularizeLLVM();
Expand Down

0 comments on commit a0a7f26

Please sign in to comment.