Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemePurgeAutoInit.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <llvm/ADT/STLExtras.h>
4#include <llvm/IR/IntrinsicInst.h>
5#include <llvm/IR/Module.h>
6
7namespace mneme {
8
9// Clang's !annotation can contain strings directly or group them in tuples.
10inline bool hasAutoInitAnnotation(const llvm::Instruction &I) {
11 const auto *Annotations = I.getMetadata(llvm::LLVMContext::MD_annotation);
12 if (!Annotations)
13 return false;
14
15 auto IsAutoInit = [](const llvm::Metadata *MD) {
16 const auto *Str = llvm::dyn_cast_or_null<llvm::MDString>(MD);
17 return Str && Str->getString() == "auto-init";
18 };
19
20 for (const llvm::MDOperand &Op : Annotations->operands()) {
21 if (IsAutoInit(Op.get()))
22 return true;
23
24 if (const auto *Group = llvm::dyn_cast_or_null<llvm::MDNode>(Op.get()))
25 for (const llvm::MDOperand &GroupOp : Group->operands())
26 if (IsAutoInit(GroupOp.get()))
27 return true;
28 }
29 return false;
30}
31
32// Strip only the memsets emitted for -ftrivial-auto-var-init.
33inline bool purgeAutoInit(llvm::Module &M) {
34 bool Changed = false;
35 for (auto &F : M)
36 for (auto &BB : F)
37 for (auto &I : llvm::make_early_inc_range(BB)) {
38 auto *II = llvm::dyn_cast<llvm::IntrinsicInst>(&I);
39 if (II && II->getIntrinsicID() == llvm::Intrinsic::memset &&
41 II->eraseFromParent();
42 Changed = true;
43 }
44 }
45 return Changed;
46}
47
48} // namespace mneme
Definition MnemeAnnotation.cpp:3
bool purgeAutoInit(llvm::Module &M)
Definition MnemePurgeAutoInit.hpp:33
bool hasAutoInitAnnotation(const llvm::Instruction &I)
Definition MnemePurgeAutoInit.hpp:10