Skip to content

Commit

Permalink
Add SPV_INTEL_bindless_images preview extension (#2535)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSidims committed Apr 25, 2024
1 parent ded9ea3 commit a840244
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ EXT(SPV_EXT_relaxed_printf_string_address_space)
EXT(SPV_INTEL_fp_max_error)
EXT(SPV_INTEL_cache_controls)
EXT(SPV_INTEL_maximum_registers)
EXT(SPV_INTEL_bindless_images)
2 changes: 2 additions & 0 deletions lib/SPIRV/SPIRVInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ const static char Float[] = "float";
const static char Half[] = "half";
const static char Int[] = "int";
const static char UInt[] = "uint";
const static char Long[] = "long";
const static char ULong[] = "ulong";
const static char Void[] = "void";
} // namespace kSPIRVImageSampledTypeName

Expand Down
1 change: 1 addition & 0 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,7 @@ Instruction *SPIRVToLLVM::transSPIRVBuiltinFromInst(SPIRVInstruction *BI,
case internal::OpJointMatrixLoadINTEL:
case OpCooperativeMatrixLoadKHR:
case internal::OpCooperativeMatrixLoadCheckedINTEL:
case internal::OpConvertHandleToImageINTEL:
AddRetTypePostfix = true;
break;
default: {
Expand Down
9 changes: 8 additions & 1 deletion lib/SPIRV/SPIRVUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ Type *getLLVMTypeForSPIRVImageSampledTypePostfix(StringRef Postfix,
if (Postfix == kSPIRVImageSampledTypeName::Int ||
Postfix == kSPIRVImageSampledTypeName::UInt)
return Type::getInt32Ty(Ctx);
if (Postfix == kSPIRVImageSampledTypeName::Long ||
Postfix == kSPIRVImageSampledTypeName::ULong)
return Type::getInt64Ty(Ctx);
llvm_unreachable("Invalid sampled type postfix");
return nullptr;
}
Expand Down Expand Up @@ -2219,7 +2222,7 @@ class SPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {

void init(StringRef UniqUnmangledName) override {
UnmangledName = UniqUnmangledName.str();
switch (OC) {
switch (static_cast<unsigned>(OC)) {
case OpConvertUToF:
case OpUConvert:
case OpSatConvertUToS:
Expand Down Expand Up @@ -2384,6 +2387,10 @@ class SPIRVFriendlyIRMangleInfo : public BuiltinFuncMangleInfo {
}
break;
}
case internal::OpConvertHandleToImageINTEL:
case internal::OpConvertHandleToSamplerINTEL:
addUnsignedArg(0);
break;
default:;
// No special handling is needed
}
Expand Down
61 changes: 61 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3779,5 +3779,66 @@ template <Op OC> class SPIRVReadClockKHRInstBase : public SPIRVUnaryInst<OC> {
_SPIRV_OP(ReadClockKHR)
#undef _SPIRV_OP

template <Op OC> class SPIRVBindlessImagesInstBase : public SPIRVUnaryInst<OC> {
protected:
SPIRVCapVec getRequiredCapability() const override {
return getVec(internal::CapabilityBindlessImagesINTEL);
}

std::optional<ExtensionID> getRequiredExtension() const override {

Check failure on line 3788 in lib/SPIRV/libSPIRV/SPIRVInstruction.h

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

no template named 'optional' in namespace 'std'; did you mean 'Optional'? [clang-diagnostic-error]

Check failure on line 3788 in lib/SPIRV/libSPIRV/SPIRVInstruction.h

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

no template named 'optional' in namespace 'std'; did you mean 'Optional'? [clang-diagnostic-error]
return ExtensionID::SPV_INTEL_bindless_images;
}

void validate() const override {
SPIRVUnary::validate();

// validate is a const method, whilst getOperand is non-const method
// because it may call a method of class Module that may modify LiteralMap
// of Module field. That modification is not impacting validate method for
// these instructions, so const_cast is safe here.
using SPVBindlessImagesInst = SPIRVBindlessImagesInstBase<OC>;
SPIRVValue *Input =
const_cast<SPVBindlessImagesInst *>(this)->getOperand(0);
SPIRVType *InCompTy = Input->getType();

auto StringAddrMod = [](SPIRVAddressingModelKind Kind) -> std::string {
if (Kind == AddressingModelPhysical32)
return std::string("Physical32");
if (Kind == AddressingModelPhysical64)
return std::string("Physical64");
return std::string("AddressingModel: ") + std::to_string(Kind);
};

auto InstName = OpCodeNameMap::map(OC);
auto AddrMod = this->getModule()->getAddressingModel();
SPIRVErrorLog &SPVErrLog = this->getModule()->getErrorLog();
SPVErrLog.checkError(
(InCompTy->isTypeInt(32) && AddrMod == AddressingModelPhysical32) ||
(InCompTy->isTypeInt(64) && AddrMod == AddressingModelPhysical64),
SPIRVEC_InvalidInstruction,
InstName +
"\nParameter value must be a 32-bit scalar in case of "
"Physical32 addressing model or a 64-bit scalar in case of "
"Physical64 addressing model\n"
"Type size: " +
std::to_string(InCompTy->getBitWidth()) +
"\nAddressing model: " + StringAddrMod(AddrMod) + "\n");

SPIRVType *ResTy = this->getType();
SPVErrLog.checkError(
(ResTy->isTypeImage() && OC == internal::OpConvertHandleToImageINTEL) ||
(ResTy->isTypeSampler() &&
OC == internal::OpConvertHandleToSamplerINTEL),
SPIRVEC_InvalidInstruction,
InstName + "\nIncorrect return type of the instruction must be "
"image/sampler\n");
}
};
#define _SPIRV_OP(x) \
typedef SPIRVBindlessImagesInstBase<internal::Op##x> SPIRV##x;
_SPIRV_OP(ConvertHandleToImageINTEL)
_SPIRV_OP(ConvertHandleToSamplerINTEL)
#undef _SPIRV_OP

} // namespace SPIRV
#endif // SPIRV_LIBSPIRV_SPIRVINSTRUCTION_H
1 change: 1 addition & 0 deletions lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
add(internal::CapabilityRegisterLimitsINTEL, "RegisterLimitsINTEL");
add(internal::CapabilityCooperativeMatrixCheckedInstructionsINTEL,
"CooperativeMatrixCheckedInstructionsINTEL");
add(internal::CapabilityBindlessImagesINTEL, "BindlessImagesINTEL");
}
SPIRV_DEF_NAMEMAP(Capability, SPIRVCapabilityNameMap)

Expand Down
4 changes: 4 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVOpCodeEnumInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ _SPIRV_OP_INTERNAL(ComplexFDivINTEL, internal::ComplexFDivINTEL)
_SPIRV_OP_INTERNAL(MaskedGatherINTEL, internal::OpMaskedGatherINTEL)
_SPIRV_OP_INTERNAL(MaskedScatterINTEL, internal::OpMaskedScatterINTEL)
_SPIRV_OP_INTERNAL(RoundFToTF32INTEL, internal::RoundFToTF32INTEL)
_SPIRV_OP_INTERNAL(ConvertHandleToImageINTEL,
internal::ConvertHandleToImageINTEL)
_SPIRV_OP_INTERNAL(ConvertHandleToSamplerINTEL,
internal::ConvertHandleToSamplerINTEL)
7 changes: 7 additions & 0 deletions lib/SPIRV/libSPIRV/spirv_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ enum InternalOp {
IOpMaskedGatherINTEL = 6428,
IOpMaskedScatterINTEL = 6429,
IOpJointMatrixGetElementCoordINTEL = 6440,
IOpConvertHandleToImageINTEL = 6529,
IOpConvertHandleToSamplerINTEL = 6530,
IOpPrev = OpMax - 2,
IOpForward
};
Expand Down Expand Up @@ -114,6 +116,7 @@ enum InternalCapability {
ICapabilityJointMatrixWIInstructionsINTEL = 6435,
ICapabilityCacheControlsINTEL = 6441,
ICapRegisterLimitsINTEL = 6460

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]

Check failure on line 118 in lib/SPIRV/libSPIRV/spirv_internal.hpp

View workflow job for this annotation

GitHub Actions / clang-format & clang-tidy

missing ',' between enumerators [clang-diagnostic-error]
ICapabilityBindlessImagesINTEL = 6528
};

enum InternalFunctionControlMask { IFunctionControlOptNoneINTELMask = 0x10000 };
Expand Down Expand Up @@ -198,6 +201,10 @@ _SPIRV_OP(Capability, TensorFloat32RoundingINTEL)
_SPIRV_OP(Op, RoundFToTF32INTEL)

_SPIRV_OP(Capability, CacheControlsINTEL)

_SPIRV_OP(Capability, BindlessImagesINTEL)
_SPIRV_OP(Op, ConvertHandleToImageINTEL)
_SPIRV_OP(Op, ConvertHandleToSamplerINTEL)
#undef _SPIRV_OP

constexpr SourceLanguage SourceLanguagePython =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_INTEL_bindless_images
; RUN: llvm-spirv %t.spv -o %t.spt --to-text
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
; RUN: llvm-spirv %t.spv -o %t.rev.bc -r --spirv-target-env=SPV-IR
; RUN: llvm-dis %t.rev.bc -o %t.rev.ll
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

; RUN: not llvm-spirv %t.bc 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
; CHECK-ERROR: RequiresExtension: Feature requires the following SPIR-V extension:
; CHECK-ERROR-NEXT: SPV_INTEL_bindless_images

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
target triple = "spir64-unknown-unknown"

; CHECK-SPIRV: Capability BindlessImagesINTEL
; CHECK-SPIRV: Extension "SPV_INTEL_bindless_images"
; CHECK-SPIRV-DAG: TypeVoid [[#VoidTy:]]
; CHECK-SPIRV-DAG: TypeInt [[#Int64Ty:]] 64
; CHECK-SPIRV-DAG: Constant [[#Int64Ty]] [[#Const42:]] 42 0
; CHECK-SPIRV-DAG: TypeImage [[#IntImgTy:]] [[#Int64Ty]]
; CHECK-SPIRV-DAG: TypeSampler [[#SamplerTy:]]
; CHECK-SPIRV: FunctionParameter [[#Int64Ty]] [[#Input:]]
; CHECK-SPIRV: ConvertHandleToImageINTEL [[#IntImgTy]] [[#]] [[#Input]]
; CHECK-SPIRV: ConvertHandleToSamplerINTEL [[#SamplerTy]] [[#]] [[#Const42]]

; CHECK-LLVM: call spir_func %spirv.Image._long_2_0_0_0_0_0_0 addrspace(1)* @_Z76__spirv_ConvertHandleToImageINTEL_RPU3AS133__spirv_Image__long_2_0_0_0_0_0_0m(i64 %{{.*}})
; CHECK-LLVM: call spir_func %spirv.Sampler addrspace(1)* @_Z35__spirv_ConvertHandleToSamplerINTELm(i64 42)

%spirv.Image._long_2_0_0_0_0_0_0 = type opaque
%spirv.Sampler = type opaque

define spir_func void @foo(i64 %in) {
%img = call spir_func %spirv.Image._long_2_0_0_0_0_0_0 addrspace(1)* @_Z33__spirv_ConvertHandleToImageINTELl(i64 %in)
%samp = call spir_func %spirv.Sampler addrspace(1)* @_Z35__spirv_ConvertHandleToSamplerINTELl(i64 42)
ret void
}

declare spir_func %spirv.Image._long_2_0_0_0_0_0_0 addrspace(1)* @_Z33__spirv_ConvertHandleToImageINTELl(i64)

declare spir_func %spirv.Sampler addrspace(1)* @_Z35__spirv_ConvertHandleToSamplerINTELl(i64)

!opencl.spir.version = !{!0}
!spirv.Source = !{!1}
!llvm.ident = !{!2}

!0 = !{i32 1, i32 2}
!1 = !{i32 4, i32 100000}
!2 = !{!"clang version 17.0.0"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
; RUN: llvm-as %s -o %t.bc
; RUN: not llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_bindless_images 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR-1
; CHECK-ERROR-1: InvalidInstruction: Can't translate llvm instruction:
; CHECK-ERROR-1-NEXT: ConvertHandleToImageINTEL
; CHECK-ERROR-1-NEXT: Parameter value must be a 32-bit scalar in case of Physical32 addressing model or a 64-bit scalar in case of Physical64 addressing model
; CHECK-ERROR-1-NEXT: Type size: 32
; CHECK-ERROR-1-NEXT: Addressing model: Physical64

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
target triple = "spir64-unknown-unknown"

%spirv.Image._long_2_0_0_0_0_0_0 = type opaque
%spirv.Sampler = type opaque

define spir_func void @foo(i32 %in) {
%img = call spir_func %spirv.Image._long_2_0_0_0_0_0_0 addrspace(1)* @_Z33__spirv_ConvertHandleToImageINTELi(i32 %in)
%samp = call spir_func %spirv.Sampler addrspace(1)* @_Z35__spirv_ConvertHandleToSamplerINTELl(i64 42)
ret void
}

declare spir_func %spirv.Image._long_2_0_0_0_0_0_0 addrspace(1)* @_Z33__spirv_ConvertHandleToImageINTELi(i32)

declare spir_func %spirv.Sampler addrspace(1)* @_Z35__spirv_ConvertHandleToSamplerINTELl(i64)

!opencl.spir.version = !{!0}
!spirv.Source = !{!1}
!llvm.ident = !{!2}

!0 = !{i32 1, i32 2}
!1 = !{i32 4, i32 100000}
!2 = !{!"clang version 17.0.0"}

0 comments on commit a840244

Please sign in to comment.