Simplify map of SPIR-V numeric constants.
Previously, we used a union of (int64, float) to store the cached value.
However, the union-based code turned out to be more complex than just
type-punning the float's bits to an int via memcpy (which we needed to
do anyway). The union-based approach was also likely to be UB by the
letter of the law--we were creating float keys by storing into fFloat,
then checking the map by comparing equality on fInt.
Change-Id: I62c7ff4b5fab8bb1be8836c23f746ef254053b6f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/350957
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLSPIRVCodeGenerator.cpp b/src/sksl/SkSLSPIRVCodeGenerator.cpp
index c33fdc2..e7e8f14 100644
--- a/src/sksl/SkSLSPIRVCodeGenerator.cpp
+++ b/src/sksl/SkSLSPIRVCodeGenerator.cpp
@@ -2610,7 +2610,7 @@
}
SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) {
- ConstantValuePair key(i.value(), i.type().numberKind());
+ SPIRVNumberConstant key{i.value(), i.type().numberKind()};
auto [iter, newlyCreated] = fNumberConstants.insert({key, (SpvId)-1});
if (newlyCreated) {
SpvId result = this->nextId();
@@ -2622,15 +2622,17 @@
}
SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
- ConstantValuePair key(f.value(), f.type().numberKind());
+ // Convert the float literal into its bit-representation.
+ float value = f.value();
+ uint32_t valueBits;
+ static_assert(sizeof(valueBits) == sizeof(value));
+ memcpy(&valueBits, &value, sizeof(value));
+
+ SPIRVNumberConstant key{valueBits, f.type().numberKind()};
auto [iter, newlyCreated] = fNumberConstants.insert({key, (SpvId)-1});
if (newlyCreated) {
SpvId result = this->nextId();
- float value = f.value();
- uint32_t valueBits;
- static_assert(sizeof(valueBits) == sizeof(value));
- memcpy(&valueBits, &value, sizeof(valueBits));
- this->writeInstruction(SpvOpConstant, this->getType(f.type()), result, valueBits,
+ this->writeInstruction(SpvOpConstant, this->getType(f.type()), result, (SpvId) valueBits,
fConstantBuffer);
iter->second = result;
}