Skip to content

Commit 021aaf0

Browse files
[SPIR-V] Support SPV_INTEL_arbitrary_precision_integers extension
1 parent 6e08094 commit 021aaf0

10 files changed

+149
-7
lines changed

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVBaseInfo.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
129129
return Capabilities;
130130
}
131131

132+
CapabilityList
133+
getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension) {
134+
const SPIRV::ExtensionEntry *Entry =
135+
SPIRV::lookupSymbolicOperandsEnabledByExtension(
136+
Extension, SPIRV::OperandCategory::CapabilityOperand);
137+
138+
CapabilityList Capabilities;
139+
while (Entry &&
140+
Entry->Category == SPIRV::OperandCategory::CapabilityOperand &&
141+
Entry->ReqExtension == Extension) {
142+
Capabilities.push_back(
143+
static_cast<SPIRV::Capability::Capability>(Entry->Value));
144+
++Entry;
145+
}
146+
147+
return Capabilities;
148+
}
149+
132150
ExtensionList
133151
getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category,
134152
uint32_t Value) {

llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVBaseInfo.h

+2
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category,
223223
CapabilityList
224224
getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category,
225225
uint32_t Value);
226+
CapabilityList
227+
getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension);
226228
ExtensionList
227229
getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category,
228230
uint32_t Value);

llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,15 @@ SPIRVType *SPIRVGlobalRegistry::getOpTypeInt(uint32_t Width,
8181
MachineIRBuilder &MIRBuilder,
8282
bool IsSigned) {
8383
assert(Width <= 64 && "Unsupported integer width!");
84-
if (Width <= 8)
84+
const SPIRVSubtarget &ST =
85+
cast<SPIRVSubtarget>(MIRBuilder.getMF().getSubtarget());
86+
if (ST.canUseExtension(
87+
SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers)) {
88+
MIRBuilder.buildInstr(SPIRV::OpExtension)
89+
.addImm(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers);
90+
MIRBuilder.buildInstr(SPIRV::OpCapability)
91+
.addImm(SPIRV::Capability::ArbitraryPrecisionIntegersINTEL);
92+
} else if (Width <= 8)
8593
Width = 8;
8694
else if (Width <= 16)
8795
Width = 16;

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,12 @@ void RequirementHandler::initAvailableCapabilities(const SPIRVSubtarget &ST) {
593593
// TODO: verify if this needs some checks.
594594
addAvailableCaps({Capability::Float16, Capability::Float64});
595595

596+
// Add capabilities enabled by extensions.
597+
for (auto Extension : ST.getAllAvailableExtensions()) {
598+
CapabilityList EnabledCapabilities =
599+
getCapabilitiesEnabledByExtension(Extension);
600+
addAvailableCaps(EnabledCapabilities);
601+
}
596602
// TODO: add OpenCL extensions.
597603
}
598604
} // namespace SPIRV

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ using namespace llvm;
2727
#define GET_SUBTARGETINFO_CTOR
2828
#include "SPIRVGenSubtargetInfo.inc"
2929

30+
cl::list<SPIRV::Extension::Extension> Extensions(
31+
"spirv-extensions", cl::desc("SPIR-V extensions"), cl::ZeroOrMore,
32+
cl::Hidden,
33+
cl::values(
34+
clEnumValN(
35+
SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers,
36+
"SPV_INTEL_arbitrary_precision_integers",
37+
"Allows generating arbitrary width integer types"),
38+
clEnumValN(SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration,
39+
"SPV_KHR_no_integer_wrap_decoration",
40+
"Adds decorations to indicate that a given instruction does "
41+
"not cause integer wrapping")));
42+
3043
// Compare version numbers, but allow 0 to mean unspecified.
3144
static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo) {
3245
return Target == 0 || Target >= VerToCompareTo;
@@ -90,14 +103,14 @@ bool SPIRVSubtarget::canDirectlyComparePointers() const {
90103
return isAtLeastVer(SPIRVVersion, 14);
91104
}
92105

93-
// TODO: use command line args for this rather than defaults.
94106
void SPIRVSubtarget::initAvailableExtensions() {
95107
AvailableExtensions.clear();
96108
if (!isOpenCLEnv())
97109
return;
98-
// A default extension for testing.
99-
AvailableExtensions.insert(
100-
SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration);
110+
111+
for (auto Extension : Extensions) {
112+
AvailableExtensions.insert(Extension);
113+
}
101114
}
102115

103116
// TODO: use command line args for this rather than just defaults.

llvm/lib/Target/SPIRV/SPIRVSubtarget.h

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
7979
// TODO: implement command line args or other ways to determine this.
8080
bool hasOpenCLFullProfile() const { return true; }
8181
bool hasOpenCLImageSupport() const { return true; }
82+
const SmallSet<SPIRV::Extension::Extension, 4> &
83+
getAllAvailableExtensions() const {
84+
return AvailableExtensions;
85+
}
8286
bool canUseExtension(SPIRV::Extension::Extension E) const;
8387
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const;
8488

@@ -106,6 +110,10 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
106110
const SPIRVRegisterInfo *getRegisterInfo() const override {
107111
return &InstrInfo.getRegisterInfo();
108112
}
113+
114+
static bool classof(const TargetSubtargetInfo *ST) {
115+
return ST->getTargetTriple().isSPIRV();
116+
}
109117
};
110118
} // namespace llvm
111119

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

