[llvm] [Instrumentor] Improve stub printer (for C/C++ and value packs) (PR #198366)

Kevin Sala Penades via llvm-commits llvm-commits at lists.llvm.org
Fri May 22 15:44:10 PDT 2026


================
@@ -0,0 +1,264 @@
+//===-- Instrumentor Runtime Helper Header -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// This header provides helper structures and functions for reading data
+// generated by the LLVM Instrumentor pass and passed to runtime functions.
+//
+// Generated with runtime prefix: __instrumentor_
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef INSTRUMENTOR_RUNTIME_H
+#define INSTRUMENTOR_RUNTIME_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+#include <string.h>
+
+#ifdef __cplusplus
+}
+#endif
+
+/// Header for each value in a value pack.
+/// Value packs are used to pass function arguments and other variable-length
+/// data to the runtime. The format is:
+///   [ValueHeader][Padding][Value Data]
+/// where padding aligns the value data to 8-byte boundaries.
+typedef struct {
+  uint32_t size;    // Size of the value in bytes
+  uint32_t type_id; // LLVM Type::TypeID of the value
+} ValuePackHeader;
+
+/// Iterator for reading values from a value pack.
+typedef struct {
+  const char *current;  // Current position in the pack
+  uint64_t offset;      // Byte offset from the start
+  uint32_t count;       // Number of elements in the pack
+  uint32_t index;       // Current element index
+} ValuePackIterator;
+
+/// Initialize a value pack iterator.
+/// \param iter The iterator to initialize
+/// \param pack_ptr Pointer to the start of the value pack
+/// \param num_elements Number of elements in the pack
+static inline void initValuePackIterator(ValuePackIterator *iter, const void *pack_ptr, uint32_t num_elements) {
+  iter->current = (const char *)pack_ptr;
+  iter->offset = 0;
+  iter->count = num_elements;
+  iter->index = 0;
+}
+
+/// Get the header for the current value.
+static inline ValuePackHeader getValuePackHeader(const ValuePackIterator *iter) {
+  const ValuePackHeader *header = (const ValuePackHeader *)iter->current;
+  return *header;
+}
+
+/// Get a pointer to the current value data.
+static inline const void *getValuePackData(const ValuePackIterator *iter) {
+  // Skip header (8 bytes: size + type_id)
+  const char *data_start = iter->current + sizeof(ValuePackHeader);
+  // Calculate padding for 8-byte alignment
+  ValuePackHeader header = getValuePackHeader(iter);
+  uint32_t padding = (8 - (header.size % 8)) % 8;
+  // Skip padding
+  return data_start + padding;
+}
+
+/// Move to the next value in the pack.
+static inline void nextValuePack(ValuePackIterator *iter) {
+  if (iter->index >= iter->count) {
+    iter->current = NULL;
+    return;
+  }
+  ValuePackHeader header = getValuePackHeader(iter);
+  uint32_t padding = (8 - (header.size % 8)) % 8;
----------------
kevinsala wrote:

Where does this 8 byte come from?

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


More information about the llvm-commits mailing list