Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport to 16] Add SPV_INTEL_bindless_images preview extension (#2535) #2546

Open
wants to merge 1 commit into
base: llvm_release_160
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ EXT(SPV_INTEL_fpga_argument_interfaces)
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 @@ -291,6 +291,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 @@ -3272,6 +3272,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 @@ -1555,6 +1555,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 @@ -2231,7 +2234,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 @@ -2396,6 +2399,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 @@ -3746,5 +3746,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 {
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 @@ -641,6 +641,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 @@ -25,3 +25,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)
9 changes: 8 additions & 1 deletion lib/SPIRV/libSPIRV/spirv_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ enum InternalOp {
IOpRoundFToTF32INTEL = 6426,
IOpMaskedGatherINTEL = 6428,
IOpMaskedScatterINTEL = 6429,
IOpConvertHandleToImageINTEL = 6529,
IOpConvertHandleToSamplerINTEL = 6530,
IOpPrev = OpMax - 2,
IOpForward
};
Expand All @@ -104,7 +106,8 @@ enum InternalCapability {
ICapabilityTensorFloat32RoundingINTEL = 6425,
ICapabilityMaskedGatherScatterINTEL = 6427,
ICapabilityCacheControlsINTEL = 6441,
ICapRegisterLimitsINTEL = 6460
ICapRegisterLimitsINTEL = 6460,
ICapabilityBindlessImagesINTEL = 6528
};

enum InternalFunctionControlMask { IFunctionControlOptNoneINTELMask = 0x10000 };
Expand Down Expand Up @@ -184,6 +187,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,46 @@
; 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 target("spirv.Image", i64, 2, 0, 0, 0, 0, 0, 0) @_Z76__spirv_ConvertHandleToImageINTEL_RPU3AS133__spirv_Image__long_2_0_0_0_0_0_0m(i64 %{{.*}})
; CHECK-LLVM: call spir_func target("spirv.Sampler") @_Z35__spirv_ConvertHandleToSamplerINTELm(i64 42)

define spir_func void @foo(i64 %in) {
%img = call spir_func target("spirv.Image", i64, 2, 0, 0, 0, 0, 0, 0) @_Z33__spirv_ConvertHandleToImageINTELl(i64 %in)
%samp = call spir_func target("spirv.Sampler") @_Z35__spirv_ConvertHandleToSamplerINTELl(i64 42)
ret void
}

declare spir_func target("spirv.Image", i64, 2, 0, 0, 0, 0, 0, 0) @_Z33__spirv_ConvertHandleToImageINTELl(i64)

declare spir_func target("spirv.Sampler") @_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,28 @@
; 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"

define spir_func void @foo(i32 %in) {
%img = call spir_func target("spirv.Image", i32, 2, 0, 0, 0, 0, 0, 0) @_Z33__spirv_ConvertHandleToImageINTELi(i32 %in)
%samp = call spir_func target("spirv.Sampler") @_Z35__spirv_ConvertHandleToSamplerINTELl(i64 42)
ret void
}

declare spir_func target("spirv.Image", i32, 2, 0, 0, 0, 0, 0, 0) @_Z33__spirv_ConvertHandleToImageINTELi(i32)

declare spir_func target("spirv.Sampler") @_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"}
Loading