Skip to content

MC,AMDGPU: Don't pad .text with s_code_end if it would otherwise be empty #147980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/lib/MC/MCAsmStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,8 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,

void MCAsmStreamer::emitInstruction(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCStreamer::emitInstruction(Inst, STI);

if (MAI->isAIX() && CurFrag)
// Now that a machine instruction has been assembled into this section, make
// a line entry for any .loc directive that has been seen.
Expand Down
6 changes: 2 additions & 4 deletions llvm/lib/MC/MCObjectStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,10 @@ void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCStreamer::emitInstruction(Inst, STI);

MCSection *Sec = getCurrentSectionOnly();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emitInstruction is a performance bottleneck. Caution needed when making changes.

getCurrentSectionOnly is efficient. getCurrentSection is not, and the nullness check is redundant. I haven't checked in detail, but adding Sec->setHasInstructions(true); to MCAsmStreamer is likely better .

Sec->setHasInstructions(true);

// Now that a machine instruction has been assembled into this section, make
// a line entry for any .loc directive that has been seen.
MCDwarfLineEntry::make(this, getCurrentSectionOnly());
MCSection *Sec = getCurrentSectionOnly();
MCDwarfLineEntry::make(this, Sec);

// If this instruction doesn't need relaxation, just emit it as data.
MCAssembler &Assembler = getAssembler();
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/MC/MCStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,9 @@ void MCStreamer::visitUsedExpr(const MCExpr &Expr) {
}

void MCStreamer::emitInstruction(const MCInst &Inst, const MCSubtargetInfo &) {
if (MCSection *Sec = getCurrentSection().first)
Sec->setHasInstructions(true);

// Scan for values.
for (unsigned i = Inst.getNumOperands(); i--;)
if (Inst.getOperand(i).isExpr())
Expand Down
8 changes: 6 additions & 2 deletions llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,16 @@ bool AMDGPUAsmPrinter::doFinalization(Module &M) {
// Pad with s_code_end to help tools and guard against instruction prefetch
// causing stale data in caches. Arguably this should be done by the linker,
// which is why this isn't done for Mesa.
// Don't do it if there is no code.
const MCSubtargetInfo &STI = *getGlobalSTI();
if ((AMDGPU::isGFX10Plus(STI) || AMDGPU::isGFX90A(STI)) &&
(STI.getTargetTriple().getOS() == Triple::AMDHSA ||
STI.getTargetTriple().getOS() == Triple::AMDPAL)) {
OutStreamer->switchSection(getObjFileLowering().getTextSection());
getTargetStreamer()->EmitCodeEnd(STI);
MCSection *TextSect = getObjFileLowering().getTextSection();
if (TextSect->hasInstructions()) {
OutStreamer->switchSection(TextSect);
getTargetStreamer()->EmitCodeEnd(STI);
}
}

// Assign expressions which can only be resolved when all other functions are
Expand Down
9 changes: 9 additions & 0 deletions llvm/test/CodeGen/AMDGPU/empty-text.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; Test that there is no s_code_end padding if .text is othertwise empty.

; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx1200 < %s | FileCheck %s --check-prefixes=GCN

@globalVar = global i32 37

declare amdgpu_ps void @funcDecl()

; GCN-NOT: .fill