[llvm] [Instrumentor] Add Instrumentor pass (PR #138958)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Wed May 21 01:38:36 PDT 2025
================
@@ -0,0 +1,645 @@
+//===-- Instrumentor.cpp - Highly configurable instrumentation pass -------===//
+//
+// 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/Transforms/IPO/Instrumentor.h"
+#include "llvm/Transforms/IPO/InstrumentorConfigFile.h"
+
+#include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/iterator.h"
+#include "llvm/IR/Constant.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DebugInfoMetadata.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/InstrTypes.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/IR/Verifier.h"
+#include "llvm/IRReader/IRReader.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Regex.h"
+
+#include <cassert>
+#include <cstdint>
+#include <functional>
+#include <iterator>
+#include <string>
+#include <system_error>
+#include <type_traits>
+
+using namespace llvm;
+using namespace llvm::instrumentor;
+
+#define DEBUG_TYPE "instrumentor"
+
+static cl::opt<std::string> WriteJSONConfig(
+ "instrumentor-write-config-file",
+ cl::desc(
+ "Write the instrumentor configuration into the specified JSON file"),
+ cl::init(""));
+static cl::opt<std::string> ReadJSONConfig(
+ "instrumentor-read-config-file",
+ cl::desc(
+ "Read the instrumentor configuration from the specified JSON file"),
+ cl::init(""));
+
+namespace {
+
+template <typename IRBuilderTy> void ensureDbgLoc(IRBuilderTy &IRB) {
+ if (IRB.getCurrentDebugLocation())
+ return;
+ auto *BB = IRB.GetInsertBlock();
+ if (auto *SP = BB->getParent()->getSubprogram())
+ IRB.SetCurrentDebugLocation(DILocation::get(BB->getContext(), 0, 0, SP));
+}
+
+template <typename IRBTy>
+Value *tryToCast(IRBTy &IRB, Value *V, Type *Ty, const DataLayout &DL,
+ bool AllowTruncate = false) {
+ if (!V)
+ return Constant::getAllOnesValue(Ty);
+ auto *VTy = V->getType();
+ if (VTy == Ty)
+ return V;
+ if (VTy->isAggregateType())
+ return V;
+ auto RequestedSize = DL.getTypeSizeInBits(Ty);
+ auto ValueSize = DL.getTypeSizeInBits(VTy);
+ bool IsTruncate = RequestedSize < ValueSize;
+ if (IsTruncate && !AllowTruncate)
+ return V;
+ if (IsTruncate && AllowTruncate)
+ return tryToCast(IRB,
+ IRB.CreateIntCast(V, IRB.getIntNTy(RequestedSize),
+ /*IsSigned=*/false),
+ Ty, DL, AllowTruncate);
+ if (VTy->isPointerTy() && Ty->isPointerTy())
+ return IRB.CreatePointerBitCastOrAddrSpaceCast(V, Ty);
+ if (VTy->isIntegerTy() && Ty->isIntegerTy())
+ return IRB.CreateIntCast(V, Ty, /*IsSigned=*/false);
+ if (VTy->isFloatingPointTy() && Ty->isIntOrPtrTy()) {
+ switch (ValueSize) {
+ case 64:
+ return tryToCast(IRB, IRB.CreateBitCast(V, IRB.getInt64Ty()), Ty, DL,
+ AllowTruncate);
+ case 32:
+ return tryToCast(IRB, IRB.CreateBitCast(V, IRB.getInt32Ty()), Ty, DL,
+ AllowTruncate);
+ case 16:
+ return tryToCast(IRB, IRB.CreateBitCast(V, IRB.getInt16Ty()), Ty, DL,
+ AllowTruncate);
+ case 8:
+ return tryToCast(IRB, IRB.CreateBitCast(V, IRB.getInt8Ty()), Ty, DL,
+ AllowTruncate);
----------------
arsenm wrote:
This shouldn't need to maintain a list of integer types, just get the width. This also doesn't handle vectors
https://github.com/llvm/llvm-project/pull/138958
More information about the llvm-commits
mailing list