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

tudasc / TypeART / 13182742139

06 Feb 2025 03:56PM UTC coverage: 88.83%. First build
13182742139

Pull #158

github

web-flow
Merge 52ae5a8ce into a2bf692a1
Pull Request #158: Use -fpass-plugin in new compiler wrapper

63 of 72 new or added lines in 3 files covered. (87.5%)

4159 of 4682 relevant lines covered (88.83%)

178151.46 hits per line

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

83.68
/lib/passes/TypeARTPass.cpp
1
// TypeART library
2
//
3
// Copyright (c) 2017-2025 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/MemOpArgCollector.h"
23
#include "instrumentation/MemOpInstrumentation.h"
24
#include "instrumentation/TypeARTFunctions.h"
25
#include "support/ConfigurationBase.h"
26
#include "support/Logger.h"
27
#include "support/ModuleDumper.h"
28
#include "support/Table.h"
29
#include "support/Util.h"
30
#include "typegen/TypeGenerator.h"
31

32
#include "llvm/ADT/DenseMap.h"
33
#include "llvm/ADT/STLExtras.h"
1,645✔
34
#include "llvm/ADT/ScopeExit.h"
1,645✔
35
#include "llvm/ADT/SmallVector.h"
1,645✔
36
#include "llvm/ADT/Statistic.h"
37
#include "llvm/ADT/StringRef.h"
38
#include "llvm/IR/DataLayout.h"
39
#include "llvm/IR/Function.h"
40
#include "llvm/IR/LegacyPassManager.h"
41
#include "llvm/IR/Module.h"
42
#include "llvm/IR/PassManager.h"
43
#include "llvm/Pass.h"
44
#include "llvm/Passes/PassBuilder.h"
45
#include "llvm/Passes/PassPlugin.h"
46
#include "llvm/Support/CommandLine.h"
1,645✔
47
#include "llvm/Support/raw_ostream.h"
48

49
#include <cassert>
50
#include <cstddef>
51
#include <llvm/Support/Error.h>
52
#include <optional>
53
#include <sstream>
54
#include <string>
55
#include <utility>
4,935✔
56

1,645✔
57
namespace llvm {
58
class BasicBlock;
59
}  // namespace llvm
60

61
using namespace llvm;
62

63
extern llvm::cl::OptionCategory typeart_category;
64

65
static cl::opt<std::string> cl_typeart_configuration_file(
2,137✔
66
    "typeart-config", cl::init(""),
2,137✔
67
    cl::desc(
2,137✔
68
        "Location of the configuration file to configure the TypeART pass. Commandline arguments are prioritized."),
2,137✔
69
    cl::cat(typeart_category));
4,274✔
70

71
#define DEBUG_TYPE "typeart"
72

73
ALWAYS_ENABLED_STATISTIC(NumInstrumentedMallocs, "Number of instrumented mallocs");
74
ALWAYS_ENABLED_STATISTIC(NumInstrumentedFrees, "Number of instrumented frees");
75
ALWAYS_ENABLED_STATISTIC(NumInstrumentedAlloca, "Number of instrumented (stack) allocas");
76
ALWAYS_ENABLED_STATISTIC(NumInstrumentedGlobal, "Number of instrumented globals");
77

