summaryrefslogtreecommitdiff
path: root/source/slang/slang-ir.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-03-18 09:35:23 -0700
committerGitHub <noreply@github.com>2022-03-18 09:35:23 -0700
commit2e1a84add57efd9f8a50a88d0569a48ae4b6d834 (patch)
tree3e1ce103fa75a62eb3f1efd3c832e468bc9e8aed /source/slang/slang-ir.cpp
parent42ca6758046e11451b0788092f9c95fc7f788da6 (diff)
Fix type truncation during SCCP. (#2163)
Diffstat (limited to 'source/slang/slang-ir.cpp')
-rw-r--r--source/slang/slang-ir.cpp42
1 files changed, 40 insertions, 2 deletions
diff --git a/source/slang/slang-ir.cpp b/source/slang/slang-ir.cpp
index 721488f82..bb1b0a9d2 100644
--- a/source/slang/slang-ir.cpp
+++ b/source/slang/slang-ir.cpp
@@ -2081,7 +2081,32 @@ namespace Slang
memset(&keyInst, 0, sizeof(keyInst));
keyInst.m_op = kIROp_IntLit;
keyInst.typeUse.usedValue = type;
- keyInst.value.intVal = inValue;
+ // Truncate the input value based on `type`.
+ switch (type->getOp())
+ {
+ case kIROp_Int8Type:
+ keyInst.value.intVal = static_cast<int8_t>(inValue);
+ break;
+ case kIROp_Int16Type:
+ keyInst.value.intVal = static_cast<int16_t>(inValue);
+ break;
+ case kIROp_IntType:
+ keyInst.value.intVal = static_cast<int32_t>(inValue);
+ break;
+ case kIROp_UInt8Type:
+ keyInst.value.intVal = static_cast<uint8_t>(inValue);
+ break;
+ case kIROp_UInt16Type:
+ keyInst.value.intVal = static_cast<uint16_t>(inValue);
+ break;
+ case kIROp_BoolType:
+ case kIROp_UIntType:
+ keyInst.value.intVal = static_cast<uint32_t>(inValue);
+ break;
+ default:
+ keyInst.value.intVal = inValue;
+ break;
+ }
return _findOrEmitConstant(keyInst);
}
@@ -2091,7 +2116,20 @@ namespace Slang
memset(&keyInst, 0, sizeof(keyInst));
keyInst.m_op = kIROp_FloatLit;
keyInst.typeUse.usedValue = type;
- keyInst.value.floatVal = inValue;
+ // Truncate the input value based on `type`.
+ switch (type->getOp())
+ {
+ case kIROp_FloatType:
+ keyInst.value.floatVal = static_cast<float>(inValue);
+ break;
+ case kIROp_HalfType:
+ keyInst.value.floatVal = HalfToFloat(FloatToHalf((float)inValue));
+ break;
+ default:
+ keyInst.value.floatVal = inValue;
+ break;
+ }
+
return _findOrEmitConstant(keyInst);
}