Skip to content

[RISCV] Put Large Code Model Constant Pools in .text #151393

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 6 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
17 changes: 9 additions & 8 deletions llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
/// Given a constant with the SectionKind, return a section that it should be
/// placed in.
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;

/// Similar to the function above, but append \p SectionSuffix to the section
/// name.
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C, Align &Alignment,
const Function *F,
StringRef SectionSuffix) const override;

MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
Expand Down Expand Up @@ -152,8 +153,8 @@ class TargetLoweringObjectFileMachO : public TargetLoweringObjectFile {
const TargetMachine &TM) const override;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;

/// The mach-o version of this method defaults to returning a stub reference.
const MCExpr *getTTypeGlobalReference(const GlobalValue *GV,
Expand Down Expand Up @@ -221,8 +222,8 @@ class TargetLoweringObjectFileCOFF : public TargetLoweringObjectFile {
/// Given a mergeable constant with the specified size and relocation
/// information, return a section that it should be placed in.
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;
};

class TargetLoweringObjectFileWasm : public TargetLoweringObjectFile {
Expand Down Expand Up @@ -283,8 +284,8 @@ class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
/// Given a constant with the SectionKind, return a section that it should be
/// placed in.
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;

static XCOFF::StorageClass getStorageClassForGlobal(const GlobalValue *GV);

Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/Target/TargetLoweringObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
/// placed in.
virtual MCSection *getSectionForConstant(const DataLayout &DL,
SectionKind Kind, const Constant *C,
Align &Alignment) const;
Align &Alignment,
const Function *F) const;

/// Similar to the function above, but append \p SectionSuffix to the section
/// name.
virtual MCSection *getSectionForConstant(const DataLayout &DL,
SectionKind Kind, const Constant *C,
Align &Alignment,
Align &Alignment, const Function *F,
StringRef SectionSuffix) const;

virtual MCSection *
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,7 +2893,8 @@ void AsmPrinter::emitConstantPool() {
C = CPE.Val.ConstVal;

MCSection *S = getObjFileLowering().getSectionForConstant(
getDataLayout(), Kind, C, Alignment, getConstantSectionSuffix(C));
getDataLayout(), Kind, C, Alignment, &MF->getFunction(),
getConstantSectionSuffix(C));

// The number of sections are small, just do a linear search from the
// last section to the first.
Expand Down Expand Up @@ -4221,8 +4222,8 @@ MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
SectionKind Kind = CPE.getSectionKind(&DL);
const Constant *C = CPE.Val.ConstVal;
Align Alignment = CPE.Alignment;
auto *S =
getObjFileLowering().getSectionForConstant(DL, Kind, C, Alignment);
auto *S = getObjFileLowering().getSectionForConstant(
DL, Kind, C, Alignment, &MF->getFunction());
if (S && TM.getTargetTriple().isOSBinFormatCOFF()) {
if (MCSymbol *Sym =
static_cast<const MCSectionCOFF *>(S)->getCOMDATSymbol()) {
Expand Down
24 changes: 12 additions & 12 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,8 +1033,8 @@ bool TargetLoweringObjectFileELF::shouldPutJumpTableInFunctionSection(
/// Given a mergeable constant with the specified size and relocation
/// information, return a section that it should be placed in.
MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (Kind.isMergeableConst4() && MergeableConst4Section)
return MergeableConst4Section;
if (Kind.isMergeableConst8() && MergeableConst8Section)
Expand All @@ -1052,11 +1052,11 @@ MCSection *TargetLoweringObjectFileELF::getSectionForConstant(

MCSection *TargetLoweringObjectFileELF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
StringRef SectionSuffix) const {
const Function *F, StringRef SectionSuffix) const {
// TODO: Share code between this function and
// MCObjectInfo::initELFMCObjectFileInfo.
if (SectionSuffix.empty())
return getSectionForConstant(DL, Kind, C, Alignment);
return getSectionForConstant(DL, Kind, C, Alignment, F);

auto &Context = getContext();
if (Kind.isMergeableConst4() && MergeableConst4Section)
Expand Down Expand Up @@ -1469,8 +1469,8 @@ MCSection *TargetLoweringObjectFileMachO::SelectSectionForGlobal(
}

MCSection *TargetLoweringObjectFileMachO::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
// If this constant requires a relocation, we have to put it in the data
// segment, not in the text segment.
if (Kind.isData() || Kind.isReadOnlyWithRel())
Expand Down Expand Up @@ -2132,8 +2132,8 @@ static std::string scalarConstantToHexString(const Constant *C) {
}

MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (Kind.isMergeableConst() && C &&
getContext().getAsmInfo()->hasCOFFComdatConstants()) {
// This creates comdat sections with the given symbol name, but unless
Expand Down Expand Up @@ -2173,8 +2173,8 @@ MCSection *TargetLoweringObjectFileCOFF::getSectionForConstant(
COFF::IMAGE_COMDAT_SELECT_ANY);
}

return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C,
Alignment);
return TargetLoweringObjectFile::getSectionForConstant(DL, Kind, C, Alignment,
F);
}

//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -2595,8 +2595,8 @@ bool TargetLoweringObjectFileXCOFF::shouldPutJumpTableInFunctionSection(
/// Given a mergeable constant with the specified size and relocation
/// information, return a section that it should be placed in.
MCSection *TargetLoweringObjectFileXCOFF::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
// TODO: Enable emiting constant pool to unique sections when we support it.
if (Alignment > Align(16))
report_fatal_error("Alignments greater than 16 not yet supported.");
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/Lanai/LanaiTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ bool LanaiTargetObjectFile::isConstantInSmallSection(const DataLayout &DL,
}

MCSection *LanaiTargetObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (isConstantInSmallSection(DL, C))
return SmallDataSection;

// Otherwise, we work the same as ELF.
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
Alignment);
Alignment, F);
}
4 changes: 2 additions & 2 deletions llvm/lib/Target/Lanai/LanaiTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class LanaiTargetObjectFile : public TargetLoweringObjectFileELF {
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;
};
} // end namespace llvm

Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Target/Mips/MipsTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,15 @@ bool MipsTargetObjectFile::IsConstantInSmallSection(
}

