Skip to content

Commit

Permalink
[SPIRV] Make access qualifier optional for spirv.Image type (llvm#110852
Browse files Browse the repository at this point in the history
)

The SPIRV backend has a special type named `spirv.Image`. This type is
meant to correspond to the OpTypeImage instruction in SPIR-V, but there
is one difference. The access qualifier operand in OpTypeImage is
optional. On top of that, the access qualifiers are only valid for
kernels, and not for shaders.

We want to reuse this type when generating shader from HLSL, but we
can't use the access qualifier. This commit make the access qualifer
optional in the target extension type.

The same is done for `spirv.SampledImage`.

Contributes to llvm#81036
  • Loading branch information
s-perron authored and xgupta committed Oct 4, 2024
1 parent 3602dcd commit 2aa341b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 29 deletions.
10 changes: 5 additions & 5 deletions llvm/docs/SPIRVUsage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,19 @@ using target extension types and are represented as follows:

.. table:: SPIR-V Opaque Types

================== ====================== =========================================================================================
================== ====================== ===========================================================================================
SPIR-V Type LLVM type name LLVM type arguments
================== ====================== =========================================================================================
OpTypeImage ``spirv.Image`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, access qualifier
================== ====================== ===========================================================================================
OpTypeImage ``spirv.Image`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, [access qualifier]
OpTypeSampler ``spirv.Sampler`` (none)
OpTypeSampledImage ``spirv.SampledImage`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, access qualifier
OpTypeSampledImage ``spirv.SampledImage`` sampled type, dimensionality, depth, arrayed, MS, sampled, image format, [access qualifier]
OpTypeEvent ``spirv.Event`` (none)
OpTypeDeviceEvent ``spirv.DeviceEvent`` (none)
OpTypeReserveId ``spirv.ReserveId`` (none)
OpTypeQueue ``spirv.Queue`` (none)
OpTypePipe ``spirv.Pipe`` access qualifier
OpTypePipeStorage ``spirv.PipeStorage`` (none)
================== ====================== =========================================================================================
================== ====================== ===========================================================================================

All integer arguments take the same value as they do in their `corresponding
SPIR-V instruction <https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_type_declaration_instructions>`_.
Expand Down
18 changes: 13 additions & 5 deletions llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2678,19 +2678,27 @@ getImageType(const TargetExtType *ExtensionType,
"SPIR-V image builtin type must have sampled type parameter!");
const SPIRVType *SampledType =
GR->getOrCreateSPIRVType(ExtensionType->getTypeParameter(0), MIRBuilder);
assert(ExtensionType->getNumIntParameters() == 7 &&
assert((ExtensionType->getNumIntParameters() == 7 ||
ExtensionType->getNumIntParameters() == 6) &&
"Invalid number of parameters for SPIR-V image builtin!");

SPIRV::AccessQualifier::AccessQualifier accessQualifier =
SPIRV::AccessQualifier::None;
if (ExtensionType->getNumIntParameters() == 7) {
accessQualifier = Qualifier == SPIRV::AccessQualifier::WriteOnly
? SPIRV::AccessQualifier::WriteOnly
: SPIRV::AccessQualifier::AccessQualifier(
ExtensionType->getIntParameter(6));
}

// Create or get an existing type from GlobalRegistry.
return GR->getOrCreateOpTypeImage(
MIRBuilder, SampledType,
SPIRV::Dim::Dim(ExtensionType->getIntParameter(0)),
ExtensionType->getIntParameter(1), ExtensionType->getIntParameter(2),
ExtensionType->getIntParameter(3), ExtensionType->getIntParameter(4),
SPIRV::ImageFormat::ImageFormat(ExtensionType->getIntParameter(5)),
Qualifier == SPIRV::AccessQualifier::WriteOnly
? SPIRV::AccessQualifier::WriteOnly
: SPIRV::AccessQualifier::AccessQualifier(
ExtensionType->getIntParameter(6)));
accessQualifier);
}

static SPIRVType *getSampledImageType(const TargetExtType *OpaqueType,
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/Target/SPIRV/SPIRVDuplicatesTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ make_descr_image(const Type *SampledTy, unsigned Dim, unsigned Depth,
inline SpecialTypeDescriptor
make_descr_sampled_image(const Type *SampledTy, const MachineInstr *ImageTy) {
assert(ImageTy->getOpcode() == SPIRV::OpTypeImage);
unsigned AC = AccessQualifier::AccessQualifier::None;
if (ImageTy->getNumOperands() > 8)
AC = ImageTy->getOperand(8).getImm();
return std::make_tuple(
SampledTy,
ImageAttrs(
ImageTy->getOperand(2).getImm(), ImageTy->getOperand(3).getImm(),
ImageTy->getOperand(4).getImm(), ImageTy->getOperand(5).getImm(),
ImageTy->getOperand(6).getImm(), ImageTy->getOperand(7).getImm(),
ImageTy->getOperand(8).getImm())
ImageTy->getOperand(6).getImm(), ImageTy->getOperand(7).getImm(), AC)
.Val,
SpecialTypeKind::STK_SampledImage);
}
Expand Down
23 changes: 13 additions & 10 deletions llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,16 +1149,19 @@ SPIRVType *SPIRVGlobalRegistry::getOrCreateOpTypeImage(
return Res;
Register ResVReg = createTypeVReg(MIRBuilder);
DT.add(TD, &MIRBuilder.getMF(), ResVReg);
return MIRBuilder.buildInstr(SPIRV::OpTypeImage)
.addDef(ResVReg)
.addUse(getSPIRVTypeID(SampledType))
.addImm(Dim)
.addImm(Depth) // Depth (whether or not it is a Depth image).
.addImm(Arrayed) // Arrayed.
.addImm(Multisampled) // Multisampled (0 = only single-sample).
.addImm(Sampled) // Sampled (0 = usage known at runtime).
.addImm(ImageFormat)
.addImm(AccessQual);
auto MIB = MIRBuilder.buildInstr(SPIRV::OpTypeImage)
.addDef(ResVReg)
.addUse(getSPIRVTypeID(SampledType))
.addImm(Dim)
.addImm(Depth) // Depth (whether or not it is a Depth image).
.addImm(Arrayed) // Arrayed.
.addImm(Multisampled) // Multisampled (0 = only single-sample).
.addImm(Sampled) // Sampled (0 = usage known at runtime).
.addImm(ImageFormat);

if (AccessQual != SPIRV::AccessQualifier::None)
MIB.addImm(AccessQual);
return MIB;
}

SPIRVType *
Expand Down
17 changes: 10 additions & 7 deletions llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ void RequirementHandler::initAvailableCapabilitiesForVulkan(

// Provided by all supported Vulkan versions.
addAvailableCaps({Capability::Int16, Capability::Int64, Capability::Float16,
Capability::Float64, Capability::GroupNonUniform});
Capability::Float64, Capability::GroupNonUniform,
Capability::Image1D, Capability::SampledBuffer,
Capability::ImageBuffer});
}

} // namespace SPIRV
Expand Down Expand Up @@ -776,12 +778,13 @@ static void addOpTypeImageReqs(const MachineInstr &MI,
}

// Has optional access qualifier.
// TODO: check if it's OpenCL's kernel.
if (MI.getNumOperands() > 8 &&
MI.getOperand(8).getImm() == SPIRV::AccessQualifier::ReadWrite)
Reqs.addRequirements(SPIRV::Capability::ImageReadWrite);
else
Reqs.addRequirements(SPIRV::Capability::ImageBasic);
if (ST.isOpenCLEnv()) {
if (MI.getNumOperands() > 8 &&
MI.getOperand(8).getImm() == SPIRV::AccessQualifier::ReadWrite)
Reqs.addRequirements(SPIRV::Capability::ImageReadWrite);
else
Reqs.addRequirements(SPIRV::Capability::ImageBasic);
}
}

// Add requirements for handling atomic float instructions
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,7 @@ multiclass AccessQualifierOperand<bits<32> value, list<Capability> reqCapabiliti
defm ReadOnly : AccessQualifierOperand<0, [Kernel]>;
defm WriteOnly : AccessQualifierOperand<1, [Kernel]>;
defm ReadWrite : AccessQualifierOperand<2, [Kernel]>;
defm None : AccessQualifierOperand<3, []>;

//===----------------------------------------------------------------------===//
// Multiclass used to define FunctionParameterAttribute enum values and at the
Expand Down
20 changes: 20 additions & 0 deletions llvm/test/CodeGen/SPIRV/ShaderBufferImage.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-vulkan-library %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-library %s -o - -filetype=obj | spirv-val %}

; CHECK-NOT: OpCapability ImageBasic
; CHECK-NOT: OpCapability ImageReadWrite
; CHECK: OpCapability ImageBuffer
; CHECK-NOT: OpCapability ImageBasic
; CHECK-NOT: OpCapability ImageReadWrite

; CHECK-DAG: [[Float:%[0-9]+]] = OpTypeFloat 32
; CHECK-DAG: [[Void:%[0-9]+]] = OpTypeVoid
; CHECK-DAG: [[ImageType:%[0-9]+]] = OpTypeImage [[Float]] Buffer 2 0 0 2 R32i {{$}}
; CHECK-DAG: [[ImageFuncType:%[0-9]+]] = OpTypeFunction [[Void]] [[ImageType]]

; CHECK: {{%[0-9]+}} = OpFunction [[Void]] DontInline [[ImageFuncType]]
define void @ImageWithNoAccessQualifier(target("spirv.Image", float, 5, 2, 0, 0, 2, 24) %img) #0 {
ret void
}

attributes #0 = { convergent noinline norecurse "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
21 changes: 21 additions & 0 deletions llvm/test/CodeGen/SPIRV/ShaderImage.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-vulkan-library %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-vulkan-library %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: [[Float:%[0-9]+]] = OpTypeFloat 32
; CHECK-DAG: [[Void:%[0-9]+]] = OpTypeVoid
; CHECK-DAG: [[ImageType:%[0-9]+]] = OpTypeImage [[Float]] Buffer 2 0 0 1 R32i {{$}}
; CHECK-DAG: [[ImageFuncType:%[0-9]+]] = OpTypeFunction [[Void]] [[ImageType]]
; CHECK-DAG: [[SampledImageType:%[0-9]+]] = OpTypeSampledImage [[ImageType]]
; CHECK-DAG: [[SampledImageFuncType:%[0-9]+]] = OpTypeFunction [[Void]] [[SampledImageType]]

; CHECK: {{%[0-9]+}} = OpFunction [[Void]] DontInline [[ImageFuncType]]
define void @ImageWithNoAccessQualifier(target("spirv.Image", float, 5, 2, 0, 0, 1, 24) %img) #0 {
ret void
}

; CHECK: {{%[0-9]+}} = OpFunction [[Void]] DontInline [[SampledImageFuncType]]
define void @SampledImageWithNoAccessQualifier(target("spirv.SampledImage", float, 5, 2, 0, 0, 1, 24) %img) #0 {
ret void
}

attributes #0 = { convergent noinline norecurse "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }

0 comments on commit 2aa341b

Please sign in to comment.