Skip to content

Commit d35a1ad

Browse files
NikitaRudenkoIntelvladimirlaz
authored andcommitted
Extend LLVMSPIRVOpts.h interface allowing pass options to spirv passes
1 parent 68d4c4c commit d35a1ad

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

llvm-spirv/include/LLVMSPIRVLib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ ModulePass *createPreprocessMetadata();
215215
/// ostream.
216216
ModulePass *createSPIRVWriterPass(std::ostream &Str);
217217

218+
/// Create and return a pass that writes the module to the specified
219+
/// ostream.
220+
ModulePass *createSPIRVWriterPass(std::ostream &Str,
221+
const SPIRV::TranslatorOpts &Opts);
222+
218223
} // namespace llvm
219224

220225
#endif // SPIRV_H

llvm-spirv/lib/SPIRV/SPIRVWriterPass.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,26 @@ using namespace llvm;
2121
PreservedAnalyses SPIRVWriterPass::run(Module &M) {
2222
// FIXME: at the moment LLVM/SPIR-V translation errors are ignored.
2323
std::string Err;
24-
writeSpirv(&M, OS, Err);
24+
writeSpirv(&M, Opts, OS, Err);
2525
return PreservedAnalyses::all();
2626
}
2727

2828
namespace {
2929
class WriteSPIRVPass : public ModulePass {
3030
std::ostream &OS; // std::ostream to print on
31+
SPIRV::TranslatorOpts Opts;
32+
3133
public:
3234
static char ID; // Pass identification, replacement for typeid
33-
explicit WriteSPIRVPass(std::ostream &O) : ModulePass(ID), OS(O) {}
35+
WriteSPIRVPass(std::ostream &OS, const SPIRV::TranslatorOpts &Opts)
36+
: ModulePass(ID), OS(OS), Opts(Opts) {}
3437

3538
StringRef getPassName() const override { return "SPIRV Writer"; }
3639

3740
bool runOnModule(Module &M) override {
3841
// FIXME: at the moment LLVM/SPIR-V translation errors are ignored.
3942
std::string Err;
40-
writeSpirv(&M, OS, Err);
43+
writeSpirv(&M, Opts, OS, Err);
4144
return false;
4245
}
4346
};
@@ -46,5 +49,14 @@ class WriteSPIRVPass : public ModulePass {
4649
char WriteSPIRVPass::ID = 0;
4750

4851
ModulePass *llvm::createSPIRVWriterPass(std::ostream &Str) {
49-
return new WriteSPIRVPass(Str);
52+
SPIRV::TranslatorOpts DefaultOpts;
53+
// To preserve old behavior of the translator, let's enable all extensions
54+
// by default in this API
55+
DefaultOpts.enableAllExtensions();
56+
return createSPIRVWriterPass(Str, DefaultOpts);
57+
}
58+
59+
ModulePass *llvm::createSPIRVWriterPass(std::ostream &Str,
60+
const SPIRV::TranslatorOpts &Opts) {
61+
return new WriteSPIRVPass(Str, Opts);
5062
}

llvm-spirv/lib/SPIRV/SPIRVWriterPass.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef SPIRV_SPIRVWRITERPASS_H
1616
#define SPIRV_SPIRVWRITERPASS_H
1717

18+
#include "LLVMSPIRVOpts.h"
1819
#include "llvm/ADT/StringRef.h"
1920

2021
namespace llvm {
@@ -27,16 +28,27 @@ class PreservedAnalyses;
2728
/// manager.
2829
ModulePass *createSPIRVWriterPass(std::ostream &Str);
2930

31+
/// \brief Create and return a pass that writes the module to the specified
32+
/// ostream. Note that this pass is designed for use with the legacy pass
33+
/// manager.
34+
ModulePass *createSPIRVWriterPass(std::ostream &Str,
35+
const SPIRV::TranslatorOpts &Opts);
36+
3037
/// \brief Pass for writing a module of IR out to a SPIRV file.
3138
///
3239
/// Note that this is intended for use with the new pass manager. To construct
3340
/// a pass for the legacy pass manager, use the function above.
3441
class SPIRVWriterPass {
3542
std::ostream &OS;
43+
SPIRV::TranslatorOpts Opts;
3644

3745
public:
3846
/// \brief Construct a SPIRV writer pass around a particular output stream.
39-
explicit SPIRVWriterPass(std::ostream &OS) : OS(OS) {}
47+
explicit SPIRVWriterPass(std::ostream &OS) : OS(OS) {
48+
Opts.enableAllExtensions();
49+
}
50+
SPIRVWriterPass(std::ostream &OS, const SPIRV::TranslatorOpts &Opts)
51+
: OS(OS), Opts(Opts) {}
4052

4153
/// \brief Run the SPIRV writer pass, and output the module to the selected
4254
/// output stream.

0 commit comments

Comments
 (0)