Skip to content

Commit e99c565

Browse files
authored
MC,AMDGPU: Don't pad .text with s_code_end if it would otherwise be empty (#147980)
We don't want that padding in a module that only contains data, not code. Also fix MCSection::hasInstructions() so it works with the asm streamer too.
1 parent d8f8961 commit e99c565

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,11 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
24322432

24332433
void MCAsmStreamer::emitInstruction(const MCInst &Inst,
24342434
const MCSubtargetInfo &STI) {
2435+
if (CurFrag) {
2436+
MCSection *Sec = getCurrentSectionOnly();
2437+
Sec->setHasInstructions(true);
2438+
}
2439+
24352440
if (MAI->isAIX() && CurFrag)
24362441
// Now that a machine instruction has been assembled into this section, make
24372442
// a line entry for any .loc directive that has been seen.

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,16 @@ bool AMDGPUAsmPrinter::doFinalization(Module &M) {
486486
// Pad with s_code_end to help tools and guard against instruction prefetch
487487
// causing stale data in caches. Arguably this should be done by the linker,
488488
// which is why this isn't done for Mesa.
489+
// Don't do it if there is no code.
489490
const MCSubtargetInfo &STI = *getGlobalSTI();
490491
if ((AMDGPU::isGFX10Plus(STI) || AMDGPU::isGFX90A(STI)) &&
491492
(STI.getTargetTriple().getOS() == Triple::AMDHSA ||
492493
STI.getTargetTriple().getOS() == Triple::AMDPAL)) {
493-
OutStreamer->switchSection(getObjFileLowering().getTextSection());
494-
getTargetStreamer()->EmitCodeEnd(STI);
494+
MCSection *TextSect = getObjFileLowering().getTextSection();
495+
if (TextSect->hasInstructions()) {
496+
OutStreamer->switchSection(TextSect);
497+
getTargetStreamer()->EmitCodeEnd(STI);
498+
}
495499
}
496500

497501
// Assign expressions which can only be resolved when all other functions are
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; Test that there is no s_code_end padding if .text is otherwise empty.
2+
3+
; RUN: llc -mtriple=amdgcn--amdpal -mcpu=gfx1200 < %s | FileCheck %s --check-prefixes=GCN
4+
5+
@globalVar = global i32 37
6+
7+
declare amdgpu_ps void @funcDecl()
8+
9+
; GCN-NOT: .fill

0 commit comments

Comments
 (0)