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