diff --git a/README.md b/README.md index 7606227c90..d50d54c528 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/tools/llvm-spirv/llvm-spirv.cpp b/tools/llvm-spirv/llvm-spirv.cpp index 6bb03537bd..39cab241ed 100644 --- a/tools/llvm-spirv/llvm-spirv.cpp +++ b/tools/llvm-spirv/llvm-spirv.cpp @@ -93,6 +93,15 @@ static cl::opt IsRegularization("s", cl::desc("Regularize LLVM to be representable by SPIR-V")); +using SPIRV::VersionNumber; + +static cl::opt 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. @@ -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 MB = @@ -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) { @@ -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; } @@ -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"; @@ -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();