File 2161-Annotate-BIF-calls-with-the-name-of-the-BIF.patch of Package erlang

From bd776308c1f491364257095d4f36f56dc8f9ec53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= <bjorn@erlang.org>
Date: Mon, 4 Jul 2022 06:22:59 +0200
Subject: [PATCH 1/7] Annotate BIF calls with the name of the BIF

---
 erts/emulator/beam/jit/arm/beam_asm.hpp  |  2 ++
 erts/emulator/beam/jit/arm/instr_bif.cpp | 22 ++++++++++++++++++++++
 erts/emulator/beam/jit/x86/instr_bif.cpp | 18 ++++++++++++++++--
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/erts/emulator/beam/jit/arm/beam_asm.hpp b/erts/emulator/beam/jit/arm/beam_asm.hpp
index e2946fdbc7..15964e8190 100644
--- a/erts/emulator/beam/jit/arm/beam_asm.hpp
+++ b/erts/emulator/beam/jit/arm/beam_asm.hpp
@@ -1353,6 +1353,8 @@ protected:
 
     void emit_validate_unicode(Label next, Label fail, arm::Gp value);
 
+    void ubif_comment(const ArgWord &Bif);
+
     void emit_bif_is_eq_ne_exact_immed(const ArgSource &LHS,
                                        const ArgSource &RHS,
                                        const ArgRegister &Dst,
diff --git a/erts/emulator/beam/jit/arm/instr_bif.cpp b/erts/emulator/beam/jit/arm/instr_bif.cpp
index 186343374a..66efc4beba 100644
--- a/erts/emulator/beam/jit/arm/instr_bif.cpp
+++ b/erts/emulator/beam/jit/arm/instr_bif.cpp
@@ -30,6 +30,15 @@ extern "C"
 #include "erl_msacc.h"
 }
 
+void BeamModuleAssembler::ubif_comment(const ArgWord &Bif) {
+    if (logger.file()) {
+        ErtsCodeMFA *mfa = ubif2mfa((void *)Bif.get());
+        if (mfa) {
+            comment("UBIF: %T/%d", mfa->function, mfa->arity);
+        }
+    }
+}
+
 /* ARG2 = argument vector, ARG4 (!) = bif function pointer
  *
  * Result is returned in ARG1 (will be THE_NON_VALUE if the BIF call failed). */
@@ -104,6 +113,7 @@ void BeamModuleAssembler::emit_i_bif1(const ArgSource &Src1,
 
     a.str(src1.reg, getXRef(0));
 
+    ubif_comment(Bif);
     emit_i_bif(Fail, Bif, Dst);
 }
 
@@ -116,6 +126,7 @@ void BeamModuleAssembler::emit_i_bif2(const ArgSource &Src1,
 
     a.stp(src1.reg, src2.reg, getXRef(0));
 
+    ubif_comment(Bif);
     emit_i_bif(Fail, Bif, Dst);
 }
 
@@ -131,6 +142,7 @@ void BeamModuleAssembler::emit_i_bif3(const ArgSource &Src1,
     a.stp(src1.reg, src2.reg, getXRef(0));
     a.str(src3.reg, getXRef(2));
 
+    ubif_comment(Bif);
     emit_i_bif(Fail, Bif, Dst);
 }
 
@@ -161,6 +173,7 @@ void BeamModuleAssembler::emit_nofail_bif1(const ArgSource &Src1,
 
     a.str(src1.reg, getXRef(0));
 
+    ubif_comment(Bif);
     mov_arg(ARG4, Bif);
     fragment_call(ga->get_i_bif_guard_shared());
     mov_arg(Dst, ARG1);
@@ -174,6 +187,7 @@ void BeamModuleAssembler::emit_nofail_bif2(const ArgSource &Src1,
 
     a.stp(src1.reg, src2.reg, getXRef(0));
 
+    ubif_comment(Bif);
     mov_arg(ARG4, Bif);
     fragment_call(ga->get_i_bif_guard_shared());
     mov_arg(Dst, ARG1);
@@ -564,6 +578,10 @@ void BeamModuleAssembler::emit_call_light_bif(const ArgWord &Bif,
     mov_arg(ARG8, Bif);
     a.adr(ARG3, entry);
 
+    if (logger.file()) {
+        BeamFile_ImportEntry *e = &beam->imports.entries[Exp.get()];
+        comment("BIF: %T:%T/%d", e->module, e->function, e->arity);
+    }
     fragment_call(ga->get_call_light_bif_shared());
 }
 
@@ -776,6 +794,10 @@ void BeamModuleAssembler::emit_call_bif_mfa(const ArgAtom &M,
 
     a.adr(ARG3, current_label);
     a.sub(ARG2, ARG3, imm(sizeof(ErtsCodeMFA)));
+    comment("HBIF: %T:%T/%d",
+            e->info.mfa.module,
+            e->info.mfa.function,
+            A.get());
     a.mov(ARG4, imm(func));
 
     a.b(resolve_fragment(ga->get_call_bif_shared(), disp128MB));
diff --git a/erts/emulator/beam/jit/x86/instr_bif.cpp b/erts/emulator/beam/jit/x86/instr_bif.cpp
index 470c9a5a02..33e80c6b90 100644
--- a/erts/emulator/beam/jit/x86/instr_bif.cpp
+++ b/erts/emulator/beam/jit/x86/instr_bif.cpp
@@ -99,7 +99,7 @@ void BeamGlobalAssembler::emit_i_bif_body_shared() {
 }
 
 void BeamModuleAssembler::emit_setup_guard_bif(const std::vector<ArgVal> &args,
-                                               const ArgWord &bif) {
+                                               const ArgWord &Bif) {
     bool is_contiguous_mem = false;
 
     ASSERT(args.size() > 0 && args.size() <= 3);
@@ -125,7 +125,13 @@ void BeamModuleAssembler::emit_setup_guard_bif(const std::vector<ArgVal> &args,
         }
     }
 
-    mov_arg(ARG4, bif);
+    if (logger.file()) {
+        ErtsCodeMFA *mfa = ubif2mfa((void *)Bif.get());
+        if (mfa) {
+            comment("UBIF: %T/%d", mfa->function, mfa->arity);
+        }
+    }
+    mov_arg(ARG4, Bif);
 }
 
 void BeamModuleAssembler::emit_i_bif1(const ArgSource &Src1,
@@ -626,6 +632,10 @@ void BeamModuleAssembler::emit_call_light_bif(const ArgWord &Bif,
     a.mov(RET, imm(Bif.get()));
     a.lea(ARG3, x86::qword_ptr(entry));
 
+    if (logger.file()) {
+        BeamFile_ImportEntry *e = &beam->imports.entries[Exp.get()];
+        comment("BIF: %T:%T/%d", e->module, e->function, e->arity);
+    }
     fragment_call(ga->get_call_light_bif_shared());
 }
 
@@ -842,6 +852,10 @@ void BeamModuleAssembler::emit_call_bif_mfa(const ArgAtom &M,
     e = erts_active_export_entry(M.get(), F.get(), A.get());
     ASSERT(e != NULL && e->bif_number != -1);
 
+    comment("HBIF: %T:%T/%d",
+            e->info.mfa.module,
+            e->info.mfa.function,
+            A.get());
     func = (BeamInstr)bif_table[e->bif_number].f;
     emit_call_bif(ArgWord(func));
 }
-- 
2.35.3

openSUSE Build Service is sponsored by