LLDB  mainline
RenderScriptExpressionOpts.cpp
Go to the documentation of this file.
1 //===-- RenderScriptExpressionOpts.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 <string>
10 
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/MC/TargetRegistry.h"
18 #include "llvm/Target/TargetMachine.h"
19 #include "llvm/Target/TargetOptions.h"
20 
21 #include "clang/Basic/TargetOptions.h"
22 
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/Log.h"
26 
28 #include "RenderScriptRuntime.h"
30 
31 using namespace lldb_private;
32 using namespace lldb_renderscript;
33 
34 // [``slang``](https://android.googlesource.com/platform/frameworks/compile/slang),
35 // the compiler frontend for RenderScript embeds an ARM specific triple in IR
36 // that is shipped in the app, after generating IR that has some assumptions
37 // that an ARM device is the target. As the IR is then compiled on a device of
38 // unknown (at time the IR was generated at least) architecture, when calling
39 // RenderScript API function as part of debugger expressions, we have to
40 // perform a fixup pass that removes those assumptions right before the module
41 // is sent to be generated by the llvm backend.
42 
43 static bool registerRSDefaultTargetOpts(clang::TargetOptions &proto,
44  const llvm::Triple::ArchType &arch) {
45  switch (arch) {
46  case llvm::Triple::ArchType::x86:
47  proto.Triple = "i686--linux-android";
48  proto.CPU = "atom";
49  proto.Features.push_back("+long64");
50  // Fallthrough for common x86 family features
51  LLVM_FALLTHROUGH;
52  case llvm::Triple::ArchType::x86_64:
53  proto.Features.push_back("+mmx");
54  proto.Features.push_back("+sse");
55  proto.Features.push_back("+sse2");
56  proto.Features.push_back("+sse3");
57  proto.Features.push_back("+ssse3");
58  proto.Features.push_back("+sse4.1");
59  proto.Features.push_back("+sse4.2");
60  break;
61  case llvm::Triple::ArchType::mipsel:
62  // pretend this is `arm' for the front-end
63  proto.Triple = "armv7-none-linux-android";
64  proto.CPU = "";
65  proto.Features.push_back("+long64");
66  break;
67  case llvm::Triple::ArchType::mips64el:
68  // pretend this is `aarch64' for the front-end
69  proto.Triple = "aarch64-none-linux-android";
70  proto.CPU = "";
71  break;
72  default:
73  return false;
74  }
75  return true;
76 }
77 
78 bool RenderScriptRuntimeModulePass::runOnModule(llvm::Module &module) {
79  bool changed_module = false;
80  Log *log(
82 
83  std::string err;
84  llvm::StringRef real_triple =
85  m_process_ptr->GetTarget().GetArchitecture().GetTriple().getTriple();
86  const llvm::Target *target_info =
87  llvm::TargetRegistry::lookupTarget(std::string(real_triple), err);
88  if (!target_info) {
89  if (log)
90  log->Warning("couldn't determine real target architecture: '%s'",
91  err.c_str());
92  return false;
93  }
94 
95  llvm::Optional<llvm::Reloc::Model> reloc_model = llvm::None;
96  assert(m_process_ptr && "no available lldb process");
97  switch (m_process_ptr->GetTarget().GetArchitecture().GetMachine()) {
98  case llvm::Triple::ArchType::x86:
99  changed_module |= fixupX86FunctionCalls(module);
100  // For some reason this triple gets totally missed by the backend, and must
101  // be set manually. There a reference in bcc/Main.cpp about auto feature-
102  // detection being removed from LLVM3.5, but I can't see that discussion
103  // anywhere public.
104  real_triple = "i686--linux-android";
105  break;
106  case llvm::Triple::ArchType::x86_64:
107  changed_module |= fixupX86_64FunctionCalls(module);
108  break;
109  case llvm::Triple::ArchType::mipsel:
110  case llvm::Triple::ArchType::mips64el:
111  // No actual IR fixup pass is needed on MIPS, but the datalayout and
112  // targetmachine do need to be explicitly set.
113 
114  // bcc explicitly compiles MIPS code to use the static relocation model due
115  // to an issue with relocations in mclinker. see
116  // libbcc/support/CompilerConfig.cpp for details
117  reloc_model = llvm::Reloc::Static;
118  changed_module = true;
119  break;
120  case llvm::Triple::ArchType::arm:
121  case llvm::Triple::ArchType::aarch64:
122  // ARM subtargets need no fixup passes as they are the initial target as
123  // generated by the
124  // slang compiler frontend.
125  break;
126  default:
127  if (log)
128  log->Warning("Ignoring unknown renderscript target");
129  return false;
130  }
131 
132  if (changed_module) {
133  llvm::TargetOptions options;
134  llvm::TargetMachine *target_machine = target_info->createTargetMachine(
135  real_triple, "", "", options, reloc_model);
136  assert(target_machine &&
137  "failed to identify RenderScriptRuntime target machine");
138  // We've been using a triple and datalayout of some ARM variant all along,
139  // so we need to let the backend know that this is no longer the case.
140  if (log) {
141  LLDB_LOGF(log, "%s - Changing RS target triple to '%s'", __FUNCTION__,
142  real_triple.str().c_str());
143  LLDB_LOGF(
144  log, "%s - Changing RS datalayout to '%s'", __FUNCTION__,
145  target_machine->createDataLayout().getStringRepresentation().c_str());
146  }
147  module.setTargetTriple(real_triple);
148  module.setDataLayout(target_machine->createDataLayout());
149  }
150  return changed_module;
151 }
152 
154 
155 namespace lldb_private {
156 
157 bool RenderScriptRuntime::GetOverrideExprOptions(clang::TargetOptions &proto) {
158  auto *process = GetProcess();
159  assert(process);
161  proto, process->GetTarget().GetArchitecture().GetMachine());
162 }
163 
165  if (!m_ir_passes)
166  m_ir_passes = new RSIRPasses(GetProcess());
167  assert(m_ir_passes);
168 
169  passes.EarlyPasses = m_ir_passes->EarlyPasses;
170  passes.LatePasses = m_ir_passes->LatePasses;
171 
172  return true;
173 }
174 
175 namespace lldb_renderscript {
176 
178  IRPasses();
179  assert(process);
180 
181  EarlyPasses = std::make_shared<llvm::legacy::PassManager>();
182  assert(EarlyPasses);
183  EarlyPasses->add(new RenderScriptRuntimeModulePass(process));
184 }
185 
186 RSIRPasses::~RSIRPasses() = default;
187 
188 } // namespace lldb_renderscript
189 } // namespace lldb_private
RenderScriptRuntimeModulePass
Definition: RenderScriptExpressionOpts.h:31
LLDB_LOGF
#define LLDB_LOGF(log,...)
Definition: Log.h:249
lldb_private::Process
Definition: Process.h:340
lldb_private::Log::Warning
void void void void Warning(const char *fmt,...) __attribute__((format(printf
Definition: Log.cpp:172
lldb_private::lldb_renderscript::fixupX86FunctionCalls
bool fixupX86FunctionCalls(llvm::Module &module)
Definition: RenderScriptx86ABIFixups.cpp:267
Process.h
Target.h
lldb_private::lldb_renderscript::RSIRPasses
Definition: RenderScriptExpressionOpts.h:45
LIBLLDB_LOG_EXPRESSIONS
#define LIBLLDB_LOG_EXPRESSIONS
Definition: Logging.h:22
LIBLLDB_LOG_LANGUAGE
#define LIBLLDB_LOG_LANGUAGE
Definition: Logging.h:42
RenderScriptRuntimeModulePass::runOnModule
bool runOnModule(llvm::Module &module) override
Definition: RenderScriptExpressionOpts.cpp:78
lldb_private::LLVMUserExpression::IRPasses
Definition: LLVMUserExpression.h:48
Log.h
lldb_private::RenderScriptRuntime::GetOverrideExprOptions
bool GetOverrideExprOptions(clang::TargetOptions &prototype)
Definition: RenderScriptExpressionOpts.cpp:157
RenderScriptRuntime.h
lldb_private::GetLogIfAllCategoriesSet
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:58
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::lldb_renderscript::RSIRPasses::~RSIRPasses
~RSIRPasses()
lldb_private::RenderScriptRuntime::GetIRPasses
bool GetIRPasses(LLVMUserExpression::IRPasses &passes) override
Definition: RenderScriptExpressionOpts.cpp:164
registerRSDefaultTargetOpts
static bool registerRSDefaultTargetOpts(clang::TargetOptions &proto, const llvm::Triple::ArchType &arch)
Definition: RenderScriptExpressionOpts.cpp:43
lldb_private::lldb_renderscript::RSIRPasses::RSIRPasses
RSIRPasses(lldb_private::Process *process)
Definition: RenderScriptExpressionOpts.cpp:177
RenderScriptExpressionOpts.h
lldb_private::lldb_renderscript::fixupX86_64FunctionCalls
bool fixupX86_64FunctionCalls(llvm::Module &module)
Definition: RenderScriptx86ABIFixups.cpp:271
RenderScriptx86ABIFixups.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Log
Definition: Log.h:49
RenderScriptRuntimeModulePass::ID
static char ID
Definition: RenderScriptExpressionOpts.h:33
lldb_private::LLVMUserExpression::IRPasses::EarlyPasses
std::shared_ptr< llvm::legacy::PassManager > EarlyPasses
Definition: LLVMUserExpression.h:49
lldb_private::LLVMUserExpression::IRPasses::LatePasses
std::shared_ptr< llvm::legacy::PassManager > LatePasses
Definition: LLVMUserExpression.h:51