LLVM  8.0.1
FaultMaps.h
Go to the documentation of this file.
1 //===- FaultMaps.h - The "FaultMaps" section --------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_CODEGEN_FAULTMAPS_H
11 #define LLVM_CODEGEN_FAULTMAPS_H
12 
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/Support/Endian.h"
15 #include <cassert>
16 #include <cstddef>
17 #include <cstdint>
18 #include <map>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class AsmPrinter;
24 class MCExpr;
25 class raw_ostream;
26 
27 class FaultMaps {
28 public:
29  enum FaultKind {
34  };
35 
36  explicit FaultMaps(AsmPrinter &AP);
37 
38  static const char *faultTypeToString(FaultKind);
39 
40  void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel);
42  void reset() {
43  FunctionInfos.clear();
44  }
45 
46 private:
47  static const char *WFMP;
48 
49  struct FaultInfo {
51  const MCExpr *FaultingOffsetExpr = nullptr;
52  const MCExpr *HandlerOffsetExpr = nullptr;
53 
54  FaultInfo() = default;
55 
56  explicit FaultInfo(FaultMaps::FaultKind Kind, const MCExpr *FaultingOffset,
57  const MCExpr *HandlerOffset)
58  : Kind(Kind), FaultingOffsetExpr(FaultingOffset),
59  HandlerOffsetExpr(HandlerOffset) {}
60  };
61 
62  using FunctionFaultInfos = std::vector<FaultInfo>;
63 
64  // We'd like to keep a stable iteration order for FunctionInfos to help
65  // FileCheck based testing.
66  struct MCSymbolComparator {
67  bool operator()(const MCSymbol *LHS, const MCSymbol *RHS) const {
68  return LHS->getName() < RHS->getName();
69  }
70  };
71 
72  std::map<const MCSymbol *, FunctionFaultInfos, MCSymbolComparator>
73  FunctionInfos;
74  AsmPrinter &AP;
75 
76  void emitFunctionInfo(const MCSymbol *FnLabel, const FunctionFaultInfos &FFI);
77 };
78 
79 /// A parser for the __llvm_faultmaps section generated by the FaultMaps class
80 /// above. This parser is version locked with with the __llvm_faultmaps section
81 /// generated by the version of LLVM that includes it. No guarantees are made
82 /// with respect to forward or backward compatibility.
84  using FaultMapVersionType = uint8_t;
85  using Reserved0Type = uint8_t;
86  using Reserved1Type = uint16_t;
87  using NumFunctionsType = uint32_t;
88 
89  static const size_t FaultMapVersionOffset = 0;
90  static const size_t Reserved0Offset =
91  FaultMapVersionOffset + sizeof(FaultMapVersionType);
92  static const size_t Reserved1Offset = Reserved0Offset + sizeof(Reserved0Type);
93  static const size_t NumFunctionsOffset =
94  Reserved1Offset + sizeof(Reserved1Type);
95  static const size_t FunctionInfosOffset =
96  NumFunctionsOffset + sizeof(NumFunctionsType);
97 
98  const uint8_t *P;
99  const uint8_t *E;
100 
101  template <typename T> static T read(const uint8_t *P, const uint8_t *E) {
102  assert(P + sizeof(T) <= E && "out of bounds read!");
103  return support::endian::read<T, support::little, 1>(P);
104  }
105 
106 public:
108  using FaultKindType = uint32_t;
111 
112  static const size_t FaultKindOffset = 0;
113  static const size_t FaultingPCOffsetOffset =
114  FaultKindOffset + sizeof(FaultKindType);
115  static const size_t HandlerPCOffsetOffset =
116  FaultingPCOffsetOffset + sizeof(FaultingPCOffsetType);
117 
118  const uint8_t *P;
119  const uint8_t *E;
120 
121  public:
122  static const size_t Size =
123  HandlerPCOffsetOffset + sizeof(HandlerPCOffsetType);
124 
125  explicit FunctionFaultInfoAccessor(const uint8_t *P, const uint8_t *E)
126  : P(P), E(E) {}
127 
129  return read<FaultKindType>(P + FaultKindOffset, E);
130  }
131 
133  return read<FaultingPCOffsetType>(P + FaultingPCOffsetOffset, E);
134  }
135 
137  return read<HandlerPCOffsetType>(P + HandlerPCOffsetOffset, E);
138  }
139  };
140 
142  using FunctionAddrType = uint64_t;
144  using ReservedType = uint32_t;
145 
146  static const size_t FunctionAddrOffset = 0;
147  static const size_t NumFaultingPCsOffset =
148  FunctionAddrOffset + sizeof(FunctionAddrType);
149  static const size_t ReservedOffset =
150  NumFaultingPCsOffset + sizeof(NumFaultingPCsType);
151  static const size_t FunctionFaultInfosOffset =
152  ReservedOffset + sizeof(ReservedType);
153  static const size_t FunctionInfoHeaderSize = FunctionFaultInfosOffset;
154 
155  const uint8_t *P = nullptr;
156  const uint8_t *E = nullptr;
157 
158  public:
159  FunctionInfoAccessor() = default;
160 
161  explicit FunctionInfoAccessor(const uint8_t *P, const uint8_t *E)
162  : P(P), E(E) {}
163 
164  FunctionAddrType getFunctionAddr() const {
165  return read<FunctionAddrType>(P + FunctionAddrOffset, E);
166  }
167 
169  return read<NumFaultingPCsType>(P + NumFaultingPCsOffset, E);
170  }
171 
173  assert(Index < getNumFaultingPCs() && "index out of bounds!");
174  const uint8_t *Begin = P + FunctionFaultInfosOffset +
176  return FunctionFaultInfoAccessor(Begin, E);
177  }
178 
180  size_t MySize = FunctionInfoHeaderSize +
181  getNumFaultingPCs() * FunctionFaultInfoAccessor::Size;
182 
183  const uint8_t *Begin = P + MySize;
184  assert(Begin < E && "out of bounds!");
185  return FunctionInfoAccessor(Begin, E);
186  }
187  };
188 
189  explicit FaultMapParser(const uint8_t *Begin, const uint8_t *End)
190  : P(Begin), E(End) {}
191 
192  FaultMapVersionType getFaultMapVersion() const {
193  auto Version = read<FaultMapVersionType>(P + FaultMapVersionOffset, E);
194  assert(Version == 1 && "only version 1 supported!");
195  return Version;
196  }
197 
199  return read<NumFunctionsType>(P + NumFunctionsOffset, E);
200  }
201 
203  const uint8_t *Begin = P + FunctionInfosOffset;
204  return FunctionInfoAccessor(Begin, E);
205  }
206 };
207 
208 raw_ostream &
210 
213 
215 
216 } // end namespace llvm
217 
218 #endif // LLVM_CODEGEN_FAULTMAPS_H
FaultingPCOffsetType getFaultingPCOffset() const
Definition: FaultMaps.h:132
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
HandlerPCOffsetType getHandlerPCOffset() const
Definition: FaultMaps.h:136
FunctionAddrType getFunctionAddr() const
Definition: FaultMaps.h:164
void reset()
Definition: FaultMaps.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
FunctionInfoAccessor getFirstFunctionInfo() const
Definition: FaultMaps.h:202
void recordFaultingOp(FaultKind FaultTy, const MCSymbol *HandlerLabel)
Definition: FaultMaps.cpp:31
FaultMapParser(const uint8_t *Begin, const uint8_t *End)
Definition: FaultMaps.h:189
NumFunctionsType getNumFunctions() const
Definition: FaultMaps.h:198
FaultMaps(AsmPrinter &AP)
Definition: FaultMaps.cpp:29
#define P(N)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:79
FunctionFaultInfoAccessor getFunctionFaultInfoAt(uint32_t Index) const
Definition: FaultMaps.h:172
A parser for the __llvm_faultmaps section generated by the FaultMaps class above. ...
Definition: FaultMaps.h:83
void serializeToFaultMapSection()
Definition: FaultMaps.cpp:50
FunctionFaultInfoAccessor(const uint8_t *P, const uint8_t *E)
Definition: FaultMaps.h:125
static const char * faultTypeToString(FaultKind)
Definition: FaultMaps.cpp:108
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:66
uint32_t Size
Definition: Profile.cpp:47
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FaultMapVersionType getFaultMapVersion() const
Definition: FaultMaps.h:192
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
NumFaultingPCsType getNumFaultingPCs() const
Definition: FaultMaps.h:168
FunctionInfoAccessor getNextFunctionInfo() const
Definition: FaultMaps.h:179
const uint64_t Version
Definition: InstrProf.h:895
FunctionInfoAccessor(const uint8_t *P, const uint8_t *E)
Definition: FaultMaps.h:161