OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AutomaticIRMetadataGuard Class Reference

#include <AutomaticIRMetadataGuard.h>

+ Collaboration diagram for AutomaticIRMetadataGuard:

Classes

struct  InstructionInfo
 

Public Member Functions

 AutomaticIRMetadataGuard (CgenState *cgen_state, const std::string &ppfile, const size_t ppline, const std::string &ppfunc)
 
 ~AutomaticIRMetadataGuard ()
 
void done () noexcept
 
void rememberPreexistingInstructions () noexcept
 
void rememberOurInstructions () noexcept
 
void markInstructions () noexcept
 

Private Types

using OurInstructions = std::unordered_map< llvm::Instruction *, InstructionInfo >
 

Private Member Functions

std::string makeBaseFilename ()
 
std::string makeQueryEngineFilename ()
 
std::string replacePunctuation (std::string text)
 

Private Attributes

CgenStatecgen_state_
 
const std::string ppfile_
 
const size_t ppline_
 
const std::string ppfunc_
 
std::unordered_set
< llvm::Instruction * > 
preexisting_instructions_
 
OurInstructionsour_instructions_
 
bool done_
 
bool this_is_root_
 
bool enabled_
 

Static Private Attributes

static std::unordered_map
< CgenState *, OurInstructions
instructions_
 
static const std::string detailed_footnote_prefix_ {"HEAVY.AI Debugging Info: "}
 
static std::mutex instructions_mutex_
 

Detailed Description

Definition at line 31 of file AutomaticIRMetadataGuard.h.

Member Typedef Documentation

using AutomaticIRMetadataGuard::OurInstructions = std::unordered_map<llvm::Instruction*, InstructionInfo>
private

Definition at line 183 of file AutomaticIRMetadataGuard.h.

Constructor & Destructor Documentation

AutomaticIRMetadataGuard::AutomaticIRMetadataGuard ( CgenState cgen_state,
const std::string &  ppfile,
const size_t  ppline,
const std::string &  ppfunc 
)
inline

Definition at line 33 of file AutomaticIRMetadataGuard.h.

References cgen_state_, CHECK, enabled_, g_enable_automatic_ir_metadata, instructions_, instructions_mutex_, CgenState::module_, our_instructions_, rememberPreexistingInstructions(), and this_is_root_.

37  : cgen_state_(cgen_state)
38  , ppfile_(ppfile)
39  , ppline_(ppline)
40  , ppfunc_(ppfunc)
41  , our_instructions_(nullptr)
42  , done_(false) {
43  std::lock_guard<std::mutex> lock(instructions_mutex_);
46  if (enabled_) {
51  }
52  }
void rememberPreexistingInstructions() noexcept
llvm::Module * module_
Definition: CgenState.h:373
static std::unordered_map< CgenState *, OurInstructions > instructions_
bool g_enable_automatic_ir_metadata
Definition: Execute.cpp:170
#define CHECK(condition)
Definition: Logger.h:291

+ Here is the call graph for this function:

AutomaticIRMetadataGuard::~AutomaticIRMetadataGuard ( )
inline

Definition at line 54 of file AutomaticIRMetadataGuard.h.

References done().

54 { done(); }

+ Here is the call graph for this function:

Member Function Documentation

void AutomaticIRMetadataGuard::done ( )
inlinenoexcept

Definition at line 56 of file AutomaticIRMetadataGuard.h.

References cgen_state_, done_, enabled_, instructions_, instructions_mutex_, markInstructions(), rememberOurInstructions(), and this_is_root_.

Referenced by ~AutomaticIRMetadataGuard().

56  {
57  if (enabled_ && !done_) {
58  std::lock_guard<std::mutex> lock(instructions_mutex_);
60  if (this_is_root_) {
63  }
64  done_ = true;
65  }
66  }
static std::unordered_map< CgenState *, OurInstructions > instructions_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string AutomaticIRMetadataGuard::makeBaseFilename ( )
inlineprivate

Definition at line 137 of file AutomaticIRMetadataGuard.h.

References ppfile_, and split().

Referenced by rememberOurInstructions().

