Skip to content

[MC][ELF] Emit instructions directly into fragment #94950

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

Merged
merged 3 commits into from
Jul 4, 2024
Merged
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
33 changes: 10 additions & 23 deletions llvm/lib/MC/MCELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,12 +511,6 @@ static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
void MCELFStreamer::emitInstToData(const MCInst &Inst,
const MCSubtargetInfo &STI) {
MCAssembler &Assembler = getAssembler();
SmallVector<MCFixup, 4> Fixups;
SmallString<256> Code;
Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);

for (auto &Fixup : Fixups)
fixSymbolsInTLSFixups(Fixup.getValue());

// There are several possibilities here:
//
Expand All @@ -526,9 +520,7 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
//
// If bundling is enabled:
// - If we're not in a bundle-locked group, emit the instruction into a
// fragment of its own. If there are no fixups registered for the
// instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
// MCDataFragment.
// fragment of its own.
// - If we're in a bundle-locked group, append the instruction to the current
// data fragment because we want all the instructions in a group to get into
// the same fragment. Be careful not to do that for the first instruction in
Expand All @@ -542,16 +534,6 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
// The bundle-locking directive ensures this is a new data fragment.
DF = cast<MCDataFragment>(getCurrentFragment());
CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
} else if (!isBundleLocked() && Fixups.size() == 0) {
// Optimize memory usage by emitting the instruction to a
// MCCompactEncodedInstFragment when not in a bundle-locked group and
// there are no fixups registered.
MCCompactEncodedInstFragment *CEIF =
getContext().allocFragment<MCCompactEncodedInstFragment>();
insert(CEIF);
CEIF->getContents().append(Code.begin(), Code.end());
CEIF->setHasInstructions(STI);
return;
} else {
DF = getContext().allocFragment<MCDataFragment>();
insert(DF);
Expand All @@ -571,17 +553,22 @@ void MCELFStreamer::emitInstToData(const MCInst &Inst,
DF = getOrCreateDataFragment(&STI);
}

// Add the fixups and data.
// Emit instruction directly into data fragment.
size_t FixupStartIndex = DF->getFixups().size();
size_t CodeOffset = DF->getContents().size();
Assembler.getEmitter().encodeInstruction(Inst, DF->getContents(),
DF->getFixups(), STI);

auto Fixups = MutableArrayRef(DF->getFixups()).slice(FixupStartIndex);
for (auto &Fixup : Fixups) {
Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
DF->getFixups().push_back(Fixup);
Fixup.setOffset(Fixup.getOffset() + CodeOffset);
fixSymbolsInTLSFixups(Fixup.getValue());
}

DF->setHasInstructions(STI);
if (!Fixups.empty() && Fixups.back().getTargetKind() ==
getAssembler().getBackend().RelaxFixupKind)
DF->setLinkerRelaxable();
DF->getContents().append(Code.begin(), Code.end());
}

void MCELFStreamer::emitBundleAlignMode(Align Alignment) {
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 @@ -393,10 +393,8 @@ void MCObjectStreamer::emitInstToFragment(const MCInst &Inst,
getContext().allocFragment<MCRelaxableFragment>(Inst, STI);
insert(IF);

SmallString<128> Code;
getAssembler().getEmitter().encodeInstruction(Inst, Code, IF->getFixups(),
STI);
IF->getContents().append(Code.begin(), Code.end());
getAssembler().getEmitter().encodeInstruction(Inst, IF->getContents(),
IF->getFixups(), STI);
}

#ifndef NDEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ uint64_t SystemZMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNum,
uint32_t BitOffset = MIBitSize - RawBitOffset - OpBitSize;
Fixups.push_back(MCFixup::create(BitOffset >> 3, MO.getExpr(),
(MCFixupKind)Kind, MI.getLoc()));
assert(Fixups.size() <= 2 && "More than two memory operands in MI?");
return 0;
}
llvm_unreachable("Unexpected operand type!");
Expand Down
Loading