+52
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ def ExtensionEntries : GenericTable {
7777
let PrimaryKeyName = "lookupExtensionByCategoryAndValue";
7878
}
7979

80+
// Function to lookup symbolic operands enabled by a given extension.
81+
def lookupSymbolicOperandsEnabledByExtension : SearchIndex {
82+
let Table = ExtensionEntries;
83+
let Key = ["ReqExtension", "Category"];
84+
}
85+
8086
//===----------------------------------------------------------------------===//
8187
// Lookup table for matching symbolic operands (category + 32-bit value) to
8288
// SPIR-V capabilities. If an operand requires more than one capability, there
@@ -243,6 +249,51 @@ defm SPV_KHR_shader_clock : ExtensionOperand<54>;
243249
defm SPV_INTEL_unstructured_loop_controls : ExtensionOperand<55>;
244250
defm SPV_EXT_demote_to_helper_invocation : ExtensionOperand<56>;
245251
defm SPV_INTEL_fpga_reg : ExtensionOperand<57>;
252+
defm SPV_INTEL_blocking_pipes : ExtensionOperand<58>;
253+
defm SPV_GOOGLE_user_type : ExtensionOperand<59>;
254+
defm SPV_KHR_physical_storage_buffer : ExtensionOperand<60>;
255+
defm SPV_INTEL_kernel_attributes : ExtensionOperand<61>;
256+
defm SPV_KHR_non_semantic_info : ExtensionOperand<62>;
257+
defm SPV_INTEL_io_pipes : ExtensionOperand<63>;
258+
defm SPV_KHR_ray_tracing : ExtensionOperand<64>;
259+
defm SPV_KHR_ray_query : ExtensionOperand<65>;
260+
defm SPV_INTEL_fpga_memory_accesses : ExtensionOperand<66>;
261+
defm SPV_INTEL_arbitrary_precision_integers : ExtensionOperand<67>;
262+
defm SPV_EXT_shader_atomic_float_add : ExtensionOperand<68>;
263+
defm SPV_KHR_terminate_invocation : ExtensionOperand<69>;
264+
defm SPV_KHR_fragment_shading_rate : ExtensionOperand<70>;
265+
defm SPV_EXT_shader_image_int64 : ExtensionOperand<71>;
266+
defm SPV_INTEL_fp_fast_math_mode : ExtensionOperand<72>;
267+
defm SPV_INTEL_fpga_cluster_attributes : ExtensionOperand<73>;
268+
defm SPV_INTEL_loop_fuse : ExtensionOperand<74>;
269+
defm SPV_EXT_shader_atomic_float_min_max : ExtensionOperand<75>;
270+
defm SPV_KHR_workgroup_memory_explicit_layout : ExtensionOperand<76>;
271+
defm SPV_KHR_linkonce_odr : ExtensionOperand<77>;
272+
defm SPV_KHR_expect_assume : ExtensionOperand<78>;
273+
defm SPV_INTEL_fpga_dsp_control : ExtensionOperand<79>;
274+
defm SPV_NV_bindless_texture : ExtensionOperand<80>;
275+
defm SPV_INTEL_fpga_invocation_pipelining_attributes : ExtensionOperand<81>;
276+
defm SPV_KHR_subgroup_uniform_control_flow : ExtensionOperand<82>;
277+
defm SPV_HUAWEI_subpass_shading : ExtensionOperand<83>;
278+
defm SPV_KHR_integer_dot_product : ExtensionOperand<84>;
279+
defm SPV_EXT_shader_atomic_float16_add : ExtensionOperand<85>;
280+
defm SPV_INTEL_runtime_aligned : ExtensionOperand<86>;
281+
defm SPV_KHR_bit_instructions : ExtensionOperand<87>;
282+
defm SPV_NV_ray_tracing_motion_blur : ExtensionOperand<88>;
283+
defm SPV_KHR_uniform_group_instructions : ExtensionOperand<89>;
284+
defm SPV_KHR_subgroup_rotate : ExtensionOperand<90>;
285+
defm SPV_INTEL_split_barrier : ExtensionOperand<91>;
286+
defm SPV_KHR_ray_cull_mask : ExtensionOperand<92>;
287+
defm SPV_KHR_fragment_shader_barycentric : ExtensionOperand<93>;
288+
defm SPV_EXT_relaxed_printf_string_address_space : ExtensionOperand<94>;
289+
defm SPV_EXT_ycbcr_attachments : ExtensionOperand<95>;
290+
defm SPV_EXT_mesh_shader : ExtensionOperand<96>;
291+
defm SPV_ARM_core_builtins : ExtensionOperand<97>;
292+
defm SPV_EXT_opacity_micromap : ExtensionOperand<98>;
293+
defm SPV_NV_shader_invocation_reorder : ExtensionOperand<99>;
294+
defm SPV_INTEL_usm_storage_classes : ExtensionOperand<100>;
295+
defm SPV_INTEL_fpga_latency_control : ExtensionOperand<101>;
296+
defm SPV_INTEL_fpga_argument_interfaces : ExtensionOperand<102>;
246297

