Skip to content

Commit 6e52cc8

Browse files
aleino-nvslangbot
andauthored
wgsl: signedness mismatch fixes (shader-slang#5692)
* Enable tests/language-feature/enums/strongly-typed-id.slang * Fix operator signedness mismatch issue This helps to address issue shader-slang#5606. * wgsl: Insert casts for integer type return values This closes shader-slang#5606. * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
1 parent 947b99e commit 6e52cc8

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

source/slang/slang-ir-wgsl-legalize.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,30 @@ struct LegalizeWGSLEntryPointContext
14291429
}
14301430
}
14311431

1432+
void legalizeFunc(IRFunc* func)
1433+
{
1434+
// Insert casts to convert integer return types
1435+
auto funcReturnType = func->getResultType();
1436+
if (isIntegralType(funcReturnType))
1437+
{
1438+
for (auto block : func->getBlocks())
1439+
{
1440+
if (auto returnInst = as<IRReturn>(block->getTerminator()))
1441+
{
1442+
auto returnedValue = returnInst->getOperand(0);
1443+
auto returnedValueType = returnedValue->getDataType();
1444+
if (isIntegralType(returnedValueType))
1445+
{
1446+
IRBuilder builder(returnInst);
1447+
builder.setInsertBefore(returnInst);
1448+
auto newOp = builder.emitCast(funcReturnType, returnedValue);
1449+
builder.replaceOperand(returnInst->getOperands(), newOp);
1450+
}
1451+
}
1452+
}
1453+
}
1454+
}
1455+
14321456
void legalizeSwitch(IRSwitch* switchInst)
14331457
{
14341458
// WGSL Requires all switch statements to contain a default case.
@@ -1491,6 +1515,28 @@ struct LegalizeWGSLEntryPointContext
14911515
inst->getOperand(0));
14921516
builder.replaceOperand(inst->getOperands(), newLhs);
14931517
}
1518+
else if (
1519+
isIntegralType(inst->getOperand(0)->getDataType()) &&
1520+
isIntegralType(inst->getOperand(1)->getDataType()))
1521+
{
1522+
// If integer operands differ in signedness, convert the signed one to unsigned.
1523+
// We're assuming that the cases where this is bad have already been caught by
1524+
// common validation checks.
1525+
IntInfo opIntInfo[2] = {
1526+
getIntTypeInfo(inst->getOperand(0)->getDataType()),
1527+
getIntTypeInfo(inst->getOperand(1)->getDataType())};
1528+
if (opIntInfo[0].isSigned != opIntInfo[1].isSigned)
1529+
{
1530+
int signedOpIndex = (int)opIntInfo[1].isSigned;
1531+
opIntInfo[signedOpIndex].isSigned = false;
1532+
IRBuilder builder(inst);
1533+
builder.setInsertBefore(inst);
1534+
auto newOp = builder.emitCast(
1535+
builder.getType(getIntTypeOpFromInfo(opIntInfo[signedOpIndex])),
1536+
inst->getOperand(signedOpIndex));
1537+
builder.replaceOperand(inst->getOperands() + signedOpIndex, newOp);
1538+
}
1539+
}
14941540
}
14951541

14961542
void processInst(IRInst* inst)
@@ -1529,6 +1575,9 @@ struct LegalizeWGSLEntryPointContext
15291575
legalizeBinaryOp(inst);
15301576
break;
15311577

1578+
case kIROp_Func:
1579+
legalizeFunc(static_cast<IRFunc*>(inst));
1580+
[[fallthrough]];
15321581
default:
15331582
for (auto child : inst->getModifiableChildren())
15341583
{

tests/expected-failure-github.txt

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ tests/bugs/buffer-swizzle-store.slang.3 syn (wgpu)
1313
tests/compute/interface-shader-param-in-struct.slang.4 syn (wgpu)
1414
tests/compute/interface-shader-param.slang.5 syn (wgpu)
1515
tests/language-feature/constants/static-const-in-generic-interface.slang.1 syn (wgpu)
16-
tests/language-feature/enums/strongly-typed-id.slang.1 syn (wgpu)
1716
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)

tests/language-feature/enums/strongly-typed-id.slang

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
2-
// WGSL: No matching overload for operator... #5606
3-
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu
42

53
enum MyId : uint {}
64
extension MyId { uint get() { return (uint)this; } }

0 commit comments

Comments
 (0)