Skip to content

Commit 084fce1

Browse files
committed
spirv-opt: Fix assertion with fp8
This patch fixes an assertion crash in Debug build. However, hex_float.h contains some errors when converting float32 to smaller floating point types, which causes incorrect results. Please see KhronosGroup/glslang#4241 for details. fixes: #6533
1 parent 4c2ec2a commit 084fce1

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

source/opt/folding_rules.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ std::vector<uint32_t> GetWordsFromScalarFloatConstant(
153153
const analysis::FloatConstant* c) {
154154
assert(c != nullptr);
155155
uint32_t width = c->type()->AsFloat()->width();
156-
assert(width == 16 || width == 32 || width == 64);
156+
assert(width == 8 || width == 16 || width == 32 || width == 64);
157157
if (width == 64) {
158158
utils::FloatProxy<double> result(c->GetDouble());
159159
return result.GetWords();

test/opt/fold_test.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ OpCapability Int16
226226
OpCapability Int64
227227
OpCapability CooperativeMatrixKHR
228228
OpExtension "SPV_KHR_cooperative_matrix"
229+
OpExtension "SPV_EXT_float8"
229230
%1 = OpExtInstImport "GLSL.std.450"
230231
OpMemoryModel Logical GLSL450
231232
OpEntryPoint Fragment %main "main"
@@ -238,6 +239,8 @@ OpName %main "main"
238239
%float = OpTypeFloat 32
239240
%double = OpTypeFloat 64
240241
%half = OpTypeFloat 16
242+
%float8e4m3 = OpTypeFloat 8 Float8E4M3EXT
243+
%float8e5m2 = OpTypeFloat 8 Float8E5M2EXT
241244
%101 = OpConstantTrue %bool ; Need a def with an numerical id to define id maps.
242245
%true = OpConstantTrue %bool
243246
%false = OpConstantFalse %bool
@@ -444,6 +447,10 @@ OpName %main "main"
444447
%half_null = OpConstantNull %half
445448
%half_0_1 = OpConstantComposite %v2half %108 %half_1
446449
%v4half_0_1_0_0 = OpConstantComposite %v4half %108 %half_1 %108 %108
450+
%float8e4m3_1 = OpConstant %float8e4m3 1.0
451+
%float8e4m3_0x1_4p_0 = OpConstant %float8e4m3 0x1.4p+0
452+
%float8e5m2_1 = OpConstant %float8e5m2 1.0
453+
%float8e5m2_0x1_4p_0 = OpConstant %float8e5m2 0x1.4p+0
447454
%106 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
448455
%v4float_0_0_0_0 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
449456
%v4float_0_0_0_1 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_1
@@ -1346,7 +1353,39 @@ INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTest,
13461353
"%2 = OpBitReverse %uint %111\n" +
13471354
"OpReturn\n" +
13481355
"OpFunctionEnd",
1349-
2, 0)
1356+
2, 0),
1357+
// Test case 98: Bit-cast float8e4m3 1 to float8e4m3
1358+
InstructionFoldingCase<uint32_t>(
1359+
Header() + "%main = OpFunction %void None %void_func\n" +
1360+
"%main_lab = OpLabel\n" +
1361+
"%2 = OpBitcast %float8e4m3 %float8e4m3_1\n" +
1362+
"OpReturn\n" +
1363+
"OpFunctionEnd",
1364+
2, 0x38),
1365+
// Test case 99: Bit-cast float8e4m3 1 to ubyte
1366+
InstructionFoldingCase<uint32_t>(
1367+
Header() + "%main = OpFunction %void None %void_func\n" +
1368+
"%main_lab = OpLabel\n" +
1369+
"%2 = OpBitcast %ubyte %float8e4m3_0x1_4p_0\n" +
1370+
"OpReturn\n" +
1371+
"OpFunctionEnd",
1372+
2, 0x3a), // FIXME: float8e4m3 1.4 is between 1.375 (0x3b) ~ 1.5 (0x3c)
1373+
// Test case 100: Bit-cast float8e5m2 1 to float8e5m2
1374+
InstructionFoldingCase<uint32_t>(
1375+
Header() + "%main = OpFunction %void None %void_func\n" +
1376+
"%main_lab = OpLabel\n" +
1377+
"%2 = OpBitcast %float8e5m2 %float8e5m2_1\n" +
1378+
"OpReturn\n" +
1379+
"OpFunctionEnd",
1380+
2, 0x3c),
1381+
// Test case 101: Bit-cast float8e5m2 1 to ubyte
1382+
InstructionFoldingCase<uint32_t>(
1383+
Header() + "%main = OpFunction %void None %void_func\n" +
1384+
"%main_lab = OpLabel\n" +
1385+
"%2 = OpBitcast %ubyte %float8e5m2_0x1_4p_0\n" +
1386+
"OpReturn\n" +
1387+
"OpFunctionEnd",
1388+
2, 0x3d) // FIXME: float8e5m2 1.4 is between 1.25 (0x3d) ~ 1.5 (0x3e)
13501389
));
13511390
// clang-format on
13521391

0 commit comments

Comments
 (0)