137  {
138  std::vector<std::string> fnames = split(ppfile_, "/");
139  if (!fnames.empty()) {
140  return fnames.back();
141  }
142  return ppfile_;
143  }
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string AutomaticIRMetadataGuard::makeQueryEngineFilename ( )
inlineprivate

Definition at line 145 of file AutomaticIRMetadataGuard.h.

References f(), anonymous_namespace{Utm.h}::n, ppfile_, and split().

Referenced by rememberOurInstructions().

145  {
146  std::vector<std::string> fnames = split(ppfile_, "/");
147  bool copying{false};
148  std::string f;
149  for (auto n : fnames) {
150  if (copying && !n.empty()) {
151  if (!f.empty()) {
152  f += "/";
153  }
154  f += n;
155  }
156  if (n == "QueryEngine") {
157  copying = true;
158  }
159  }
160  if (f.empty() && fnames.size() > 0) {
161  f = fnames.back();
162  } else if (f.empty()) {
163  f = ppfile_;
164  }
165  return f;
166  }
std::vector< std::string > split(std::string_view str, std::string_view delim, std::optional< size_t > maxsplit)
split apart a string into a vector of substrings
torch::Tensor f(torch::Tensor x, torch::Tensor W_target, torch::Tensor b_target)
constexpr double n
Definition: Utm.h:38

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AutomaticIRMetadataGuard::markInstructions ( )
inlinenoexcept

Definition at line 112 of file AutomaticIRMetadataGuard.h.

References cgen_state_, CgenState::context_, detailed_footnote_prefix_, CgenState::module_, and our_instructions_.

Referenced by done().

112  {
113  // iterate over all LLVM instructions in the module
114  for (auto func_it = cgen_state_->module_->begin();
115  func_it != cgen_state_->module_->end();
116  ++func_it) {
117  for (auto bb_it = func_it->begin(); bb_it != func_it->end(); ++bb_it) {
118  for (auto instr_it = bb_it->begin(); instr_it != bb_it->end(); ++instr_it) {
119  auto our_it = our_instructions_->find(&*instr_it);
120  if (our_it == our_instructions_->end()) {
121  continue;
122  }
123  unsigned kind_id =
124  cgen_state_->context_.getMDKindID(our_it->second.main_filename_);
125  auto value = llvm::MDNode::get(
127  llvm::MDString::get(
129  detailed_footnote_prefix_ + our_it->second.detailed_footnote_));
130  our_it->first->setMetadata(kind_id, value);
131  }
132  }
133  }
134  }
llvm::Module * module_
Definition: CgenState.h:373
llvm::LLVMContext & context_
Definition: CgenState.h:382
static const std::string detailed_footnote_prefix_

+ Here is the caller graph for this function:

void AutomaticIRMetadataGuard::rememberOurInstructions ( )
inlinenoexcept

Definition at line 84 of file AutomaticIRMetadataGuard.h.

References cgen_state_, makeBaseFilename(), makeQueryEngineFilename(), CgenState::module_, our_instructions_, ppfunc_, ppline_, preexisting_instructions_, replacePunctuation(), and to_string().

Referenced by done().

84  {
85  // iterate over all LLVM instructions in the module
86  for (auto func_it = cgen_state_->module_->begin();
87  func_it != cgen_state_->module_->end();
88  ++func_it) {
89  for (auto bb_it = func_it->begin(); bb_it != func_it->end(); ++bb_it) {
90  for (auto instr_it = bb_it->begin(); instr_it != bb_it->end(); ++instr_it) {
91  // remember any new instructions as ours, skipping
92  // instructions already remembered as preexisting
93  llvm::Instruction* i = &*instr_it;
94  if (!preexisting_instructions_.count(i)) {
95  std::string qefile = makeQueryEngineFilename();
96  std::string footnote =
97  ppfunc_ + " after " + qefile + " line #" + std::to_string(ppline_);
98  auto it = our_instructions_->find(i);
99  if (it == our_instructions_->end()) {
100  std::string bfile = replacePunctuation(makeBaseFilename());
101  our_instructions_->emplace(i, InstructionInfo{bfile, footnote});
102  } else {
103  it->second.detailed_footnote_ =
104  footnote + ", " + it->second.detailed_footnote_;
105  }
106  }
107  }
108  }
109  }
110  }
std::string to_string(char const *&&v)
llvm::Module * module_
Definition: CgenState.h:373
std::unordered_set< llvm::Instruction * > preexisting_instructions_
std::string replacePunctuation(std::string text)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void AutomaticIRMetadataGuard::rememberPreexistingInstructions ( )
inlinenoexcept

