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

tudasc / TypeART / 25177145272

30 Apr 2026 04:30PM UTC coverage: 84.647% (-5.6%) from 90.246%
25177145272

Pull #188

github

web-flow
Merge 88918912c into 278119205
Pull Request #188: GPU memory allocation support

127 of 259 new or added lines in 19 files covered. (49.03%)

200 existing lines in 18 files now uncovered.

4510 of 5328 relevant lines covered (84.65%)

27776.73 hits per line

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

82.7
/lib/passes/TypeARTPass.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2026 TypeART Authors
4
// Distributed under the BSD 3-Clause license.
5
// (See accompanying file LICENSE.txt or copy at
6
// https://opensource.org/licenses/BSD-3-Clause)
7
//
8
// Project home: https://github.com/tudasc/TypeART
9
//
10
// SPDX-License-Identifier: BSD-3-Clause
11
//
12

13
#include "Commandline.h"
14
#include "TypeARTConfiguration.h"
15
#include "analysis/MemInstFinder.h"
16
#include "configuration/Configuration.h"
17
#include "configuration/EnvironmentConfiguration.h"
18
#include "configuration/FileConfiguration.h"
19
#include "configuration/PassBuilderUtil.h"
20
#include "configuration/PassConfiguration.h"
21
#include "configuration/TypeARTOptions.h"
22
#include "instrumentation/CallBackFunctionInserter.h"
23
#include "instrumentation/MemOpArgCollector.h"
24
#include "instrumentation/MemOpInstrumentation.h"
25
#include "instrumentation/TypeARTFunctions.h"
26
#include "instrumentation/TypeIDProvider.h"
27
#include "support/ConfigurationBase.h"
28
#include "support/CudaUtil.h"
29
#include "support/GpuUtil.h"
30
#include "support/Logger.h"
31
#include "support/ModuleDumper.h"
32
#include "support/Table.h"
33
#include "support/Util.h"
34
#include "typegen/TypeGenerator.h"
35

36
#include "llvm/ADT/DenseMap.h"
37
#include "llvm/ADT/STLExtras.h"
38
#include "llvm/ADT/ScopeExit.h"
39
#include "llvm/ADT/SmallVector.h"
40
#include "llvm/ADT/Statistic.h"
41
#include "llvm/ADT/StringRef.h"
42
#include "llvm/IR/DataLayout.h"
43
#include "llvm/IR/Function.h"
44
#include "llvm/IR/LegacyPassManager.h"
45
#include "llvm/IR/Module.h"
46
#include "llvm/IR/PassManager.h"
47
#include "llvm/Pass.h"
48
#include "llvm/Passes/PassBuilder.h"
49
#if LLVM_VERSION_MAJOR < 22
50
#include "llvm/Passes/PassPlugin.h"
51
#else
52
#include "llvm/Plugins/PassPlugin.h"
53
#endif
54
#include "llvm/Support/CommandLine.h"
55
#include "llvm/Support/raw_ostream.h"
56

57
#include <cassert>
58
#include <cstddef>
59
#include <llvm/ADT/DenseSet.h>
60
#include <llvm/Config/llvm-config.h>
61
#include <llvm/IR/Constant.h>
62
#include <llvm/Support/Error.h>
63
#include <memory>
64
#include <optional>
65
#include <sstream>
66
#include <string>
67
#include <utility>
68

69
namespace llvm {
70
class BasicBlock;
71
}  // namespace llvm
72

73
using namespace llvm;
74

75
extern llvm::cl::OptionCategory typeart_category;
76

77
static cl::opt<std::string> cl_typeart_configuration_file(
3,577✔
78
    "typeart-config", cl::init(""),
3,577✔
79
    cl::desc(
3,577✔
80
        "Location of the configuration file to configure the TypeART pass. Commandline arguments are prioritized."),
3,577✔
81
    cl::cat(typeart_category));
7,154✔
82

83
#define DEBUG_TYPE "typeart"
84

