1//===----------- BPFPreserveDIType.cpp - Preserve DebugInfo Types ---------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Preserve Debuginfo types encoded in __builtin_btf_type_id() metadata.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BPF.h"
14#include "BPFCORE.h"
15#include "llvm/BinaryFormat/Dwarf.h"
16#include "llvm/DebugInfo/BTF/BTF.h"
17#include "llvm/IR/DebugInfoMetadata.h"
18#include "llvm/IR/GlobalVariable.h"
19#include "llvm/IR/Instruction.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/PassManager.h"
23#include "llvm/IR/Type.h"
24#include "llvm/IR/Value.h"
25#include "llvm/Pass.h"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
27
28#define DEBUG_TYPE "bpf-preserve-di-type"
29
30using namespace llvm;
31
32namespace {
33
34static bool BPFPreserveDITypeImpl(Function &F) {
35 LLVM_DEBUG(dbgs() << "********** preserve debuginfo type **********\n");
36
37 Module *M = F.getParent();
38
39 // Bail out if no debug info.
40 if (M->debug_compile_units().empty())
41 return false;
42
43 std::vector<CallInst *> PreserveDITypeCalls;
44
45 for (auto &BB : F) {
46 for (auto &I : BB) {
47 auto *Call = dyn_cast<CallInst>(Val: &I);
48 if (!Call)
49 continue;
50
51 const auto *GV = dyn_cast<GlobalValue>(Val: Call->getCalledOperand());
52 if (!GV)
53 continue;
54
55 if (GV->getName().starts_with(Prefix: "llvm.bpf.btf.type.id")) {
56 if (!Call->getMetadata(KindID: LLVMContext::MD_preserve_access_index))
57 report_fatal_error(
58 reason: "Missing metadata for llvm.bpf.btf.type.id intrinsic");
59 PreserveDITypeCalls.push_back(x: Call);
60 }
61 }
62 }
63
64 if (PreserveDITypeCalls.empty())
65 return false;
66
67 std::string BaseName = "llvm.btf_type_id.";
68 static int Count = 0;
69 for (auto *Call : PreserveDITypeCalls) {
70 const ConstantInt *Flag = dyn_cast<ConstantInt>(Val: Call->getArgOperand(i: 1));
71 assert(Flag);
72 uint64_t FlagValue = Flag->getValue().getZExtValue();
73
74 if (FlagValue >= BPFCoreSharedInfo::MAX_BTF_TYPE_ID_FLAG)
75 report_fatal_error(reason: "Incorrect flag for llvm.bpf.btf.type.id intrinsic");
76
77 MDNode *MD = Call->getMetadata(KindID: LLVMContext::MD_preserve_access_index);
78
79 uint32_t Reloc;
80 if (FlagValue == BPFCoreSharedInfo::BTF_TYPE_ID_LOCAL_RELOC) {
81 Reloc = BTF::BTF_TYPE_ID_LOCAL;
82 } else {
83 Reloc = BTF::BTF_TYPE_ID_REMOTE;
84 }
85 DIType *Ty = cast<DIType>(Val: MD);
86 while (auto *DTy = dyn_cast<DIDerivedType>(Val: Ty)) {
87 unsigned Tag = DTy->getTag();
88 if (Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type)
89 break;
90 Ty = DTy->getBaseType();
91 }
92
93 MD = Ty;
94
95 BasicBlock *BB = Call->getParent();
96 IntegerType *VarType = Type::getInt64Ty(C&: BB->getContext());
97 std::string GVName =
98 BaseName + std::to_string(val: Count) + "$" + std::to_string(val: Reloc);
99 GlobalVariable *GV = new GlobalVariable(
100 *M, VarType, false, GlobalVariable::ExternalLinkage, nullptr, GVName);
101 GV->addAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr);
102 GV->setMetadata(KindID: LLVMContext::MD_preserve_access_index, Node: MD);
103
104 // Load the global variable which represents the type info.
105 auto *LDInst = new LoadInst(Type::getInt64Ty(C&: BB->getContext()), GV, "",
106 Call->getIterator());
107 Instruction *PassThroughInst =
108 BPFCoreSharedInfo::insertPassThrough(M, BB, Input: LDInst, Before: Call);
109 Call->replaceAllUsesWith(V: PassThroughInst);
110 Call->eraseFromParent();
111 Count++;
112 }
113
114 return true;
115}
116} // End anonymous namespace
117
118PreservedAnalyses BPFPreserveDITypePass::run(Function &F,
119 FunctionAnalysisManager &AM) {
120 return BPFPreserveDITypeImpl(F) ? PreservedAnalyses::none()
121 : PreservedAnalyses::all();
122}
123