Skip to content

Commit efe0e10

Browse files
[SPIR-V] Support SPV_INTEL_arbitrary_precision_integers_extension, misc utils for other extensions
Differential Revision: https://reviews.llvm.org/D158764
1 parent 466a814 commit efe0e10

12 files changed

+170
-31
lines changed

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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 9 additions & 1 deletion
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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,12 @@ void RequirementHandler::initAvailableCapabilitiesForOpenCL(
574574
// TODO: verify if this needs some checks.
575575
addAvailableCaps({Capability::Float16, Capability::Float64});
576576

577-
// Add cap for SPV_INTEL_optnone.
578-
// FIXME: this should be added only if the target has the extension.
579-
addAvailableCaps({Capability::OptNoneINTEL});
577+
// Add capabilities enabled by extensions.
578+
for (auto Extension : ST.getAllAvailableExtensions()) {
579+
CapabilityList EnabledCapabilities =
580+
getCapabilitiesEnabledByExtension(Extension);
581+
addAvailableCaps(EnabledCapabilities);
582+
}
580583

581584
// TODO: add OpenCL extensions.
582585
}

llvm/lib/Target/SPIRV/SPIRVSubtarget.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ 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(SPIRV::Extension::SPV_INTEL_arbitrary_precision_integers,
35+
"SPV_INTEL_arbitrary_precision_integers",
36+
"Allows generating arbitrary width integer types"),
37+
clEnumValN(SPIRV::Extension::SPV_INTEL_optnone, "SPV_INTEL_optnone",
38+
"Adds OptNoneINTEL value for Function Control mask that "
39+
"indicates a request to not optimize the function"),
40+
clEnumValN(SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration,
41+
"SPV_KHR_no_integer_wrap_decoration",
42+
"Adds decorations to indicate that a given instruction does "
43+
"not cause integer wrapping")));
44+
3045
// Compare version numbers, but allow 0 to mean unspecified.
3146
static bool isAtLeastVer(uint32_t Target, uint32_t VerToCompareTo) {
3247
return Target == 0 || Target >= VerToCompareTo;
@@ -100,17 +115,13 @@ bool SPIRVSubtarget::canDirectlyComparePointers() const {
100115
return isAtLeastVer(SPIRVVersion, 14);
101116
}
102117

103-
// TODO: use command line args for this rather than defaults.
104118
void SPIRVSubtarget::initAvailableExtensions() {
105119
AvailableExtensions.clear();
106120
if (!isOpenCLEnv())
107121
return;
108-
// A default extension for testing.
109-
// FIXME: This should be changed when we can select extensions through a
110-
// command line flag.
111-
AvailableExtensions.insert(
112-
SPIRV::Extension::SPV_KHR_no_integer_wrap_decoration);
113-
AvailableExtensions.insert(SPIRV::Extension::SPV_INTEL_optnone);
122+
123+
for (auto Extension : Extensions)
124+
AvailableExtensions.insert(Extension);
114125
}
115126

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

llvm/lib/Target/SPIRV/SPIRVSubtarget.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
8686
// TODO: implement command line args or other ways to determine this.
8787
bool hasOpenCLFullProfile() const { return true; }
8888
bool hasOpenCLImageSupport() const { return true; }
89+
const SmallSet<SPIRV::Extension::Extension, 4> &
90+
getAllAvailableExtensions() const {
91+
return AvailableExtensions;
92+
}
8993
bool canUseExtension(SPIRV::Extension::Extension E) const;
9094
bool canUseExtInstSet(SPIRV::InstructionSet::InstructionSet E) const;
9195

@@ -113,6 +117,10 @@ class SPIRVSubtarget : public SPIRVGenSubtargetInfo {
113117
const SPIRVRegisterInfo *getRegisterInfo() const override {
114118
return &InstrInfo.getRegisterInfo();
115119
}
120+
121+
static bool classof(const TargetSubtargetInfo *ST) {
122+
return ST->getTargetTriple().isSPIRV();
123+
}
116124
};
117125
} // namespace llvm
118126

llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td

Lines changed: 53 additions & 1 deletion
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,7 +249,52 @@ 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>;
246-
defm SPV_INTEL_optnone : ExtensionOperand<58>;
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>;
297+
defm SPV_INTEL_optnone : ExtensionOperand<103>;
247298

248299
//===----------------------------------------------------------------------===//
249300
// Multiclass used to define Capabilities enum values and at the same time
@@ -397,6 +448,7 @@ defm ComputeDerivativeGroupLinearNV : CapabilityOperand<5350, 0, 0, [], []>;
397448
defm FragmentDensityEXT : CapabilityOperand<5291, 0, 0, [], [Shader]>;
398449
defm PhysicalStorageBufferAddressesEXT : CapabilityOperand<5347, 0, 0, [], [Shader]>;
399450
defm CooperativeMatrixNV : CapabilityOperand<5357, 0, 0, [], [Shader]>;
451+
defm ArbitraryPrecisionIntegersINTEL : CapabilityOperand<5844, 0, 0, [SPV_INTEL_arbitrary_precision_integers], [Int8, Int16]>;
400452
defm OptNoneINTEL : CapabilityOperand<6094, 0, 0, [SPV_INTEL_optnone], []>;
401453

402454
//===----------------------------------------------------------------------===//
Lines changed: 35 additions & 0 deletions
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown --spirv-extensions=SPV_INTEL_optnone %s -o - | FileCheck %s --check-prefix=CHECK-EXTENSION
2+
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s --check-prefix=CHECK-NO-EXTENSION
3+
4+
; CHECK-EXTENSION: OpCapability OptNoneINTEL
5+
; CHECK-EXTENSION: OpExtension "SPV_INTEL_optnone"
6+
; CHECK-NO-EXTENSION-NOT: OpCapability OptNoneINTEL
7+
; CHECK-NO-EXTENSION-NOT: OpExtension "SPV_INTEL_optnone"
8+
9+
;; Per SPIR-V spec:
10+
;; FunctionControlDontInlineMask = 0x2 (2)
11+
; CHECK-EXTENSION: %[[#]] = OpFunction %[[#]] DontInline
12+
13+
; Function Attrs: nounwind optnone noinline
14+
define spir_func void @_Z3foov() #0 {
15+
entry:
16+
ret void
17+
}
18+
19+
attributes #0 = { nounwind optnone noinline }

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

Lines changed: 1 addition & 1 deletion
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/optnone.ll

Lines changed: 0 additions & 17 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
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)