Skip to content

Commit f762612

Browse files
authored
Explicitly enable used SPIRV extensions (#3715)
This PR makes it so instead of enabling all the SPIRV extension and disabling the problematic ones, we only enable extensions that we actually use. To find out the list of extensions I've started out from the list of extensions used in other projects that use the SPIRV-LLVM tool ([DPC++](https://github.com/intel/llvm/blob/0d5266b566a920905c6f8a6767f3454c4268ca08/sycl-jit/jit-compiler/lib/translation/SPIRVLLVMTranslation.cpp#L29)), narrowed it down and added missing extensions.
1 parent 1b1f0d2 commit f762612

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

third_party/intel/lib/Target/SPIRV/SPIRVTranslation.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "llvm/IR/Verifier.h"
77
#include "llvm/MC/TargetRegistry.h"
88
#include "llvm/Pass.h"
9-
#include "llvm/TargetParser/Triple.h"
109

1110
#if defined(LLVM_SPIRV_BACKEND_TARGET_PRESENT)
1211
namespace llvm {
@@ -106,16 +105,40 @@ class SmallVectorBuffer : public std::streambuf {
106105
SmallVectorBuffer(llvm::SmallVectorImpl<char> &O) : OS(O) {}
107106
};
108107

109-
std::string translateLLVMIRToSPIRV(llvm::Module &module) {
110-
// initLLVM();
108+
static SPIRV::TranslatorOpts getSPIRVOopts() {
109+
SPIRV::TranslatorOpts SPIRVOpts;
110+
static constexpr std::array<SPIRV::ExtensionID, 12> AllowedExtensions{
111+
SPIRV::ExtensionID::SPV_EXT_shader_atomic_float_add,
112+
SPIRV::ExtensionID::SPV_INTEL_arbitrary_precision_integers,
113+
SPIRV::ExtensionID::SPV_INTEL_arithmetic_fence,
114+
SPIRV::ExtensionID::SPV_INTEL_bfloat16_conversion,
115+
SPIRV::ExtensionID::SPV_INTEL_cache_controls,
116+
SPIRV::ExtensionID::SPV_INTEL_kernel_attributes,
117+
SPIRV::ExtensionID::SPV_INTEL_memory_access_aliasing,
118+
SPIRV::ExtensionID::SPV_INTEL_subgroups,
119+
SPIRV::ExtensionID::SPV_INTEL_unstructured_loop_controls,
120+
SPIRV::ExtensionID::SPV_INTEL_vector_compute,
121+
SPIRV::ExtensionID::SPV_KHR_bit_instructions,
122+
SPIRV::ExtensionID::SPV_KHR_non_semantic_info};
123+
124+
SPIRVOpts.setMemToRegEnabled(true);
125+
SPIRVOpts.setPreserveOCLKernelArgTypeMetadataThroughString(true);
126+
SPIRVOpts.setPreserveAuxData(false);
127+
SPIRVOpts.setSPIRVAllowUnknownIntrinsics({"llvm.genx.GenISA."});
128+
SPIRV::TranslatorOpts TransOpt{SPIRV::VersionNumber::SPIRV_1_4};
111129

130+
for (auto &Ext : AllowedExtensions)
131+
SPIRVOpts.setAllowedToUseExtension(Ext, true);
132+
return SPIRVOpts;
133+
}
134+
135+
std::string translateLLVMIRToSPIRV(llvm::Module &module) {
112136
llvm::SmallVector<char, 0> buffer;
113137

114138
// verify and store llvm
115139
llvm::legacy::PassManager pm;
116140
pm.add(llvm::createVerifierPass());
117141
pm.run(module);
118-
// module->print(llvm::outs(), nullptr);
119142

120143
if (module.materializeAll()) {
121144
llvm::errs() << "SPIRVTranslation: failed to read the LLVM module IR!";
@@ -129,14 +152,7 @@ std::string translateLLVMIRToSPIRV(llvm::Module &module) {
129152
std::ostream OS(&StreamBuf);
130153
std::string Err;
131154

132-
SPIRV::TranslatorOpts SPIRVOpts;
133-
SPIRVOpts.enableAllExtensions();
134-
SPIRVOpts.setAllowedToUseExtension(
135-
SPIRV::ExtensionID::SPV_KHR_untyped_pointers, false);
136-
SPIRVOpts.setMemToRegEnabled(true);
137-
SPIRVOpts.setPreserveOCLKernelArgTypeMetadataThroughString(true);
138-
SPIRVOpts.setPreserveAuxData(false);
139-
SPIRVOpts.setSPIRVAllowUnknownIntrinsics({"llvm.genx.GenISA."});
155+
SPIRV::TranslatorOpts SPIRVOpts = getSPIRVOopts();
140156

141157
#if defined(LLVM_SPIRV_BACKEND_TARGET_PRESENT)
142158
int SpvTranslateMode = 0;

0 commit comments

Comments
 (0)