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

tudasc / TypeART / 25175874405

30 Apr 2026 04:03PM UTC coverage: 89.231% (-1.0%) from 90.246%
25175874405

Pull #188

github

web-flow
Merge 61645e210 into 278119205
Pull Request #188: GPU memory allocation support

210 of 297 new or added lines in 20 files covered. (70.71%)

4 existing lines in 3 files now uncovered.

5071 of 5683 relevant lines covered (89.23%)

33116.84 hits per line

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

85.25
/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"
1,672✔
35

1,672✔
36
#include "llvm/ADT/DenseMap.h"
1,672✔
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"
1,672✔
48
#include "llvm/Passes/PassBuilder.h"
1,696✔
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>
5,016✔
63
#include <memory>
1,672✔
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(
4,428✔
78
    "typeart-config", cl::init(""),
4,428✔
79
    cl::desc(
4,428✔
80
        "Location of the configuration file to configure the TypeART pass. Commandline arguments are prioritized."),
4,428✔
81
    cl::cat(typeart_category));
8,856✔
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,938✔
93
  if (!cl_typeart_configuration_file.empty()) {
4,938✔
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,938✔
98
  if (config_file != nullptr) {
4,938✔
99
    LOG_DEBUG("Using env var for types file " << config_file)
100
    return std::string{config_file};
30✔
101
  }
102
  LOG_INFO("No configuration file set.")
4,908✔
103
  return {};
4,908✔
104
}
4,938✔
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 {
138,278✔
117
    return *pass_config;
138,278✔
118
  }
119

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

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

129
    const auto init = config_file_path.has_value()
4,932✔
130
                          ? config::TypeARTConfigInit{config_file_path.value()}
30✔
131
                          : config::TypeARTConfigInit{{}, config::TypeARTConfigInit::FileConfigurationMode::Empty};
3,290✔
132

133
    auto typeart_config = [&](const auto& init_value) {
8,252✔
134
      if (init_value.mode == config::TypeARTConfigInit::FileConfigurationMode::Empty) {
4,932✔
135
        return config::make_typeart_configuration_from_opts(pass_opts.value_or(config::TypeARTConfigOptions{}));
19,608✔
136
      }
137
      return config::make_typeart_configuration(init_value);
30✔
138
    }(init);
4,932✔
139

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

148
    meminst_finder = analysis::create_finder(configuration());
3,314✔
149

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

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

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

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

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

178
      auto arg_collector =
179
          std::make_unique<MemOpArgCollector>(configuration(), typeManager.get(), instrumentation_helper);
4,926✔
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,926✔
182
      auto mem_instrument = std::make_unique<MemOpInstrumentation>(configuration(), functions.get(),
9,852✔
183
                                                                   instrumentation_helper, std::move(cb_provider));
4,926✔
184

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

