Skip to content

Commit 28adf89

Browse files
authored
reinterpret and 16-bit value packing. (shader-slang#1933)
* `reinterpret` and 16-bit value packing. * Update `half-texture` cross-compile test reference result. * Revert inadvertent reformatting of slang-ir-inst-defs.h Co-authored-by: Yong He <yhe@nvidia.com>
1 parent cc075b7 commit 28adf89

16 files changed

+490
-32
lines changed

build/visual-studio/slang/slang.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-generic-function.h" />
252252
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-generic-type.h" />
253253
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-generics.h" />
254+
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-reinterpret.h" />
254255
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-tuple-types.h" />
255256
<ClInclude Include="..\..\..\source\slang\slang-ir-missing-return.h" />
256257
<ClInclude Include="..\..\..\source\slang\slang-ir-optix-entry-point-uniforms.h" />
@@ -380,6 +381,7 @@
380381
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-generic-function.cpp" />
381382
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-generic-type.cpp" />
382383
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-generics.cpp" />
384+
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-reinterpret.cpp" />
383385
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-tuple-types.cpp" />
384386
<ClCompile Include="..\..\..\source\slang\slang-ir-missing-return.cpp" />
385387
<ClCompile Include="..\..\..\source\slang\slang-ir-optix-entry-point-uniforms.cpp" />

build/visual-studio/slang/slang.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@
204204
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-generics.h">
205205
<Filter>Header Files</Filter>
206206
</ClInclude>
207+
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-reinterpret.h">
208+
<Filter>Header Files</Filter>
209+
</ClInclude>
207210
<ClInclude Include="..\..\..\source\slang\slang-ir-lower-tuple-types.h">
208211
<Filter>Header Files</Filter>
209212
</ClInclude>
@@ -587,6 +590,9 @@
587590
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-generics.cpp">
588591
<Filter>Source Files</Filter>
589592
</ClCompile>
593+
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-reinterpret.cpp">
594+
<Filter>Source Files</Filter>
595+
</ClCompile>
590596
<ClCompile Include="..\..\..\source\slang\slang-ir-lower-tuple-types.cpp">
591597
<Filter>Source Files</Filter>
592598
</ClCompile>

source/slang/core.meta.slang

+6
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,12 @@ __generic<T, U>
19071907
__intrinsic_op($(kIROp_CreateExistentialObject))
19081908
T createDynamicObject(uint typeId, U value);
19091909

1910+
// Reinterpret
1911+
__generic<T, U>
1912+
[__unsafeForceInlineEarly]
1913+
__intrinsic_op($(kIROp_Reinterpret))
1914+
T reinterpret(U value);
1915+
19101916
// Specialized function
19111917

19121918
/// Given a string returns an integer hash of that string.

source/slang/slang-diagnostic-defs.h

+3
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ DIAGNOSTIC(41000, Warning, unreachableCode, "unreachable code detected")
503503
DIAGNOSTIC(41010, Warning, missingReturn, "control flow may reach end of non-'void' function")
504504

505505
DIAGNOSTIC(41011, Error, typeDoesNotFitAnyValueSize, "type '$0' does not fit in the size required by its conforming interface.")
506+
507+
DIAGNOSTIC(41012, Error, typeCannotBePackedIntoAnyValue, "type '$0' contains fields that cannot be packed into an AnyValue.")
508+
506509
//
507510
// 5xxxx - Target code generation.
508511
//

source/slang/slang-emit-hlsl.cpp

+26-5
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,9 @@ bool HLSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
479479
emitType(inst->getDataType());
480480
m_writer->emit(")");
481481
break;
482-
482+
case BaseType::Half:
483+
m_writer->emit("f16tof32");
484+
break;
483485
case BaseType::Float:
484486
// Note: at present HLSL only supports
485487
// reinterpreting integer bits as a `float`.
@@ -511,11 +513,18 @@ bool HLSLSourceEmitter::tryEmitInstExprImpl(IRInst* inst, const EmitOpInfo& inOu
511513
case BaseType::UInt:
512514
case BaseType::Int:
513515
break;
514-
516+
case BaseType::UInt16:
517+
case BaseType::Int16:
518+
break;
515519
case BaseType::Float:
516520
m_writer->emit("asuint(");
517521
closeCount++;
518522
break;
523+
524+
case BaseType::Half:
525+
m_writer->emit("f32tof16(");
526+
closeCount++;
527+
break;
519528
}
520529

521530
emitOperand(inst->getOperand(0), getInfo(EmitOp::General));
@@ -750,20 +759,32 @@ void HLSLSourceEmitter::emitSimpleTypeImpl(IRType* type)
750759
case kIROp_VoidType:
751760
case kIROp_BoolType:
752761
case kIROp_Int8Type:
753-
case kIROp_Int16Type:
754762
case kIROp_IntType:
755763
case kIROp_Int64Type:
756764
case kIROp_UInt8Type:
757-
case kIROp_UInt16Type:
758765
case kIROp_UIntType:
759766
case kIROp_UInt64Type:
760767
case kIROp_FloatType:
761768
case kIROp_DoubleType:
762-
case kIROp_HalfType:
763769
{
764770
m_writer->emit(getDefaultBuiltinTypeName(type->getOp()));
765771
return;
766772
}
773+
case kIROp_Int16Type:
774+
{
775+
m_writer->emit("min16int");
776+
return;
777+
}
778+
case kIROp_UInt16Type:
779+
{
780+
m_writer->emit("min16uint");
781+
return;
782+
}
783+
case kIROp_HalfType:
784+
{
785+
m_writer->emit("min16float");
786+
return;
787+
}
767788
case kIROp_StructType:
768789
m_writer->emit(getName(type));
769790
return;

source/slang/slang-emit.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "slang-ir-lower-generics.h"
2222
#include "slang-ir-lower-tuple-types.h"
2323
#include "slang-ir-lower-bit-cast.h"
24+
#include "slang-ir-lower-reinterpret.h"
2425
#include "slang-ir-optix-entry-point-uniforms.h"
2526
#include "slang-ir-restructure.h"
2627
#include "slang-ir-restructure-scoping.h"
@@ -327,6 +328,8 @@ Result linkAndOptimizeIR(
327328

328329
eliminateDeadCode(irModule);
329330

331+
lowerReinterpret(targetRequest, irModule, sink);
332+
330333
// For targets that supports dynamic dispatch, we need to lower the
331334
// generics / interface types to ordinary functions and types using
332335
// function pointers.

0 commit comments

Comments
 (0)