[spirv-reader][ir] Pre-register all functions.
Loop over all functions and register the function signature before
processing the function bodies. This fixes an issue where the function
can be called before being seen. If the function is unseen, we'll
generate the incorrect signature.
Bug: 42250952
Change-Id: I20c998cea8359ca18a52a349d8a2c7fec09bb7dd
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/248394
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
diff --git a/src/tint/lang/spirv/reader/parser/parser.cc b/src/tint/lang/spirv/reader/parser/parser.cc
index 994df0d..a0b4f78 100644
--- a/src/tint/lang/spirv/reader/parser/parser.cc
+++ b/src/tint/lang/spirv/reader/parser/parser.cc
@@ -1194,6 +1194,9 @@
/// Emit the functions.
void EmitFunctions() {
+ // Add all the functions in a first pass and then fill in the function bodies. This means
+ // the function will exist fixing an issues where calling a function that hasn't been seen
+ // generates the wrong signature.
for (auto& func : *spirv_context_->module()) {
current_spirv_function_ = &func;
@@ -1220,6 +1223,13 @@
}
functions_.Add(func.result_id(), current_function_);
+ current_spirv_function_ = nullptr;
+ }
+
+ for (auto& func : *spirv_context_->module()) {
+ current_spirv_function_ = &func;
+
+ current_function_ = Function(func.result_id());
EmitBlockParent(current_function_->Block(), *func.entry());
// No terminator was emitted, that means then end of block is
@@ -1227,8 +1237,8 @@
if (!current_function_->Block()->Terminator()) {
current_function_->Block()->Append(b_.Unreachable());
}
+ current_spirv_function_ = nullptr;
}
- current_spirv_function_ = nullptr;
}
/// Emit entry point attributes.
diff --git a/test/tint/unittest/reader/SpvParserTest_EmitFunctions_CalleePrecedesCaller.spvasm b/test/tint/unittest/reader/SpvParserTest_EmitFunctions_CalleePrecedesCaller.spvasm
index 664bd69..d18163c 100644
--- a/test/tint/unittest/reader/SpvParserTest_EmitFunctions_CalleePrecedesCaller.spvasm
+++ b/test/tint/unittest/reader/SpvParserTest_EmitFunctions_CalleePrecedesCaller.spvasm
@@ -36,6 +36,7 @@
OpFunctionEnd
%1 = OpFunction %void None %8
%17 = OpLabel
+%cr = OpFunctionCall %void %root
OpReturn
OpFunctionEnd