[llvm-branch-commits] [llvm] [BOLT] Add binary introspection/JIT manager (PR #81346)

Amir Ayupov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Mar 30 19:34:32 PDT 2024


================
@@ -0,0 +1,367 @@
+//===- bolt/Rewrite/JITRewriteInstance.cpp - JIT rewriter -------------===//
+//
+// 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 "bolt/Rewrite/JITRewriteInstance.h"
+#include "bolt/Core/BinaryContext.h"
+#include "bolt/Core/BinaryEmitter.h"
+#include "bolt/Core/BinaryFunction.h"
+#include "bolt/Core/JumpTable.h"
+#include "bolt/Core/MCPlusBuilder.h"
+#include "bolt/Profile/DataAggregator.h"
+#include "bolt/Rewrite/BinaryPassManager.h"
+#include "bolt/Rewrite/RewriteInstance.h"
+#include "bolt/Utils/Utils.h"
+#include "llvm/MC/MCAsmLayout.h"
+#include "llvm/MC/MCObjectStreamer.h"
+#include "llvm/Object/SymbolSize.h"
+#include "llvm/Support/Errc.h"
+#include "llvm/Support/FileSystem.h"
+#include <memory>
+
+namespace opts {
+
+using namespace llvm;
+extern cl::opt<unsigned> AlignText;
+extern cl::opt<bool> PrintSections;
+extern cl::opt<bool> PrintDisasm;
+extern cl::opt<bool> PrintCFG;
+extern cl::opt<unsigned> Verbosity;
+} // namespace opts
+
+namespace llvm {
+namespace bolt {
+
+#define DEBUG_TYPE "bolt"
+
+Expected<std::unique_ptr<JITRewriteInstance>>
+JITRewriteInstance::createJITRewriteInstance(JournalingStreams Logger,
+                                             bool IsPIC) {
+  Error Err = Error::success();
+  std::unique_ptr<JITRewriteInstance> JITRI(
+      new JITRewriteInstance(Logger, IsPIC, Err));
+  if (Err)
+    return std::move(Err);
+  return std::move(JITRI);
+}
+
+JITRewriteInstance::JITRewriteInstance(JournalingStreams Logger, bool IsPIC,
+                                       Error &Err)
+    : StrPool(StrAllocator) {
+  ErrorAsOutParameter EAO(&Err);
+  Triple TheTriple(sys::getDefaultTargetTriple().c_str());
+
+  auto BCOrErr = BinaryContext::createBinaryContext(
+      TheTriple, StringRef("JIT input file"), nullptr, IsPIC, nullptr, Logger);
+  if (Error E = BCOrErr.takeError()) {
+    Err = std::move(E);
+    return;
+  }
+  BC = std::move(BCOrErr.get());
+  BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(
+      createMCPlusBuilder(BC->TheTriple->getArch(), BC->MIA.get(),
+                          BC->MII.get(), BC->MRI.get(), BC->STI.get())));
+  BC->FirstAllocAddress = 0;
+  BC->LayoutStartAddress = 0xffffffffffffffff;
+}
+
+JITRewriteInstance::~JITRewriteInstance() {}
+
+void JITRewriteInstance::adjustCommandLineOptions() {
+  if (!opts::AlignText.getNumOccurrences())
+    opts::AlignText = BC->PageAlign;
+}
+
+Error JITRewriteInstance::preprocessProfileData() {
+  if (!ProfileReader)
+    return Error::success();
+  if (Error E = ProfileReader->preprocessProfile(*BC.get()))
+    return Error(std::move(E));
+  return Error::success();
+}
+
+Error JITRewriteInstance::processProfileDataPreCFG() {
+  if (!ProfileReader)
+    return Error::success();
+  if (Error E = ProfileReader->readProfilePreCFG(*BC.get()))
+    return Error(std::move(E));
+  return Error::success();
+}
+
+Error JITRewriteInstance::processProfileData() {
+  if (!ProfileReader)
+    return Error::success();
+  if (Error E = ProfileReader->readProfile(*BC.get()))
+    return Error(std::move(E));
+  return Error::success();
+}
+
+Error JITRewriteInstance::disassembleFunctions() {
----------------
aaupov wrote:

I believe that this and other functions are common across *RewriteInstance classes. Is it worth defining a BaseRewriteInstance class and providing them there?

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


More information about the llvm-branch-commits mailing list