opt: change Get* functions to return const& (#5331)

GetCapabilities returned a const*, and GetExtensions did not exist.
This commit adds GetExtensions, and changes the return value to
be a const&.

This commit also removes the overload to GetCapabilities which returns
a mutable set, as it is unused.

Signed-off-by: Nathan Gauër <brioche@google.com>
This commit is contained in:
Nathan Gauër 2023-07-20 16:18:19 +02:00 committed by GitHub
parent 876ccc6cd5
commit bf03d40922
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View File

@ -33,7 +33,11 @@ class FeatureManager {
return capabilities_.contains(cap);
}
const CapabilitySet* GetCapabilities() const { return &capabilities_; }
// Returns the capabilities the module declares.
inline const CapabilitySet& GetCapabilities() const { return capabilities_; }
// Returns the extensions the module imports.
inline const ExtensionSet& GetExtensions() const { return extensions_; }
uint32_t GetExtInstImportId_GLSLstd450() const {
return extinst_importid_GLSLstd450_;
@ -77,8 +81,6 @@ class FeatureManager {
// Removes the given |capability| from the current FeatureManager.
void RemoveCapability(spv::Capability capability);
CapabilitySet* GetCapabilities() { return &capabilities_; }
// Analyzes |module| and records imported external instruction sets.
void AddExtInstImportIds(Module* module);

View File

@ -770,7 +770,7 @@ void IRContext::AddCombinatorsForExtension(Instruction* extension) {
}
void IRContext::InitializeCombinators() {
for (auto capability : *get_feature_mgr()->GetCapabilities()) {
for (auto capability : get_feature_mgr()->GetCapabilities()) {
AddCombinatorsForCapability(uint32_t(capability));
}

View File

@ -87,6 +87,25 @@ OpExtension "SPV_KHR_storage_buffer_storage_class"
Extension::kSPV_KHR_storage_buffer_storage_class));
}
TEST_F(FeatureManagerTest, GetExtensionsReturnsExtensions) {
const std::string text = R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpExtension "SPV_KHR_variable_pointers"
OpExtension "SPV_KHR_storage_buffer_storage_class"
)";
std::unique_ptr<IRContext> context =
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
ASSERT_NE(context, nullptr);
const auto& extensions = context->get_feature_mgr()->GetExtensions();
EXPECT_EQ(extensions.size(), 2);
EXPECT_TRUE(extensions.contains(Extension::kSPV_KHR_variable_pointers));
EXPECT_TRUE(
extensions.contains(Extension::kSPV_KHR_storage_buffer_storage_class));
}
// Test capability checks.
TEST_F(FeatureManagerTest, ExplicitlyPresent1) {
const std::string text = R"(
@ -142,6 +161,24 @@ OpMemoryModel Logical GLSL450
context->get_feature_mgr()->HasCapability(spv::Capability::Kernel));
}
TEST_F(FeatureManagerTest, GetCapabilitiesReturnsImplicitCapabilities) {
const std::string text = R"(
OpCapability Tessellation
OpMemoryModel Logical GLSL450
)";
std::unique_ptr<IRContext> context =
BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
ASSERT_NE(context, nullptr);
const auto& capabilities = context->get_feature_mgr()->GetCapabilities();
// Tesselation implies Shader, which implies Matrix.
EXPECT_EQ(capabilities.size(), 3);
EXPECT_TRUE(capabilities.contains(spv::Capability::Tessellation));
EXPECT_TRUE(capabilities.contains(spv::Capability::Shader));
EXPECT_TRUE(capabilities.contains(spv::Capability::Matrix));
}
} // namespace
} // namespace opt
} // namespace spvtools