78
namespace typeart::pass {
79

80
std::optional<std::string> get_configuration_file_path() {
2,344✔
81
  if (!cl_typeart_configuration_file.empty()) {
2,344✔
82
    LOG_DEBUG("Using cl::opt for config file " << cl_typeart_configuration_file.getValue());
83
    return cl_typeart_configuration_file.getValue();
×
84
  }
85
  const char* config_file = std::getenv("TYPEART_CONFIG_FILE");
2,344✔
86
  if (config_file != nullptr) {
2,344✔
87
    LOG_DEBUG("Using env var for types file " << config_file)
88
    return std::string{config_file};
×
89
  }
90
  LOG_INFO("No configuration file set.")
2,344✔
91
  return {};
2,344✔
92
}
2,344✔
93

94
class TypeArtPass : public llvm::PassInfoMixin<TypeArtPass> {
95
  std::optional<config::TypeARTConfigOptions> pass_opts{std::nullopt};
×
96
  std::unique_ptr<config::Configuration> pass_config;
97

98
  struct TypeArtFunc {
99
    const std::string name;
100
    llvm::Value* f{nullptr};
101
  };
102

103
  TypeArtFunc typeart_alloc{"__typeart_alloc"};
4,274✔
104
  TypeArtFunc typeart_alloc_global{"__typeart_alloc_global"};
4,274✔
105
  TypeArtFunc typeart_alloc_stack{"__typeart_alloc_stack"};
4,274✔
106
  TypeArtFunc typeart_free{"__typeart_free"};
4,274✔
107
  TypeArtFunc typeart_leave_scope{"__typeart_leave_scope"};
4,274✔
108

109
  TypeArtFunc typeart_alloc_omp        = typeart_alloc;
4,274✔
110
  TypeArtFunc typeart_alloc_stacks_omp = typeart_alloc_stack;
4,274✔
111
  TypeArtFunc typeart_free_omp         = typeart_free;
4,274✔
112
  TypeArtFunc typeart_leave_scope_omp  = typeart_leave_scope;
4,274✔
113

114
  std::unique_ptr<analysis::MemInstFinder> meminst_finder;
115
  std::unique_ptr<TypeGenerator> typeManager;
116
  InstrumentationHelper instrumentation_helper;
117
  TAFunctions functions;
118
  std::unique_ptr<InstrumentationContext> instrumentation_context;
119

120
  const config::Configuration& configuration() const {
63,303✔
121
    return *pass_config;
63,303✔
122
  }
123

124
 public:
125
  TypeArtPass() = default;
×
126
  explicit TypeArtPass(config::TypeARTConfigOptions opts) : pass_opts(opts) {
8,548✔
127
    // LOG_INFO("Created with \n" << opts)
128
  }
4,274✔
129

130
  bool doInitialization(Module& m) {
2,338✔
131
    auto config_file_path = get_configuration_file_path();
2,338✔
132

133
    const auto init = config_file_path.has_value()
2,338✔
134
                          ? config::TypeARTConfigInit{config_file_path.value()}
×
135
                          : config::TypeARTConfigInit{{}, config::TypeARTConfigInit::FileConfigurationMode::Empty};
1,645✔
136

137
    auto typeart_config = [&](const auto& init_value) {
3,983✔
138
      if (init_value.mode == config::TypeARTConfigInit::FileConfigurationMode::Empty) {
2,338✔
139
        return config::make_typeart_configuration_from_opts(pass_opts.value_or(config::TypeARTConfigOptions{}));
9,352✔
140
      }
141
      return config::make_typeart_configuration(init_value);
×
142
    }(init);
2,338✔
143

144
    if (typeart_config) {
2,338✔
145
      LOG_INFO("Emitting TypeART configuration content\n" << typeart_config.get()->getOptions())
2,338✔
146
      pass_config = std::move(*typeart_config);
2,338✔
147
    } else {
2,338✔
148
      LOG_FATAL("Could not load TypeART configuration.")
×
149
      std::exit(EXIT_FAILURE);
×
150
    }
151

152
    meminst_finder = analysis::create_finder(configuration());
1,645✔
153

154
    const std::string types_file =
155
        configuration().getValueOr(config::ConfigStdArgs::types, {config::ConfigStdArgValues::types});
1,645✔
156

157
    const TypegenImplementation typesgen_parser =
1,645✔
158
        configuration().getValueOr(config::ConfigStdArgs::typegen, {config::ConfigStdArgValues::typegen});
1,645✔
159
    typeManager = make_typegen(types_file, typesgen_parser);
1,645✔
160

161
    LOG_DEBUG("Propagating type infos.");
162
    const auto [loaded, error] = typeManager->load();
1,645✔
163
    if (loaded) {
2,338✔
164
      LOG_DEBUG("Existing type configuration successfully loaded from " << types_file);
165
    } else {
1,093✔
166
      LOG_DEBUG("No valid existing type configuration found: " << types_file << ". Reason: " << error.message());
167
    }
168

169
    instrumentation_helper.setModule(m);
1,645✔
170
    ModuleData mdata{&m};
1,645✔
171
    typeManager->registerModule(mdata);
1,645✔
172

173
    auto arg_collector =
174
        std::make_unique<MemOpArgCollector>(configuration(), typeManager.get(), instrumentation_helper);
1,645✔
175
    // const bool instrument_stack_lifetime = configuration()[config::ConfigStdArgs::stack_lifetime];
176
    auto mem_instrument = std::make_unique<MemOpInstrumentation>(configuration(), functions, instrumentation_helper);
1,645✔
177
    instrumentation_context =
1,645✔
178
        std::make_unique<InstrumentationContext>(std::move(arg_collector), std::move(mem_instrument));
1,645✔
179

180
    return true;
181
  }
1,645✔
182

183
  bool doFinalization() {
2,338✔
184
    /*
185
     * Persist the accumulated type definition information for this module.
186
     */
187
    const std::string types_file = configuration()[config::ConfigStdArgs::types];
2,338✔
188
    LOG_DEBUG("Writing type file to " << types_file);
189

190
    const auto [stored, error] = typeManager->store();
2,338✔
191
    if (stored) {
2,338✔
192
      LOG_DEBUG("Success!");
193
    } else {
2,338✔
194
      LOG_FATAL("Failed writing type config to " << types_file << ". Reason: " << error.message());
×
195
    }
196

197
    const bool print_stats = configuration()[config::ConfigStdArgs::stats];
2,338✔
198
    if (print_stats) {
2,338✔
199
      auto& out = llvm::errs();
2,311✔
200
      printStats(out);
2,311✔
201
    }
2,311✔
202
    return false;
203
  }
2,338✔
204

205
  void declareInstrumentationFunctions(Module& m) {
18,823✔
206
    // Remove this return if problems come up during compilation
207
    if (typeart_alloc_global.f != nullptr && typeart_alloc_stack.f != nullptr && typeart_alloc.f != nullptr &&
35,308✔
208
        typeart_free.f != nullptr && typeart_leave_scope.f != nullptr) {
16,485✔
209
      return;
16,485✔
210
    }
211

212
    TAFunctionDeclarator decl(m, instrumentation_helper, functions);
2,338✔
213

214
    auto alloc_arg_types      = instrumentation_helper.make_parameters(IType::ptr, IType::type_id, IType::extent);
2,338✔
215
    auto free_arg_types       = instrumentation_helper.make_parameters(IType::ptr);
2,338✔
216
    auto leavescope_arg_types = instrumentation_helper.make_parameters(IType::stack_count);
2,338✔
217

218
    typeart_alloc.f        = decl.make_function(IFunc::heap, typeart_alloc.name, alloc_arg_types);
2,338✔
219
    typeart_alloc_stack.f  = decl.make_function(IFunc::stack, typeart_alloc_stack.name, alloc_arg_types);
2,338✔
220
    typeart_alloc_global.f = decl.make_function(IFunc::global, typeart_alloc_global.name, alloc_arg_types);
2,338✔
221
    typeart_free.f         = decl.make_function(IFunc::free, typeart_free.name, free_arg_types);
2,338✔
222
    typeart_leave_scope.f  = decl.make_function(IFunc::scope, typeart_leave_scope.name, leavescope_arg_types);
2,338✔
223

224
    typeart_alloc_omp.f = decl.make_function(IFunc::heap_omp, typeart_alloc_omp.name, alloc_arg_types, true);
2,338✔
225
    typeart_alloc_stacks_omp.f =
2,338✔
226
        decl.make_function(IFunc::stack_omp, typeart_alloc_stacks_omp.name, alloc_arg_types, true);
2,338✔
227
    typeart_free_omp.f = decl.make_function(IFunc::free_omp, typeart_free_omp.name, free_arg_types, true);
2,338✔
228
    typeart_leave_scope_omp.f =
2,338✔
229
        decl.make_function(IFunc::scope_omp, typeart_leave_scope_omp.name, leavescope_arg_types, true);
2,338✔
230
  }
18,823✔
231

232
  void printStats(llvm::raw_ostream& out) {
2,311✔
233
    const auto scope_exit_cleanup_counter = llvm::make_scope_exit([&]() {
4,622✔
234
      NumInstrumentedAlloca  = 0;
2,311✔
235
      NumInstrumentedFrees   = 0;
2,311✔
236
      NumInstrumentedGlobal  = 0;
2,311✔
237
      NumInstrumentedMallocs = 0;
2,311✔
238
    });
2,311✔
239
    meminst_finder->printStats(out);
2,311✔
240

241
    const auto get_ta_mode = [&]() {
4,622✔
242
      const bool heap   = configuration()[config::ConfigStdArgs::heap];
2,311✔
243
      const bool stack  = configuration()[config::ConfigStdArgs::stack];
2,311✔
244
      const bool global = configuration()[config::ConfigStdArgs::global];
2,311✔
245

246
      if (heap) {
2,311✔
247
        if (stack) {
1,581✔
248
          return " [Heap & Stack]";
402✔
249
        }
250
        return " [Heap]";
1,179✔
251
      }
252

253
      if (stack) {
730✔
254
        return " [Stack]";
730✔
255
      }
256

257
      if (global) {
×
258
        return " [Global]";
×
259
      }
260

261
      LOG_ERROR("Did not find heap or stack, or combination thereof!");
×
262
      assert((heap || stack || global) && "Needs stack, heap, global or combination thereof");
×
263
      return " [Unknown]";
×
264
    };
2,311✔
265

266
    Table stats("TypeArtPass");
2,311✔
267
    stats.wrap_header_ = true;
2,311✔
268
    stats.title_ += get_ta_mode();
2,311✔
269
    stats.put(Row::make("Malloc", NumInstrumentedMallocs.getValue()));
2,311✔
270
    stats.put(Row::make("Free", NumInstrumentedFrees.getValue()));
2,311✔
271
    stats.put(Row::make("Alloca", NumInstrumentedAlloca.getValue()));
2,311✔
272
    stats.put(Row::make("Global", NumInstrumentedGlobal.getValue()));
2,311✔
273

274
    std::ostringstream stream;
2,311✔
275
    stats.print(stream);
2,311✔
276
    out << stream.str();
2,311✔
277
  }
2,311✔
278

279
  llvm::PreservedAnalyses run(llvm::Module& m, llvm::ModuleAnalysisManager&) {
2,338✔
280
    bool changed{false};
2,338✔
281
    changed |= doInitialization(m);
2,338✔
282
    const bool heap = configuration()[config::ConfigStdArgs::heap];  // Must happen after doInit
2,338✔
283
    dump_module(m, heap ? util::module::ModulePhase::kBase : util::module::ModulePhase::kOpt);
2,338✔
284
    changed |= runOnModule(m);
2,338✔
285
    dump_module(m, heap ? util::module::ModulePhase::kHeap : util::module::ModulePhase::kStack);
2,338✔
286
    changed |= doFinalization();
2,338✔
287
    return changed ? llvm::PreservedAnalyses::none() : llvm::PreservedAnalyses::all();
2,338✔
288
  }
289

290
  bool runOnModule(llvm::Module& m) {
2,338✔
291
    meminst_finder->runOnModule(m);
2,338✔
292
    const bool instrument_global = configuration()[config::ConfigStdArgs::global];
2,338✔
293
    bool globals_were_instrumented{false};
2,338✔
294
    if (instrument_global) {
2,338✔
295
      declareInstrumentationFunctions(m);
1,159✔
296

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

305
    const auto instrumented_function = llvm::count_if(m.functions(), [&](auto& f) { return runOnFunc(f); }) > 0;
60,597✔
306
    return instrumented_function || globals_were_instrumented;
2,338✔
307
  }
308

309
  bool runOnFunc(llvm::Function& f) {
58,259✔
310
    using namespace typeart;
311

312
    if (f.isDeclaration() || util::starts_with_any_of(f.getName(), "__typeart")) {
58,259✔
313
      return false;
40,595✔
314
    }
315

316
    if (!meminst_finder->hasFunctionData(f)) {
17,664✔
317
      LOG_WARNING("No allocation data could be retrieved for function: " << f.getName());
×
318
      return false;
×
319
    }
320

321
    LOG_DEBUG("Running on function: " << f.getName())
322

323
    // FIXME this is required when "PassManagerBuilder::EP_OptimizerLast" is used as the function (constant) pointer are
324
    // nullpointer/invalidated
325
    declareInstrumentationFunctions(*f.getParent());
17,664✔
326

327
    bool mod{false};
17,664✔
328
    //  auto& c = f.getContext();
329
    DataLayout dl(f.getParent());
17,664✔
330

331
    llvm::SmallDenseMap<BasicBlock*, size_t> allocCounts;
17,664✔
332

333
    const auto& fData   = meminst_finder->getFunctionData(f);
17,664✔
334
    const auto& mallocs = fData.mallocs;
17,664✔
335
    const auto& allocas = fData.allocas;
17,664✔
336
    const auto& frees   = fData.frees;
17,664✔
337

338
    const bool instrument_heap  = configuration()[config::ConfigStdArgs::heap];
17,664✔
339
    const bool instrument_stack = configuration()[config::ConfigStdArgs::stack];
17,664✔
340

341
    if (instrument_heap) {
17,664✔
342
      // instrument collected calls of bb:
343
      const auto heap_count = instrumentation_context->handleHeap(mallocs);
12,459✔
344
      const auto free_count = instrumentation_context->handleFree(frees);
12,459✔
345

346
      NumInstrumentedMallocs += heap_count;
12,459✔
347
      NumInstrumentedFrees += free_count;
12,459✔
348

349
      mod |= heap_count > 0 || free_count > 0;
12,459✔
350
    }
12,459✔
351

352
    if (instrument_stack) {
17,664✔
353
      const auto stack_count = instrumentation_context->handleStack(allocas);
6,497✔
354
      NumInstrumentedAlloca += stack_count;
6,497✔
355
      mod |= stack_count > 0;
6,497✔
356
    }
6,497✔
357

358
    return mod;
17,664✔
359
  }
58,259✔
360
};
361

362
class LegacyTypeArtPass : public llvm::ModulePass {
363
 private:
364
  TypeArtPass pass_impl_;
365

366
 public:
367
  static char ID;  // NOLINT
368

369
  LegacyTypeArtPass() : ModulePass(ID){};
×
370

371
  bool doInitialization(llvm::Module&) override;
372

373
  bool runOnModule(llvm::Module& module) override;
374

375
  bool doFinalization(llvm::Module&) override;
376

377
  ~LegacyTypeArtPass() override = default;
×
378
};
379

380
bool LegacyTypeArtPass::doInitialization(llvm::Module& m) {
×
381
  return pass_impl_.doInitialization(m);
×
382
}
383

384
bool LegacyTypeArtPass::runOnModule(llvm::Module& module) {
×
385
  bool changed{false};
×
386
  changed |= pass_impl_.runOnModule(module);
×
387
  return changed;
×
388
}
389

390
bool LegacyTypeArtPass::doFinalization(llvm::Module&) {
×
391
  return pass_impl_.doFinalization();
×
392
  ;
393
}
394

395
}  // namespace typeart::pass
396

397
//.....................
398
// New PM
399
//.....................
400
llvm::PassPluginLibraryInfo getTypeartPassPluginInfo() {
2,137✔
401
  using namespace llvm;
402
  return {LLVM_PLUGIN_API_VERSION, "TypeART", LLVM_VERSION_STRING, [](PassBuilder& pass_builder) {
4,274✔
403
            pass_builder.registerPipelineStartEPCallback([](auto& MPM, OptimizationLevel) {
2,344✔
404
              auto parameters = typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config,
207✔
405
                                                                         "typeart<heap;stats>", "typeart");
207✔
406
              if (!parameters) {
207✔
NEW
407
                LOG_FATAL("Error parsing heap params: " << parameters.takeError())
×
NEW
408
                return;
×
409
              }
410
              MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
207✔
411
            });
207✔
412
            pass_builder.registerOptimizerLastEPCallback([](auto& MPM, OptimizationLevel) {
2,344✔
413
              auto parameters = typeart::util::pass::parsePassParameters(typeart::config::pass::parse_typeart_config,
207✔
414
                                                                         "typeart<no-heap;stack;stats>", "typeart");
207✔
415
              if (!parameters) {
207✔
NEW
416
                LOG_FATAL("Error parsing stack params: " << parameters.takeError())
×
NEW
417
                return;
×
418
              }
419
              MPM.addPass(typeart::pass::TypeArtPass(typeart::pass::TypeArtPass(parameters.get())));
207✔
420
            });
207✔
421
            pass_builder.registerPipelineParsingCallback(
4,274✔
422
                [](StringRef name, ModulePassManager& module_pm, ArrayRef<PassBuilder::PipelineElement>) {
5,997✔
423
                  if (typeart::util::pass::checkParametrizedPassName(name, "typeart")) {
3,860✔
424
                    auto parameters = typeart::util::pass::parsePassParameters(
3,860✔
425
                        typeart::config::pass::parse_typeart_config, name, "typeart");
3,860✔
426
                    if (!parameters) {
3,860✔
427
                      LOG_FATAL("Error parsing params: " << parameters.takeError())
×
428
                      return false;
×
429
                    }
430
                    module_pm.addPass(typeart::pass::TypeArtPass(parameters.get()));
3,860✔
431
                    return true;
3,860✔
432
                  }
3,860✔
433
                  LOG_FATAL("Not a valid parametrized pass name: " << name)
×
434
                  return false;
×
435
                });
3,860✔
436
          }};
2,137✔
437
}
438

439
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() {
2,137✔
440
  return getTypeartPassPluginInfo();
2,137✔
441
}
442

443
//.....................
444
// Old PM
445
//.....................
446
char typeart::pass::LegacyTypeArtPass::ID = 0;  // NOLINT
447

448
static RegisterPass<typeart::pass::LegacyTypeArtPass> x("typeart", "TypeArt Pass");  // NOLINT
2,137✔
449

450
ModulePass* createTypeArtPass() {
×
451
  return new typeart::pass::LegacyTypeArtPass();
×
452
}
453

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