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 if (Reloc == BTF::BTF_TYPE_ID_REMOTE) {
94 if (Ty->getName().empty()) {
95 if (isa<DISubroutineType>(Val: Ty))
96 report_fatal_error(
97 reason: "SubroutineType not supported for BTF_TYPE_ID_REMOTE reloc");
98 else
99 report_fatal_error(reason: "Empty type name for BTF_TYPE_ID_REMOTE reloc");
100 }
101 }
102 MD = Ty;
103
104 BasicBlock *BB = Call->getParent();
105 IntegerType *VarType = Type::getInt64Ty(C&: BB->getContext());
106 std::string GVName =
107 BaseName + std::to_string(val: Count) + "$" + std::to_string(val: Reloc);
108 GlobalVariable *GV = new GlobalVariable(
109 *M, VarType, false, GlobalVariable::ExternalLinkage, nullptr, GVName);
110 GV->addAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr);
111 GV->setMetadata(KindID: LLVMContext::MD_preserve_access_index, Node: MD);
112
113 // Load the global variable which represents the type info.
114 auto *LDInst = new LoadInst(Type::getInt64Ty(C&: BB->getContext()), GV, "",
115 Call->getIterator());
116 Instruction *PassThroughInst =
117 BPFCoreSharedInfo::insertPassThrough(M, BB, Input: LDInst, Before: Call);
118 Call->replaceAllUsesWith(V: PassThroughInst);
119 Call->eraseFromParent();
120 Count++;
121 }
122
123 return true;
124}
125} // End anonymous namespace
126
127PreservedAnalyses BPFPreserveDITypePass::run(Function &F,
128 FunctionAnalysisManager &AM) {
129 return BPFPreserveDITypeImpl(F) ? PreservedAnalyses::none()
130 : PreservedAnalyses::all();
131}
132