Skip to content

Commit 0336a3a

Browse files
kaizhangNVcsyonghe
andauthored
Fix issue of infinity float literal (shader-slang#5489)
* Fix issue of infinity float literal * add parameters for the test * Correct the way to construct inf and nan In WGSL, expression of "1.0/0.0" is not allowed, it will report compile error, so to construct infinity or nan, we have to assign the float literal to a variable and then use it to bypass the compile error. By doing so, we add getInfinity and getNan functions to the builtin prelude to wgsl. --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent fc7de92 commit 0336a3a

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

source/slang/slang-emit-wgsl.cpp

+59-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@
2626
namespace Slang
2727
{
2828

29+
// In WGSL, expression of "1.0/0.0" is not allowed, it will report compile error,
30+
// so to construct infinity or nan, we have to assign the float literal to a variable
31+
// and then use it to bypass the compile error.
32+
static const char* kWGSLBuiltinPreludeGetInfinity = R"(
33+
fn _slang_getInfinity(positive: bool) -> f32
34+
{
35+
let a = select(f32(-1.0), f32(1.0), positive);
36+
let b = f32(0.0);
37+
return a / b;
38+
}
39+
)";
40+
41+
static const char* kWGSLBuiltinPreludeGetNan = R"(
42+
fn _slang_getNan() -> f32
43+
{
44+
let a = f32(0.0);
45+
let b = f32(0.0);
46+
return a / b;
47+
}
48+
)";
49+
50+
void WGSLSourceEmitter::ensurePrelude(const char* preludeText)
51+
{
52+
IRStringLit* stringLit;
53+
if (!m_builtinPreludes.tryGetValue(preludeText, stringLit))
54+
{
55+
IRBuilder builder(m_irModule);
56+
stringLit = builder.getStringValue(UnownedStringSlice(preludeText));
57+
m_builtinPreludes[preludeText] = stringLit;
58+
}
59+
m_requiredPreludes.add(stringLit);
60+
}
61+
2962
void WGSLSourceEmitter::emitSwitchCaseSelectorsImpl(
3063
const SwitchRegion::Case* const currentCase,
3164
const bool isDefault)
@@ -878,8 +911,32 @@ void WGSLSourceEmitter::emitSimpleValueImpl(IRInst* inst)
878911

879912
case BaseType::Float:
880913
{
881-
m_writer->emit(litInst->value.floatVal);
882-
m_writer->emit("f");
914+
IRConstant::FloatKind kind = litInst->getFloatKind();
915+
switch (kind)
916+
{
917+
case IRConstant::FloatKind::Nan:
918+
{
919+
ensurePrelude(kWGSLBuiltinPreludeGetNan);
920+
m_writer->emit("_slang_getNan()");
921+
break;
922+
}
923+
case IRConstant::FloatKind::PositiveInfinity:
924+
{
925+
ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
926+
m_writer->emit("_slang_getInfinity(true)");
927+
break;
928+
}
929+
case IRConstant::FloatKind::NegativeInfinity:
930+
{
931+
ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
932+
m_writer->emit("_slang_getInfinity(false)");
933+
break;
934+
}
935+
default:
936+
m_writer->emit(litInst->value.floatVal);
937+
m_writer->emit("f");
938+
break;
939+
}
883940
}
884941
break;
885942

source/slang/slang-emit-wgsl.h

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
5151
void emit(const AddressSpace addressSpace);
5252

5353
virtual bool shouldFoldInstIntoUseSites(IRInst* inst) SLANG_OVERRIDE;
54+
Dictionary<const char*, IRStringLit*> m_builtinPreludes;
55+
56+
protected:
57+
void ensurePrelude(const char* preludeText);
5458

5559
private:
5660
// Emit the matrix type with 'rowCountWGSL' WGSL-rows and 'colCountWGSL' WGSL-columns

tests/bugs/inf-float-literal.slang

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj
22
//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj
33
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute -shaderobj
4-
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
4+
//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -slang -compute -shaderobj
55

66

77
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer
@@ -24,4 +24,4 @@ void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
2424
}
2525

2626
outputBuffer[idx] = a;
27-
}
27+
}

tests/expected-failure-github.txt

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ tests/bugs/gh-3980.slang.7 syn (wgpu)
3232
tests/bugs/gh-471.slang.1 syn (wgpu)
3333
tests/bugs/gh-518.slang.2 syn (wgpu)
3434
tests/bugs/gh-566.slang.1 syn (wgpu)
35-
tests/bugs/inf-float-literal.slang.3 syn (wgpu)
3635
tests/bugs/mutating/resource-specialization-inout.slang.1 syn (wgpu)
3736
tests/bugs/nested-switch.slang.3 syn (wgpu)
3837
tests/bugs/obfuscate-specialization-naming.slang.2 syn (wgpu)

0 commit comments

Comments
 (0)