Skip to content

Commit f1de181

Browse files
authored
Switch to direct-to-spirv backend as default. (shader-slang#4002)
* Switch to direct-to-spirv backend as default. * Fix slang-test. * Fix. * Fix.
1 parent 0d92068 commit f1de181

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+221
-115
lines changed

.github/workflows/windows-selfhosted.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ jobs:
3737
.\make-slang-tag-version.bat
3838
3939
MSBuild.exe slang.sln -v:m -m -property:Configuration=${{matrix.configuration}} -property:Platform=${{matrix.platform}} -maxcpucount:12
40-
- name: test-spirv-direct
40+
- name: test-glsl
4141
run: |
4242
$ErrorActionPreference = "SilentlyContinue"
4343
where.exe spirv-dis
4444
spirv-dis --version
4545
$env:SLANG_RUN_SPIRV_VALIDATION='1'
46-
.\bin\windows-${{matrix.testPlatform}}\${{matrix.configuration}}\slang-test.exe tests/ -use-test-server -server-count 8 -emit-spirv-directly -expected-failure-list tests/expected-failure.txt -api vk 2>&1
46+
.\bin\windows-${{matrix.testPlatform}}\${{matrix.configuration}}\slang-test.exe tests/ -use-test-server -server-count 8 -emit-spirv-via-glsl -expected-failure-list tests/expected-failure.txt -api vk 2>&1
4747
4848
- name: test
4949
run: |

slang.h

-4
Original file line numberDiff line numberDiff line change
@@ -706,11 +706,7 @@ extern "C"
706706
/* When set, will generate SPIRV directly rather than via glslang. */
707707
SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY = 1 << 10,
708708
};
709-
#if defined(SLANG_CONFIG_DEFAULT_SPIRV_DIRECT)
710709
constexpr static SlangTargetFlags kDefaultTargetFlags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
711-
#else
712-
constexpr static SlangTargetFlags kDefaultTargetFlags = 0;
713-
#endif
714710

