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
MnemeLLVMUtils.hpp
Go to the documentation of this file.
1#pragma once
2#include "llvm/ADT/DenseMapInfo.h"
3#include <llvm/ADT/ArrayRef.h>
4#include <llvm/ADT/SmallVector.h>
5#include <llvm/ADT/StringRef.h>
6#include <llvm/ADT/Twine.h>
7#include <llvm/Demangle/Demangle.h>
8#include <llvm/IR/Function.h>
9#include <llvm/IR/Module.h>
10#include <llvm/Support/raw_ostream.h>
11#include <string>
12
13#include "mneme/MnemeLogger.hpp"
14
15namespace llvm {
16template <> struct DenseMapInfo<std::string> {
17 static inline std::string getEmptyKey() {
18 return std::string(); // Define an empty key
19 }
20 static inline std::string getTombstoneKey() {
21 return std::string("<TOMBSTONE_KEY>");
22 }
23 static unsigned getHashValue(const std::string &Key) {
24 // Use std::hash for hashing the string
25 return std::hash<std::string>{}(Key);
26 }
27 static bool isEqual(const std::string &LHS, const std::string &RHS) {
28 return LHS == RHS;
29 }
30};
31} // namespace llvm
32//
33namespace mneme {
34namespace util {
35inline std::string demangleKernelName(const std::string &Name) {
36 return llvm::demangle(Name.substr(0, Name.find("__intern__")));
37}
38
39template <typename Ty>
40void writeScalar(llvm::raw_ostream &OS, const Ty &Value) {
41 OS << llvm::StringRef(reinterpret_cast<const char *>(&Value), sizeof(Value));
42}
43
44inline void writeBytes(llvm::raw_ostream &OS, llvm::StringRef Data) {
45 if (!Data.empty())
46 OS << Data;
47}
48
49inline void writeBytes(llvm::raw_ostream &OS, llvm::ArrayRef<uint8_t> Data) {
50 writeBytes(OS, llvm::StringRef(reinterpret_cast<const char *>(Data.data()),
51 Data.size()));
52}
53} // namespace util
54
55inline static llvm::SmallVector<std::string> getArgNames(llvm::Function &F) {
56 llvm::SmallVector<std::string> Info;
57 for (auto &A : F.args()) {
58 auto name = A.getName().str();
59 if (name.size() > 0)
60 Info.push_back(name);
61 else
62 Info.push_back("");
63 }
64 return Info;
65}
66
67inline static llvm::SmallVector<bool> canSpecialize(llvm::Function &F) {
68 llvm::SmallVector<bool> RRInfo;
69 for (auto &A : F.args()) {
70 llvm::Type *ArgType = A.getType();
71 if (ArgType->isIntegerTy(1)) {
72 RRInfo.emplace_back(true);
73 } else if (ArgType->isIntegerTy(8)) {
74 RRInfo.emplace_back(true);
75 } else if (ArgType->isIntegerTy(32)) {
76 RRInfo.emplace_back(true);
77 } else if (ArgType->isIntegerTy(64)) {
78 RRInfo.emplace_back(true);
79 } else if (ArgType->isFloatTy()) {
80 RRInfo.emplace_back(true);
81 } else if (ArgType->isDoubleTy()) {
82 RRInfo.emplace_back(true);
83 } else if (ArgType->isX86_FP80Ty() || ArgType->isPPC_FP128Ty() ||
84 ArgType->isFP128Ty()) {
85 RRInfo.emplace_back(true);
86 } else {
87 RRInfo.emplace_back(false);
88 }
89 }
90 return RRInfo;
91}
92
93inline static llvm::SmallVector<std::function<double(void *)>>
94convertToDouble(llvm::Function &F) {
95 llvm::SmallVector<std::function<double(void *)>> RRInfo;
96 for (auto &A : F.args()) {
97 llvm::Type *ArgType = A.getType();
98 if (ArgType->isIntegerTy(1)) {
99 RRInfo.emplace_back([](void *data) {
100 bool val = *(bool *)data;
101 return (double)val;
102 });
103 } else if (ArgType->isIntegerTy(8)) {
104 RRInfo.emplace_back([](void *data) {
105 int8_t val = *(int8_t *)data;
106 return (double)val;
107 });
108 } else if (ArgType->isIntegerTy(32)) {
109 RRInfo.emplace_back([](void *data) {
110 int32_t val = *(int32_t *)data;
111 return (double)val;
112 });
113 } else if (ArgType->isIntegerTy(64)) {
114 RRInfo.emplace_back([](void *data) {
115 int64_t val = *(int64_t *)data;
116 return (double)val;
117 });
118 } else if (ArgType->isFloatTy()) {
119 RRInfo.emplace_back([](void *data) {
120 float val = *(float *)data;
121 return (double)val;
122 });
123 } else if (ArgType->isDoubleTy()) {
124 RRInfo.emplace_back([](void *data) {
125 double val = *(double *)data;
126 return (double)val;
127 });
128 } else if (ArgType->isX86_FP80Ty() || ArgType->isPPC_FP128Ty() ||
129 ArgType->isFP128Ty()) {
130 RRInfo.emplace_back([](void *data) {
131 double val = *(double *)data;
132 return (double)val;
133 });
134 } else {
135 RRInfo.emplace_back([](void *data) {
136 double val = *(double *)data;
137 return -1.0;
138 });
139 }
140 }
141 return RRInfo;
142}
143
144inline static llvm::SmallVector<uint64_t, 8> getFuncDescr(llvm::Function &F) {
145 llvm::SmallVector<uint64_t, 8> RRInfo;
146 auto DL = F.getParent()->getDataLayout();
147 for (auto &A : F.args()) {
148 // Datatypes such as structs passed by value to kernels are copied
149 // into a parameter vector. Over here we test whether an argument is
150 // byval, if it is we know on the host side this invocation forwards
151 // the arguments by value
152 if (A.hasByRefAttr() || A.hasByValAttr()) {
153 RRInfo.emplace_back(DL.getTypeStoreSize(A.getPointeeInMemoryValueType()));
154 } else {
155 RRInfo.emplace_back(DL.getTypeStoreSize(A.getType()));
156 }
157 }
158 return RRInfo;
159}
160} // namespace mneme
Definition attribute.cpp:39
Definition MnemeLLVMUtils.hpp:34
std::string demangleKernelName(const std::string &Name)
Definition MnemeLLVMUtils.hpp:35
void writeBytes(llvm::raw_ostream &OS, llvm::StringRef Data)
Definition MnemeLLVMUtils.hpp:44
void writeScalar(llvm::raw_ostream &OS, const Ty &Value)
Definition MnemeLLVMUtils.hpp:40
Definition MnemeAnnotation.cpp:3
@ Info
Definition MnemeConfig.hpp:23
static bool isEqual(const std::string &LHS, const std::string &RHS)
Definition MnemeLLVMUtils.hpp:27
static std::string getTombstoneKey()
Definition MnemeLLVMUtils.hpp:20
static unsigned getHashValue(const std::string &Key)
Definition MnemeLLVMUtils.hpp:23
static std::string getEmptyKey()
Definition MnemeLLVMUtils.hpp:17