diff options
| author | Mukund Keshava <mkeshava@nvidia.com> | 2025-02-20 14:12:15 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-20 08:42:15 +0000 |
| commit | 9580e311e0cefb0f8e11afc316783a67201654eb (patch) | |
| tree | 8df2cbc763bb4da95eb1c6a33448884c5ed35eaf | |
| parent | 187ec444486ecf24d1baf897d179de4ee1b6b864 (diff) | |
HLSL: Add 'f' suffix to float literals in code generation (#6381)
* HLSL: Add 'f' suffix to float literals in code generation
Fixes #6078
1) Previously, Slang would emit HLSL float literals without a suffix (e.g.,"1.5"),
which caused DXC to interpret them as 64-bit doubles by default unless
the -HV 202x flag was used. This could cause validation errors when these literals
were used with intrinsics that only accept 32-bit floats (like ddx, ddy).
This change modifies the HLSL emitter to explicitly add 'f' suffix to
32-bit float literals, ensuring they are correctly typed regardless of DXC's version or flags.
For example:
- "1.5" becomes "1.5f"
- "(0.0 / 0.0)" becomes "(0.0f / 0.0f)" for NaN
- "float4(1.0, 2.0, 3.0, 4.0)" becomes "float4(1.0f, 2.0f, 3.0f, 4.0f)"
2) Added a test case to verify the behavior with various float literal scenarios
including basic literals, intrinsic calls, and vector construction.
3) Remove some tests that were marked as known failures
* Add dxil test as suggested by jkwak-work
| -rw-r--r-- | source/slang/slang-emit-hlsl.cpp | 17 | ||||
| -rw-r--r-- | tests/expected-failure-github.txt | 2 | ||||
| -rw-r--r-- | tests/hlsl/float-literal-suffix.slang | 10 |
3 files changed, 22 insertions, 7 deletions
diff --git a/source/slang/slang-emit-hlsl.cpp b/source/slang/slang-emit-hlsl.cpp index 1cb9b769f..59d40d3a1 100644 --- a/source/slang/slang-emit-hlsl.cpp +++ b/source/slang/slang-emit-hlsl.cpp @@ -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; + } } - break; } default: diff --git a/tests/expected-failure-github.txt b/tests/expected-failure-github.txt index 60d632785..8a399b2e0 100644 --- a/tests/expected-failure-github.txt +++ b/tests/expected-failure-github.txt @@ -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) diff --git a/tests/hlsl/float-literal-suffix.slang b/tests/hlsl/float-literal-suffix.slang new file mode 100644 index 000000000..52ae4823f --- /dev/null +++ b/tests/hlsl/float-literal-suffix.slang @@ -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); +}
\ No newline at end of file |
