• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ahueck / llvm-dimeta / 15085917963

17 May 2025 02:01PM UTC coverage: 83.355% (+0.06%) from 83.296%
15085917963

push

github

web-flow
Value typing (#42)

1153 of 1664 branches covered (69.29%)

Branch coverage included in aggregate %.

200 of 218 new or added lines in 14 files covered. (91.74%)

1 existing line in 1 file now uncovered.

2112 of 2253 relevant lines covered (93.74%)

4268.46 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.08
/lib/type/Dimeta.cpp
1
//  llvm-dimeta library
2
//  Copyright (c) 2022-2025 llvm-dimeta authors
3
//  Distributed under the BSD 3-Clause license.
4
//  (See accompanying file LICENSE)
5
//  SPDX-License-Identifier: BSD-3-Clause
6
//
7

8
#include "Dimeta.h"
9

10
#include "DIFinder.h"
11
#include "DIRootType.h"
12
#include "DITypeExtractor.h"
13
#include "DataflowAnalysis.h"
14
#include "DefUseAnalysis.h"
15
#include "DimetaData.h"
16
#include "DimetaParse.h"
17
#include "MemoryOps.h"
18
#include "Util.h"
19
#include "ValuePath.h"
20
#include "support/Logger.h"
21

22
#include "llvm/ADT/ArrayRef.h"
23
#include "llvm/ADT/STLExtras.h"
24
#include "llvm/ADT/SmallVector.h"
25
#include "llvm/ADT/ilist_iterator.h"
26
#include "llvm/BinaryFormat/Dwarf.h"
27
#include "llvm/Config/llvm-config.h"
28
#include "llvm/IR/Argument.h"
29
#include "llvm/IR/Constants.h"
30
#include "llvm/IR/DebugInfoMetadata.h"
31
#include "llvm/IR/Function.h"
32
#include "llvm/IR/GlobalVariable.h"
33
#include "llvm/IR/InstIterator.h"
34
#include "llvm/IR/InstrTypes.h"
35
#include "llvm/IR/Instructions.h"
36
#include "llvm/IR/IntrinsicInst.h"
37
#include "llvm/IR/Metadata.h"
38
#include "llvm/IR/Operator.h"
39
#include "llvm/IR/Value.h"
40
#include "llvm/Support/Casting.h"
41
#include "llvm/Support/Debug.h"
42
#include "llvm/Support/ErrorHandling.h"
43
#include "llvm/Support/raw_ostream.h"
44

45
#include <cassert>
46
#include <iterator>
47
#include <string>
48

49
namespace llvm {
50
class DbgVariableIntrinsic;
51
}  // namespace llvm
52

53
#if LLVM_VERSION_MAJOR == 10
54
// For FindDbgAddrUses:
55
#include "llvm/Transforms/Utils/Local.h"
56
#endif
57

58
namespace dimeta {
59

60
namespace experimental {
61
std::optional<llvm::DIType*> di_type_for(const llvm::Value* value);
62
}
63

64
llvm::SmallVector<llvm::DIType*, 4> collect_types(const llvm::CallBase* call,
1,388✔
65
                                                  llvm::ArrayRef<dataflow::ValuePath> paths_to_type) {
66
  using namespace llvm;
67
  SmallVector<llvm::DIType*, 4> di_types;
1,388✔
68
  llvm::transform(paths_to_type, dimeta::util::optional_back_inserter(di_types), [&](const auto& path) {
2,776✔
69
    return type::find_type(dataflow::CallValuePath{call, path});
1,388✔
70
  });
71
  return di_types;
1,388✔
72
}
1,388!
73

74
auto final_ditype(std::optional<llvm::DIType*> root_ditype) -> std::pair<std::optional<llvm::DIType*>, int> {
5,902✔
75
  if (!root_ditype) {
5,902✔
76
    return {{}, 0};
10✔
77
  }
78
  int level{0};
5,892✔
79
  llvm::DIType* type = *root_ditype;
5,892✔
80
  while (llvm::isa<llvm::DIDerivedType>(type)) {
11,478✔
81
    auto ditype = llvm::dyn_cast<llvm::DIDerivedType>(type);
5,650✔
82
    if (ditype->getTag() == llvm::dwarf::DW_TAG_pointer_type) {
5,650✔
83
      ++level;
3,614✔
84
    }
3,614✔
85
    // void*-based derived types have basetype=null:
86
    if (ditype->getBaseType() == nullptr) {
5,650✔
87
      return {type, level};
64✔
88
    }
89
    type = ditype->getBaseType();
5,586✔
90
  }
5,650✔
91

92
  return {type, level};
5,828✔
93
}
5,902✔
94

95
std::optional<llvm::DIType*> type_for_malloclike(const llvm::CallBase* call) {
1,296✔
96
  auto local = difinder::get_array_access_assignment(call);
1,296✔
97
  if (local) {
1,296✔
98
    LOG_DEBUG("Call has local variable " << *call)
99
    // LOG_DEBUG("Call type " << log::ditype_str(local.value()->getType()))
100
    auto base_type = local.value().var->getType();
56✔
101
    if (local.value().array_access) {
56✔
102
      if (auto* array_type = llvm::dyn_cast<llvm::DICompositeType>(base_type)) {
56!
103
        LOG_DEBUG("Returning type of access to array " << log::ditype_str(array_type))
104
        return array_type->getBaseType();
28✔
105
      }
NEW
106
    }
×
107
    return base_type;
28✔
108
  }
56✔
109

110
  const auto ditype_paths = dataflow::type_for_heap_call(call);
1,240✔
111

112
  LOG_DEBUG("Found paths, now collecting types")
113
  const auto ditypes_vector = collect_types(call, ditype_paths);
1,240✔
114
  if (ditypes_vector.empty()) {
1,240✔
115
    return {};
10✔
116
  }
117
  return *ditypes_vector.begin();
1,230✔
118
}
1,296✔
119

120
std::optional<llvm::DIType*> type_for_newlike(const llvm::CallBase* call) {
392✔
121
  auto* heapalloc_md = call->getMetadata("heapallocsite");
392✔
122
  assert(heapalloc_md != nullptr && "Missing required heapallocsite metadata.");
784!
123
  if (auto* type = llvm::dyn_cast<llvm::DIType>(heapalloc_md)) {
784!
124
    //    util::DIPrinter printer(llvm::outs(), call->getParent()->getParent()->getParent());
125
    //    printer.traverseType(type);
126
    //    llvm::dbgs() << "Final Type: " << *type << "\n";
127
    return type;
392✔
128
  }
129
  return {};
×
130
}
392✔
131

132
std::optional<DimetaData> type_for(const llvm::CallBase* call) {
10,912✔
133
  using namespace llvm;
134
  const dimeta::memory::MemOps mem_ops;
10,912✔
135

136
  auto* cb_fun = call->getCalledFunction();
10,912✔
137
  if (!cb_fun) {
10,912!
138
    return {};
8✔
139
  }
140

141
  if (!mem_ops.isAlloc(cb_fun->getName())) {
10,904✔
142
    LOG_TRACE("Skipping call base: " << cb_fun->getName());
143
    return {};
9,138✔
144
  }
145

146
  std::optional<llvm::DIType*> extracted_type{};
1,766✔
147
  int pointer_level_offset{0};
1,766✔
148

149
  const auto is_cuda_like = mem_ops.isCudaLike(cb_fun->getName());
1,766✔
150
  if (is_cuda_like) {
1,766✔
151
    LOG_DEBUG("Type for cuda-like " << cb_fun->getName())
152
    extracted_type = experimental::di_type_for(call->getOperand(0));
78✔
153

154
    // when wrapped in, e.g., cudaMalloc<float>(float**, ...), we remove one pointer level:
155
    // auto* parent    = call->getFunction();
156
    // const auto name = std::string{cb_fun->getName()} + "<";
157
    // LOG_DEBUG(name << " vs. " << util::try_demangle(*makeparent))
158
    // if (extracted_type && util::try_demangle(*parent).find(name) != std::string::npos) {
159
    //   LOG_DEBUG("Reset cuda-like pointer level")
160
    //   auto ditype = llvm::dyn_cast<llvm::DIDerivedType>(extracted_type.value());
161
    //   if (ditype->getTag() == llvm::dwarf::DW_TAG_pointer_type) {
162
    //     extracted_type = ditype->getBaseType();
163
    //   }
164
    // }
165
  }
78✔
166

167
  const auto is_cxx_new = mem_ops.isNewLike(cb_fun->getName());
1,766✔
168

169
#ifdef DIMETA_USE_HEAPALLOCSITE
170
  if (is_cxx_new) {
1,766✔
171
    if (call->getMetadata("heapallocsite")) {
406✔
172
      LOG_TRACE("Type for new-like " << cb_fun->getName())
173
      extracted_type = type_for_newlike(call);
392✔
174
      // !heapallocsite gives the type after "new", i.e., new int -> int, new int*[n] -> int*.
175
      // Our malloc-related algorithm would return int* and int** respectively, however, hence:
176
      pointer_level_offset += 1;
392✔
177
    } else {
392✔
178
      LOG_DEBUG("new-like allocation does not have heapallocsite metadata.")
179
    }
180
  }
406✔
181
#endif
182

183
  if (!extracted_type) {
1,766✔
184
    LOG_DEBUG("Type for malloc-like: " << cb_fun->getName())
185
    extracted_type = type_for_malloclike(call);
1,296✔
186
  }
1,296✔
187
  auto source_loc                        = difinder::find_location(call);
1,766✔
188
  const auto [final_type, pointer_level] = final_ditype(extracted_type);
5,298✔
189
  const auto meta = DimetaData{DimetaData::MemLoc::kHeap,           {}, extracted_type, final_type, source_loc,
5,298✔
190
                               pointer_level + pointer_level_offset};
1,766✔
191
  return meta;
1,766✔
192
}
10,912✔
193

194
std::optional<DimetaData> type_for(const llvm::AllocaInst* ai) {
3,973✔
195
  const auto local_di_var = difinder::find_local_variable(ai);
3,973✔
196

197
  if (local_di_var) {
3,973✔
198
    auto extracted_type                    = local_di_var.value()->getType();
3,810✔
199
    auto source_loc                        = difinder::find_location(ai);
3,810✔
200
    const auto [final_type, pointer_level] = final_ditype(extracted_type);
11,430✔
201
    const auto meta =
3,810✔
202
        DimetaData{DimetaData::MemLoc::kStack, local_di_var, extracted_type, final_type, source_loc, pointer_level};
11,430✔
203
    return meta;
3,810✔
204
  }
3,810✔
205

206
  LOG_DEBUG("No local_variable for " << *ai)
207

208
  return {};
163✔
209
}
3,973✔
210

211
std::optional<DimetaData> type_for(const llvm::GlobalVariable* gv) {
539✔
212
  llvm::SmallVector<llvm::DIGlobalVariableExpression*, 2> dbg_info;
539✔
213
  gv->getDebugInfo(dbg_info);
539✔
214
  if (!dbg_info.empty()) {
539✔
215
    auto gv_expr                           = *dbg_info.begin();
326✔
216
    auto gv_type                           = gv_expr->getVariable()->getType();
326✔
217
    const auto [final_type, pointer_level] = final_ditype(gv_type);
978✔
218
    return DimetaData{DimetaData::MemLoc::kGlobal, gv_expr->getVariable(), gv_type, final_type, {}, pointer_level};
978✔
219
  }
326✔
220
  return {};
213✔
221
}
539✔
222

223
std::optional<CompileUnitTypeList> compile_unit_types(const llvm::Module* module) {
1,079✔
224
  CompileUnitTypeList list;
1,079✔
225
  for (auto* compile_unit : module->debug_compile_units()) {
2,153✔
226
    CompileUnitTypes current_cu;
1,074✔
227
    current_cu.name = compile_unit->getFilename();
1,074✔
228
    for (auto* retained_type : compile_unit->getRetainedTypes()) {
1,771✔
229
      if (auto* type = llvm::dyn_cast<llvm::DIType>(retained_type)) {
1,394!
230
        auto dimeta_result = parser::make_dimetadata(type);
697✔
231
        if (!dimeta_result) {
697!
232
          continue;
×
233
        }
234
        current_cu.types.push_back(dimeta_result->type_);
697✔
235
      }
697!
236
    }
697!
237
    list.push_back(current_cu);
1,074✔
238
  }
1,074✔
239
  return (list.empty() ? std::optional<CompileUnitTypeList>{} : list);
1,079✔
240
}
1,079✔
241

242
namespace experimental {
243
std::optional<llvm::DIType*> di_type_for(const llvm::Value* value) {
78✔
244
  auto paths                = dataflow::experimental::path_from_value(value);
78✔
245
  const auto ditypes_vector = collect_types(nullptr, paths);
78✔
246
  if (ditypes_vector.empty()) {
78!
NEW
247
    return {};
×
248
  }
249

250
  return *ditypes_vector.begin();
78✔
251
}
78✔
252

253
std::optional<QualifiedType> type_for(const llvm::Value* value) {
70✔
254
  auto paths                = dataflow::experimental::path_from_value(value);
70✔
255
  const auto ditypes_vector = collect_types(nullptr, paths);
70✔
256
  if (ditypes_vector.empty()) {
70!
NEW
257
    return {};
×
258
  }
259

260
  for (const auto& type : ditypes_vector) {
140!
261
    auto dimeta_result = parser::make_dimetadata(type);
70✔
262
    if (!dimeta_result) {
70!
NEW
263
      continue;
×
264
    }
265
    return dimeta_result->type_;
70✔
266
  }
70!
267

NEW
268
  return {};
×
269
}
70✔
270

271
}  // namespace experimental
272

273
}  // namespace dimeta
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc