Skip to content

Commit 9580e31

Browse files
authored
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
1 parent 187ec44 commit 9580e31

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

source/slang/slang-emit-hlsl.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -1252,23 +1252,30 @@ void HLSLSourceEmitter::emitSimpleValueImpl(IRInst* inst)
12521252
{
12531253
case IRConstant::FloatKind::Nan:
12541254
{
1255-
m_writer->emit("(0.0 / 0.0)");
1255+
m_writer->emit("(0.0f / 0.0f)");
12561256
return;
12571257
}
12581258
case IRConstant::FloatKind::PositiveInfinity:
12591259
{
1260-
m_writer->emit("(1.0 / 0.0)");
1260+
m_writer->emit("(1.0f / 0.0f)");
12611261
return;
12621262
}
12631263
case IRConstant::FloatKind::NegativeInfinity:
12641264
{
1265-
m_writer->emit("(-1.0 / 0.0)");
1265+
m_writer->emit("(-1.0f / 0.0f)");
12661266
return;
12671267
}
12681268
default:
1269-
break;
1269+
{
1270+
m_writer->emit(constantInst->value.floatVal);
1271+
// Add 'f' suffix for 32-bit float literals to ensure DXC treats them as float
1272+
if (constantInst->getDataType()->getOp() == kIROp_FloatType)
1273+
{
1274+
m_writer->emit("f");
1275+
}
1276+
return;
1277+
}
12701278
}
1271-
break;
12721279
}
12731280

12741281
default:

tests/expected-failure-github.txt

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
tests/language-feature/spirv-asm/imageoperands-warning.slang (vk)
21
tests/language-feature/saturated-cooperation/simple.slang (vk)
32
tests/language-feature/saturated-cooperation/fuse3.slang (vk)
43
tests/language-feature/saturated-cooperation/fuse-product.slang (vk)
54
tests/language-feature/saturated-cooperation/fuse.slang (vk)
6-
tests/bugs/byte-address-buffer-interlocked-add-f32.slang (vk)
75
tests/render/render0.hlsl (mtl)
86
tests/render/multiple-stage-io-locations.slang (mtl)
97
tests/render/nointerpolation.hlsl (mtl)

tests/hlsl/float-literal-suffix.slang

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//TEST:SIMPLE(filecheck=HLSL):-target hlsl -profile ps_6_6 -entry fragmentMain
2+
//TEST:SIMPLE(filecheck=DXIL):-target dxil -profile ps_6_6 -entry fragmentMain
3+
4+
float4 fragmentMain(float2 uv : TEXCOORD) : SV_Target
5+
{
6+
//HLSL:, ddx({{.*}}1.5f)
7+
//DXIL: = call float @dx.op.unary.f32(i32 {{.*}} ; DerivCoarseX(value)
8+
float val = 1.5;
9+
return float4(1.0, ddx(val), 0.0, 1.0);
10+
}

0 commit comments

Comments
 (0)