[llvm] [SPIRV] Add support for `cl_khr_extended_bit_ops` (PR #120571)

Dmitry Sidorov via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 9 06:50:27 PST 2025


================
@@ -983,6 +983,37 @@ static bool buildBarrierInst(const SPIRV::IncomingCall *Call, unsigned Opcode,
   return true;
 }
 
+/// Helper function for building extended bit operations.
+static bool buildExtendedBitOpsInst(const SPIRV::IncomingCall *Call,
+                                    unsigned Opcode,
+                                    MachineIRBuilder &MIRBuilder,
+                                    SPIRVGlobalRegistry *GR) {
+  const SPIRV::DemangledBuiltin *Builtin = Call->Builtin;
+  const auto *ST =
+      static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
+  if ((Opcode == SPIRV::OpBitFieldInsert ||
+       Opcode == SPIRV::OpBitFieldSExtract ||
+       Opcode == SPIRV::OpBitFieldUExtract || Opcode == SPIRV::OpBitReverse) &&
+      !ST->canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions)) {
+    std::string DiagMsg = std::string(Builtin->Name) +
+                          ": the builtin requires the following SPIR-V "
+                          "extension: SPV_KHR_bit_instructions";
+    report_fatal_error(DiagMsg.c_str(), false);
----------------
MrSidims wrote:

Since __spirv_BitFieldInsert and other __spirv_ spellings for bit instructions are untested in the backend it's hard for me to understand whether this code would work correctly. My expectations are:
1. if OpenCL builtin calls present in LLVM IR input to the backend and **SPV_KHR_bit_instructions** is not enabled, then the error must be emitted;
2. if OpenCL builtin calls present in LLVM IR input to the backend and **SPV_KHR_bit_instructions** is enabled, then the error must not be emitted and **BitInstructions** capability must be generated;
3. if SPIR-V friendly calls like present in the module, then (unless there is a mechanism of distinguishing between Kernel and Shader capabilities in the backend of which I'm not yet aware of) the error must not be emitted, **BitInstructions** must not be generated, **Shader** capability must present in the module.

Will this code ensure the above behavior or may be I'm missing something, and my expected behavior is incorrect?

https://github.com/llvm/llvm-project/pull/120571


More information about the llvm-commits mailing list