[llvm] [llvm-objdump] Add support for HIP offload bundles (PR #114834)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 19 15:50:51 PST 2025


================
@@ -0,0 +1,482 @@
+//===- OffloadBundle.cpp - Utilities for handling offloading code  -*- C++ -*-===//
+//
+// 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/ADT/StringSwitch.h"
+#include "llvm/BinaryFormat/Magic.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/MC/StringTableBuilder.h"
+#include "llvm/Object/Archive.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/COFF.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/Error.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
+#include "llvm/Object/OffloadBundle.h"
+#include "llvm/Support/Alignment.h"
+#include "llvm/Support/BinaryStreamReader.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/Timer.h"
+
+using namespace llvm;
+using namespace llvm::object;
+
+static llvm::TimerGroup
+    OffloadBundlerTimerGroup("Offload Bundler Timer Group",
+                             "Timer group for offload bundler");
+
+// Extract an Offload bundle (usually a Offload Bundle) from a fat_bin
+// section
+Error extractOffloadBundle(MemoryBufferRef Contents, uint64_t SectionOffset,
+                           StringRef FileName,
+                           SmallVectorImpl<OffloadBundleFatBin> &Bundles) {
+
+  uint64_t Offset = 0;
+  int64_t NextbundleStart = 0;
+
+  // There could be multiple offloading bundles stored at this section.
+  while (NextbundleStart >= 0) {
+
+    std::unique_ptr<MemoryBuffer> Buffer =
+        MemoryBuffer::getMemBuffer(Contents.getBuffer().drop_front(Offset), "",
+                                   /*RequiresNullTerminator*/ false);
+
+    // Create the FatBinBindle object. This will also create the Bundle Entry
+    // list info.
+    auto FatBundleOrErr =
+        OffloadBundleFatBin::create(*Buffer, SectionOffset + Offset, FileName);
+    if (!FatBundleOrErr)
+      return FatBundleOrErr.takeError();
+
+    // Add current Bundle to list.
+    Bundles.emplace_back(std::move(**FatBundleOrErr));
+
+    // Find the next bundle by searching for the magic string
+    StringRef Str = Buffer->getBuffer();
+    NextbundleStart =
+        (int64_t)Str.find(StringRef("__CLANG_OFFLOAD_BUNDLE__"), 24);
+
+    if (NextbundleStart >= 0)
+      Offset += NextbundleStart;
+  }
+
+  return Error::success();
+}
+
+Error OffloadBundleFatBin::readEntries(StringRef Buffer,
+                                       uint64_t SectionOffset) {
+  uint64_t NumOfEntries = 0;
+
+  // get Reader
----------------
jhuber6 wrote:

```suggestion
```
Nit, comments should be grammatically correct sentences and helpful.

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


More information about the llvm-commits mailing list