Skip to content

Commit 00bce99

Browse files
committed
[lldb] add architecture depended IR passes
Function calls support in LLDB expressions for RISCV: 5 of 6 Adds architecture depended IR passes handler, that can apply architecture specific IR passes before IRForTarget.
1 parent dabd3e3 commit 00bce99

File tree

6 files changed

+137
-0
lines changed

6 files changed

+137
-0
lines changed

lldb/include/lldb/Core/Architecture.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/Core/PluginInterface.h"
1313
#include "lldb/Target/DynamicRegisterInfo.h"
1414
#include "lldb/Target/MemoryTagManager.h"
15+
#include "llvm/IR/LegacyPassManager.h"
1516

1617
namespace lldb_private {
1718

@@ -129,6 +130,17 @@ class Architecture : public PluginInterface {
129130
RegisterContext &reg_context) const {
130131
return false;
131132
}
133+
134+
// Takes a Pass Manager and adds passes for this Architecture that should be
135+
// run before IRForTarget
136+
virtual std::unique_ptr<llvm::legacy::PassManager>
137+
GetArchitectureCustomPasses(const ExecutionContext &exe_ctx,
138+
const llvm::StringRef expr) const {
139+
return nullptr;
140+
}
141+
142+
static constexpr llvm::StringLiteral s_target_incompatibility_marker =
143+
"target_incompatibility_detected";
132144
};
133145

134146
} // namespace lldb_private

lldb/source/Plugins/Architecture/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ add_subdirectory(Arm)
22
add_subdirectory(Mips)
33
add_subdirectory(PPC64)
44
add_subdirectory(AArch64)
5+
add_subdirectory(RISCV)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===-- ArchitectureRISCV.cpp----------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "Plugins/Architecture/RISCV/ArchitectureRISCV.h"
10+
#include "lldb/Core/PluginManager.h"
11+
#include "lldb/Target/RegisterContext.h"
12+
#include "lldb/Target/Thread.h"
13+
#include "lldb/Utility/ArchSpec.h"
14+
15+
#include "llvm/IR/LegacyPassManager.h"
16+
17+
#include "DirectToIndirectFCR.h"
18+
19+
using namespace lldb_private;
20+
using namespace lldb;
21+
22+
LLDB_PLUGIN_DEFINE(ArchitectureRISCV)
23+
24+
void ArchitectureRISCV::Initialize() {
25+
PluginManager::RegisterPlugin(GetPluginNameStatic(),
26+
"RISCV-specific algorithms",
27+
&ArchitectureRISCV::Create);
28+
}
29+
30+
void ArchitectureRISCV::Terminate() {
31+
PluginManager::UnregisterPlugin(&ArchitectureRISCV::Create);
32+
}
33+
34+
std::unique_ptr<Architecture> ArchitectureRISCV::Create(const ArchSpec &arch) {
35+
if (!arch.GetTriple().isRISCV())
36+
return nullptr;
37+
return std::unique_ptr<Architecture>(new ArchitectureRISCV());
38+
}
39+
40+
void ArchitectureRISCV::OverrideStopInfo(Thread &thread) const {}
41+
42+
std::unique_ptr<llvm::legacy::PassManager>
43+
ArchitectureRISCV::GetArchitectureCustomPasses(
44+
const ExecutionContext &exe_ctx, const llvm::StringRef expr) const {
45+
// LLDB generates additional support functions like
46+
// '_$__lldb_valid_pointer_check', that do not require custom passes
47+
if (expr != "$__lldb_expr")
48+
return nullptr;
49+
50+
std::unique_ptr<llvm::legacy::PassManager> custom_passes =
51+
std::make_unique<llvm::legacy::PassManager>();
52+
auto *P = createDirectToIndirectFCR(exe_ctx);
53+
custom_passes->add(P);
54+
return custom_passes;
55+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- ArchitectureRISCV.h -------------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include "lldb/Core/Architecture.h"
12+
13+
namespace lldb_private {
14+
15+
class ArchitectureRISCV : public Architecture {
16+
public:
17+
static llvm::StringRef GetPluginNameStatic() { return "riscv"; }
18+
static void Initialize();
19+
static void Terminate();
20+
21+
llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
22+
23+
void OverrideStopInfo(Thread &thread) const override;
24+
25+
std::unique_ptr<llvm::legacy::PassManager>
26+
GetArchitectureCustomPasses(const ExecutionContext &exe_ctx,
27+
const llvm::StringRef expr) const override;
28+
29+
private:
30+
static std::unique_ptr<Architecture> Create(const ArchSpec &arch);
31+
ArchitectureRISCV() = default;
32+
};
33+
34+
} // namespace lldb_private
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_lldb_library(lldbPluginArchitectureRISCV PLUGIN
2+
ArchitectureRISCV.cpp
3+
DirectToIndirectFCR.cpp
4+
5+
LINK_LIBS
6+
lldbPluginProcessUtility
7+
lldbCore
8+
lldbTarget
9+
lldbUtility
10+
LINK_COMPONENTS
11+
Support
12+
)

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
#include "llvm/Support/Error.h"
4444
#include "llvm/Support/FileSystem.h"
4545
#include "llvm/Support/TargetSelect.h"
46+
#include "llvm/TargetParser/Triple.h"
4647

4748
#include "llvm/IR/LLVMContext.h"
49+
#include "llvm/IR/LegacyPassManager.h"
4850
#include "llvm/IR/Module.h"
4951
#include "llvm/Support/DynamicLibrary.h"
5052
#include "llvm/Support/ErrorHandling.h"
@@ -1441,6 +1443,27 @@ lldb_private::Status ClangExpressionParser::DoPrepareForExecution(
14411443
custom_passes.EarlyPasses->run(*llvm_module_up);
14421444
}
14431445

1446+
std::unique_ptr<llvm::legacy::PassManager> arch_passes(nullptr);
1447+
if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) {
1448+
Architecture *arch = target_sp->GetArchitecturePlugin();
1449+
if (arch) {
1450+
arch_passes =
1451+
arch->GetArchitectureCustomPasses(exe_ctx, m_expr.FunctionName());
1452+
}
1453+
}
1454+
1455+
if (arch_passes)
1456+
arch_passes->run(*llvm_module_up);
1457+
1458+
if (llvm_module_up->getNamedMetadata(
1459+
Architecture::s_target_incompatibility_marker)) {
1460+
err.SetErrorToGenericError();
1461+
err.SetErrorStringWithFormat(
1462+
"%s - Architecture passes failure on function %s\n. Function "
1463+
"contains unsupported function calls",
1464+
__FUNCTION__, m_expr.FunctionName());
1465+
}
1466+
14441467
execution_unit_sp = std::make_shared<IRExecutionUnit>(
14451468
m_llvm_context, // handed off here
14461469
llvm_module_up, // handed off here

0 commit comments

Comments
 (0)