summaryrefslogtreecommitdiff
path: root/src/ir/debuginfo.h
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2024-06-12 11:05:28 -0700
committerGitHub <noreply@github.com>2024-06-12 11:05:28 -0700
commitac21c8cc9204e09fab070d2fd915e7ab583a5dac (patch)
tree190a13113804b3c6ea01dd71f44694e1b6111be0 /src/ir/debuginfo.h
parent475841dd5f0c50d7072f6dfc26dffd66e02abc10 (diff)
downloadbinaryen-ac21c8cc9204e09fab070d2fd915e7ab583a5dac.tar.gz
binaryen-ac21c8cc9204e09fab070d2fd915e7ab583a5dac.tar.bz2
binaryen-ac21c8cc9204e09fab070d2fd915e7ab583a5dac.zip
[DebugInfo] Copy debug info in call-utils.h (#6652)
We automatically copy debuginfo in replaceCurrent(), but there are a few places that do other operations than simple replacements. call-utils.h will turn a call_ref with a select target into two direct calls, and we were missing the logic to copy debuginfo from the call_ref to the calls. To make this work, refactor out the copying logic from wasm-traversal, into debuginfo.h, and use it in call-utils.h. debuginfo.h itself is renamed from debug.h (as now this needs to be included from wasm-traversal, which nearly everything does, and it turns out some files have internal stuff like a debug() helper that ends up conflicing with the old debug namespace). Also rename the old copyDebugInfo function to copyDebugInfoBetweenFunctions which is more explicit. That is also moved from the header to a cpp file because it depends on wasm-traversal (so we'd end up with recursive headers otherwise). That is fine, as that method is called after copying a function, which is not that frequent. The new copyDebugInfoToReplacement (which was refactored out of wasm-traversal) is in the header because it can be called very frequently (every single instruction we optimize) and we want it to get inlined.
Diffstat (limited to 'src/ir/debuginfo.h')
-rw-r--r--src/ir/debuginfo.h72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/ir/debuginfo.h b/src/ir/debuginfo.h
new file mode 100644
index 000000000..96c4d8c2a
--- /dev/null
+++ b/src/ir/debuginfo.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2019 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef wasm_ir_debuginfo_h
+#define wasm_ir_debuginfo_h
+
+#include "wasm.h"
+
+namespace wasm::debuginfo {
+
+// Given an original expression and another that replaces it, copy the debuginfo
+// from the former to the latter. Note the expression may not be an exclusive
+// replacement of the other (the other may be replaced by several expressions,
+// all of whom may end up with the same debug info).
+inline void copyOriginalToReplacement(Expression* original,
+ Expression* replacement,
+ Function* func) {
+ auto& debugLocations = func->debugLocations;
+ // Early exit if there is no debug info at all. Also, leave if we already
+ // have debug info on the new replacement, which we don't want to trample:
+ // if there is no debug info we do want to copy, as a replacement operation
+ // suggests the new code plays the same role (it is an optimized version of
+ // the old), but if the code is already annotated, trust that.
+ if (debugLocations.empty() || debugLocations.count(replacement)) {
+ return;
+ }
+
+ auto iter = debugLocations.find(original);
+ if (iter != debugLocations.end()) {
+ debugLocations[replacement] = iter->second;
+ // Note that we do *not* erase the debug info of the expression being
+ // replaced, because it may still exist: we might replace
+ //
+ // (call
+ // (block ..
+ //
+ // with
+ //
+ // (block
+ // (call ..
+ //
+ // We still want the call here to have its old debug info.
+ //
+ // (In most cases, of course, we do remove the replaced expression,
+ // which means we accumulate unused garbage in debugLocations, but
+ // that's not that bad; we use arena allocation for Expressions, after
+ // all.)
+ }
+}
+
+// Given an expression and a copy of it in another function, copy the debug
+// info into the second function.
+void copyBetweenFunctions(Expression* origin,
+ Expression* copy,
+ Function* originFunc,
+ Function* copyFunc);
+} // namespace wasm::debuginfo
+
+#endif // wasm_ir_debuginfo_h