[llvm] [Object] Extract format-agnostic BBAddrMap decoder (PR #188435)
Haohai Wen via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 2 01:33:00 PDT 2026
================
@@ -0,0 +1,217 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements shared utilities for basic-block address maps.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/BBAddrMap.h"
+#include "llvm/Object/Error.h"
+
+using namespace llvm;
+using namespace object;
+
+namespace {
+
+// Helper to extract and decode the next ULEB128 value as unsigned int.
+// Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned
+// int limit.
+// Also returns zero if ULEBSizeErr is already in an error state.
+// ULEBSizeErr is an out variable if an error occurs.
+template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
+static IntTy readULEB128As(const DataExtractor &Data,
+ DataExtractor::Cursor &Cur, Error &ULEBSizeErr) {
+ // Bail out and do not extract data if ULEBSizeErr is already set.
+ if (ULEBSizeErr)
+ return 0;
+ uint64_t Offset = Cur.tell();
+ uint64_t Value = Data.getULEB128(Cur);
+ if (Value > std::numeric_limits<IntTy>::max()) {
+ ULEBSizeErr = createError("ULEB128 value at offset 0x" +
+ Twine::utohexstr(Offset) + " exceeds UINT" +
+ Twine(std::numeric_limits<IntTy>::digits) +
+ "_MAX (0x" + Twine::utohexstr(Value) + ")");
+ return 0;
+ }
+ return static_cast<IntTy>(Value);
+}
+} // end anonymous namespace
+
+Expected<std::vector<BBAddrMap>>
+llvm::object::decodeBBAddrMapPayload(AddressExtractor &Extractor,
+ std::vector<PGOAnalysisMap> *PGOAnalyses) {
+ const DataExtractor &Data = Extractor.getDataExtractor();
+ std::vector<BBAddrMap> FunctionEntries;
+
+ DataExtractor::Cursor Cur(0);
+ Error ULEBSizeErr = Error::success();
+ Error MetadataDecodeErr = Error::success();
+
+ uint8_t Version = 0;
+ uint16_t Feature = 0;
+ BBAddrMap::Features FeatEnable{};
+ while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
+ Cur.tell() < Data.getData().size()) {
+ Version = Data.getU8(Cur);
----------------
HaohaiWen wrote:
Done.
https://github.com/llvm/llvm-project/pull/188435
More information about the llvm-commits
mailing list