191
  bool doFinalization() {
4,926✔
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,926✔
197
    TypeSerializationImplementation mode = configuration()[config::ConfigStdArgs::type_serialization];
4,926✔
198
    if (emit_type_file_always || mode == TypeSerializationImplementation::FILE) {
3,230✔
199
      const std::string types_file = configuration()[config::ConfigStdArgs::types];
2,152✔
200
      LOG_DEBUG("Writing type file to " << types_file);
201

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

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

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

234
  const auto get_ta_mode = [&]() {
9,744✔
235
    const bool heap   = configuration()[config::ConfigStdArgs::heap];
4,872✔
236
    const bool stack  = configuration()[config::ConfigStdArgs::stack];
4,872✔
237
    const bool global = configuration()[config::ConfigStdArgs::global];
4,872✔
238

239
    if (heap) {
4,872✔
240
      if (stack) {
3,335✔
241
        return " [Heap & Stack]";
837✔
242
      }
243
      return " [Heap]";
2,498✔
244
    }
245

246
    if (stack) {
1,537✔
247
      return " [Stack]";
1,537✔
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
  };
4,872✔
258

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

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

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

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

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

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

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

316
  const auto instrumented_function = llvm::count_if(m.functions(), [&](auto& f) {
167,158✔
317
                                       if (tor_funcs.contains(&f)) {
157,306✔
318
                                         LOG_DEBUG("Function is in LLVM global ctor or dtor " << f.getName())
319
                                         return false;
1,256✔
320
                                       }
321
                                       return runOnFunc(f);
156,050✔
322
                                     }) > 0;
162,232✔
323
  return instrumented_function || globals_were_instrumented;
4,926✔
324
}
4,926✔
325

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

329
  if (f.isDeclaration() || util::starts_with_any_of(f.getName(), "__typeart", "typeart", "__sanitizer", "__tysan")) {
156,050✔
330
    return false;
124,851✔
331
  }
332

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

337
  if (!meminst_finder->hasFunctionData(f)) {
31,199✔
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};
31,199✔
349
//  auto& c = f.getContext();
350
#if LLVM_VERSION_MAJOR > 19
351
  DataLayout dl(f.getParent()->getDataLayout());
9,968✔
352
#else
353
  DataLayout dl(f.getParent());
21,231✔
354
#endif
355

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

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

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

369
    NumInstrumentedMallocs += heap_count;
21,680✔
370
    NumInstrumentedFrees += free_count;
21,680✔
371

372
    mod |= heap_count > 0 || free_count > 0;
21,680✔
373
  }
21,680✔
374

375
  if (instrument_stack) {
31,199✔
376
    const auto stack_count = instrumentation_context->handleStack(allocas);
11,834✔
377
    NumInstrumentedAlloca += stack_count;
11,834✔
378
    mod |= stack_count > 0;
11,834✔
379
  }
11,834✔
380

381
  return mod;
31,199✔
382
}
156,050✔
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() {
4,428✔
424
  using namespace llvm;
425
  return {
8,856✔
426
    LLVM_PLUGIN_API_VERSION, "TypeART", LLVM_VERSION_STRING, [](PassBuilder& pass_builder) {
8,856✔
427
      pass_builder.registerPipelineStartEPCallback([](auto& MPM, OptimizationLevel) {
4,938✔
428
        auto parameters = typeart::util::pass::parsePassParameters(
510✔
429
            typeart::config::pass::parse_typeart_config, "typeart<heap;stats;type-serialization=hybrid>", "typeart");
510✔
430
        if (!parameters) {
510✔
431
          LOG_FATAL("Error parsing heap params: " << parameters.takeError())
×
432
          return;
×
433
        }
434
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
510✔
435
      });
510✔
436
#if LLVM_VERSION_MAJOR > 19
437
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel, ThinOrFullLTOPhase) {
1,618✔
438
#else
439
      pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel) {
3,320✔
440
#endif
441
        auto parameters = typeart::util::pass::parsePassParameters(
510✔
442
            typeart::config::pass::parse_typeart_config, "typeart<no-heap;stack;stats;type-serialization=hybrid>",
510✔
443
            "typeart");
510✔
444
        if (!parameters) {
510✔
445
          LOG_FATAL("Error parsing stack params: " << parameters.takeError())
×
446
          return;
×
447
        }
448
        MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
510✔
449
      });
510✔
450
      pass_builder.registerPipelineParsingCallback([](StringRef name, ModulePassManager& module_pm,
12,264✔
451
                                                      ArrayRef<PassBuilder::PipelineElement>) {
452
        if (typeart::util::pass::checkParametrizedPassName(name, "typeart")) {
7,836✔
453
          auto parameters =
454
              typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config, name, "typeart");
7,836✔
455
          if (!parameters) {
7,836✔
456
            LOG_FATAL("Error parsing params: " << parameters.takeError())
×
457
            return false;
×
458
          }
459
          module_pm.addPass(typeart::pass::TypeArtPass(parameters.get()));
7,836✔
460
          return true;
7,836✔
461
        }
7,836✔
462
        LOG_FATAL("Not a valid parametrized pass name: " << name)
×
463
        return false;
×
464
      });
7,836✔
465
    }
4,428✔
466
  };
467
}
468

469
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
4,428✔
470
  return getTypeartPassPluginInfo();
4,428✔
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
4,428✔
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