/// Return true if this constant should be placed into small data section.
MCSection *MipsTargetObjectFile::getSectionForConstant(const DataLayout &DL,
SectionKind Kind,
const Constant *C,
Align &Alignment) const {
MCSection *MipsTargetObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (IsConstantInSmallSection(DL, C, *TM))
return SmallDataSection;

// Otherwise, we work the same as ELF.
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
Alignment);
Alignment, F);
}

const MCExpr *
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/Mips/MipsTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class MipsTargetMachine;
const TargetMachine &TM) const;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;
/// Describe a TLS variable address within debug info.
const MCExpr *getDebugThreadLocalSymbol(const MCSymbol *Sym) const override;
};
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/NVPTX/NVPTXTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class NVPTXTargetObjectFile : public TargetLoweringObjectFile {
~NVPTXTargetObjectFile() override;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override {
const Constant *C, Align &Alignment,
const Function *F) const override {
return ReadOnlySection;
}

Expand Down
18 changes: 15 additions & 3 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,20 @@ bool RISCVELFTargetObjectFile::isConstantInSmallSection(
}

MCSection *RISCVELFTargetObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {

// The large code model has to put constant pools close to the program, so we
// put them in the .text section. Large code model doesn't support PIC, so
// there should be no dynamic relocations that would require `.data.rel.ro`
// (which could be too far away anyway).
if (TM->getCodeModel() == CodeModel::Large) {
if (F)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it actually possible for F to be null here? It looks like every caller passes in a Function.

Copy link
Member Author

Choose a reason for hiding this comment

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

It will never make it from the call-site to here, but the X86 Asm Printer makes a call without a function, which is why I chose a pointer rather than a reference, and pointers need to be checked for null-ness. It won't be hit, but I'd prefer it to cope with nullptr in case there are more call-sites in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Especially as it is not the case that every Constant has a Function, at the conceptual level in LLVM

Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally, if you actually need a constant, either you're emitting a function, or you're emitting a global. Otherwise, what's actually going to use the constant?

But I guess it's not a big deal either way.

return SectionForGlobal(F, SectionKind::getText(), *TM);
else
return TextSection;
}

if (C && isConstantInSmallSection(DL, C)) {
if (Kind.isMergeableConst4())
return SmallROData4Section;
Expand All @@ -177,5 +189,5 @@ MCSection *RISCVELFTargetObjectFile::getSectionForConstant(

// Otherwise, we work the same as ELF.
return TargetLoweringObjectFileELF::getSectionForConstant(DL, Kind, C,
Alignment);
Alignment, F);
}
4 changes: 2 additions & 2 deletions llvm/lib/Target/RISCV/RISCVTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF {
bool isConstantInSmallSection(const DataLayout &DL, const Constant *CN) const;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;

void getModuleMetadata(Module &M) override;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/SPIRV/SPIRVTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class SPIRVTargetObjectFile : public TargetLoweringObjectFile {
// sequence of instructions in a specific order. We put all the instructions
// in the single text section.
MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override {
const Constant *C, Align &Alignment,
const Function *F) const override {
return TextSection;
}
MCSection *getExplicitSectionGlobal(const GlobalObject *GO, SectionKind Kind,
Expand Down
13 changes: 6 additions & 7 deletions llvm/lib/Target/TargetLoweringObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,8 @@ MCSection *TargetLoweringObjectFile::getSectionForJumpTable(
const Function &F, const TargetMachine &TM,
const MachineJumpTableEntry *JTE) const {
Align Alignment(1);
return getSectionForConstant(F.getDataLayout(),
SectionKind::getReadOnly(), /*C=*/nullptr,
Alignment);
return getSectionForConstant(F.getDataLayout(), SectionKind::getReadOnly(),
/*C=*/nullptr, Alignment, &F);
}

bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
Expand All @@ -406,8 +405,8 @@ bool TargetLoweringObjectFile::shouldPutJumpTableInFunctionSection(
/// Given a mergable constant with the specified size and relocation
/// information, return a section that it should be placed in.
MCSection *TargetLoweringObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (Kind.isReadOnly() && ReadOnlySection != nullptr)
return ReadOnlySection;

Expand All @@ -416,11 +415,11 @@ MCSection *TargetLoweringObjectFile::getSectionForConstant(

MCSection *TargetLoweringObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
StringRef SectionPrefix) const {
const Function *F, StringRef SectionPrefix) const {
// Fallback to `getSectionForConstant` without `SectionPrefix` parameter if it
// is empty.
if (SectionPrefix.empty())
return getSectionForConstant(DL, Kind, C, Alignment);
return getSectionForConstant(DL, Kind, C, Alignment, F);
report_fatal_error(
"TargetLoweringObjectFile::getSectionForConstant that "
"accepts SectionPrefix is not implemented for the object file format");
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/X86/X86AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ void X86AsmPrinter::emitEndOfAsmFile(Module &M) {
Align Alignment(1);
MCSection *ReadOnlySection = getObjFileLowering().getSectionForConstant(
getDataLayout(), SectionKind::getReadOnly(),
/*C=*/nullptr, Alignment);
/*C=*/nullptr, Alignment, /*F=*/nullptr);
OutStreamer->switchSection(ReadOnlySection);
OutStreamer->emitLabel(AddrSymbol);

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/XCore/XCoreTargetObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ MCSection *XCoreTargetObjectFile::SelectSectionForGlobal(
}

MCSection *XCoreTargetObjectFile::getSectionForConstant(
const DataLayout &DL, SectionKind Kind, const Constant *C,
Align &Alignment) const {
const DataLayout &DL, SectionKind Kind, const Constant *C, Align &Alignment,
const Function *F) const {
if (Kind.isMergeableConst4()) return MergeableConst4Section;
if (Kind.isMergeableConst8()) return MergeableConst8Section;
if (Kind.isMergeableConst16()) return MergeableConst16Section;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/XCore/XCoreTargetObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ static const unsigned CodeModelLargeSize = 256;
const TargetMachine &TM) const override;

MCSection *getSectionForConstant(const DataLayout &DL, SectionKind Kind,
const Constant *C,
Align &Alignment) const override;
const Constant *C, Align &Alignment,
const Function *F) const override;
};
} // end namespace llvm

Expand Down
Loading