Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wgsl: signedness mismatch fixes #5692

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions source/slang/slang-ir-wgsl-legalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,30 @@ struct LegalizeWGSLEntryPointContext
}
}

void legalizeFunc(IRFunc* func)
{
// Insert casts to convert integer return types
auto funcReturnType = func->getResultType();
if (isIntegralType(funcReturnType))
{
for (auto block : func->getBlocks())
{
if (auto returnInst = as<IRReturn>(block->getTerminator()))
{
auto returnedValue = returnInst->getOperand(0);
auto returnedValueType = returnedValue->getDataType();
if (isIntegralType(returnedValueType))
{
IRBuilder builder(returnInst);
builder.setInsertBefore(returnInst);
auto newOp = builder.emitCast(funcReturnType, returnedValue);
builder.replaceOperand(returnInst->getOperands(), newOp);
}
}
}
}
}

void legalizeSwitch(IRSwitch* switchInst)
{
// WGSL Requires all switch statements to contain a default case.
Expand Down Expand Up @@ -1491,6 +1515,28 @@ struct LegalizeWGSLEntryPointContext
inst->getOperand(0));
builder.replaceOperand(inst->getOperands(), newLhs);
}
else if (
isIntegralType(inst->getOperand(0)->getDataType()) &&
isIntegralType(inst->getOperand(1)->getDataType()))
{
// If integer operands differ in signedness, convert the signed one to unsigned.
// We're assuming that the cases where this is bad have already been caught by
// common validation checks.
IntInfo opIntInfo[2] = {
getIntTypeInfo(inst->getOperand(0)->getDataType()),
getIntTypeInfo(inst->getOperand(1)->getDataType())};
if (opIntInfo[0].isSigned != opIntInfo[1].isSigned)
{
int signedOpIndex = (int)opIntInfo[1].isSigned;
opIntInfo[signedOpIndex].isSigned = false;
IRBuilder builder(inst);
builder.setInsertBefore(inst);
auto newOp = builder.emitCast(
builder.getType(getIntTypeOpFromInfo(opIntInfo[signedOpIndex])),
inst->getOperand(signedOpIndex));
builder.replaceOperand(inst->getOperands() + signedOpIndex, newOp);
}
}
}

void processInst(IRInst* inst)
Expand Down Expand Up @@ -1529,6 +1575,9 @@ struct LegalizeWGSLEntryPointContext
legalizeBinaryOp(inst);
break;

case kIROp_Func:
legalizeFunc(static_cast<IRFunc*>(inst));
[[fallthrough]];
default:
for (auto child : inst->getModifiableChildren())
{
Expand Down
1 change: 0 additions & 1 deletion tests/expected-failure-github.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ tests/bugs/buffer-swizzle-store.slang.3 syn (wgpu)
tests/compute/interface-shader-param-in-struct.slang.4 syn (wgpu)
tests/compute/interface-shader-param.slang.5 syn (wgpu)
tests/language-feature/constants/static-const-in-generic-interface.slang.1 syn (wgpu)
tests/language-feature/enums/strongly-typed-id.slang.1 syn (wgpu)
tests/language-feature/shader-params/interface-shader-param-ordinary.slang.4 syn (wgpu)
2 changes: 0 additions & 2 deletions tests/language-feature/enums/strongly-typed-id.slang
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=CHECK): -shaderobj
// WGSL: No matching overload for operator... #5606
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-wgpu

enum MyId : uint {}
extension MyId { uint get() { return (uint)this; } }
Expand Down
Loading