715711
/*!
716712
@brief Options to control floating-point precision guarantees for a target.

source/slang/hlsl.meta.slang

+3-3
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ extension __TextureImpl<T,Shape,isArray,0,sampleCount,$(access),isShadow, 0,form
19611961
case spirv:
19621962
return spirv_asm
19631963
{
1964-
OpImageWrite $this $location $newValue;
1964+
OpImageWrite $this $location __convertTexel(newValue);
19651965
};
19661966
}
19671967
}
@@ -2073,7 +2073,7 @@ extension __TextureImpl<T,Shape,isArray,1,sampleCount,$(access),isShadow, 0,form
20732073
case spirv:
20742074
return spirv_asm
20752075
{
2076-
OpImageWrite $this $location $newValue Sample $sampleIndex;
2076+
OpImageWrite $this $location __convertTexel(newValue) Sample $sampleIndex;
20772077
};
20782078
}
20792079
}
@@ -10650,7 +10650,7 @@ ${{{{
1065010650
case hlsl: __intrinsic_asm "($0)[$1] = $2";
1065110651
case glsl: __intrinsic_asm "imageStore($0, int($1), $V2)";
1065210652
case spirv: spirv_asm {
10653-
OpImageWrite $this $index $newValue;
10653+
OpImageWrite $this $index __convertTexel(newValue);
1065410654
};
1065510655
}
1065610656
}

source/slang/slang-ast-expr.h

+1
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ class SPIRVAsmOperand
683683
SampledType, // __sampledType(T), this becomes a 4 vector of the component type of T
684684
ImageType, // __imageType(texture), returns the equivalaent OpTypeImage of a given texture typed value.
685685
SampledImageType, // __sampledImageType(texture), returns the equivalent OpTypeSampledImage of a given texture typed value.
686+
ConvertTexel, // __convertTexel(value), converts `value` to the native texel type of a texture.
686687
TruncateMarker, // __truncate, an invented instruction which coerces to the result type by truncating the element count
687688
EntryPoint, // __entryPoint, a placeholder for the id of a referencing entryPoint.
688689
BuiltinVar,

source/slang/slang-check-expr.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -4415,6 +4415,7 @@ namespace Slang
44154415
|| operand.flavor == SPIRVAsmOperand::SlangValueAddr
44164416
|| operand.flavor == SPIRVAsmOperand::ImageType
44174417
|| operand.flavor == SPIRVAsmOperand::SampledImageType
4418+
|| operand.flavor == SPIRVAsmOperand::ConvertTexel
44184419
|| operand.flavor == SPIRVAsmOperand::RayPayloadFromLocation
44194420
|| operand.flavor == SPIRVAsmOperand::RayAttributeFromLocation
44204421
|| operand.flavor == SPIRVAsmOperand::RayCallableFromLocation)

source/slang/slang-compiler-options.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ namespace Slang
336336

337337
bool shouldEmitSPIRVDirectly()
338338
{
339-
return getBoolOption(CompilerOptionName::EmitSpirvDirectly);
339+
if (getBoolOption(CompilerOptionName::EmitSpirvViaGLSL))
340+
return false;
341+
return true;
340342
}
341343

342344
bool shouldUseScalarLayout()

source/slang/slang-compiler.h

+5
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,11 @@ namespace Slang
24022402

24032403
HLSLToVulkanLayoutOptions* getHLSLToVulkanLayoutOptions() { return m_targetReq->getHLSLToVulkanLayoutOptions(); }
24042404

2405+
bool shouldEmitSPIRVDirectly()
2406+
{
2407+
return isKhronosTarget(m_targetReq) && getOptionSet().shouldEmitSPIRVDirectly();
2408+
}
2409+
24052410
private:
24062411
RefPtr<IRModule> createIRModuleForLayout(DiagnosticSink* sink);
24072412

source/slang/slang-emit-spirv.cpp

+35-29
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ struct SPIRVEmitContext
18671867
setImageFormatCapabilityAndExtension(SpvImageFormatUnknown, SpvCapabilityShader);
18681868
return emitOpTypeImage(
18691869
assignee,
1870-
dropVector((IRType*)sampledType),
1870+
getVectorElementType((IRType*)sampledType),
18711871
dim,
18721872
SpvLiteralInteger::from32(ImageOpConstants::unknownDepthImage),
18731873
SpvLiteralInteger::from32(0),
@@ -2018,12 +2018,12 @@ struct SPIRVEmitContext
20182018
//
20192019
// The op itself
20202020
//
2021-
2021+
auto sampledElementType = getSPIRVSampledElementType(sampledType);
20222022
if (inst->isCombined())
20232023
{
20242024
auto imageType = emitOpTypeImage(
20252025
nullptr,
2026-
dropVector((IRType*)sampledType),
2026+
sampledElementType,
20272027
dim,
20282028
SpvLiteralInteger::from32(depth),
20292029
SpvLiteralInteger::from32(arrayed),
@@ -2038,7 +2038,7 @@ struct SPIRVEmitContext
20382038

20392039
return emitOpTypeImage(
20402040
assignee,
2041-
dropVector((IRType*)sampledType),
2041+
sampledElementType,
20422042
dim,
20432043
SpvLiteralInteger::from32(depth),
20442044
SpvLiteralInteger::from32(arrayed),
@@ -4827,20 +4827,13 @@ struct SPIRVEmitContext
48274827
}
48284828
}
48294829

4830-
IRType* dropVector(IRType* t)
4831-
{
4832-
if(const auto v = as<IRVectorType>(t))
4833-
return v->getElementType();
4834-
return t;
4835-
};
4836-
48374830
SpvInst* emitIntCast(SpvInstParent* parent, IRIntCast* inst)
48384831
{
48394832
const auto fromTypeV = inst->getOperand(0)->getDataType();
48404833
const auto toTypeV = inst->getDataType();
48414834
SLANG_ASSERT(!as<IRVectorType>(fromTypeV) == !as<IRVectorType>(toTypeV));
4842-
const auto fromType = dropVector(fromTypeV);
4843-
const auto toType = dropVector(toTypeV);
4835+
const auto fromType = getVectorElementType(fromTypeV);
4836+
const auto toType = getVectorElementType(toTypeV);
48444837

48454838
if (as<IRBoolType>(fromType))
48464839
{
@@ -4906,8 +4899,8 @@ struct SPIRVEmitContext
49064899
const auto fromTypeV = inst->getOperand(0)->getDataType();
49074900
const auto toTypeV = inst->getDataType();
49084901
SLANG_ASSERT(!as<IRVectorType>(fromTypeV) == !as<IRVectorType>(toTypeV));
4909-
const auto fromType = dropVector(fromTypeV);
4910-
const auto toType = dropVector(toTypeV);
4902+
const auto fromType = getVectorElementType(fromTypeV);
4903+
const auto toType = getVectorElementType(toTypeV);
49114904
SLANG_ASSERT(isFloatingType(fromType));
49124905
SLANG_ASSERT(isFloatingType(toType));
49134906
SLANG_ASSERT(!isTypeEqual(fromType, toType));
@@ -4920,8 +4913,8 @@ struct SPIRVEmitContext
49204913
const auto fromTypeV = inst->getOperand(0)->getDataType();
49214914
const auto toTypeV = inst->getDataType();
49224915
SLANG_ASSERT(!as<IRVectorType>(fromTypeV) == !as<IRVectorType>(toTypeV));
4923-
const auto fromType = dropVector(fromTypeV);
4924-
const auto toType = dropVector(toTypeV);
4916+
const auto fromType = getVectorElementType(fromTypeV);
4917+
const auto toType = getVectorElementType(toTypeV);
49254918
SLANG_ASSERT(isFloatingType(toType));
49264919

49274920
if (isIntegralType(fromType))
@@ -4956,8 +4949,8 @@ struct SPIRVEmitContext
49564949
const auto fromTypeV = inst->getOperand(0)->getDataType();
49574950
const auto toTypeV = inst->getDataType();
49584951
SLANG_ASSERT(!as<IRVectorType>(fromTypeV) == !as<IRVectorType>(toTypeV));
4959-
const auto fromType = dropVector(fromTypeV);
4960-
const auto toType = dropVector(toTypeV);
4952+
const auto fromType = getVectorElementType(fromTypeV);
4953+
const auto toType = getVectorElementType(toTypeV);
49614954
SLANG_ASSERT(isFloatingType(fromType));
49624955

49634956
if (as<IRBoolType>(toType))
@@ -5175,7 +5168,7 @@ struct SPIRVEmitContext
51755168

51765169
SpvInst* emitVectorOrScalarArithmetic(SpvInstParent* parent, IRInst* instToRegister, IRInst* type, IROp op, UInt operandCount, ArrayView<IRInst*> operands)
51775170
{
5178-
IRType* elementType = dropVector(operands[0]->getDataType());
5171+
IRType* elementType = getVectorElementType(operands[0]->getDataType());
51795172
IRBasicType* basicType = as<IRBasicType>(elementType);
51805173
bool isFloatingPoint = false;
51815174
bool isBool = false;
@@ -5813,7 +5806,7 @@ struct SPIRVEmitContext
58135806
// Make a 4 vector of the component type
58145807
IRBuilder builder(m_irModule);
58155808
const auto elementType = cast<IRType>(operand->getValue());
5816-
const auto sampledType = builder.getVectorType(dropVector(elementType), 4);
5809+
const auto sampledType = builder.getVectorType(getSPIRVSampledElementType(getVectorElementType(elementType)), 4);
58175810
emitOperand(ensureInst(sampledType));
58185811
break;
58195812
}
@@ -5867,7 +5860,7 @@ struct SPIRVEmitContext
58675860
// Make a 4 vector of the component type
58685861
IRBuilder builder(m_irModule);
58695862
const auto elementType = cast<IRType>(operand->getValue());
5870-
return builder.getVectorType(dropVector(elementType), 4);
5863+
return builder.getVectorType(getVectorElementType(elementType), 4);
58715864
}
58725865
case kIROp_SPIRVAsmOperandEnum:
58735866
case kIROp_SPIRVAsmOperandLiteral:
@@ -5884,9 +5877,22 @@ struct SPIRVEmitContext
58845877
const auto toIdOperand = spvInst->getSPIRVOperands()[1];
58855878
const auto fromType = getSlangType(spvInst->getSPIRVOperands()[2]);
58865879
const auto fromIdOperand = spvInst->getSPIRVOperands()[3];
5887-
5888-
// The component types must be the same
5889-
SLANG_ASSERT(isTypeEqual(dropVector(toType), dropVector(fromType)));
5880+
auto fromElementType = getSPIRVSampledElementType(fromType);
5881+
SpvInst* fromSpvInst = nullptr;
5882+
// If the component types are not the same, convert them to be so.
5883+
if (!isTypeEqual(getVectorElementType(toType), fromElementType))
5884+
{
5885+
auto newFromType = replaceVectorElementType(fromType, getVectorElementType(toType));
5886+
fromSpvInst = emitInstCustomOperandFunc(
5887+
parent,
5888+
nullptr,
5889+
SpvOpFConvert,
5890+
[&]() {
5891+
emitOperand(newFromType);
5892+
emitOperand(kResultID),
5893+
emitSpvAsmOperand(fromIdOperand);
5894+
});
5895+
}
58905896

58915897
// If we don't need truncation, but a different result ID is
58925898
// expected, then just unify them in the idMap
@@ -5901,7 +5907,7 @@ struct SPIRVEmitContext
59015907
[&](){
59025908
emitOperand(toType);
59035909
emitSpvAsmOperand(toIdOperand);
5904-
emitSpvAsmOperand(fromIdOperand);
5910+
fromSpvInst ? emitOperand(fromSpvInst) : emitSpvAsmOperand(fromIdOperand);
59055911
}
59065912
);
59075913
}
@@ -5915,7 +5921,7 @@ struct SPIRVEmitContext
59155921
[&](){
59165922
emitOperand(toType);
59175923
emitSpvAsmOperand(toIdOperand);
5918-
emitSpvAsmOperand(fromIdOperand);
5924+
fromSpvInst ? emitOperand(fromSpvInst) : emitSpvAsmOperand(fromIdOperand);
59195925
emitOperand(SpvLiteralInteger::from32(0));
59205926
}
59215927
);
@@ -5930,7 +5936,7 @@ struct SPIRVEmitContext
59305936
[&](){
59315937
emitOperand(toType);
59325938
emitSpvAsmOperand(toIdOperand);
5933-
emitSpvAsmOperand(fromIdOperand);
5939+
fromSpvInst ? emitOperand(fromSpvInst) : emitSpvAsmOperand(fromIdOperand);
59345940
}
59355941
);
59365942
}
@@ -5950,7 +5956,7 @@ struct SPIRVEmitContext
59505956
[&](){
59515957
emitOperand(toType);
59525958
emitSpvAsmOperand(toIdOperand);
5953-
emitSpvAsmOperand(fromIdOperand);
5959+
fromSpvInst ? emitOperand(fromSpvInst) : emitSpvAsmOperand(fromIdOperand);
59545960
emitOperand(emitOpUndef(parent, nullptr, fromVector));
59555961
for(Int32 i = 0; i < toVectorSize; ++i)
59565962
emitOperand(SpvLiteralInteger::from32(i));

source/slang/slang-emit.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -967,9 +967,9 @@ Result linkAndOptimizeIR(
967967
// bit_cast on basic types.
968968
lowerBitCast(targetProgram, irModule, sink);
969969

970-
bool emitSpirvDirectly = targetProgram->getOptionSet().shouldEmitSPIRVDirectly();
970+
bool emitSpirvDirectly = targetProgram->shouldEmitSPIRVDirectly();
971971

972-
if (isKhronosTarget(targetRequest) && emitSpirvDirectly)
972+
if (emitSpirvDirectly)
973973
{
974974
performIntrinsicFunctionInlining(irModule);
975975
eliminateDeadCode(irModule);

source/slang/slang-ir-autodiff-fwd.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,7 @@ InstPair ForwardDiffTranscriber::transcribeInstImpl(IRBuilder* builder, IRInst*
19301930
case kIROp_SPIRVAsmOperandBuiltinVar:
19311931
case kIROp_SPIRVAsmOperandGLSL450Set:
19321932
case kIROp_SPIRVAsmOperandDebugPrintfSet:
1933+
case kIROp_SPIRVAsmOperandConvertTexel:
19331934
case kIROp_SPIRVAsmOperandId:
19341935
case kIROp_SPIRVAsmOperandResult:
19351936
case kIROp_SPIRVAsmOperandTruncate:

source/slang/slang-ir-inst-defs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ INST(SPIRVAsmInst, SPIRVAsmInst, 1, 0)
11831183
// This isn't hoistable, as we sometimes need to change the used value and
11841184
// instructions around the specific asm block
11851185
INST(SPIRVAsmOperandInst, SPIRVAsmOperandInst, 1, 0)
1186+
INST(SPIRVAsmOperandConvertTexel, SPIRVAsmOperandConvertTexel, 1, 0)
11861187
//a late resolving type to handle the case of ray objects (resolving late due to constexpr data requirment)
11871188
INST(SPIRVAsmOperandRayPayloadFromLocation, SPIRVAsmOperandRayPayloadFromLocation, 1, 0)
11881189
INST(SPIRVAsmOperandRayAttributeFromLocation, SPIRVAsmOperandRayAttributeFromLocation, 1, 0)
@@ -1196,7 +1197,6 @@ INST(SPIRVAsmInst, SPIRVAsmInst, 1, 0)
11961197
// A reference to the glsl450 instruction set.
11971198
INST(SPIRVAsmOperandGLSL450Set, SPIRVAsmOperandGLSL450Set, 0, HOISTABLE)
11981199
INST(SPIRVAsmOperandDebugPrintfSet, SPIRVAsmOperandDebugPrintfSet, 0, HOISTABLE)
1199-
12001200
// A string which is given a unique ID in the backend, used to refer to
12011201
// results of other instrucions in the same asm block
12021202
INST(SPIRVAsmOperandId, SPIRVAsmOperandId, 1, HOISTABLE)

source/slang/slang-ir-insts.h

+1
Original file line numberDiff line numberDiff line change
@@ -4227,6 +4227,7 @@ struct IRBuilder
42274227
IRSPIRVAsmOperand* emitSPIRVAsmOperandLiteral(IRInst* literal);
42284228
IRSPIRVAsmOperand* emitSPIRVAsmOperandInst(IRInst* inst);
42294229
IRSPIRVAsmOperand* createSPIRVAsmOperandInst(IRInst* inst);
4230+
IRSPIRVAsmOperand* emitSPIRVAsmOperandConvertTexel(IRInst* inst);
42304231
IRSPIRVAsmOperand* emitSPIRVAsmOperandRayPayloadFromLocation(IRInst* inst);
42314232
IRSPIRVAsmOperand* emitSPIRVAsmOperandRayAttributeFromLocation(IRInst* inst);
42324233
IRSPIRVAsmOperand* emitSPIRVAsmOperandRayCallableFromLocation(IRInst* inst);

source/slang/slang-ir-lower-buffer-element-type.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ namespace Slang
231231
{
232232
// For spirv, we always want to lower all matrix types, because matrix types
233233
// are considered abstract types.
234-
if (!target->getOptionSet().shouldEmitSPIRVDirectly())
234+
if (!target->shouldEmitSPIRVDirectly())
235235
{
236236
// For other targets, we only lower the matrix types if they differ from the default
237237
// matrix layout.
@@ -280,7 +280,7 @@ namespace Slang
280280
// For spirv backend, we always want to lower all array types, even if the element type
281281
// comes out the same. This is because different layout rules may have different array
282282
// stride requirements.
283-
if (!target->getOptionSet().shouldEmitSPIRVDirectly())
283+
if (!target->shouldEmitSPIRVDirectly())
284284
{
285285
if (!loweredInnerTypeInfo.convertLoweredToOriginal)
286286
{
@@ -327,7 +327,7 @@ namespace Slang
327327
// For spirv backend, we always want to lower all array types, even if the element type
328328
// comes out the same. This is because different layout rules may have different array
329329
// stride requirements.
330-
if (!target->getOptionSet().shouldEmitSPIRVDirectly())
330+
if (!target->shouldEmitSPIRVDirectly())
331331
{
332332
// For non-spirv target, we skip lowering this type if all field types are unchanged.
333333
if (isTrivial)
@@ -404,7 +404,7 @@ namespace Slang
404404
return info;
405405
}
406406

407-
if (target->getOptionSet().shouldEmitSPIRVDirectly())
407+
if (target->shouldEmitSPIRVDirectly())
408408
{
409409
switch (target->getTargetReq()->getTarget())
410410
{
@@ -838,7 +838,7 @@ namespace Slang
838838
return IRTypeLayoutRules::getNatural();
839839

840840
// If we are just emitting GLSL, we can just use the general layout rule.
841-
if (!target->getOptionSet().shouldEmitSPIRVDirectly())
841+
if (!target->shouldEmitSPIRVDirectly())
842842
return IRTypeLayoutRules::getNatural();
843843

844844
// If the user specified a scalar buffer layout, then just use that.

0 commit comments

Comments
 (0)