Definition at line 68 of file AutomaticIRMetadataGuard.h.

References cgen_state_, CHECK_EQ, CgenState::module_, and preexisting_instructions_.

Referenced by AutomaticIRMetadataGuard().

68  {
69  // iterate over all LLVM instructions in the module
70  for (auto func_it = cgen_state_->module_->begin();
71  func_it != cgen_state_->module_->end();
72  ++func_it) {
73  for (auto bb_it = func_it->begin(); bb_it != func_it->end(); ++bb_it) {
74  for (auto instr_it = bb_it->begin(); instr_it != bb_it->end(); ++instr_it) {
75  // remember all the instructions that already existed
76  // before this guard object was constructed
77  CHECK_EQ(preexisting_instructions_.count(&*instr_it), 0U);
78  preexisting_instructions_.insert(&*instr_it);
79  }
80  }
81  }
82  }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
llvm::Module * module_
Definition: CgenState.h:373
std::unordered_set< llvm::Instruction * > preexisting_instructions_

+ Here is the caller graph for this function:

std::string AutomaticIRMetadataGuard::replacePunctuation ( std::string  text)
inlineprivate

Definition at line 168 of file AutomaticIRMetadataGuard.h.

Referenced by rememberOurInstructions().

168  {
169  static const std::unordered_set<std::string::value_type> allowed_punct{'_', '.'};
170  for (auto& ch : text) {
171  if (std::ispunct(ch) && !allowed_punct.count(ch)) {
172  ch = '_';
173  }
174  }
175  return text;
176  }

+ Here is the caller graph for this function:

Member Data Documentation

CgenState* AutomaticIRMetadataGuard::cgen_state_
private
const std::string AutomaticIRMetadataGuard::detailed_footnote_prefix_ {"HEAVY.AI Debugging Info: "}
inlinestaticprivate

Definition at line 200 of file AutomaticIRMetadataGuard.h.

Referenced by markInstructions().

bool AutomaticIRMetadataGuard::done_
private

Definition at line 194 of file AutomaticIRMetadataGuard.h.

Referenced by done().

bool AutomaticIRMetadataGuard::enabled_
private

Definition at line 196 of file AutomaticIRMetadataGuard.h.

Referenced by AutomaticIRMetadataGuard(), and done().

std::unordered_map<CgenState*, OurInstructions> AutomaticIRMetadataGuard::instructions_
inlinestaticprivate

Definition at line 198 of file AutomaticIRMetadataGuard.h.

Referenced by AutomaticIRMetadataGuard(), and done().

std::mutex AutomaticIRMetadataGuard::instructions_mutex_
inlinestaticprivate

Definition at line 202 of file AutomaticIRMetadataGuard.h.

Referenced by AutomaticIRMetadataGuard(), and done().

OurInstructions* AutomaticIRMetadataGuard::our_instructions_
private
const std::string AutomaticIRMetadataGuard::ppfile_
private

Definition at line 187 of file AutomaticIRMetadataGuard.h.

Referenced by makeBaseFilename(), and makeQueryEngineFilename().

const std::string AutomaticIRMetadataGuard::ppfunc_
private

Definition at line 189 of file AutomaticIRMetadataGuard.h.

Referenced by rememberOurInstructions().

const size_t AutomaticIRMetadataGuard::ppline_
private

Definition at line 188 of file AutomaticIRMetadataGuard.h.

Referenced by rememberOurInstructions().

std::unordered_set<llvm::Instruction*> AutomaticIRMetadataGuard::preexisting_instructions_
private
bool AutomaticIRMetadataGuard::this_is_root_
private

Definition at line 195 of file AutomaticIRMetadataGuard.h.

Referenced by AutomaticIRMetadataGuard(), and done().


The documentation for this class was generated from the following file: