Skip to content

Fix mccas with maskray's changes #11063

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 39 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e105cf9
MC: Remove bundle alignment mode
MaskRay Jul 16, 2025
496fd25
[MCCAS] remove `writeFragmentPadding` references
cachemeifyoucan Jul 17, 2025
2c92989
MC: Restructure MCFragment as a fixed part and a variable tail
MaskRay Jul 16, 2025
ec83ebf
MCFragment: Remove MCDataFragment/MCRelaxableFragment type aliases
MaskRay Jul 16, 2025
4c55edc
MC: Rework .reloc directive and fix the offset when it evaluates to a…
MaskRay Jul 17, 2025
4fd2dd5
MCAssembler: Modify Contents when VarFixups is not empty
MaskRay Jul 17, 2025
9e89cb7
MC: Simplify fragment reuse determination
MaskRay Jul 18, 2025
7b6b5b5
MCFragment: Refactor LEB
MaskRay Jul 19, 2025
968a3e6
MC: Replace FT_PseudoProbe with FT_LEB
MaskRay Jul 19, 2025
c6b6e16
MCStreamer: Simplify with newFragment. NFC
MaskRay Jul 19, 2025
7e38cb1
WasmObjectWrier: Simplify fragment walk in .init_array
MaskRay Jul 19, 2025
bb4cbd2
MC: Optimize getOrCreateDataFragment
MaskRay Jul 19, 2025
1583f31
X86AsmBackend: Remove some overhead from auto padding feature
MaskRay Jul 20, 2025
95084d6
MC: Optimize emitInstruction and simplify fragment-in-BSS check
MaskRay Jul 20, 2025
1d923ea
MC: Encode FT_Align in fragment's variable-size tail
MaskRay Jul 20, 2025
fa3395e
MC: Refactor FT_Align fragments when linker relaxation is enabled
MaskRay Jul 20, 2025
96f121a
MC: Rename isVirtualSection to isBssSection
MaskRay Jul 20, 2025
cb6dd00
MC: Replace getOrCreateDataFragment with getCurrentFragment
MaskRay Jul 20, 2025
c7aab25
MC: Fix fragment-in-BSS check
MaskRay Jul 20, 2025
4e420df
WIP fix MCCAS from MaskRay MC changes, remove obsolete fragment types…
rastogishubham Jul 25, 2025
84bc22e
WIP MCCAS MaskRay Fix, fix MCRelaxableFragment
rastogishubham Jul 25, 2025
637e8f8
WIP MCCAS MaskRay Fix, fix MCDwarfCallFrameFragment
rastogishubham Jul 25, 2025
303cbbc
WIP MCCAS MaskRay Fix, fix MCPseudoProbeFragment
rastogishubham Jul 25, 2025
fa104f2
WIP MCCAS MaskRay Fix, fix MCLEBFragment
rastogishubham Jul 25, 2025
e41c5b2
WIP MCCAS MaskRay Fix, fix MCDwarfLineAddrFragment
rastogishubham Jul 25, 2025
8d4cdfc
WIP MCCAS MaskRay Fix, do some work for MCAlignFragment
rastogishubham Jul 25, 2025
f1102ae
WIP MCCAS MaskRay Fix, Fix some stuff for MCDataFragment
rastogishubham Jul 25, 2025
c404734
WIP MCCAS MaskRay Fix, Fix some more stuff for MCDataFragment
rastogishubham Jul 25, 2025
f656afc
WIP MCCAS MaskRayFix, Replace isVirtualSection with isBssSection
rastogishubham Jul 25, 2025
90d2cc1
WIP MCCAS MaskRay Fix, Change isa<MCAlignFragment> to F.getKind() == …
rastogishubham Jul 25, 2025
04ed4ab
WIP MCCAS MaskRay Fix, some more work for MCAlignFragment
rastogishubham Jul 25, 2025
4f997d5
MCCAS MaskRay Fix, a little more MCLEBFragment fix
rastogishubham Jul 25, 2025
6ee9a06
WIP MCCAS MaskRay Fix, fixed bug with no more classof function in MCE…
rastogishubham Jul 25, 2025
36becd8
WIP fix MCCAS, builds but round trip verification error due to FragTy…
rastogishubham Jul 25, 2025
e932c21
Revert "WIP fix MCCAS, builds but round trip verification error due t…
rastogishubham Jul 25, 2025
dea9801
WIP MCCAS MaskRay Fix, works for small cpp file but not on bootstrap …
rastogishubham Jul 25, 2025
3681cec
Edit MCAlignFragmentRef::create and materialize to match what is ther…
rastogishubham Jul 25, 2025
03db9b7
Fix getFragmentContents to match what we see in MCAssembler.cpp write…
rastogishubham Jul 25, 2025
a52419f
Fix MCAlignFragmentRef Size calc
rastogishubham Jul 25, 2025
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
5 changes: 3 additions & 2 deletions lld/ELF/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1671,8 +1671,9 @@ void RelocationScanner::scan(Relocs<RelTy> rels) {
}

