@@ -135,6 +135,7 @@ static cl::opt<bool>
135135
136136extern bool YkAllocLLVMBBAddrMapSection;
137137extern bool YkExtendedLLVMBBAddrMapSection;
138+ extern bool YkStackMapOffsetFix;
138139
139140const char DWARFGroupName[] = " dwarf" ;
140141const char DWARFGroupDescription[] = " DWARF Emission" ;
@@ -1699,6 +1700,9 @@ void AsmPrinter::emitFunctionBody() {
16991700 bool HasAnyRealCode = false ;
17001701 int NumInstsInFunction = 0 ;
17011702
1703+ // Reset YkLastCallLabel so we don't use it across different functions.
1704+ YkLastCallLabel = nullptr ;
1705+
17021706 bool CanDoExtraAnalysis = ORE->allowExtraAnalysis (DEBUG_TYPE);
17031707 for (auto &MBB : *MF) {
17041708 // Print a label for the basic block.
@@ -1800,6 +1804,31 @@ void AsmPrinter::emitFunctionBody() {
18001804 }
18011805
18021806 emitInstruction (&MI);
1807+ // Generate labels for function calls so we can record the correct
1808+ // instruction offset. The conditions for generating the label must be
1809+ // the same as the ones for generating the stackmap call in
1810+ // `Transforms/Yk/Stackmaps.cpp`, as otherwise we could end up with
1811+ // wrong offsets (e.g. we create a label here but the corresponding
1812+ // stackmap call was ommitted, and this label is then used for the
1813+ // following stackmap call).
1814+
1815+ // Convoluted way of finding our whether this function call is an
1816+ // intrinsic.
1817+ bool IsIntrinsic = false ;
1818+ if (MI.getNumExplicitDefs () < MI.getNumOperands ()) {
1819+ IsIntrinsic = MI.getOperand (MI.getNumExplicitDefs ()).isIntrinsicID ();
1820+ }
1821+ if (YkStackMapOffsetFix && MI.isCall () && !MI.isInlineAsm () &&
1822+ (MI.getOpcode () != TargetOpcode::STACKMAP) &&
1823+ (MI.getOpcode () != TargetOpcode::PATCHPOINT) && !IsIntrinsic) {
1824+ // YKFIXME: We don't need to emit labels (and stackmap calls) for
1825+ // functions that cannot guard fail, e.g. inlined functions, or
1826+ // functions we don't have IR for.
1827+ MCSymbol *MILabel =
1828+ OutStreamer->getContext ().createTempSymbol (" ykstackcall" , true );
1829+ OutStreamer->emitLabel (MILabel);
1830+ YkLastCallLabel = MILabel;
1831+ }
18031832 if (CanDoExtraAnalysis) {
18041833 MCInst MCI;
18051834 MCI.setOpcode (MI.getOpcode ());
0 commit comments