Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/KhronosGroup/SPIRV-Tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorgmitrano-unity <89797527+gmitrano-unity@users.noreply.github.com>2022-10-06 17:35:18 +0300
committerGitHub <noreply@github.com>2022-10-06 17:35:18 +0300
commit1cecf91701def0981f987c101e282bec31f7d542 (patch)
tree1e32105802b693cfbf21d19458e0963718afcdb2 /source
parenta6e6454ef27f9c3c652d6a37281c3b9ed45d4667 (diff)
Support Narrow Types in BitCast Folding Rule (#4941)
* Support Narrow Types in BitCast Folding Rule This change adds support for narrow types in the BitCastScalarOrVector folding rule. According to Section 2.2.1 of the SPIR-V spec, types that are narrower than 32 bits are automatically either sign extended, or zero extended depending on the type. With that guaranteed, we should be able to use the first 32-bit word of any narrow type for the folding logic without performing any special conversions. In order to reduce code duplication, this change moves the GetU32BitValue and GetU64BitValue functions from IntConstant to ScalarConstant. Without this move, we would have needed an identical version of GetU32BitValue on FloatConstant. * Add Tests for 16-bit BitCast Folding This change adds several new test cases to the IntegerInstructionFoldingTest which trigger the 16-bit BitCast logic. The logic for half types was also added to the integer case since we can't easily validate half float types in C++ code. It's easier to validate them as unsigned integers instead. Pllus this also allows us to verify the SPIR-V constant sign extension logic too. * Add 8-Bit Folding Test Cases This change adds a couple more test cases to the integer instruction folding test suite in order to ensure that the BitCast logic also works correctly with the Int8 shader capability.
Diffstat (limited to 'source')
-rw-r--r--source/opt/constants.h30
-rw-r--r--source/opt/folding_rules.cpp13
2 files changed, 23 insertions, 20 deletions
diff --git a/source/opt/constants.h b/source/opt/constants.h
index c039ae087..588ca3e76 100644
--- a/source/opt/constants.h
+++ b/source/opt/constants.h
@@ -163,6 +163,21 @@ class ScalarConstant : public Constant {
return is_zero;
}
+ uint32_t GetU32BitValue() const {
+ // Relies on unsigned values smaller than 32-bit being zero extended. See
+ // section 2.2.1 of the SPIR-V spec.
+ assert(words().size() == 1);
+ return words()[0];
+ }
+
+ uint64_t GetU64BitValue() const {
+ // Relies on unsigned values smaller than 64-bit being zero extended. See
+ // section 2.2.1 of the SPIR-V spec.
+ assert(words().size() == 2);
+ return static_cast<uint64_t>(words()[1]) << 32 |
+ static_cast<uint64_t>(words()[0]);
+ }
+
protected:
ScalarConstant(const Type* ty, const std::vector<uint32_t>& w)
: Constant(ty), words_(w) {}
@@ -189,13 +204,6 @@ class IntConstant : public ScalarConstant {
return words()[0];
}
- uint32_t GetU32BitValue() const {
- // Relies on unsigned values smaller than 32-bit being zero extended. See
- // section 2.2.1 of the SPIR-V spec.
- assert(words().size() == 1);
- return words()[0];
- }
-
int64_t GetS64BitValue() const {
// Relies on unsigned values smaller than 64-bit being sign extended. See
// section 2.2.1 of the SPIR-V spec.
@@ -204,14 +212,6 @@ class IntConstant : public ScalarConstant {
static_cast<uint64_t>(words()[0]);
}
- uint64_t GetU64BitValue() const {
- // Relies on unsigned values smaller than 64-bit being zero extended. See
- // section 2.2.1 of the SPIR-V spec.
- assert(words().size() == 2);
- return static_cast<uint64_t>(words()[1]) << 32 |
- static_cast<uint64_t>(words()[0]);
- }
-
// Make a copy of this IntConstant instance.
std::unique_ptr<IntConstant> CopyIntConstant() const {
return MakeUnique<IntConstant>(type_->AsInteger(), words_);
diff --git a/source/opt/folding_rules.cpp b/source/opt/folding_rules.cpp
index 3f10bd009..3d803addc 100644
--- a/source/opt/folding_rules.cpp
+++ b/source/opt/folding_rules.cpp
@@ -136,25 +136,28 @@ std::vector<uint32_t> GetWordsFromScalarIntConstant(
const analysis::IntConstant* c) {
assert(c != nullptr);
uint32_t width = c->type()->AsInteger()->width();
- assert(width == 32 || width == 64);
+ assert(width == 8 || width == 16 || width == 32 || width == 64);
if (width == 64) {
uint64_t uval = static_cast<uint64_t>(c->GetU64());
return ExtractInts(uval);
}
- return {c->GetU32()};
+ // Section 2.2.1 of the SPIR-V spec guarantees that all integer types
+ // smaller than 32-bits are automatically zero or sign extended to 32-bits.
+ return {c->GetU32BitValue()};
}
std::vector<uint32_t> GetWordsFromScalarFloatConstant(
const analysis::FloatConstant* c) {
assert(c != nullptr);
uint32_t width = c->type()->AsFloat()->width();
- assert(width == 32 || width == 64);
+ assert(width == 16 || width == 32 || width == 64);
if (width == 64) {
utils::FloatProxy<double> result(c->GetDouble());
return result.GetWords();
}
- utils::FloatProxy<float> result(c->GetFloat());
- return result.GetWords();
+ // Section 2.2.1 of the SPIR-V spec guarantees that all floating-point types
+ // smaller than 32-bits are automatically zero extended to 32-bits.
+ return {c->GetU32BitValue()};
}
std::vector<uint32_t> GetWordsFromNumericScalarOrVectorConstant(