Skip to content

Commit

Permalink
Add SPV_EXT_image_raw10_raw12 writer support
Browse files Browse the repository at this point in the history
Add basic LLVM-to-SPIR-V support for the SPV_EXT_image_raw10_raw12
extension, enabling the extension if constants from the extension are
found in the LLVM IR.

The extension adds 2 new return values for `OpImageQueryFormat`, which
are integer constants that may appear anywhere in LLVM IR.
Distinguishing between the extension's constants and arbitrary
constants that happen to have the same value as the extension's
constants is not possible in general.  Hence this patch only covers
some common use cases, where the result of `OpImageQueryFormat` is
directly used in an integer comparison or switch instruction.
  • Loading branch information
svenvh committed Aug 21, 2023
1 parent d021f75 commit 40fdfac
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/SPIRV/OCLToSPIRV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"

#include <algorithm>
#include <set>

using namespace llvm;
using namespace PatternMatch;
using namespace SPIRV;
using namespace OCLUtil;

Expand Down Expand Up @@ -1355,10 +1357,56 @@ void OCLToSPIRVBase::visitCallScalToVec(CallInst *CI, StringRef MangledName,
});
}

namespace {
// Return true if any users of the CallInst use any of the constants
// introduced by the SPV_EXT_image_raw10_raw12 extension.
bool usesSpvExtImageRaw10Raw12Constants(const CallInst *CI) {
const std::array ExtConstants{
OCLImageChannelDataTypeOffset + ImageChannelDataTypeUnsignedIntRaw10EXT,
OCLImageChannelDataTypeOffset + ImageChannelDataTypeUnsignedIntRaw12EXT};

// The return values for `OpImageQueryFormat` added by the extension are
// integer constants that may appear anywhere in LLVM IR. Only detect some
// common use patterns here.
for (auto *U : CI->users()) {
for (auto C : ExtConstants) {
ICmpInst::Predicate Pred;
if (match(U, m_c_ICmp(Pred, m_Value(), m_SpecificInt(C)))) {
return true;
}
if (auto *Switch = dyn_cast<SwitchInst>(U)) {
if (any_of(Switch->cases(), [C](const auto &Case) {
return Case.getCaseValue()->equalsInt(C);
})) {
return true;
}
}
}
}
return false;
}
} // anonymous namespace

void OCLToSPIRVBase::visitCallGetImageChannel(CallInst *CI,
StringRef DemangledName,
unsigned int Offset) {
assert(CI->getCalledFunction() && "Unexpected indirect call");

if (Offset == OCLImageChannelDataTypeOffset) {
// See if any of the SPV_EXT_image_raw10_raw12 constants are used, and
// add the extension if not already there.
if (usesSpvExtImageRaw10Raw12Constants(CI)) {
const char *ExtStr = "SPV_EXT_image_raw10_raw12";
NamedMDNode *NMD = M->getOrInsertNamedMetadata(kSPIRVMD::Extension);
if (none_of(NMD->operands(), [ExtStr](MDNode *N) {
return N->getOperand(0).equalsStr(ExtStr);
})) {
MDString *MDS = MDString::get(*Ctx, ExtStr);
NMD->addOperand(MDNode::get(*Ctx, MDS));
}
}
}

Op OC = OpNop;
OCLSPIRVBuiltinMap::find(DemangledName.str(), &OC);
mutateCallInst(CI, OC).changeReturnType(
Expand Down
19 changes: 19 additions & 0 deletions test/extensions/EXT/SPV_EXT_image_raw10_raw12/constant-use.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 -triple spir-unknown-unknown -O1 -cl-std=CL2.0 -emit-llvm-bc %s -o %t.bc
// RUN: llvm-spirv --spirv-ext=+SPV_EXT_image_raw10_raw12 %t.bc -o %t.spv
// RUN: llvm-spirv --spirv-ext=+SPV_EXT_image_raw10_raw12 %t.spv -to-text -o %t.spt
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV

// CHECK-SPIRV-NOT: Extension "SPV_EXT_image_raw10_raw12"

// Test that use of constant values equal to the extension's constants do not enable the extension.

kernel void test_raw1012(global int *dst, int value) {
switch (value) {
case 0x10E3: // same value as CLK_UNSIGNED_INT_RAW10_EXT
*dst = 10;
break;
case 0x10E4: // same value as CLK_UNSIGNED_INT_RAW12_EXT
*dst = 12;
break;
}
}
43 changes: 43 additions & 0 deletions test/extensions/EXT/SPV_EXT_image_raw10_raw12/writer.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %clang_cc1 -triple spir-unknown-unknown -O1 -cl-std=CL2.0 -fdeclare-opencl-builtins -finclude-default-header -emit-llvm-bc %s -o %t.bc
// RUN: llvm-spirv --spirv-ext=+SPV_EXT_image_raw10_raw12 %t.bc -o %t.spv
// RUN: llvm-spirv --spirv-ext=+SPV_EXT_image_raw10_raw12 %t.spv -to-text -o %t.spt
// RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV

// RUN: llvm-spirv -r %t.spv -o %t.rev.bc
// RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefixes=CHECK-COMMON,CHECK-LLVM
// RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o %t.rev.bc
// RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefixes=CHECK-COMMON,CHECK-SPV-IR

// RUN: not llvm-spirv --spirv-ext=-SPV_EXT_image_raw10_raw12 %t.bc -o %t.spv 2>&1 | FileCheck %s --check-prefix=CHECK-EXT-OFF
// CHECK-EXT-OFF: Feature requires the following SPIR-V extension
// CHECK-EXT-OFF-NEXT: SPV_EXT_image_raw10_raw12

// CHECK-SPIRV: Extension "SPV_EXT_image_raw10_raw12"

// CHECK-COMMON: test_raw1012
// CHECK-LLVM: _Z27get_image_channel_data_type14ocl_image2d_ro
// CHECK-SPV-IR: call spir_func i32 @_Z24__spirv_ImageQueryFormatPU3AS133__spirv_Image__void_1_0_0_0_0_0_0
// CHECK-COMMON: switch i32
// CHECK-COMMON: i32 4323,
// CHECK-COMMON: i32 4324,
// CHECK-COMMON: icmp eq i32 %{{.*}}, 4323
// CHECK-COMMON: icmp eq i32 %{{.*}}, 4324

kernel void test_raw1012(global int *dst, read_only image2d_t img) {
switch (get_image_channel_data_type(img)) {
case CLK_SNORM_INT8:
*dst = 8;
break;
case CLK_UNSIGNED_INT_RAW10_EXT:
*dst = 10;
break;
case CLK_UNSIGNED_INT_RAW12_EXT:
*dst = 12;
break;
}

if (get_image_channel_data_type(img) == CLK_UNSIGNED_INT_RAW10_EXT)
*dst = 1010;
else if (CLK_UNSIGNED_INT_RAW12_EXT == get_image_channel_data_type(img))
*dst = 1212;
}

0 comments on commit 40fdfac

Please sign in to comment.