[llvm] 5dd86aa - [WebAssembly] Add TargetInstrInfo::getCalleeOperand

Heejin Ahn via llvm-commits llvm-commits at lists.llvm.org
Wed May 26 11:44:23 PDT 2021


Author: Heejin Ahn
Date: 2021-05-26T11:43:59-07:00
New Revision: 5dd86aadf0b014913bd35bb7435808eb081bc049

URL: https://github.com/llvm/llvm-project/commit/5dd86aadf0b014913bd35bb7435808eb081bc049
DIFF: https://github.com/llvm/llvm-project/commit/5dd86aadf0b014913bd35bb7435808eb081bc049.diff

LOG: [WebAssembly] Add TargetInstrInfo::getCalleeOperand

DwarfDebug unconditionally assumes for all call instructions the 0th
operand is the callee operand, which seems to be true for other targets,
but not for WebAssembly. This adds `TargetInstrInfo::getCallOperand`
method whose default implementation returns `getOperand(0)` and makes
WebAssembly overrides it to use its own utility method to get the callee
operand.

This also fixes an existing bug in `WebAssembly::getCalleeOp`, which was
uncovered by this CL.

Reviewed By: dschuff, djtodoro

Differential Revision: https://reviews.llvm.org/D102978

Added: 
    llvm/test/DebugInfo/WebAssembly/call-site.ll

Modified: 
    llvm/include/llvm/CodeGen/TargetInstrInfo.h
    llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
    llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp
    llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp
    llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/TargetInstrInfo.h b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
index bc4fb7d5234ab..21758e0cfc2ac 100644
--- a/llvm/include/llvm/CodeGen/TargetInstrInfo.h
+++ b/llvm/include/llvm/CodeGen/TargetInstrInfo.h
@@ -1975,6 +1975,11 @@ class TargetInstrInfo : public MCInstrInfo {
     return OptLevel >= CodeGenOpt::Aggressive ? 4 : 2;
   }
 
+  /// Returns the callee operand from the given \p MI.
+  virtual const MachineOperand &getCalleeOperand(const MachineInstr &MI) const {
+    return MI.getOperand(0);
+  }
+
 private:
   mutable std::unique_ptr<MIRFormatter> Formatter;
   unsigned CallFrameSetupOpcode, CallFrameDestroyOpcode;

diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index e6f646560f03c..85f82bfbdf08c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -938,8 +938,10 @@ void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
       // If this is a direct call, find the callee's subprogram.
       // In the case of an indirect call find the register that holds
       // the callee.
-      const MachineOperand &CalleeOp = MI.getOperand(0);
-      if (!CalleeOp.isGlobal() && !CalleeOp.isReg())
+      const MachineOperand &CalleeOp = TII->getCalleeOperand(MI);
+      if (!CalleeOp.isGlobal() &&
+          (!CalleeOp.isReg() ||
+           !Register::isPhysicalRegister(CalleeOp.getReg())))
         continue;
 
       unsigned CallReg = 0;

diff  --git a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp
index b9344bba8dc90..824d336651360 100644
--- a/llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp
+++ b/llvm/lib/Target/WebAssembly/Utils/WebAssemblyUtilities.cpp
@@ -91,7 +91,7 @@ const MachineOperand &WebAssembly::getCalleeOp(const MachineInstr &MI) {
   case WebAssembly::CALL_INDIRECT_S:
   case WebAssembly::RET_CALL_INDIRECT:
   case WebAssembly::RET_CALL_INDIRECT_S:
-    return MI.getOperand(MI.getNumOperands() - 1);
+    return MI.getOperand(MI.getNumExplicitOperands() - 1);
   default:
     llvm_unreachable("Not a call instruction");
   }

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp
index 5dbe7a7908242..5484c0db7775d 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp
@@ -14,6 +14,7 @@
 
 #include "WebAssemblyInstrInfo.h"
 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
+#include "Utils/WebAssemblyUtilities.h"
 #include "WebAssembly.h"
 #include "WebAssemblyMachineFunctionInfo.h"
 #include "WebAssemblySubtarget.h"
@@ -214,3 +215,8 @@ WebAssemblyInstrInfo::getSerializableTargetIndices() const {
       {WebAssembly::TI_LOCAL_INDIRECT, "wasm-local-indirect"}};
   return makeArrayRef(TargetIndices);
 }
+
+const MachineOperand &
+WebAssemblyInstrInfo::getCalleeOperand(const MachineInstr &MI) const {
+  return WebAssembly::getCalleeOp(MI);
+}

diff  --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h
index 5762fd964c2a0..f45a3792467a5 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.h
@@ -68,6 +68,8 @@ class WebAssemblyInstrInfo final : public WebAssemblyGenInstrInfo {
 
   ArrayRef<std::pair<int, const char *>>
   getSerializableTargetIndices() const override;
+
+  const MachineOperand &getCalleeOperand(const MachineInstr &MI) const override;
 };
 
 } // end namespace llvm

diff  --git a/llvm/test/DebugInfo/WebAssembly/call-site.ll b/llvm/test/DebugInfo/WebAssembly/call-site.ll
new file mode 100644
index 0000000000000..8cd9ba493f503
--- /dev/null
+++ b/llvm/test/DebugInfo/WebAssembly/call-site.ll
@@ -0,0 +1,47 @@
+; RUN: llc -filetype=obj %s -o - | llvm-dwarfdump - | FileCheck %s
+
+;; This checks if the call site information is correctly written in debug info.
+;; This is a regression test for the bug that DwarfDebug unconditionally assumed
+;; the callee operand was getOperand(0), which was not true for WebAssembly.
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+; CHECK: 0x00000026:   DW_TAG_subprogram
+; CHECK:                 DW_AT_name  ("call_direct")
+; CHECK: 0x0000003d:     DW_TAG_GNU_call_site
+; CHECK-NEXT:              DW_AT_abstract_origin (0x00000047 "foo")
+
+define i32 @call_direct() !dbg !6 {
+entry:
+  %0 = call i32 @foo(), !dbg !8
+  ret i32 %0, !dbg !9
+}
+
+;; WebAssembly does not currently support DW_TAG_GNU_call_site for stackified
+;; registers. This just checks if the test runs without crashing.
+define i32 @call_indirect(i32 (i32, i32)* %callee) !dbg !11 {
+  %1 = call i32 %callee(i32 3, i32 5), !dbg !12
+  ret i32 %1, !dbg !13
+}
+
+declare !dbg !10 i32 @foo()
+declare void @llvm.dbg.value(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3, !4, !5}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 11.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.c", directory: "/home/llvm-project")
+!2 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!3 = !{i32 7, !"Dwarf Version", i32 4}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = !{i32 1, !"wchar_size", i32 4}
+!6 = distinct !DISubprogram(name: "call_direct", scope: !1, file: !1, line: 3, type: !7, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!7 = !DISubroutineType(types: !{null})
+!8 = !DILocation(line: 4, column: 11, scope: !6)
+!9 = !DILocation(line: 7, column: 1, scope: !6)
+!10 = !DISubprogram(name: "foo", scope: !1, file: !1, line: 30, type: !7, scopeLine: 3, spFlags: DISPFlagOptimized)
+!11 = distinct !DISubprogram(name: "call_indirect", scope: !1, file: !1, line: 3, type: !7, scopeLine: 3, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!12 = !DILocation(line: 40, column: 11, scope: !11)
+!13 = !DILocation(line: 70, column: 1, scope: !11)


        


More information about the llvm-commits mailing list