summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnders Leino <aleino@nvidia.com>2024-11-28 11:27:22 +0200
committerGitHub <noreply@github.com>2024-11-28 09:27:22 +0000
commit6e52cc811835b82bf9140189ef1f3a8561baf327 (patch)
tree7a46d77887a8b2a7f4b7c4a77e903ad49d9ba035
parent947b99e8ebaa81e9c8ee9b0f3e247d8d329041ad (diff)
wgsl: signedness mismatch fixes (#5692)
* Enable tests/language-feature/enums/strongly-typed-id.slang * Fix operator signedness mismatch issue This helps to address issue #5606. * wgsl: Insert casts for integer type return values This closes #5606. * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
-rw-r--r--source/slang/slang-ir-wgsl-legalize.cpp49
-rw-r--r--tests/expected-failure-github.txt1
-rw-r--r--tests/language-feature/enums/strongly-typed-id.slang2
3 files changed, 49 insertions, 3 deletions
diff --git a/source/slang/slang-ir-wgsl-legalize.cpp b/source/slang/slang-ir-wgsl-legalize.cpp
index facafb284..e6e375592 100644
--- a/source/slang/slang-ir-wgsl-legalize.cpp
+++ b/source/slang/slang-ir-wgsl-legalize.cpp
@@ -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.
@@ -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)
@@ -1529,6 +1575,9 @@ struct LegalizeWGSLEntryPointContext
legalizeBinaryOp(inst);
break;
+ case kIROp_Func:
+ legalizeFunc(static_cast<IRFunc*>(inst));
+ [[fallthrough]];
default:
for (auto child : inst->getModifiableChildren())
{
diff --git a/tests/expected-failure-github.txt b/tests/expected-failure-github.txt
index ba08b2b77..81a61133b 100644
--- a/tests/expected-failure-github.txt
+++ b/tests/expected-failure-github.txt
@@ -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)
diff --git a/tests/language-feature/enums/strongly-typed-id.slang b/tests/language-feature/enums/strongly-typed-id.slang
index 8625d1a4b..70f655538 100644
--- a/tests/language-feature/enums/strongly-typed-id.slang
+++ b/tests/language-feature/enums/strongly-typed-id.slang
@@ -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; } }