Skip to content

Commit

Permalink
Fix root cause of failing test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
memfrob committed Sep 15, 2022
1 parent c2aef6c commit d85c8d4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion llvm/include/llvm/CodeGen/MachineInstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ class MachineInstr
IsCall = 1 << 22,
IsReturn = 1 << 23,
IsBranch = 1 << 24,
MaxFlagShift = 24
IsTailCall = 1 << 25, // A branch instruction that also acts as a tail call.
MaxFlagShift = 25
};

private:
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/MachineInstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,8 @@ void MachineInstr::print(raw_ostream &OS, ModuleSlotTracker &MST,
OS << "is-return ";
if (getFlag(MachineInstr::IsBranch))
OS << "is-branch ";
if (getFlag(MachineInstr::IsTailCall))
OS << "is-tail-call ";

// Print the opcode name.
if (TII)
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/RISCV/ISPAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,13 @@ static void LowerToSSITHEpilogStore64(const MachineInstr *MI, MCInst &OutMI,
}

void ISPAsmPrinter::EmitInstruction(const MachineInstr *MI) {

RISCVAsmPrinter::EmitInstruction(MI);

// fn range avoids NoCFI on C code stuff
// TODO: this may or may not be in the "right" place...
if(MI->isReturn())
if(MI->isReturn() || MI->getFlag(MachineInstr::IsTailCall)) {
EmitFnRangeMetadata(CurrentFnSym, OutContext.createTempSymbol());

}
//SSITH - clean up in function epilog
if(MI->getFlag(MachineInstr::FnEpilog) && MI->getOpcode() == RISCV::LW){
//Emit our new store
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Target/RISCV/ISPMetadataPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ static void setMIFlags(MachineInstr *MI) {
MI->setFlag(MachineInstr::IsReturn);
MI->setFlag(MachineInstr::FnEpilog);
}

// These two used to use MI->setFlag() instead, which broke the tailcall tagging.
// I tried to see whether machine instructions ever have flags that may cause issues,
// but I couldn't find any cases of that.
else if ( MI->isCall() )
MI->setFlags(MachineInstr::IsCall);
MI->setFlag(MachineInstr::IsCall);
else if ( MI->isBranch() )
MI->setFlags(MachineInstr::IsBranch);
MI->setFlag(MachineInstr::IsBranch);

}

Expand Down
11 changes: 8 additions & 3 deletions llvm/lib/Target/RISCV/RISCVExpandPseudoInsts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,17 @@ bool RISCVExpandPseudo::expandPseudoBranch(MachineBasicBlock &MBB,
.add(*Func);

if (MI.getOpcode() == RISCV::PseudoTAIL ||
MI.getOpcode() == RISCV::PseudoJump)
MI.getOpcode() == RISCV::PseudoJump) {
// Emit JALR X0, Ra, 0
BuildMI(MBB, NextMBBI, DL, TII->get(RISCV::JALR), RISCV::X0)
auto &JMI = BuildMI(MBB, NextMBBI, DL, TII->get(RISCV::JALR), RISCV::X0)
.addReg(Ra)
.addImm(0);
else

// Mark tailcall instructions so that function range metadata is still correct.
if (MI.getOpcode() == RISCV::PseudoTAIL) {
JMI.setMIFlag(MachineInstr::IsTailCall);
}
} else
// Emit JALR Ra, Ra, 0
BuildMI(MBB, NextMBBI, DL, TII->get(RISCV::JALR), Ra)
.addReg(Ra)
Expand Down

0 comments on commit d85c8d4

Please sign in to comment.