Skip to content

Commit

Permalink
[Xtensa] Implement XtensaNullTargetStreamer
Browse files Browse the repository at this point in the history
It fixes crash in Xtensa AsmParser::run() during ModuleSummaryIndexAnalysis pass.
  • Loading branch information
gerekon committed Dec 26, 2024
1 parent ba0c330 commit 2befb94
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 2 additions & 0 deletions llvm/lib/Target/Xtensa/AsmParser/XtensaAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class XtensaAsmParser : public MCTargetAsmParser {
SMLoc getLoc() const { return getParser().getTok().getLoc(); }

XtensaTargetStreamer &getTargetStreamer() {
assert(getParser().getStreamer().getTargetStreamer() &&
"do not have a target streamer");
MCTargetStreamer &TS = *getParser().getStreamer().getTargetStreamer();
return static_cast<XtensaTargetStreamer &>(TS);
}
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Target/Xtensa/MCTargetDesc/XtensaMCTargetDesc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ createXtensaObjectTargetStreamer(MCStreamer &S, const MCSubtargetInfo &STI) {
return new XtensaTargetELFStreamer(S);
}

static MCTargetStreamer *createXtensaNullTargetStreamer(MCStreamer &S) {
return new XtensaTargetStreamer(S);
}

extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaTargetMC() {
// Register the MCAsmInfo.
TargetRegistry::RegisterMCAsmInfo(getTheXtensaTarget(), createXtensaMCAsmInfo);
Expand Down Expand Up @@ -119,4 +123,8 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaTargetMC() {
// Register the ELF target streamer.
TargetRegistry::RegisterObjectTargetStreamer(
getTheXtensaTarget(), createXtensaObjectTargetStreamer);

// Register the null target streamer.
TargetRegistry::RegisterNullTargetStreamer(getTheXtensaTarget(),
createXtensaNullTargetStreamer);
}
12 changes: 4 additions & 8 deletions llvm/lib/Target/Xtensa/MCTargetDesc/XtensaTargetStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class XtensaTargetStreamer : public MCTargetStreamer {

public:
XtensaTargetStreamer(MCStreamer &S);
virtual void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, SMLoc L) = 0;
virtual void emitLiteralLabel(MCSymbol *LblSym, SMLoc L) = 0;
virtual void emitLiteral(const MCExpr *Value, SMLoc L) = 0;
virtual void emitLiteral(std::string str) = 0;
virtual void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, SMLoc L) {};
virtual void emitLiteralLabel(MCSymbol *LblSym, SMLoc L) {};
virtual void emitLiteral(const MCExpr *Value, SMLoc L) {};
virtual void emitLiteral(std::string str) {};
void setLiteralSectionPrefix(StringRef Name) { LiteralSectionPrefix = Name; }
StringRef getLiteralSectionPrefix() { return LiteralSectionPrefix; }
};
Expand All @@ -37,9 +37,6 @@ class XtensaTargetAsmStreamer : public XtensaTargetStreamer {

public:
XtensaTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, SMLoc L) override {}
void emitLiteralLabel(MCSymbol *LblSym, SMLoc L) override {}
void emitLiteral(const MCExpr *Value, SMLoc L) override {}
void emitLiteral(std::string str) override;
};

Expand All @@ -50,7 +47,6 @@ class XtensaTargetELFStreamer : public XtensaTargetStreamer {
void emitLiteral(MCSymbol *LblSym, const MCExpr *Value, SMLoc L) override;
void emitLiteralLabel(MCSymbol *LblSym, SMLoc L) override;
void emitLiteral(const MCExpr *Value, SMLoc L) override;
void emitLiteral(std::string str) override {}
};
} // end namespace llvm

Expand Down
7 changes: 7 additions & 0 deletions llvm/test/CodeGen/Xtensa/null-streamer.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Test the null streamer with a target streamer.
; RUN: llc -O0 -filetype=null -mtriple=xtensa < %s

define i32 @main() {
entry:
ret i32 0
}

0 comments on commit 2befb94

Please sign in to comment.