Skip to content

Commit c7affa1

Browse files
authored
opt: add Int16 and Float16 to capability trim pass (KhronosGroup#5519)
Add support for Int16 and Float16 trim. Signed-off-by: Nathan Gauër <[email protected]>
1 parent 0a9f3d1 commit c7affa1

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

source/opt/trim_capabilities_pass.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ static bool Has16BitCapability(const FeatureManager* feature_manager) {
137137
// Handler names follow the following convention:
138138
// Handler_<Opcode>_<Capability>()
139139

140+
static std::optional<spv::Capability> Handler_OpTypeFloat_Float16(
141+
const Instruction* instruction) {
142+
assert(instruction->opcode() == spv::Op::OpTypeFloat &&
143+
"This handler only support OpTypeFloat opcodes.");
144+
145+
const uint32_t size =
146+
instruction->GetSingleWordInOperand(kOpTypeFloatSizeIndex);
147+
return size == 16 ? std::optional(spv::Capability::Float16) : std::nullopt;
148+
}
149+
140150
static std::optional<spv::Capability> Handler_OpTypeFloat_Float64(
141151
const Instruction* instruction) {
142152
assert(instruction->opcode() == spv::Op::OpTypeFloat &&
@@ -274,6 +284,16 @@ static std::optional<spv::Capability> Handler_OpTypePointer_StorageUniform16(
274284
: std::nullopt;
275285
}
276286

287+
static std::optional<spv::Capability> Handler_OpTypeInt_Int16(
288+
const Instruction* instruction) {
289+
assert(instruction->opcode() == spv::Op::OpTypeInt &&
290+
"This handler only support OpTypeInt opcodes.");
291+
292+
const uint32_t size =
293+
instruction->GetSingleWordInOperand(kOpTypeIntSizeIndex);
294+
return size == 16 ? std::optional(spv::Capability::Int16) : std::nullopt;
295+
}
296+
277297
static std::optional<spv::Capability> Handler_OpTypeInt_Int64(
278298
const Instruction* instruction) {
279299
assert(instruction->opcode() == spv::Op::OpTypeInt &&
@@ -341,12 +361,14 @@ Handler_OpImageSparseRead_StorageImageReadWithoutFormat(
341361
}
342362

343363
// Opcode of interest to determine capabilities requirements.
344-
constexpr std::array<std::pair<spv::Op, OpcodeHandler>, 10> kOpcodeHandlers{{
364+
constexpr std::array<std::pair<spv::Op, OpcodeHandler>, 12> kOpcodeHandlers{{
345365
// clang-format off
346366
{spv::Op::OpImageRead, Handler_OpImageRead_StorageImageReadWithoutFormat},
347367
{spv::Op::OpImageSparseRead, Handler_OpImageSparseRead_StorageImageReadWithoutFormat},
368+
{spv::Op::OpTypeFloat, Handler_OpTypeFloat_Float16 },
348369
{spv::Op::OpTypeFloat, Handler_OpTypeFloat_Float64 },
349370
{spv::Op::OpTypeImage, Handler_OpTypeImage_ImageMSArray},
371+
{spv::Op::OpTypeInt, Handler_OpTypeInt_Int16 },
350372
{spv::Op::OpTypeInt, Handler_OpTypeInt_Int64 },
351373
{spv::Op::OpTypePointer, Handler_OpTypePointer_StorageInputOutput16},
352374
{spv::Op::OpTypePointer, Handler_OpTypePointer_StoragePushConstant16},

source/opt/trim_capabilities_pass.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ class TrimCapabilitiesPass : public Pass {
7676
// clang-format off
7777
spv::Capability::ComputeDerivativeGroupLinearNV,
7878
spv::Capability::ComputeDerivativeGroupQuadsNV,
79+
spv::Capability::Float16,
7980
spv::Capability::Float64,
8081
spv::Capability::FragmentShaderPixelInterlockEXT,
8182
spv::Capability::FragmentShaderSampleInterlockEXT,
8283
spv::Capability::FragmentShaderShadingRateInterlockEXT,
8384
spv::Capability::Groups,
8485
spv::Capability::ImageMSArray,
86+
spv::Capability::Int16,
8587
spv::Capability::Int64,
8688
spv::Capability::Linkage,
8789
spv::Capability::MinLod,

test/opt/trim_capabilities_pass_test.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,104 @@ TEST_F(TrimCapabilitiesPassTest,
24862486
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
24872487
}
24882488

2489+
TEST_F(TrimCapabilitiesPassTest, Float16_RemovedWhenUnused) {
2490+
const std::string kTest = R"(
2491+
OpCapability Float16
2492+
; CHECK-NOT: OpCapability Float16
2493+
OpCapability Shader
2494+
OpMemoryModel Logical GLSL450
2495+
OpEntryPoint GLCompute %1 "main"
2496+
%void = OpTypeVoid
2497+
%3 = OpTypeFunction %void
2498+
%1 = OpFunction %void None %3
2499+
%6 = OpLabel
2500+
OpReturn
2501+
OpFunctionEnd;
2502+
)";
2503+
const auto result =
2504+
SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2505+
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2506+
}
2507+
2508+
TEST_F(TrimCapabilitiesPassTest, Float16_RemainsWhenUsed) {
2509+
const std::string kTest = R"(
2510+
OpCapability Float16
2511+
; CHECK: OpCapability Float16
2512+
OpCapability Shader
2513+
OpMemoryModel Logical GLSL450
2514+
OpEntryPoint GLCompute %1 "main"
2515+
%void = OpTypeVoid
2516+
%float = OpTypeFloat 16
2517+
%3 = OpTypeFunction %void
2518+
%1 = OpFunction %void None %3
2519+
%6 = OpLabel
2520+
OpReturn
2521+
OpFunctionEnd;
2522+
)";
2523+
const auto result =
2524+
SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2525+
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2526+
}
2527+
2528+
TEST_F(TrimCapabilitiesPassTest, Int16_RemovedWhenUnused) {
2529+
const std::string kTest = R"(
2530+
OpCapability Int16
2531+
; CHECK-NOT: OpCapability Int16
2532+
OpCapability Shader
2533+
OpMemoryModel Logical GLSL450
2534+
OpEntryPoint GLCompute %1 "main"
2535+
%void = OpTypeVoid
2536+
%3 = OpTypeFunction %void
2537+
%1 = OpFunction %void None %3
2538+
%6 = OpLabel
2539+
OpReturn
2540+
OpFunctionEnd;
2541+
)";
2542+
const auto result =
2543+
SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2544+
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithChange);
2545+
}
2546+
2547+
TEST_F(TrimCapabilitiesPassTest, Int16_RemainsWhenUsed) {
2548+
const std::string kTest = R"(
2549+
OpCapability Int16
2550+
; CHECK: OpCapability Int16
2551+
OpCapability Shader
2552+
OpMemoryModel Logical GLSL450
2553+
OpEntryPoint GLCompute %1 "main"
2554+
%void = OpTypeVoid
2555+
%int = OpTypeInt 16 1
2556+
%3 = OpTypeFunction %void
2557+
%1 = OpFunction %void None %3
2558+
%6 = OpLabel
2559+
OpReturn
2560+
OpFunctionEnd;
2561+
)";
2562+
const auto result =
2563+
SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2564+
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2565+
}
2566+
2567+
TEST_F(TrimCapabilitiesPassTest, UInt16_RemainsWhenUsed) {
2568+
const std::string kTest = R"(
2569+
OpCapability Int16
2570+
; CHECK: OpCapability Int16
2571+
OpCapability Shader
2572+
OpMemoryModel Logical GLSL450
2573+
OpEntryPoint GLCompute %1 "main"
2574+
%void = OpTypeVoid
2575+
%uint = OpTypeInt 16 0
2576+
%3 = OpTypeFunction %void
2577+
%1 = OpFunction %void None %3
2578+
%6 = OpLabel
2579+
OpReturn
2580+
OpFunctionEnd;
2581+
)";
2582+
const auto result =
2583+
SinglePassRunAndMatch<TrimCapabilitiesPass>(kTest, /* skip_nop= */ false);
2584+
EXPECT_EQ(std::get<1>(result), Pass::Status::SuccessWithoutChange);
2585+
}
2586+
24892587
} // namespace
24902588
} // namespace opt
24912589
} // namespace spvtools

0 commit comments

Comments
 (0)