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

ahueck / llvm-dimeta / 23296997048

19 Mar 2026 01:23PM UTC coverage: 64.594% (-9.0%) from 73.626%
23296997048

Pull #49

github

web-flow
Merge f45516abb into cefb3414f
Pull Request #49: Initial Fortran Support

1854 of 3670 branches covered (50.52%)

Branch coverage included in aggregate %.

164 of 435 new or added lines in 15 files covered. (37.7%)

20 existing lines in 6 files now uncovered.

2333 of 2812 relevant lines covered (82.97%)

11668.39 hits per line

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

64.74
/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 "DIUtil.h"
14
#include "DataflowAnalysis.h"
15
#include "DefUseAnalysis.h"
16
#include "DimetaData.h"
17
#include "DimetaParse.h"
18
#include "MemoryOps.h"
19
#include "Util.h"
20
#include "ValuePath.h"
21
#include "support/Logger.h"
22

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

46
#include <cassert>
47
#include <iterator>
48
#include <optional>
49
#include <string>
50

51
namespace llvm {
52
class DbgVariableIntrinsic;
53
}  // namespace llvm
54

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

60
namespace dimeta {
61

62
namespace fortran {
63
struct FortranType {
64
  llvm::DIType* type{nullptr};
65
  std::optional<ShapeData> shape_argument;
66
};
67
std::optional<FortranType> di_type_for(const llvm::Value* value, const llvm::CallBase* call = nullptr);
68
}  // namespace fortran
69

70
namespace experimental {
71
std::optional<llvm::DIType*> di_type_for(const llvm::Value* value);
72
}
73

74
llvm::SmallVector<llvm::DIType*, 4> collect_types(const llvm::CallBase* call,
4,028✔
75
                                                  llvm::ArrayRef<dataflow::ValuePath> paths_to_type) {
76
  using namespace llvm;
77
  SmallVector<llvm::DIType*, 4> di_types;
4,028✔
78
  llvm::transform(paths_to_type, dimeta::util::optional_back_inserter(di_types),
4,028!
79
                  [&](const auto& path) { return type::find_type(dataflow::CallValuePath{call, path}); });
8,204!
80
  return di_types;
4,028✔
81
}
4,028!
82

83
auto final_ditype(std::optional<llvm::DIType*> root_ditype) -> std::pair<std::optional<llvm::DIType*>, int> {
14,610✔
84
  if (!root_ditype) {
14,610✔
85
    return {{}, 0};
30✔
86
  }
87
  int level{0};
14,580✔
88
  llvm::DIType* type = *root_ditype;
14,580✔
89
  while (llvm::isa<llvm::DIDerivedType>(type)) {
28,252✔
90
    auto ditype = llvm::dyn_cast<llvm::DIDerivedType>(type);
13,860✔
91
    if (di::util::is_pointer(*ditype, false)) {
13,860✔
92
      ++level;
9,102✔
93
    }
9,102✔
94
    // void*-based derived types have basetype=null:
95
    if (ditype->getBaseType() == nullptr) {
13,860✔
96
      return {type, level};
188✔
97
    }
98
    type = ditype->getBaseType();
13,672✔
99
  }
13,860✔
100

101
  return {type, level};
14,392✔
102
}
14,610✔
103

104
std::optional<llvm::DIType*> type_for_malloclike(const llvm::CallBase* call) {
3,872✔
105
  auto local = difinder::get_array_access_assignment(call);
3,872✔
106
  if (local) {
3,872✔
107
    LOG_DEBUG("Call has local variable " << *call)
108
    // LOG_DEBUG("Call type " << log::ditype_str(local.value()->getType()))
109
    auto base_type = local.value().var->getType();
168✔
110
    if (local.value().array_access) {
168✔
111
      if (auto* array_type = llvm::dyn_cast<llvm::DICompositeType>(base_type)) {
168!
112
        LOG_DEBUG("Returning type of access to array " << log::ditype_str(array_type))
113
        return array_type->getBaseType();
84✔
114
      }
115
    }
×
116
    return base_type;
84✔
117
  }
168✔
118

119
  const auto ditype_paths = dataflow::type_for_heap_call(call);
3,704✔
120

121
  LOG_DEBUG("Found paths, now collecting types")
122
  const auto ditypes_vector = collect_types(call, ditype_paths);
3,704!
123
  if (ditypes_vector.empty()) {
3,704!
124
    return {};
30✔
125
  }
126
  return *ditypes_vector.begin();
3,674!
127
}
3,872✔
128

129
std::optional<llvm::DIType*> type_for_newlike(const llvm::CallBase* call) {
868✔
130
  auto* heapalloc_md = call->getMetadata("heapallocsite");
868✔
131
  assert(heapalloc_md != nullptr && "Missing required heapallocsite metadata.");
1,736!
132
  if (auto* type = llvm::dyn_cast<llvm::DIType>(heapalloc_md)) {
1,736!
133
    //    util::DIPrinter printer(llvm::outs(), call->getParent()->getParent()->getParent());
134
    //    printer.traverseType(type);
135
    //    llvm::dbgs() << "Final Type: " << *type << "\n";
136
    return type;
868✔
137
  }
138
  return {};
×
139
}
868✔
140

141
std::optional<DimetaData> type_for(const llvm::CallBase* call) {
21,992✔
142
  using namespace llvm;
143
  const dimeta::memory::MemOps mem_ops;
21,992✔
144

145
  auto* cb_fun = call->getCalledFunction();
21,992!
146
  if (!cb_fun) {
21,992!
147
    return {};
8✔
148
  }
149

150
  if (!mem_ops.isAlloc(cb_fun->getName())) {
21,984!
151
    LOG_TRACE("Skipping call base: " << cb_fun->getName());
152
    return {};
17,074✔
153
  }
154

155
  std::optional<llvm::DIType*> extracted_type{};
4,910✔
156
  std::optional<ShapeData> shape_type{};
4,910✔
157
  int pointer_level_offset{0};
4,910✔
158

159
  const auto is_fortran_like = mem_ops.isFortranLike(cb_fun->getName());
4,910!
160
  if (is_fortran_like) {
4,910!
161
    LOG_DEBUG("Type for fortran-like " << cb_fun->getName())
NEW
162
    auto fortran_type = fortran::di_type_for(call->getOperand(0), call);
×
NEW
163
    if (fortran_type) {
×
NEW
164
      extracted_type = fortran_type->type;
×
NEW
165
      if (fortran_type->shape_argument) {
×
NEW
166
        shape_type = fortran_type->shape_argument;
×
NEW
167
      }
×
NEW
168
    }
×
NEW
169
  }
×
170

171
  const auto is_cuda_like = mem_ops.isCudaLike(cb_fun->getName());
4,910!
172
  if (is_cuda_like) {
4,910✔
173
    LOG_DEBUG("Type for cuda-like " << cb_fun->getName())
174
    extracted_type = experimental::di_type_for(call->getOperand(0));
170!
175

176
    // when wrapped in, e.g., cudaMalloc<float>(float**, ...), we remove one pointer level:
177
    // auto* parent    = call->getFunction();
178
    // const auto name = std::string{cb_fun->getName()} + "<";
179
    // LOG_DEBUG(name << " vs. " << util::try_demangle(*makeparent))
180
    // if (extracted_type && util::try_demangle(*parent).find(name) != std::string::npos) {
181
    //   LOG_DEBUG("Reset cuda-like pointer level")
182
    //   auto ditype = llvm::dyn_cast<llvm::DIDerivedType>(extracted_type.value());
183
    //   if (ditype->getTag() == llvm::dwarf::DW_TAG_pointer_type) {
184
    //     extracted_type = ditype->getBaseType();
185
    //   }
186
    // }
187
  }
170✔
188

189
  const auto is_cxx_new = mem_ops.isNewLike(cb_fun->getName());
4,910!
190

191
#ifdef DIMETA_USE_HEAPALLOCSITE
192
  if (is_cxx_new) {
4,910✔
193
    if (call->getMetadata("heapallocsite")) {
910!
194
      LOG_TRACE("Type for new-like " << cb_fun->getName())
195
      extracted_type = type_for_newlike(call);
868!
196
      // !heapallocsite gives the type after "new", i.e., new int -> int, new int*[n] -> int*.
197
      // Our malloc-related algorithm would return int* and int** respectively, however, hence:
198
      pointer_level_offset += 1;
868✔
199
    } else {
868✔
200
      LOG_DEBUG("new-like allocation does not have heapallocsite metadata.")
201
    }
202
  }
910✔
203
#endif
204

205
  if (!extracted_type) {
4,910✔
206
    LOG_DEBUG("Type for malloc-like: " << cb_fun->getName())
207
    extracted_type = type_for_malloclike(call);
3,872!
208
  }
3,872✔
209
  auto source_loc                        = difinder::find_location(call);
4,910!
210
  const auto [final_type, pointer_level] = final_ditype(extracted_type);
4,910!
211
  const auto meta =
4,910✔
212
      DimetaData{DimetaData::MemLoc::kHeap,           {}, extracted_type, final_type, source_loc, shape_type,
8,956!
213
                 pointer_level + pointer_level_offset};
4,910✔
214
  return meta;
4,910!
215
}
21,992✔
216

217
std::optional<DimetaData> type_for(const llvm::AllocaInst* ai) {
9,169✔
218
  const auto local_di_var = difinder::find_local_variable(ai);
9,169✔
219

220
  const auto passed = dataflow::fortran::passed_to_fortran_helper(ai);
9,169✔
221
  if (passed) {
9,169!
222
    LOG_DEBUG("Skip allocation passed to Flang intrinsic")
NEW
223
    return {};
×
224
  }
225

226
  if (local_di_var) {
9,169✔
227
    auto extracted_type                    = local_di_var.value()->getType();
8,886✔
228
    auto source_loc                        = difinder::find_location(ai);
8,886✔
229
    const auto [final_type, pointer_level] = final_ditype(extracted_type);
23,522✔
230
    const auto meta =
8,886✔
231
        DimetaData{DimetaData::MemLoc::kStack, local_di_var, extracted_type, final_type, source_loc, {}, pointer_level};
23,522✔
232
    return meta;
8,886!
233
  }
8,886✔
234

235
  LOG_DEBUG("No local_variable for " << *ai)
236

237
  return {};
283✔
238
}
9,169✔
239

240
std::optional<DimetaData> type_for(const llvm::GlobalVariable* gv) {
1,261✔
241
  llvm::SmallVector<llvm::DIGlobalVariableExpression*, 2> dbg_info;
1,261✔
242
  gv->getDebugInfo(dbg_info);
1,261!
243
  if (!dbg_info.empty()) {
1,261!
244
    auto gv_expr                           = *dbg_info.begin();
814!
245
    auto gv_type                           = gv_expr->getVariable()->getType();
814!
246
    const auto [final_type, pointer_level] = final_ditype(gv_type);
814!
247
    return DimetaData{DimetaData::MemLoc::kGlobal, gv_expr->getVariable(), gv_type, final_type, {}, {}, pointer_level};
814!
248
  }
814✔
249
  return {};
447✔
250
}
1,261✔
251

252
std::optional<CompileUnitTypeList> compile_unit_types(const llvm::Module* module) {
2,497✔
253
  CompileUnitTypeList list;
2,497✔
254
  for (auto* compile_unit : module->debug_compile_units()) {
4,979!
255
    CompileUnitTypes current_cu;
2,482✔
256
    current_cu.name = compile_unit->getFilename();
2,482!
257
    for (auto* retained_type : compile_unit->getRetainedTypes()) {
4,178!
258
      if (auto* type = llvm::dyn_cast<llvm::DIType>(retained_type)) {
3,392!
259
        auto dimeta_result = parser::make_dimetadata(type);
1,696!
260
        if (!dimeta_result) {
1,696!
261
          continue;
×
262
        }
263
        current_cu.types.push_back(dimeta_result->type_);
1,696!
264
      }
1,696!
265
    }
1,696!
266
    list.push_back(current_cu);
2,482!
267
  }
2,482✔
268
  return (list.empty() ? std::optional<CompileUnitTypeList>{} : list);
2,497!
269
}
2,497✔
270

271
namespace fortran {
NEW
272
std::optional<FortranType> di_type_for(const llvm::Value* value, const llvm::CallBase* call) {
×
NEW
273
  assert(value != nullptr);
×
NEW
274
  auto shape = dataflow::fortran::shape_from_value(value);
×
275

NEW
276
  auto paths                = dataflow::experimental::path_from_value(value);
×
NEW
277
  const auto ditypes_vector = collect_types(call, paths);
×
NEW
278
  if (ditypes_vector.empty()) {
×
NEW
279
    return {};
×
280
  }
281

NEW
282
  return FortranType{*ditypes_vector.begin(), shape};
×
NEW
283
}
×
284
}  // namespace fortran
285

286
namespace experimental {
287
std::optional<llvm::DIType*> di_type_for(const llvm::Value* value) {
170✔
288
  auto paths                = dataflow::experimental::path_from_value(value);
170✔
289
  const auto ditypes_vector = collect_types(nullptr, paths);
170!
290
  if (ditypes_vector.empty()) {
170!
291
    return {};
×
292
  }
293

294
  return *ditypes_vector.begin();
170!
295
}
170✔
296

297
std::optional<QualifiedType> type_for(const llvm::Value* value) {
154✔
298
  auto paths                = dataflow::experimental::path_from_value(value);
154✔
299
  const auto ditypes_vector = collect_types(nullptr, paths);
154!
300
  if (ditypes_vector.empty()) {
154!
301
    return {};
×
302
  }
303

304
  for (const auto& type : ditypes_vector) {
308!
305
    auto dimeta_result = parser::make_dimetadata(type);
154!
306
    if (!dimeta_result) {
154!
307
      continue;
×
308
    }
309
    return dimeta_result->type_;
154!
310
  }
154!
311

312
  return {};
×
313
}
154✔
314

315
}  // namespace experimental
316

317
}  // 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