85
ALWAYS_ENABLED_STATISTIC(NumInstrumentedMallocs, "Number of instrumented mallocs");
86
ALWAYS_ENABLED_STATISTIC(NumInstrumentedFrees, "Number of instrumented frees");
87
ALWAYS_ENABLED_STATISTIC(NumInstrumentedAlloca, "Number of instrumented (stack) allocas");
88
ALWAYS_ENABLED_STATISTIC(NumInstrumentedGlobal, "Number of instrumented globals");
89

90
namespace typeart::pass {
91

92
std::optional<std::string> get_configuration_file_path() {
4,042✔
93
  if (!cl_typeart_configuration_file.empty()) {
4,042✔
94
    LOG_DEBUG("Using cl::opt for config file " << cl_typeart_configuration_file.getValue());
95
    return cl_typeart_configuration_file.getValue();
×
96
  }
97
  const char* config_file = std::getenv("TYPEART_CONFIG_FILE");
4,042✔
98
  if (config_file != nullptr) {
4,042✔
99
    LOG_DEBUG("Using env var for types file " << config_file)
UNCOV
100
    return std::string{config_file};
×
101
  }
102
  LOG_INFO("No configuration file set.")
4,042✔
103
  return {};
4,042✔
104
}
4,042✔
105

106
class TypeArtPass : public llvm::PassInfoMixin<TypeArtPass> {
107
  std::optional<config::TypeARTConfigOptions> pass_opts{std::nullopt};
×
108
  std::unique_ptr<config::Configuration> pass_config;
109

110
  std::unique_ptr<analysis::MemInstFinder> meminst_finder;
111
  std::unique_ptr<TypeGenerator> typeManager;
112
  InstrumentationHelper instrumentation_helper;
113
  std::unique_ptr<TAFunctionQuery> functions;
114
  std::unique_ptr<InstrumentationContext> instrumentation_context;
115

116
  const config::Configuration& configuration() const {
110,899✔
117
    return *pass_config;
110,899✔
118
  }
119

120
 public:
121
  TypeArtPass() = default;
×
122
  explicit TypeArtPass(config::TypeARTConfigOptions opts) : pass_opts(opts) {
7,154✔
123
    // LOG_INFO("Created with \n" << opts)
124
  }
7,154✔
125

126
  bool doInitialization(Module& m) {
4,042✔
127
    auto config_file_path = get_configuration_file_path();
4,042✔
128

129
    const auto init = config_file_path.has_value()
4,042✔
UNCOV
130
                          ? config::TypeARTConfigInit{config_file_path.value()}
×
131
                          : config::TypeARTConfigInit{{}, config::TypeARTConfigInit::FileConfigurationMode::Empty};
1,618✔
132

133
    auto typeart_config = [&](const auto& init_value) {
5,660✔
134
      if (init_value.mode == config::TypeARTConfigInit::FileConfigurationMode::Empty) {
4,042✔
135
        return config::make_typeart_configuration_from_opts(pass_opts.value_or(config::TypeARTConfigOptions{}));
16,168✔
136
      }
UNCOV
137
      return config::make_typeart_configuration(init_value);
×
138
    }(init);
4,042✔
139

140
    if (typeart_config) {
4,042✔
141
      LOG_INFO("Emitting TypeART configuration content\n" << typeart_config.get()->getOptions())
4,042✔
142
      pass_config = std::move(*typeart_config);
4,042✔
143
    } else {
4,042✔
UNCOV
144
      LOG_FATAL("Could not load TypeART configuration.")
×
UNCOV
145
      std::exit(EXIT_FAILURE);
×
146
    }
147

148
    meminst_finder = analysis::create_finder(configuration());
1,618✔
149

150
    const std::string types_file =
151
        configuration().getValueOr(config::ConfigStdArgs::types, {config::ConfigStdArgValues::types});
1,618✔
152

153
    const TypegenImplementation typesgen_parser =
1,618✔
154
        configuration().getValueOr(config::ConfigStdArgs::typegen, {config::ConfigStdArgValues::typegen});
1,618✔
155
    typeManager = make_typegen(types_file, typesgen_parser);
1,618✔
156

157
    LOG_DEBUG("Propagating type infos.");
158
    const auto [loaded, error] = typeManager->load();
1,618✔
159
    if (loaded) {
4,042✔
160
      LOG_DEBUG("Existing type configuration successfully loaded from " << types_file);
161
    } else {
525✔
162
      LOG_DEBUG("No valid existing type configuration found: " << types_file << ". Reason: " << error.message());
163
    }
164

165
    instrumentation_helper.setModule(m);
1,618✔
166
    ModuleData mdata{&m};
1,618✔
167
    const auto has_cu_types = typeManager->registerModule(mdata);
1,618✔
168

169
    declareInstrumentationFunctions(m);
1,618✔
170
    {
171
      auto type_id_handler = get_type_id_handler(m, &typeManager->getTypeDatabase(), configuration(), functions.get());
4,042✔
172
      // const bool heap   = configuration()[config::ConfigStdArgs::heap];
173
      if (has_cu_types) {
4,042✔
174
        LOG_DEBUG("Registering compilation unit types list")
175
        type_id_handler->registerModule(mdata);
1,566✔
176
      }
1,566✔
177

178
      auto arg_collector =
179
          std::make_unique<MemOpArgCollector>(configuration(), typeManager.get(), instrumentation_helper);
4,042✔
180
      // const bool instrument_stack_lifetime = configuration()[config::ConfigStdArgs::stack_lifetime];
181
      auto cb_provider    = make_callback_inserter(configuration(), std::move(type_id_handler), functions.get());
4,042✔
182
      auto mem_instrument = std::make_unique<MemOpInstrumentation>(configuration(), functions.get(),
8,084✔
183
                                                                   instrumentation_helper, std::move(cb_provider));
4,042✔
184

185
      instrumentation_context =
4,042✔
186
          std::make_unique<InstrumentationContext>(std::move(arg_collector), std::move(mem_instrument));
4,042✔
187
    }
1,618✔
188
    return true;
189
  }
1,618✔
190

191
  bool doFinalization() {
4,042✔
192
    /*
193
     * Persist the accumulated type definition information for this module.
194
     */
195
    // TODO: inline/hybrid types not supported in non-opaque mode
196
    const bool emit_type_file_always     = bool(LLVM_VERSION_MAJOR < 15);
4,042✔
197
    TypeSerializationImplementation mode = configuration()[config::ConfigStdArgs::type_serialization];
4,042✔
198
    if (emit_type_file_always || mode == TypeSerializationImplementation::FILE) {
4,042✔
199
      const std::string types_file = configuration()[config::ConfigStdArgs::types];
570✔
200
      LOG_DEBUG("Writing type file to " << types_file);
201

202
      const auto [stored, error] = typeManager->store();
570✔
203
      if (stored) {
570✔
204
        LOG_DEBUG("Success!");
205
      } else {
570✔
206
        LOG_FATAL("Failed writing type config to " << types_file << ". Reason: " << error.message());
×
207
      }
208
    }
570✔
209
    const bool print_stats = configuration()[config::ConfigStdArgs::stats];
4,042✔
210
    if (print_stats) {
4,042✔
211
      auto& out = llvm::errs();
3,997✔
212
      printStats(out);
3,997✔
213
    }
3,997✔
214
    return false;
4,042✔
215
  }
216

217
  void declareInstrumentationFunctions(Module& m) {
4,042✔
218
    functions = declare_instrumentation_functions(m, configuration());
4,042✔
219
  }
4,042✔
220

221
  void printStats(llvm::raw_ostream& out){
3,997✔
222
#if LLVM_VERSION_MAJOR < 22
223
      const auto scope_exit_cleanup_counter = llvm::make_scope_exit([&]() {
6,400✔
224
#else
225
      llvm::scope_exit scope_exit_cleanup_counter([&]() {
1,594✔
226
#endif
227
        NumInstrumentedAlloca  = 0;
3,997✔
228
        NumInstrumentedFrees   = 0;
3,997✔
229
        NumInstrumentedGlobal  = 0;
3,997✔
230
        NumInstrumentedMallocs = 0;
3,997✔
231
      });
3,997✔
232
  meminst_finder->printStats(out);
3,997✔
233

234
  const auto get_ta_mode = [&]() {
7,994✔
235
    const bool heap   = configuration()[config::ConfigStdArgs::heap];
3,997✔
236
    const bool stack  = configuration()[config::ConfigStdArgs::stack];
3,997✔
237
    const bool global = configuration()[config::ConfigStdArgs::global];
3,997✔
238

239
    if (heap) {
3,997✔
240
      if (stack) {
2,702✔
241
        return " [Heap & Stack]";
597✔
242
      }
243
      return " [Heap]";
2,105✔
244
    }
245

246
    if (stack) {
1,295✔
247
      return " [Stack]";
1,295✔
248
    }
249

250
    if (global) {
×
251
      return " [Global]";
×
252
    }
253

254
    LOG_ERROR("Did not find heap or stack, or combination thereof!");
×
255
    assert((heap || stack || global) && "Needs stack, heap, global or combination thereof");
×
256
    return " [Unknown]";
×
257
  };
3,997✔
258

259
  Table stats("TypeArtPass");
3,997✔
260
  stats.wrap_header_ = true;
3,997✔
261
  stats.title_ += get_ta_mode();
3,997✔
262
  stats.put(Row::make("Malloc", NumInstrumentedMallocs.getValue()));
3,997✔
263
  stats.put(Row::make("Free", NumInstrumentedFrees.getValue()));
3,997✔
264
  stats.put(Row::make("Alloca", NumInstrumentedAlloca.getValue()));
3,997✔
265
  stats.put(Row::make("Global", NumInstrumentedGlobal.getValue()));
3,997✔
266

267
  std::ostringstream stream;
3,997✔
268
  stats.print(stream);
3,997✔
269
  out << stream.str();
3,997✔
270
}
3,997✔
271

272
llvm::PreservedAnalyses
273
run(llvm::Module& m, llvm::ModuleAnalysisManager&) {
4,042✔
274
  if (gpu::is_device_module(m)) {
4,042✔
275
    LOG_DEBUG("Skipping GPU device module: " << m.getName());
NEW
276
    return llvm::PreservedAnalyses::all();
×
277
  }
278
  bool changed{false};
4,042✔
279
  changed |= doInitialization(m);
4,042✔
280
  const bool heap = configuration()[config::ConfigStdArgs::heap];  // Must happen after doInit
4,042✔
281
  dump_module(m, heap ? util::module::ModulePhase::kBase : util::module::ModulePhase::kOpt);
4,042✔
282
  changed |= runOnModule(m);
4,042✔
283
  dump_module(m, heap ? util::module::ModulePhase::kHeap : util::module::ModulePhase::kStack);
4,042✔
284
  changed |= doFinalization();
4,042✔
285
  return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
4,042✔
286
}
4,042✔
287

288
bool runOnModule(llvm::Module& m) {
4,042✔
289
  meminst_finder->runOnModule(m);
4,042✔
290
  const bool instrument_global = configuration()[config::ConfigStdArgs::global];
4,042✔
291
  bool globals_were_instrumented{false};
4,042✔
292
  if (instrument_global) {
4,042✔
293
    // declareInstrumentationFunctions(m);
294

295
    const auto& globalsList = meminst_finder->getModuleGlobals();
1,877✔
296
    if (!globalsList.empty()) {
1,877✔
297
      const auto global_count = instrumentation_context->handleGlobal(globalsList);
740✔
298
      NumInstrumentedGlobal += global_count;
740✔
299
      globals_were_instrumented = global_count > 0;
740✔
300
    }
740✔
301
  }
1,877✔
302

303
  llvm::DenseSet<const llvm::Constant*> tor_funcs;
4,042✔
304
  {
305
    const auto collect_funcs = [&tor_funcs](const auto* constant) -> bool {
5,194✔
306
      if (llvm::isa<llvm::Function>(constant)) {
1,152✔
307
        tor_funcs.insert(constant);
1,152✔
308
      }
1,152✔
309
      return false;
1,152✔
310
    };
311

312
    util::for_each_cdtor("llvm.global_ctors", m, collect_funcs);
4,042✔
313
    util::for_each_cdtor("llvm.global_dtors", m, collect_funcs);
4,042✔
314
  }
315

316
  const auto instrumented_function = llvm::count_if(m.functions(), [&](auto& f) {
135,211✔
317
                                       if (tor_funcs.contains(&f)) {
127,127✔
318
                                         LOG_DEBUG("Function is in LLVM global ctor or dtor " << f.getName())
319
                                         return false;
1,152✔
320
                                       }
321
                                       return runOnFunc(f);
125,975✔
322
                                     }) > 0;
131,169✔
323
  return instrumented_function || globals_were_instrumented;
4,042✔
324
}
4,042✔
325

326
bool runOnFunc(llvm::Function& f) {
125,975✔
327
  using namespace typeart;
328

329
  if (f.isDeclaration() || util::starts_with_any_of(f.getName(), "__typeart", "typeart", "__sanitizer", "__tysan")) {
125,975✔
330
    return false;
101,058✔
331
  }
332

333
  if (cuda::is_cuda_helper_function(f)) {
24,917✔
NEW
334
    return false;
×
335
  }
336

337
  if (!meminst_finder->hasFunctionData(f)) {
24,917✔
338
    LOG_WARNING("No allocation data could be retrieved for function: " << f.getName());
×
339
    return false;
×
340
  }
341

342
  LOG_DEBUG("Running on function: " << f.getName())
343

344
  // FIXME this is required when "PassManagerBuilder::EP_OptimizerLast" is used as the function (constant) pointer are
345
  // nullpointer/invalidated
346
  // declareInstrumentationFunctions(*f.getParent());
347

348
  bool mod{false};
24,917✔
349
//  auto& c = f.getContext();
350
#if LLVM_VERSION_MAJOR > 19
351
  DataLayout dl(f.getParent()->getDataLayout());
14,955✔
352
#else
353
  DataLayout dl(f.getParent());
9,962✔
354
#endif
355

356
  const auto& fData   = meminst_finder->getFunctionData(f);
24,917✔
357
  const auto& mallocs = fData.mallocs;
24,917✔
358
  const auto& allocas = fData.allocas;
24,917✔
359
  const auto& frees   = fData.frees;
24,917✔
360

361
  const bool instrument_heap  = configuration()[config::ConfigStdArgs::heap];
24,917✔
362
  const bool instrument_stack = configuration()[config::ConfigStdArgs::stack];
24,917✔
363

364
  if (instrument_heap) {
24,917✔
365
    // instrument collected calls of bb:
366
    const auto heap_count = instrumentation_context->handleHeap(mallocs);
17,277✔
367
    const auto free_count = instrumentation_context->handleFree(frees);
17,277✔
368

369
    NumInstrumentedMallocs += heap_count;
17,277✔
370
    NumInstrumentedFrees += free_count;
17,277✔
371

372
    mod |= heap_count > 0 || free_count > 0;
17,277✔
373
  }
17,277✔
374

375
  if (instrument_stack) {
24,917✔
376
    const auto stack_count = instrumentation_context->handleStack(allocas);
9,097✔
377
    NumInstrumentedAlloca += stack_count;
9,097✔
378
    mod |= stack_count > 0;
9,097✔
379
  }
9,097✔
380

381
  return mod;
24,917✔
382
}
125,975✔
383
};  // namespace typeart::pass
384

385
class LegacyTypeArtPass : public llvm::ModulePass {
386
 private:
387
  TypeArtPass pass_impl_;
388

389
 public:
390
  static char ID;  // NOLINT
391

392
  LegacyTypeArtPass() : ModulePass(ID) {};
×
393

394
  bool doInitialization(llvm::Module&) override;
395

396
  bool runOnModule(llvm::Module& module) override;
397

398
  bool doFinalization(llvm::Module&) override;
399

400
  ~LegacyTypeArtPass() override = default;
×
401
};
402

403
bool LegacyTypeArtPass::doInitialization(llvm::Module& m) {
×
404
  return pass_impl_.doInitialization(m);
×
405
}
406

407
bool LegacyTypeArtPass::runOnModule(llvm::Module& module) {
×
408
  bool changed{false};
×
409
  changed |= pass_impl_.runOnModule(module);
×
410
  return changed;
×
411
}
412

413
bool LegacyTypeArtPass::doFinalization(llvm::Module&) {
×
414
  return pass_impl_.doFinalization();
×
415
  ;
416
}
417

418
}  // namespace typeart::pass
419

420
//.....................
421
// New PM
422
//.....................
423
llvm::PassPluginLibraryInfo getTypeartPassPluginInfo() {
3,577✔
424
  using namespace llvm;
425
  return {
7,154✔
426
    LLVM_PLUGIN_API_VERSION, "TypeART", LLVM_VERSION_STRING, [](PassBuilder& pass_builder) {
7,154✔
427
      pass_builder.registerPipelineStartEPCallback([](auto& MPM, OptimizationLevel) {
4,042✔
428
        auto parameters = typeart::util::pass::parsePassParameters(
465✔
429
            typeart::config::pass::parse_typeart_config, "typeart<heap;stats;type-serialization=hybrid>", "typeart");
465✔
430
        if (!parameters) {
465✔
431
          LOG_FATAL("Error parsing heap params: " << parameters.takeError())
×
432
          return;
×
433
        }
434
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
465✔
435
      });
465✔
436
#if LLVM_VERSION_MAJOR > 19
437
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel, ThinOrFullLTOPhase) {
2,430✔
438
#else
439
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel) {
1,612✔
440
#endif
441
        auto parameters = typeart::util::pass::parsePassParameters(
465✔
442
            typeart::config::pass::parse_typeart_config, "typeart<no-heap;stack;stats;type-serialization=hybrid>",
465✔
443
            "typeart");
465✔
444
        if (!parameters) {
465✔
445
          LOG_FATAL("Error parsing stack params: " << parameters.takeError())
×
446
          return;
×
447
        }
448
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
465✔
449
      });
465✔
450
      pass_builder.registerPipelineParsingCallback([](StringRef name, ModulePassManager& module_pm,
9,801✔
451
                                                      ArrayRef<PassBuilder::PipelineElement>) {
452
        if (typeart::util::pass::checkParametrizedPassName(name, "typeart")) {
6,224✔
453
          auto parameters =
454
              typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config, name, "typeart");
6,224✔
455
          if (!parameters) {
6,224✔
456
            LOG_FATAL("Error parsing params: " << parameters.takeError())
×
457
            return false;
×
458
          }
459
          module_pm.addPass(typeart::pass::TypeArtPass(parameters.get()));
6,224✔
460
          return true;
6,224✔
461
        }
6,224✔
462
        LOG_FATAL("Not a valid parametrized pass name: " << name)
×
463
        return false;
×
464
      });
6,224✔
465
    }
3,577✔
466
  };
467
}
468

469
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
3,577✔
470
  return getTypeartPassPluginInfo();
3,577✔
471
}
472

473
//.....................
474
// Old PM
475
//.....................
476
char typeart::pass::LegacyTypeArtPass::ID = 0;  // NOLINT
477

478
static RegisterPass<typeart::pass::LegacyTypeArtPass> x("typeart", "TypeArt Pass");  // NOLINT
3,577✔
479

480
ModulePass* createTypeArtPass() {
×
481
  return new typeart::pass::LegacyTypeArtPass();
×
482
}
483

484
extern "C" void AddTypeArtPass(LLVMPassManagerRef pass_manager) {
×
485
  unwrap(pass_manager)->add(createTypeArtPass());
×
486
}
×
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