Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLSL: Add 'f' suffix to float literals in code generation #6381

Merged
merged 3 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions source/slang/slang-emit-hlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,23 +1252,30 @@ void HLSLSourceEmitter::emitSimpleValueImpl(IRInst* inst)
{
case IRConstant::FloatKind::Nan:
{
m_writer->emit("(0.0 / 0.0)");
m_writer->emit("(0.0f / 0.0f)");
return;
}
case IRConstant::FloatKind::PositiveInfinity:
{
m_writer->emit("(1.0 / 0.0)");
m_writer->emit("(1.0f / 0.0f)");
return;
}
case IRConstant::FloatKind::NegativeInfinity:
{
m_writer->emit("(-1.0 / 0.0)");
m_writer->emit("(-1.0f / 0.0f)");
return;
}
default:
break;
{
m_writer->emit(constantInst->value.floatVal);
// Add 'f' suffix for 32-bit float literals to ensure DXC treats them as float
if (constantInst->getDataType()->getOp() == kIROp_FloatType)
{
m_writer->emit("f");
}
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before your change, it was "break" instead of "return".
Your PR is making a change to the behavior that is not described on the PR description.
Can you explain why this change is needed?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need return here to avoid the fallback call to Super::emitSimpleValueImpl since we are overriding the behavior for ordinary float literals here.

}
}
break;
}

default:
Expand Down
2 changes: 0 additions & 2 deletions tests/expected-failure-github.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
tests/language-feature/spirv-asm/imageoperands-warning.slang (vk)
tests/language-feature/saturated-cooperation/simple.slang (vk)
tests/language-feature/saturated-cooperation/fuse3.slang (vk)
tests/language-feature/saturated-cooperation/fuse-product.slang (vk)
tests/language-feature/saturated-cooperation/fuse.slang (vk)
tests/bugs/byte-address-buffer-interlocked-add-f32.slang (vk)
tests/render/render0.hlsl (mtl)
tests/render/multiple-stage-io-locations.slang (mtl)
tests/render/nointerpolation.hlsl (mtl)
Expand Down
10 changes: 10 additions & 0 deletions tests/hlsl/float-literal-suffix.slang
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//TEST:SIMPLE(filecheck=HLSL):-target hlsl -profile ps_6_6 -entry fragmentMain
//TEST:SIMPLE(filecheck=DXIL):-target dxil -profile ps_6_6 -entry fragmentMain

float4 fragmentMain(float2 uv : TEXCOORD) : SV_Target
{
//HLSL:, ddx({{.*}}1.5f)
//DXIL: = call float @dx.op.unary.f32(i32 {{.*}} ; DerivCoarseX(value)
float val = 1.5;
return float4(1.0, ddx(val), 0.0, 1.0);
}
Loading