[lldb] [llvm] [lldb][RISCV] function calls support in lldb expressions (PR #99336)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 25 15:48:46 PDT 2024


================
@@ -0,0 +1,194 @@
+//===--- DirectToIndirectFCR.cpp - RISC-V specific pass -------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Value.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+
+#include "Plugins/Architecture/RISCV/DirectToIndirectFCR.h"
+
+#include "lldb/Core/Architecture.h"
+#include "lldb/Core/Module.h"
+#include "lldb/Symbol/Symtab.h"
+#include "lldb/Target/ExecutionContext.h"
+#include "lldb/Target/Process.h"
+#include "lldb/Target/Target.h"
+#include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/LLDBLog.h"
+#include "lldb/Utility/Log.h"
+
+#include <optional>
+
+using namespace llvm;
+using namespace lldb_private;
+
+namespace {
+std::string GetValueTypeStr(const llvm::Type *value_ty) {
+  assert(value_ty);
+  std::string str_type;
+  llvm::raw_string_ostream rso(str_type);
+  value_ty->print(rso);
+  return rso.str();
+}
+
+template <typename... Args> void LogMessage(const char *msg, Args &&...args) {
+  Log *log = GetLog(LLDBLog::Expressions);
+  LLDB_LOG(log, msg, std::forward<Args>(args)...);
+}
+} // namespace
+
+bool DirectToIndirectFCR::canBeReplaced(const llvm::CallInst *ci) {
+  assert(ci);
+  auto *return_value_ty = ci->getType();
+  if (!(return_value_ty->isIntegerTy() || return_value_ty->isVoidTy())) {
----------------
dlav-sc wrote:

>I would have expected that, at the IR level, you could perform this kind of transformation for any return type.

As you've expected I really can apply this pass on functions with any arguments' and return value's types. I just filter unsupported for some reasons function calls.

As for doubles, I think that the problem is in the ABI or MCJIT, because the IR is correct, while the resulting assembly from JIT compiler doesn't contain even float instructions, needed to pass function arguments and get a return value. My RISCV machine supports a float extension, so the assembly should contain float instructions. Currently the result of such expressions is a trash, like:
```
(double) $1 = 3.3033229080945744E-319
```
so I've decided to remove function calls that takes double arguments or have double return value.

As for pointers, as such no problems with them.
But if in function that takes char* argument pass a row c string, there will be a SIGSEGV. Most interesting that if pass variable the function successfully finished:
```
#include <stdio.h>                                                                                                                                                                                                                                                             

void foo(const char *msg) {                                                                                                                                                                                                                                                    
 printf("%s\n", msg);                                                                                                                                                                                                                                                         
}                                                                                                                                                                                                                                                                              

int main() {                                                                                                                                                                                                                                                                   
 const char *msg = "msg";                                                                                                                                                                                                                                                     
 foo(msg);                                                                                                                                                                                                                                                                    
} 
```
```
Process 302 stopped
* thread #1, name = 'pointer.x', stop reason = step over
    frame #0: 0x00000000000105cc pointer.x`main at pointer.cpp:9:6
   6   
   7    int main() {
   8      const char *msg = "msg";
-> 9      foo(msg);
   10   }
(lldb) expr foo("msg")
error: Execution was interrupted, reason: signal SIGSEGV: address not mapped to object (fault address: 0xfffffffff7b6d760).
The process has been returned to the state before expression evaluation.
(lldb) expr foo(msg)
msg
(lldb)
```
I think that the problem is in relocation.

https://github.com/llvm/llvm-project/pull/99336


More information about the llvm-commits mailing list