Skip to content

Commit

Permalink
Fix issue of infinity float literal (shader-slang#5489)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
kaizhangNV and csyonghe authored Nov 5, 2024
1 parent fc7de92 commit 0336a3a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
61 changes: 59 additions & 2 deletions source/slang/slang-emit-wgsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@
namespace Slang
{

// 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.
static const char* kWGSLBuiltinPreludeGetInfinity = R"(
fn _slang_getInfinity(positive: bool) -> f32
{
let a = select(f32(-1.0), f32(1.0), positive);
let b = f32(0.0);
return a / b;
}
)";

static const char* kWGSLBuiltinPreludeGetNan = R"(
fn _slang_getNan() -> f32
{
let a = f32(0.0);
let b = f32(0.0);
return a / b;
}
)";

void WGSLSourceEmitter::ensurePrelude(const char* preludeText)
{
IRStringLit* stringLit;
if (!m_builtinPreludes.tryGetValue(preludeText, stringLit))
{
IRBuilder builder(m_irModule);
stringLit = builder.getStringValue(UnownedStringSlice(preludeText));
m_builtinPreludes[preludeText] = stringLit;
}
m_requiredPreludes.add(stringLit);
}

void WGSLSourceEmitter::emitSwitchCaseSelectorsImpl(
const SwitchRegion::Case* const currentCase,
const bool isDefault)
Expand Down Expand Up @@ -878,8 +911,32 @@ void WGSLSourceEmitter::emitSimpleValueImpl(IRInst* inst)

case BaseType::Float:
{
m_writer->emit(litInst->value.floatVal);
m_writer->emit("f");
IRConstant::FloatKind kind = litInst->getFloatKind();
switch (kind)
{
case IRConstant::FloatKind::Nan:
{
ensurePrelude(kWGSLBuiltinPreludeGetNan);
m_writer->emit("_slang_getNan()");
break;
}
case IRConstant::FloatKind::PositiveInfinity:
{
ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
m_writer->emit("_slang_getInfinity(true)");
break;
}
case IRConstant::FloatKind::NegativeInfinity:
{
ensurePrelude(kWGSLBuiltinPreludeGetInfinity);
m_writer->emit("_slang_getInfinity(false)");
break;
}
default:
m_writer->emit(litInst->value.floatVal);
m_writer->emit("f");
break;
}
}
break;

Expand Down
4 changes: 4 additions & 0 deletions source/slang/slang-emit-wgsl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class WGSLSourceEmitter : public CLikeSourceEmitter
void emit(const AddressSpace addressSpace);

virtual bool shouldFoldInstIntoUseSites(IRInst* inst) SLANG_OVERRIDE;
Dictionary<const char*, IRStringLit*> m_builtinPreludes;

protected:
void ensurePrelude(const char* preludeText);

private:
// Emit the matrix type with 'rowCountWGSL' WGSL-rows and 'colCountWGSL' WGSL-columns
Expand Down
4 changes: 2 additions & 2 deletions tests/bugs/inf-float-literal.slang
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj
//TEST(compute,vulkan):COMPARE_COMPUTE_EX:-vk -slang -compute -shaderobj
//TEST(compute):COMPARE_COMPUTE_EX:-cpu -slang -compute -shaderobj
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -slang -compute -shaderobj


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

outputBuffer[idx] = a;
}
}
1 change: 0 additions & 1 deletion tests/expected-failure-github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ tests/bugs/gh-3980.slang.7 syn (wgpu)
tests/bugs/gh-471.slang.1 syn (wgpu)
tests/bugs/gh-518.slang.2 syn (wgpu)
tests/bugs/gh-566.slang.1 syn (wgpu)
tests/bugs/inf-float-literal.slang.3 syn (wgpu)
tests/bugs/mutating/resource-specialization-inout.slang.1 syn (wgpu)
tests/bugs/nested-switch.slang.3 syn (wgpu)
tests/bugs/obfuscate-specialization-naming.slang.2 syn (wgpu)
Expand Down

0 comments on commit 0336a3a

Please sign in to comment.