// Sort relocations by offset for more efficient searching for
// R_RISCV_PCREL_HI20, R_PPC64_ADDR64 and the branch-to-branch optimization.
if (ctx.arg.emachine == EM_RISCV ||
// R_RISCV_PCREL_HI20, ALIGN relocations, R_PPC64_ADDR64 and the
// branch-to-branch optimization.
if (is_contained({EM_RISCV, EM_LOONGARCH}, ctx.arg.emachine) ||
(ctx.arg.emachine == EM_PPC64 && sec->name == ".toc") ||
ctx.arg.branchToBranch)
llvm::stable_sort(sec->relocs(),
Expand Down
47 changes: 16 additions & 31 deletions llvm/include/llvm/MC/MCAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@

namespace llvm {

class MCAlignFragment;
class MCDwarfCallFrameFragment;
class MCDwarfLineAddrFragment;
class MCFragment;
class MCLEBFragment;
class MCRelaxableFragment;
class MCSymbol;
class MCAssembler;
class MCContext;
Expand Down Expand Up @@ -66,6 +61,9 @@ class LLVM_ABI MCAsmBackend {

MCAssembler *Asm = nullptr;

bool AllowAutoPadding = false;
bool AllowEnhancedRelaxation = false;

public:
MCAsmBackend(const MCAsmBackend &) = delete;
MCAsmBackend &operator=(const MCAsmBackend &) = delete;
Expand All @@ -79,11 +77,11 @@ class LLVM_ABI MCAsmBackend {

/// Return true if this target might automatically pad instructions and thus
/// need to emit padding enable/disable directives around sensative code.
virtual bool allowAutoPadding() const { return false; }
bool allowAutoPadding() const { return AllowAutoPadding; }
/// Return true if this target allows an unrelaxable instruction to be
/// emitted into RelaxableFragment and then we can increase its size in a
/// tricky way for optimization.
virtual bool allowEnhancedRelaxation() const { return false; }
bool allowEnhancedRelaxation() const { return AllowEnhancedRelaxation; }

/// lifetime management
virtual void reset() {}
Expand Down Expand Up @@ -126,21 +124,6 @@ class LLVM_ABI MCAsmBackend {
/// Get information on a fixup kind.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const;

/// Hook to check if extra nop bytes must be inserted for alignment directive.
/// For some targets this may be necessary in order to support linker
/// relaxation. The number of bytes to insert are returned in Size.
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF,
unsigned &Size) {
return false;
}

/// Hook which indicates if the target requires a fixup to be generated when
/// handling an align directive in an executable section
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm,
MCAlignFragment &AF) {
return false;
}

// Evaluate a fixup, returning std::nullopt to use default handling for
// `Value` and `IsResolved`. Otherwise, returns `IsResolved` with the
// expectation that the hook updates `Value`.
Expand Down Expand Up @@ -175,8 +158,9 @@ class LLVM_ABI MCAsmBackend {

/// Target specific predicate for whether a given fixup requires the
/// associated instruction to be relaxed.
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &, const MCValue &,
uint64_t, bool Resolved) const;
virtual bool fixupNeedsRelaxationAdvanced(const MCFragment &, const MCFixup &,
const MCValue &, uint64_t,
bool Resolved) const;

/// Simple predicate for targets where !Resolved implies requiring relaxation
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
Expand All @@ -197,18 +181,20 @@ class LLVM_ABI MCAsmBackend {
}

// Defined by linker relaxation targets.
virtual bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF,
bool &WasRelaxed) const {

// Return false to use default handling. Otherwise, set `Size` to the number
// of padding bytes.
virtual bool relaxAlign(MCFragment &F, unsigned &Size) { return false; }
virtual bool relaxDwarfLineAddr(MCFragment &, bool &WasRelaxed) const {
return false;
}
virtual bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF,
bool &WasRelaxed) const {
virtual bool relaxDwarfCFA(MCFragment &, bool &WasRelaxed) const {
return false;
}

// Defined by linker relaxation targets to possibly emit LEB128 relocations
// and set Value at the relocated location.
virtual std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF,
virtual std::pair<bool, bool> relaxLEB128(MCFragment &,
int64_t &Value) const {
return std::make_pair(false, false);
}
Expand Down Expand Up @@ -246,8 +232,7 @@ class LLVM_ABI MCAsmBackend {

bool isDarwinCanonicalPersonality(const MCSymbol *Sym) const;

// Return STI for fragments of type MCRelaxableFragment and MCDataFragment
// with hasInstructions() == true.
// Return STI for fragments with hasInstructions() == true.
static const MCSubtargetInfo *getSubtargetInfo(const MCFragment &F);
};

Expand Down
49 changes: 14 additions & 35 deletions llvm/include/llvm/MC/MCAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,8 @@ namespace llvm {
class MCBoundaryAlignFragment;
class MCCVDefRangeFragment;
class MCCVInlineLineTableFragment;
class MCDwarfCallFrameFragment;
class MCDwarfLineAddrFragment;
class MCEncodedFragment;
class MCFragment;
class MCFixup;
class MCLEBFragment;
class MCPseudoProbeAddrFragment;
class MCRelaxableFragment;
class MCSymbolRefExpr;
class raw_ostream;
class MCAsmBackend;
Expand Down Expand Up @@ -72,6 +67,13 @@ class MCAssembler {

SmallVector<const MCSymbol *, 0> Symbols;

struct RelocDirective {
const MCExpr &Offset;
const MCExpr *Expr;
uint32_t Kind;
};
SmallVector<RelocDirective, 0> relocDirectives;

mutable SmallVector<std::pair<SMLoc, std::string>, 0> PendingErrors;

MCDwarfLineTableParams LTParams;
Expand All @@ -85,11 +87,6 @@ class MCAssembler {
// refactoring too.
mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;

/// The bundle alignment size currently set in the assembler.
///
/// By default it's 0, which means bundling is disabled.
unsigned BundleAlignSize = 0;

/// Evaluate a fixup to a relocatable expression and the value which should be
/// placed into the fixup.
///
Expand All @@ -107,7 +104,7 @@ class MCAssembler {

/// Check whether a fixup can be satisfied, or whether it needs to be relaxed
/// (increased in size, in order to hold its value correctly).
bool fixupNeedsRelaxation(const MCRelaxableFragment &, const MCFixup &) const;
bool fixupNeedsRelaxation(const MCFragment &, const MCFixup &) const;

void layoutSection(MCSection &Sec);
/// Perform one layout iteration and return the index of the first stable
Expand All @@ -116,15 +113,14 @@ class MCAssembler {

/// Perform relaxation on a single fragment.
bool relaxFragment(MCFragment &F);
bool relaxInstruction(MCRelaxableFragment &IF);
bool relaxLEB(MCLEBFragment &IF);
bool relaxInstruction(MCFragment &F);
bool relaxLEB(MCFragment &F);
bool relaxBoundaryAlign(MCBoundaryAlignFragment &BF);
bool relaxDwarfLineAddr(MCDwarfLineAddrFragment &DF);
bool relaxDwarfCallFrameFragment(MCDwarfCallFrameFragment &DF);
bool relaxDwarfLineAddr(MCFragment &F);
bool relaxDwarfCallFrameFragment(MCFragment &F);
bool relaxCVInlineLineTable(MCCVInlineLineTableFragment &DF);
bool relaxCVDefRange(MCCVDefRangeFragment &DF);
bool relaxFill(MCFillFragment &F);
bool relaxPseudoProbeAddr(MCPseudoProbeAddrFragment &DF);

public:
/// Construct a new assembler instance.
Expand All @@ -143,8 +139,6 @@ class MCAssembler {
/// Compute the effective fragment size.
LLVM_ABI uint64_t computeFragmentSize(const MCFragment &F) const;

LLVM_ABI void layoutBundle(MCFragment *Prev, MCFragment *F) const;

// Get the offset of the given fragment inside its containing section.
uint64_t getFragmentOffset(const MCFragment &F) const { return F.Offset; }

Expand Down Expand Up @@ -203,16 +197,6 @@ class MCAssembler {
bool getRelaxAll() const { return RelaxAll; }
void setRelaxAll(bool Value) { RelaxAll = Value; }

bool isBundlingEnabled() const { return BundleAlignSize != 0; }

unsigned getBundleAlignSize() const { return BundleAlignSize; }

void setBundleAlignSize(unsigned Size) {
assert((Size == 0 || !(Size & (Size - 1))) &&
"Expect a power-of-two bundle align size");
BundleAlignSize = Size;
}

const_iterator begin() const { return Sections.begin(); }
const_iterator end() const { return Sections.end(); }

Expand All @@ -225,12 +209,7 @@ class MCAssembler {

LLVM_ABI bool registerSection(MCSection &Section);
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol);

/// Write the necessary bundle padding to \p OS.
/// Expects a fragment \p F containing instructions and its size \p FSize.
LLVM_ABI void writeFragmentPadding(raw_ostream &OS,
const MCEncodedFragment &F,
uint64_t FSize) const;
void addRelocDirective(RelocDirective RD);

LLVM_ABI void reportError(SMLoc L, const Twine &Msg) const;
// Record pending errors during layout iteration, as they may go away once the
Expand Down
3 changes: 1 addition & 2 deletions llvm/include/llvm/MC/MCCodeView.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace llvm {
class MCAssembler;
class MCCVDefRangeFragment;
class MCCVInlineLineTableFragment;
class MCDataFragment;
class MCFragment;
class MCSection;
class MCSymbol;
Expand Down Expand Up @@ -231,7 +230,7 @@ class CodeViewContext {
StringMap<unsigned> StringTable;

/// The fragment that ultimately holds our strings.
MCDataFragment *StrTabFragment = nullptr;
MCFragment *StrTabFragment = nullptr;
SmallVector<char, 0> StrTab = {'\0'};

/// Get a string table offset.
Expand Down
3 changes: 1 addition & 2 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace llvm {

class CodeViewContext;
class MCAsmInfo;
class MCDataFragment;
class MCInst;
class MCLabel;
class MCObjectFileInfo;
Expand Down Expand Up @@ -334,7 +333,7 @@ class MCContext {
void reportCommon(SMLoc Loc,
std::function<void(SMDiagnostic &, const SourceMgr *)>);

MCDataFragment *allocInitialFragment(MCSection &Sec);
MCFragment *allocInitialFragment(MCSection &Sec);

MCSymbolTableEntry &getSymbolTableEntry(StringRef Name);

Expand Down
18 changes: 3 additions & 15 deletions llvm/include/llvm/MC/MCELFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace llvm {

class ELFObjectWriter;
class MCContext;
class MCDataFragment;
class MCFragment;
class MCObjectWriter;
class MCSection;
Expand Down Expand Up @@ -51,7 +50,7 @@ class MCELFStreamer : public MCObjectStreamer {
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override;
void changeSection(MCSection *Section, uint32_t Subsection = 0) override;
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F,
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
uint64_t Offset) override;
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override;
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
Expand All @@ -65,24 +64,15 @@ class MCELFStreamer : public MCObjectStreamer {
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Align ByteAlignment) override;

void emitValueImpl(const MCExpr *Value, unsigned Size,
SMLoc Loc = SMLoc()) override;

void emitIdent(StringRef IdentString) override;

void emitValueToAlignment(Align, int64_t, uint8_t, unsigned) override;

void emitCGProfileEntry(const MCSymbolRefExpr *From,
const MCSymbolRefExpr *To, uint64_t Count) override;

// This is final. Override MCTargetStreamer::finish instead for
// target-specific code.
void finishImpl() final;

void emitBundleAlignMode(Align Alignment) override;
void emitBundleLock(bool AlignToEnd) override;
void emitBundleUnlock() override;

/// ELF object attributes section emission support
struct AttributeItem {
// This structure holds all attributes, accounting for their string /
Expand Down Expand Up @@ -151,10 +141,8 @@ class MCELFStreamer : public MCObjectStreamer {
}

private:
bool isBundleLocked() const;
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &) override;

void finalizeCGProfileEntry(const MCSymbolRefExpr *&S, uint64_t Offset);
void finalizeCGProfileEntry(const MCSymbolRefExpr *Sym, uint64_t Offset,
const MCSymbolRefExpr *&S);
void finalizeCGProfile();

bool SeenIdent = false;
Expand Down
37 changes: 4 additions & 33 deletions llvm/include/llvm/MC/MCObjectStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ class MCObjectStreamer : public MCStreamer {
std::unique_ptr<MCAssembler> Assembler;
bool EmitEHFrame;
bool EmitDebugFrame;
struct PendingMCFixup {
const MCSymbol *Sym;
MCFixup Fixup;
MCDataFragment *DF;
PendingMCFixup(const MCSymbol *McSym, MCDataFragment *F, MCFixup McFixup)
: Sym(McSym), Fixup(McFixup), DF(F) {}
};
SmallVector<PendingMCFixup, 2> PendingFixups;

struct PendingAssignment {
MCSymbol *Symbol;
Expand All @@ -59,11 +51,9 @@ class MCObjectStreamer : public MCStreamer {
DenseMap<const MCSymbol *, SmallVector<PendingAssignment, 1>>
pendingAssignments;

virtual void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &);
void emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) override;
void emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override;
void emitInstructionImpl(const MCInst &Inst, const MCSubtargetInfo &STI);
void resolvePendingFixups();

protected:
MCObjectStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
Expand All @@ -82,21 +72,6 @@ class MCObjectStreamer : public MCStreamer {
MCSymbol *emitCFILabel() override;
void emitCFISections(bool EH, bool Debug) override;

void insert(MCFragment *F) {
auto *Sec = CurFrag->getParent();
F->setParent(Sec);
F->setLayoutOrder(CurFrag->getLayoutOrder() + 1);
CurFrag->Next = F;
CurFrag = F;
Sec->curFragList()->Tail = F;
}

/// Get a data fragment to write into, creating a new one if the current
/// fragment is not a data fragment.
/// Optionally a \p STI can be passed in so that a new fragment is created
/// if the Subtarget differs from the current fragment.
MCDataFragment *getOrCreateDataFragment(const MCSubtargetInfo* STI = nullptr);

protected:
bool changeSectionImpl(MCSection *Section, uint32_t Subsection);

Expand All @@ -109,7 +84,7 @@ class MCObjectStreamer : public MCStreamer {
/// @{

void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCDataFragment &F,
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
uint64_t Offset);
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
void emitConditionalAssignment(MCSymbol *Symbol,
Expand All @@ -127,9 +102,6 @@ class MCObjectStreamer : public MCStreamer {
/// can change its size during relaxation.
void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &);

void emitBundleAlignMode(Align Alignment) override;
void emitBundleLock(bool AlignToEnd) override;
void emitBundleUnlock() override;
void emitBytes(StringRef Data) override;
void emitValueToAlignment(Align Alignment, int64_t Fill = 0,
uint8_t FillLen = 1,
Expand Down Expand Up @@ -165,9 +137,8 @@ class MCObjectStreamer : public MCStreamer {
void emitCVStringTableDirective() override;
void emitCVFileChecksumsDirective() override;
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override;
std::optional<std::pair<bool, std::string>>
emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr,
SMLoc Loc, const MCSubtargetInfo &STI) override;
void emitRelocDirective(const MCExpr &Offset, StringRef Name,
const MCExpr *Expr, SMLoc Loc = {}) override;
using MCStreamer::emitFill;
void emitFill(const MCExpr &NumBytes, uint64_t FillValue,
SMLoc Loc = SMLoc()) override;
Expand Down
Loading