247298
//===----------------------------------------------------------------------===//
248299
// Multiclass used to define Capabilities enum values and at the same time
@@ -396,6 +447,7 @@ defm ComputeDerivativeGroupLinearNV : CapabilityOperand<5350, 0, 0, [], []>;
396447
defm FragmentDensityEXT : CapabilityOperand<5291, 0, 0, [], [Shader]>;
397448
defm PhysicalStorageBufferAddressesEXT : CapabilityOperand<5347, 0, 0, [], [Shader]>;
398449
defm CooperativeMatrixNV : CapabilityOperand<5357, 0, 0, [], [Shader]>;
450+
defm ArbitraryPrecisionIntegersINTEL : CapabilityOperand<5844, 0, 0, [SPV_INTEL_arbitrary_precision_integers], []>;
399451

400452
//===----------------------------------------------------------------------===//
401453
// Multiclass used to define SourceLanguage enum values and at the same time
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown --spirv-extensions=SPV_INTEL_arbitrary_precision_integers %s -o - | FileCheck %s
2+
3+
define i6 @getConstantI6() {
4+
ret i6 2
5+
}
6+
7+
define i13 @getConstantI13() {
8+
ret i13 42
9+
}
10+
11+
;; Capabilities:
12+
; CHECK-DAG: OpExtension "SPV_INTEL_arbitrary_precision_integers"
13+
; CHECK-DAG: OpCapability ArbitraryPrecisionIntegersINTEL
14+
15+
; CHECK-NOT: DAG-FENCE
16+
17+
;; Names:
18+
; CHECK-DAG: OpName %[[#GET_I6:]] "getConstantI6"
19+
; CHECK-DAG: OpName %[[#GET_I13:]] "getConstantI13"
20+
21+
; CHECK-NOT: DAG-FENCE
22+
23+
;; Types and Constants:
24+
; CHECK-DAG: %[[#I6:]] = OpTypeInt 6 0
25+
; CHECK-DAG: %[[#I13:]] = OpTypeInt 13 0
26+
; CHECK-DAG: %[[#CST_I6:]] = OpConstant %[[#I6]] 2
27+
; CHECK-DAG: %[[#CST_I13:]] = OpConstant %[[#I13]] 42
28+
29+
; CHECK: %[[#GET_I6]] = OpFunction %[[#I6]]
30+
; CHECK: OpReturnValue %[[#CST_I6]]
31+
; CHECK: OpFunctionEnd
32+
33+
; CHECK: %[[#GET_I13]] = OpFunction %[[#I13]]
34+
; CHECK: OpReturnValue %[[#CST_I13]]
35+
; CHECK: OpFunctionEnd

llvm/test/CodeGen/SPIRV/extensions/no_wrap.ll llvm/test/CodeGen/SPIRV/extensions/SPV_KHR_no_integer_wrap_decoration.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
1+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown --spirv-extensions=SPV_KHR_no_integer_wrap_decoration %s -o - | FileCheck %s
22

33
; CHECK-DAG: OpExtension "SPV_KHR_no_integer_wrap_decoration"
44

llvm/test/CodeGen/SPIRV/transcoding/NoSignedUnsignedWrap.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
;;
88
;; Positive tests:
99
;;
10-
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-NEGATIVE
10+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown --spirv-extensions=SPV_KHR_no_integer_wrap_decoration %s -o - | FileCheck %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-NEGATIVE
1111
;;
1212
;; Negative tests:
1313
;;

0 commit comments

Comments
 (0)