2
0

StackMaps.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //===---------------------------- StackMaps.cpp ---------------------------===//
  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. #include "llvm/CodeGen/StackMaps.h"
  10. #include "llvm/CodeGen/AsmPrinter.h"
  11. #include "llvm/CodeGen/MachineFrameInfo.h"
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineInstr.h"
  14. #include "llvm/IR/DataLayout.h"
  15. #include "llvm/MC/MCContext.h"
  16. #include "llvm/MC/MCExpr.h"
  17. #include "llvm/MC/MCObjectFileInfo.h"
  18. #include "llvm/MC/MCSectionMachO.h"
  19. #include "llvm/MC/MCStreamer.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Target/TargetMachine.h"
  22. #include "llvm/Target/TargetOpcodes.h"
  23. #include "llvm/Target/TargetRegisterInfo.h"
  24. #include "llvm/Target/TargetSubtargetInfo.h"
  25. #include <iterator>
  26. using namespace llvm;
  27. #define DEBUG_TYPE "stackmaps"
  28. static cl::opt<int> StackMapVersion(
  29. "stackmap-version", cl::init(1),
  30. cl::desc("Specify the stackmap encoding version (default = 1)"));
  31. const char *StackMaps::WSMP = "Stack Maps: ";
  32. PatchPointOpers::PatchPointOpers(const MachineInstr *MI)
  33. : MI(MI), HasDef(MI->getOperand(0).isReg() && MI->getOperand(0).isDef() &&
  34. !MI->getOperand(0).isImplicit()),
  35. IsAnyReg(MI->getOperand(getMetaIdx(CCPos)).getImm() ==
  36. CallingConv::AnyReg) {
  37. #ifndef NDEBUG
  38. unsigned CheckStartIdx = 0, e = MI->getNumOperands();
  39. while (CheckStartIdx < e && MI->getOperand(CheckStartIdx).isReg() &&
  40. MI->getOperand(CheckStartIdx).isDef() &&
  41. !MI->getOperand(CheckStartIdx).isImplicit())
  42. ++CheckStartIdx;
  43. assert(getMetaIdx() == CheckStartIdx &&
  44. "Unexpected additional definition in Patchpoint intrinsic.");
  45. #endif
  46. }
  47. unsigned PatchPointOpers::getNextScratchIdx(unsigned StartIdx) const {
  48. if (!StartIdx)
  49. StartIdx = getVarIdx();
  50. // Find the next scratch register (implicit def and early clobber)
  51. unsigned ScratchIdx = StartIdx, e = MI->getNumOperands();
  52. while (ScratchIdx < e &&
  53. !(MI->getOperand(ScratchIdx).isReg() &&
  54. MI->getOperand(ScratchIdx).isDef() &&
  55. MI->getOperand(ScratchIdx).isImplicit() &&
  56. MI->getOperand(ScratchIdx).isEarlyClobber()))
  57. ++ScratchIdx;
  58. assert(ScratchIdx != e && "No scratch register available");
  59. return ScratchIdx;
  60. }
  61. StackMaps::StackMaps(AsmPrinter &AP) : AP(AP) {
  62. if (StackMapVersion != 1)
  63. llvm_unreachable("Unsupported stackmap version!");
  64. }
  65. /// Go up the super-register chain until we hit a valid dwarf register number.
  66. static unsigned getDwarfRegNum(unsigned Reg, const TargetRegisterInfo *TRI) {
  67. int RegNum = TRI->getDwarfRegNum(Reg, false);
  68. for (MCSuperRegIterator SR(Reg, TRI); SR.isValid() && RegNum < 0; ++SR)
  69. RegNum = TRI->getDwarfRegNum(*SR, false);
  70. assert(RegNum >= 0 && "Invalid Dwarf register number.");
  71. return (unsigned)RegNum;
  72. }
  73. MachineInstr::const_mop_iterator
  74. StackMaps::parseOperand(MachineInstr::const_mop_iterator MOI,
  75. MachineInstr::const_mop_iterator MOE, LocationVec &Locs,
  76. LiveOutVec &LiveOuts) const {
  77. const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
  78. if (MOI->isImm()) {
  79. switch (MOI->getImm()) {
  80. default:
  81. llvm_unreachable("Unrecognized operand type.");
  82. case StackMaps::DirectMemRefOp: {
  83. unsigned Size = AP.TM.getDataLayout()->getPointerSizeInBits();
  84. assert((Size % 8) == 0 && "Need pointer size in bytes.");
  85. Size /= 8;
  86. unsigned Reg = (++MOI)->getReg();
  87. int64_t Imm = (++MOI)->getImm();
  88. Locs.emplace_back(StackMaps::Location::Direct, Size,
  89. getDwarfRegNum(Reg, TRI), Imm);
  90. break;
  91. }
  92. case StackMaps::IndirectMemRefOp: {
  93. int64_t Size = (++MOI)->getImm();
  94. assert(Size > 0 && "Need a valid size for indirect memory locations.");
  95. unsigned Reg = (++MOI)->getReg();
  96. int64_t Imm = (++MOI)->getImm();
  97. Locs.emplace_back(StackMaps::Location::Indirect, Size,
  98. getDwarfRegNum(Reg, TRI), Imm);
  99. break;
  100. }
  101. case StackMaps::ConstantOp: {
  102. ++MOI;
  103. assert(MOI->isImm() && "Expected constant operand.");
  104. int64_t Imm = MOI->getImm();
  105. Locs.emplace_back(Location::Constant, sizeof(int64_t), 0, Imm);
  106. break;
  107. }
  108. }
  109. return ++MOI;
  110. }
  111. // The physical register number will ultimately be encoded as a DWARF regno.
  112. // The stack map also records the size of a spill slot that can hold the
  113. // register content. (The runtime can track the actual size of the data type
  114. // if it needs to.)
  115. if (MOI->isReg()) {
  116. // Skip implicit registers (this includes our scratch registers)
  117. if (MOI->isImplicit())
  118. return ++MOI;
  119. assert(TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) &&
  120. "Virtreg operands should have been rewritten before now.");
  121. const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(MOI->getReg());
  122. assert(!MOI->getSubReg() && "Physical subreg still around.");
  123. unsigned Offset = 0;
  124. unsigned DwarfRegNum = getDwarfRegNum(MOI->getReg(), TRI);
  125. unsigned LLVMRegNum = TRI->getLLVMRegNum(DwarfRegNum, false);
  126. unsigned SubRegIdx = TRI->getSubRegIndex(LLVMRegNum, MOI->getReg());
  127. if (SubRegIdx)
  128. Offset = TRI->getSubRegIdxOffset(SubRegIdx);
  129. Locs.emplace_back(Location::Register, RC->getSize(), DwarfRegNum, Offset);
  130. return ++MOI;
  131. }
  132. if (MOI->isRegLiveOut())
  133. LiveOuts = parseRegisterLiveOutMask(MOI->getRegLiveOut());
  134. return ++MOI;
  135. }
  136. void StackMaps::print(raw_ostream &OS) {
  137. const TargetRegisterInfo *TRI =
  138. AP.MF ? AP.MF->getSubtarget().getRegisterInfo() : nullptr;
  139. OS << WSMP << "callsites:\n";
  140. for (const auto &CSI : CSInfos) {
  141. const LocationVec &CSLocs = CSI.Locations;
  142. const LiveOutVec &LiveOuts = CSI.LiveOuts;
  143. OS << WSMP << "callsite " << CSI.ID << "\n";
  144. OS << WSMP << " has " << CSLocs.size() << " locations\n";
  145. unsigned Idx = 0;
  146. for (const auto &Loc : CSLocs) {
  147. OS << WSMP << "\t\tLoc " << Idx << ": ";
  148. switch (Loc.Type) {
  149. case Location::Unprocessed:
  150. OS << "<Unprocessed operand>";
  151. break;
  152. case Location::Register:
  153. OS << "Register ";
  154. if (TRI)
  155. OS << TRI->getName(Loc.Reg);
  156. else
  157. OS << Loc.Reg;
  158. break;
  159. case Location::Direct:
  160. OS << "Direct ";
  161. if (TRI)
  162. OS << TRI->getName(Loc.Reg);
  163. else
  164. OS << Loc.Reg;
  165. if (Loc.Offset)
  166. OS << " + " << Loc.Offset;
  167. break;
  168. case Location::Indirect:
  169. OS << "Indirect ";
  170. if (TRI)
  171. OS << TRI->getName(Loc.Reg);
  172. else
  173. OS << Loc.Reg;
  174. OS << "+" << Loc.Offset;
  175. break;
  176. case Location::Constant:
  177. OS << "Constant " << Loc.Offset;
  178. break;
  179. case Location::ConstantIndex:
  180. OS << "Constant Index " << Loc.Offset;
  181. break;
  182. }
  183. OS << "\t[encoding: .byte " << Loc.Type << ", .byte " << Loc.Size
  184. << ", .short " << Loc.Reg << ", .int " << Loc.Offset << "]\n";
  185. Idx++;
  186. }
  187. OS << WSMP << "\thas " << LiveOuts.size() << " live-out registers\n";
  188. Idx = 0;
  189. for (const auto &LO : LiveOuts) {
  190. OS << WSMP << "\t\tLO " << Idx << ": ";
  191. if (TRI)
  192. OS << TRI->getName(LO.Reg);
  193. else
  194. OS << LO.Reg;
  195. OS << "\t[encoding: .short " << LO.DwarfRegNum << ", .byte 0, .byte "
  196. << LO.Size << "]\n";
  197. Idx++;
  198. }
  199. }
  200. }
  201. /// Create a live-out register record for the given register Reg.
  202. StackMaps::LiveOutReg
  203. StackMaps::createLiveOutReg(unsigned Reg, const TargetRegisterInfo *TRI) const {
  204. unsigned DwarfRegNum = getDwarfRegNum(Reg, TRI);
  205. unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
  206. return LiveOutReg(Reg, DwarfRegNum, Size);
  207. }
  208. /// Parse the register live-out mask and return a vector of live-out registers
  209. /// that need to be recorded in the stackmap.
  210. StackMaps::LiveOutVec
  211. StackMaps::parseRegisterLiveOutMask(const uint32_t *Mask) const {
  212. assert(Mask && "No register mask specified");
  213. const TargetRegisterInfo *TRI = AP.MF->getSubtarget().getRegisterInfo();
  214. LiveOutVec LiveOuts;
  215. // Create a LiveOutReg for each bit that is set in the register mask.
  216. for (unsigned Reg = 0, NumRegs = TRI->getNumRegs(); Reg != NumRegs; ++Reg)
  217. if ((Mask[Reg / 32] >> Reg % 32) & 1)
  218. LiveOuts.push_back(createLiveOutReg(Reg, TRI));
  219. // We don't need to keep track of a register if its super-register is already
  220. // in the list. Merge entries that refer to the same dwarf register and use
  221. // the maximum size that needs to be spilled.
  222. std::sort(LiveOuts.begin(), LiveOuts.end(),
  223. [](const LiveOutReg &LHS, const LiveOutReg &RHS) {
  224. // Only sort by the dwarf register number.
  225. return LHS.DwarfRegNum < RHS.DwarfRegNum;
  226. });
  227. for (auto I = LiveOuts.begin(), E = LiveOuts.end(); I != E; ++I) {
  228. for (auto II = std::next(I); II != E; ++II) {
  229. if (I->DwarfRegNum != II->DwarfRegNum) {
  230. // Skip all the now invalid entries.
  231. I = --II;
  232. break;
  233. }
  234. I->Size = std::max(I->Size, II->Size);
  235. if (TRI->isSuperRegister(I->Reg, II->Reg))
  236. I->Reg = II->Reg;
  237. II->Reg = 0; // mark for deletion.
  238. }
  239. }
  240. LiveOuts.erase(
  241. std::remove_if(LiveOuts.begin(), LiveOuts.end(),
  242. [](const LiveOutReg &LO) { return LO.Reg == 0; }),
  243. LiveOuts.end());
  244. return LiveOuts;
  245. }
  246. void StackMaps::recordStackMapOpers(const MachineInstr &MI, uint64_t ID,
  247. MachineInstr::const_mop_iterator MOI,
  248. MachineInstr::const_mop_iterator MOE,
  249. bool recordResult) {
  250. MCContext &OutContext = AP.OutStreamer->getContext();
  251. MCSymbol *MILabel = OutContext.createTempSymbol();
  252. AP.OutStreamer->EmitLabel(MILabel);
  253. LocationVec Locations;
  254. LiveOutVec LiveOuts;
  255. if (recordResult) {
  256. assert(PatchPointOpers(&MI).hasDef() && "Stackmap has no return value.");
  257. parseOperand(MI.operands_begin(), std::next(MI.operands_begin()), Locations,
  258. LiveOuts);
  259. }
  260. // Parse operands.
  261. while (MOI != MOE) {
  262. MOI = parseOperand(MOI, MOE, Locations, LiveOuts);
  263. }
  264. // Move large constants into the constant pool.
  265. for (auto &Loc : Locations) {
  266. // Constants are encoded as sign-extended integers.
  267. // -1 is directly encoded as .long 0xFFFFFFFF with no constant pool.
  268. if (Loc.Type == Location::Constant && !isInt<32>(Loc.Offset)) {
  269. Loc.Type = Location::ConstantIndex;
  270. // ConstPool is intentionally a MapVector of 'uint64_t's (as
  271. // opposed to 'int64_t's). We should never be in a situation
  272. // where we have to insert either the tombstone or the empty
  273. // keys into a map, and for a DenseMap<uint64_t, T> these are
  274. // (uint64_t)0 and (uint64_t)-1. They can be and are
  275. // represented using 32 bit integers.
  276. assert((uint64_t)Loc.Offset != DenseMapInfo<uint64_t>::getEmptyKey() &&
  277. (uint64_t)Loc.Offset !=
  278. DenseMapInfo<uint64_t>::getTombstoneKey() &&
  279. "empty and tombstone keys should fit in 32 bits!");
  280. auto Result = ConstPool.insert(std::make_pair(Loc.Offset, Loc.Offset));
  281. Loc.Offset = Result.first - ConstPool.begin();
  282. }
  283. }
  284. // Create an expression to calculate the offset of the callsite from function
  285. // entry.
  286. const MCExpr *CSOffsetExpr = MCBinaryExpr::createSub(
  287. MCSymbolRefExpr::create(MILabel, OutContext),
  288. MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
  289. CSInfos.emplace_back(CSOffsetExpr, ID, std::move(Locations),
  290. std::move(LiveOuts));
  291. // Record the stack size of the current function.
  292. const MachineFrameInfo *MFI = AP.MF->getFrameInfo();
  293. const TargetRegisterInfo *RegInfo = AP.MF->getSubtarget().getRegisterInfo();
  294. bool HasDynamicFrameSize =
  295. MFI->hasVarSizedObjects() || RegInfo->needsStackRealignment(*(AP.MF));
  296. FnStackSize[AP.CurrentFnSym] =
  297. HasDynamicFrameSize ? UINT64_MAX : MFI->getStackSize();
  298. }
  299. void StackMaps::recordStackMap(const MachineInstr &MI) {
  300. assert(MI.getOpcode() == TargetOpcode::STACKMAP && "expected stackmap");
  301. int64_t ID = MI.getOperand(0).getImm();
  302. recordStackMapOpers(MI, ID, std::next(MI.operands_begin(), 2),
  303. MI.operands_end());
  304. }
  305. void StackMaps::recordPatchPoint(const MachineInstr &MI) {
  306. assert(MI.getOpcode() == TargetOpcode::PATCHPOINT && "expected patchpoint");
  307. PatchPointOpers opers(&MI);
  308. int64_t ID = opers.getMetaOper(PatchPointOpers::IDPos).getImm();
  309. auto MOI = std::next(MI.operands_begin(), opers.getStackMapStartIdx());
  310. recordStackMapOpers(MI, ID, MOI, MI.operands_end(),
  311. opers.isAnyReg() && opers.hasDef());
  312. #ifndef NDEBUG
  313. // verify anyregcc
  314. auto &Locations = CSInfos.back().Locations;
  315. if (opers.isAnyReg()) {
  316. unsigned NArgs = opers.getMetaOper(PatchPointOpers::NArgPos).getImm();
  317. for (unsigned i = 0, e = (opers.hasDef() ? NArgs + 1 : NArgs); i != e; ++i)
  318. assert(Locations[i].Type == Location::Register &&
  319. "anyreg arg must be in reg.");
  320. }
  321. #endif
  322. }
  323. void StackMaps::recordStatepoint(const MachineInstr &MI) {
  324. assert(MI.getOpcode() == TargetOpcode::STATEPOINT && "expected statepoint");
  325. StatepointOpers opers(&MI);
  326. // Record all the deopt and gc operands (they're contiguous and run from the
  327. // initial index to the end of the operand list)
  328. const unsigned StartIdx = opers.getVarIdx();
  329. recordStackMapOpers(MI, opers.getID(), MI.operands_begin() + StartIdx,
  330. MI.operands_end(), false);
  331. }
  332. /// Emit the stackmap header.
  333. ///
  334. /// Header {
  335. /// uint8 : Stack Map Version (currently 1)
  336. /// uint8 : Reserved (expected to be 0)
  337. /// uint16 : Reserved (expected to be 0)
  338. /// }
  339. /// uint32 : NumFunctions
  340. /// uint32 : NumConstants
  341. /// uint32 : NumRecords
  342. void StackMaps::emitStackmapHeader(MCStreamer &OS) {
  343. // Header.
  344. OS.EmitIntValue(StackMapVersion, 1); // Version.
  345. OS.EmitIntValue(0, 1); // Reserved.
  346. OS.EmitIntValue(0, 2); // Reserved.
  347. // Num functions.
  348. DEBUG(dbgs() << WSMP << "#functions = " << FnStackSize.size() << '\n');
  349. OS.EmitIntValue(FnStackSize.size(), 4);
  350. // Num constants.
  351. DEBUG(dbgs() << WSMP << "#constants = " << ConstPool.size() << '\n');
  352. OS.EmitIntValue(ConstPool.size(), 4);
  353. // Num callsites.
  354. DEBUG(dbgs() << WSMP << "#callsites = " << CSInfos.size() << '\n');
  355. OS.EmitIntValue(CSInfos.size(), 4);
  356. }
  357. /// Emit the function frame record for each function.
  358. ///
  359. /// StkSizeRecord[NumFunctions] {
  360. /// uint64 : Function Address
  361. /// uint64 : Stack Size
  362. /// }
  363. void StackMaps::emitFunctionFrameRecords(MCStreamer &OS) {
  364. // Function Frame records.
  365. DEBUG(dbgs() << WSMP << "functions:\n");
  366. for (auto const &FR : FnStackSize) {
  367. DEBUG(dbgs() << WSMP << "function addr: " << FR.first
  368. << " frame size: " << FR.second);
  369. OS.EmitSymbolValue(FR.first, 8);
  370. OS.EmitIntValue(FR.second, 8);
  371. }
  372. }
  373. /// Emit the constant pool.
  374. ///
  375. /// int64 : Constants[NumConstants]
  376. void StackMaps::emitConstantPoolEntries(MCStreamer &OS) {
  377. // Constant pool entries.
  378. DEBUG(dbgs() << WSMP << "constants:\n");
  379. for (const auto &ConstEntry : ConstPool) {
  380. DEBUG(dbgs() << WSMP << ConstEntry.second << '\n');
  381. OS.EmitIntValue(ConstEntry.second, 8);
  382. }
  383. }
  384. /// Emit the callsite info for each callsite.
  385. ///
  386. /// StkMapRecord[NumRecords] {
  387. /// uint64 : PatchPoint ID
  388. /// uint32 : Instruction Offset
  389. /// uint16 : Reserved (record flags)
  390. /// uint16 : NumLocations
  391. /// Location[NumLocations] {
  392. /// uint8 : Register | Direct | Indirect | Constant | ConstantIndex
  393. /// uint8 : Size in Bytes
  394. /// uint16 : Dwarf RegNum
  395. /// int32 : Offset
  396. /// }
  397. /// uint16 : Padding
  398. /// uint16 : NumLiveOuts
  399. /// LiveOuts[NumLiveOuts] {
  400. /// uint16 : Dwarf RegNum
  401. /// uint8 : Reserved
  402. /// uint8 : Size in Bytes
  403. /// }
  404. /// uint32 : Padding (only if required to align to 8 byte)
  405. /// }
  406. ///
  407. /// Location Encoding, Type, Value:
  408. /// 0x1, Register, Reg (value in register)
  409. /// 0x2, Direct, Reg + Offset (frame index)
  410. /// 0x3, Indirect, [Reg + Offset] (spilled value)
  411. /// 0x4, Constant, Offset (small constant)
  412. /// 0x5, ConstIndex, Constants[Offset] (large constant)
  413. void StackMaps::emitCallsiteEntries(MCStreamer &OS) {
  414. DEBUG(print(dbgs()));
  415. // Callsite entries.
  416. for (const auto &CSI : CSInfos) {
  417. const LocationVec &CSLocs = CSI.Locations;
  418. const LiveOutVec &LiveOuts = CSI.LiveOuts;
  419. // Verify stack map entry. It's better to communicate a problem to the
  420. // runtime than crash in case of in-process compilation. Currently, we do
  421. // simple overflow checks, but we may eventually communicate other
  422. // compilation errors this way.
  423. if (CSLocs.size() > UINT16_MAX || LiveOuts.size() > UINT16_MAX) {
  424. OS.EmitIntValue(UINT64_MAX, 8); // Invalid ID.
  425. OS.EmitValue(CSI.CSOffsetExpr, 4);
  426. OS.EmitIntValue(0, 2); // Reserved.
  427. OS.EmitIntValue(0, 2); // 0 locations.
  428. OS.EmitIntValue(0, 2); // padding.
  429. OS.EmitIntValue(0, 2); // 0 live-out registers.
  430. OS.EmitIntValue(0, 4); // padding.
  431. continue;
  432. }
  433. OS.EmitIntValue(CSI.ID, 8);
  434. OS.EmitValue(CSI.CSOffsetExpr, 4);
  435. // Reserved for flags.
  436. OS.EmitIntValue(0, 2);
  437. OS.EmitIntValue(CSLocs.size(), 2);
  438. for (const auto &Loc : CSLocs) {
  439. OS.EmitIntValue(Loc.Type, 1);
  440. OS.EmitIntValue(Loc.Size, 1);
  441. OS.EmitIntValue(Loc.Reg, 2);
  442. OS.EmitIntValue(Loc.Offset, 4);
  443. }
  444. // Num live-out registers and padding to align to 4 byte.
  445. OS.EmitIntValue(0, 2);
  446. OS.EmitIntValue(LiveOuts.size(), 2);
  447. for (const auto &LO : LiveOuts) {
  448. OS.EmitIntValue(LO.DwarfRegNum, 2);
  449. OS.EmitIntValue(0, 1);
  450. OS.EmitIntValue(LO.Size, 1);
  451. }
  452. // Emit alignment to 8 byte.
  453. OS.EmitValueToAlignment(8);
  454. }
  455. }
  456. /// Serialize the stackmap data.
  457. void StackMaps::serializeToStackMapSection() {
  458. (void)WSMP;
  459. // Bail out if there's no stack map data.
  460. assert((!CSInfos.empty() || (CSInfos.empty() && ConstPool.empty())) &&
  461. "Expected empty constant pool too!");
  462. assert((!CSInfos.empty() || (CSInfos.empty() && FnStackSize.empty())) &&
  463. "Expected empty function record too!");
  464. if (CSInfos.empty())
  465. return;
  466. MCContext &OutContext = AP.OutStreamer->getContext();
  467. MCStreamer &OS = *AP.OutStreamer;
  468. // Create the section.
  469. MCSection *StackMapSection =
  470. OutContext.getObjectFileInfo()->getStackMapSection();
  471. OS.SwitchSection(StackMapSection);
  472. // Emit a dummy symbol to force section inclusion.
  473. OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_StackMaps")));
  474. // Serialize data.
  475. DEBUG(dbgs() << "********** Stack Map Output **********\n");
  476. emitStackmapHeader(OS);
  477. emitFunctionFrameRecords(OS);
  478. emitConstantPoolEntries(OS);
  479. emitCallsiteEntries(OS);
  480. OS.AddBlankLine();
  481. // Clean up.
  482. CSInfos.clear();
  483. ConstPool.clear();
  484. }