spirv-writer: refactor getting GLSLstd450 import
Change the GenerateGLSLStd450 method to GetGLSLStd450 and have it
return the import id. That's the only interesting use.
Change-Id: I4fc70e702af160bc797680e5e75e9742e9e24f16
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/52421
Commit-Queue: David Neto <dneto@google.com>
Auto-Submit: David Neto <dneto@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc
index 6f96b21..dd8de52 100644
--- a/src/writer/spirv/builder.cc
+++ b/src/writer/spirv/builder.cc
@@ -1198,18 +1198,22 @@
return result_id;
}
-void Builder::GenerateGLSLstd450Import() {
- if (import_name_to_id_.find(kGLSLstd450) != import_name_to_id_.end()) {
- return;
+uint32_t Builder::GetGLSLstd450Import() {
+ auto where = import_name_to_id_.find(kGLSLstd450);
+ if (where != import_name_to_id_.end()) {
+ return where->second;
}
+ // It doesn't exist yet. Generate it.
auto result = result_op();
auto id = result.to_i();
push_ext_import(spv::Op::OpExtInstImport,
{result, Operand::String(kGLSLstd450)});
+ // Remember it for later.
import_name_to_id_[kGLSLstd450] = id;
+ return id;
}
uint32_t Builder::GenerateConstructorExpression(
@@ -2155,14 +2159,7 @@
op = spv::Op::OpSelect;
break;
default: {
- GenerateGLSLstd450Import();
-
- auto set_iter = import_name_to_id_.find(kGLSLstd450);
- if (set_iter == import_name_to_id_.end()) {
- error_ = std::string("unknown import ") + kGLSLstd450;
- return 0;
- }
- auto set_id = set_iter->second;
+ auto set_id = GetGLSLstd450Import();
auto inst_id = intrinsic_to_glsl_method(intrinsic);
if (inst_id == 0) {
error_ = "unknown method " + std::string(intrinsic->str());
diff --git a/src/writer/spirv/builder.h b/src/writer/spirv/builder.h
index 3daaa18..3dac6d2 100644
--- a/src/writer/spirv/builder.h
+++ b/src/writer/spirv/builder.h
@@ -306,8 +306,10 @@
/// @param stmt the statement to generate
/// @returns true on success
bool GenerateIfStatement(ast::IfStatement* stmt);
- /// Generates an import instruction
- void GenerateGLSLstd450Import();
+ /// Generates an import instruction for the "GLSL.std.450" extended instruction
+ /// set, if one doesn't exist yet, and returns the import ID.
+ /// @returns the import ID, or 0 on error.
+ uint32_t GetGLSLstd450Import();
/// Generates a constructor expression
/// @param var the variable generated for, nullptr if no variable associated.
/// @param expr the expression to generate