diff options
author | Michael Maitland <michaeltmaitland@gmail.com> | 2023-07-05 10:04:06 -0700 |
---|---|---|
committer | Michael Maitland <michaeltmaitland@gmail.com> | 2023-07-06 15:12:08 -0700 |
commit | ecf372f993fa0f1ae417a3159e5c397f36c2d48c (patch) | |
tree | 5e5b5c0af06b1aec6b8f5c082c7f826a1e51ca72 /llvm/tools/llvm-mca | |
parent | aef6d4610fabf2b4c2f9e2fcfab24fce6f59cc86 (diff) | |
download | llvm-ecf372f993fa0f1ae417a3159e5c397f36c2d48c.zip llvm-ecf372f993fa0f1ae417a3159e5c397f36c2d48c.tar.gz llvm-ecf372f993fa0f1ae417a3159e5c397f36c2d48c.tar.bz2 |
[llvm-mca][RISCV] vsetivli and vsetvli act as instruments
Since the LMUL data that is needed to create an instrument is
avaliable statically from vsetivli and vsetvli instructions,
LMUL instruments can be automatically generated so that clients
of the tool do no need to manually insert instrument comments.
Instrument comments may be placed after a vset{i}vli instruction,
which will override instrument that was automatically inserted.
As a result, clients of llvm-mca instruments do not need to update
their existing instrument comments. However, if the instrument
has the same LMUL as the vset{i}vli, then it is reccomended to
remove the instrument comment as it becomes redundant.
Differential Revision: https://reviews.llvm.org/D154526
Diffstat (limited to 'llvm/tools/llvm-mca')
-rw-r--r-- | llvm/tools/llvm-mca/CodeRegion.h | 16 | ||||
-rw-r--r-- | llvm/tools/llvm-mca/CodeRegionGenerator.cpp | 41 | ||||
-rw-r--r-- | llvm/tools/llvm-mca/CodeRegionGenerator.h | 77 |
3 files changed, 88 insertions, 46 deletions
diff --git a/llvm/tools/llvm-mca/CodeRegion.h b/llvm/tools/llvm-mca/CodeRegion.h index 61ee40b..ce107fd 100644 --- a/llvm/tools/llvm-mca/CodeRegion.h +++ b/llvm/tools/llvm-mca/CodeRegion.h @@ -172,22 +172,30 @@ public: bool isRegionActive(llvm::StringRef Description) const { return ActiveRegions.contains(Description); } + + virtual void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) = 0; + virtual void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc, + UniqueInstrument Instrument) = 0; + virtual void endRegion(llvm::StringRef Description, llvm::SMLoc Loc) = 0; }; struct AnalysisRegions : public CodeRegions { AnalysisRegions(llvm::SourceMgr &S); - void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc); - void endRegion(llvm::StringRef Description, llvm::SMLoc Loc); + void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) override; + void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc, + UniqueInstrument Instrument) override {} + void endRegion(llvm::StringRef Description, llvm::SMLoc Loc) override; }; struct InstrumentRegions : public CodeRegions { InstrumentRegions(llvm::SourceMgr &S); + void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc) override{}; void beginRegion(llvm::StringRef Description, llvm::SMLoc Loc, - UniqueInstrument Instrument); - void endRegion(llvm::StringRef Description, llvm::SMLoc Loc); + UniqueInstrument Instrument) override; + void endRegion(llvm::StringRef Description, llvm::SMLoc Loc) override; const SmallVector<Instrument *> getActiveInstruments(llvm::SMLoc Loc) const; }; diff --git a/llvm/tools/llvm-mca/CodeRegionGenerator.cpp b/llvm/tools/llvm-mca/CodeRegionGenerator.cpp index 8321cfb..5241b58 100644 --- a/llvm/tools/llvm-mca/CodeRegionGenerator.cpp +++ b/llvm/tools/llvm-mca/CodeRegionGenerator.cpp @@ -17,7 +17,6 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/MC/MCParser/MCTargetAsmParser.h" -#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCTargetOptions.h" #include "llvm/Support/Error.h" #include "llvm/Support/SMLoc.h" @@ -29,46 +28,12 @@ namespace mca { // This virtual dtor serves as the anchor for the CodeRegionGenerator class. CodeRegionGenerator::~CodeRegionGenerator() {} -// This class provides the callbacks that occur when parsing input assembly. -class MCStreamerWrapper final : public MCStreamer { - CodeRegions &Regions; - -public: - MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R) - : MCStreamer(Context), Regions(R) {} - - // We only want to intercept the emission of new instructions. - void emitInstruction(const MCInst &Inst, - const MCSubtargetInfo & /* unused */) override { - Regions.addInstruction(Inst); - } - - bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { - return true; - } - - void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, - Align ByteAlignment) override {} - void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, - uint64_t Size = 0, Align ByteAlignment = Align(1), - SMLoc Loc = SMLoc()) override {} - void emitGPRel32Value(const MCExpr *Value) override {} - void beginCOFFSymbolDef(const MCSymbol *Symbol) override {} - void emitCOFFSymbolStorageClass(int StorageClass) override {} - void emitCOFFSymbolType(int Type) override {} - void endCOFFSymbolDef() override {} - - ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const { - return Regions.getInstructionSequence(Index); - } -}; - Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions( const std::unique_ptr<MCInstPrinter> &IP) { MCTargetOptions Opts; Opts.PreserveAsmComments = false; CodeRegions &Regions = getRegions(); - MCStreamerWrapper Str(Ctx, Regions); + MCStreamerWrapper *Str = getMCStreamer(); // Need to initialize an MCTargetStreamer otherwise // certain asm directives will cause a segfault. @@ -76,13 +41,13 @@ Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions( // doesn't show up in the llvm-mca output. raw_ostream &OSRef = nulls(); formatted_raw_ostream FOSRef(OSRef); - TheTarget.createAsmTargetStreamer(Str, FOSRef, IP.get(), + TheTarget.createAsmTargetStreamer(*Str, FOSRef, IP.get(), /*IsVerboseAsm=*/true); // Create a MCAsmParser and setup the lexer to recognize llvm-mca ASM // comments. std::unique_ptr<MCAsmParser> Parser( - createMCAsmParser(Regions.getSourceMgr(), Ctx, Str, MAI)); + createMCAsmParser(Regions.getSourceMgr(), Ctx, *Str, MAI)); MCAsmLexer &Lexer = Parser->getLexer(); MCACommentConsumer *CCP = getCommentConsumer(); Lexer.setCommentConsumer(CCP); diff --git a/llvm/tools/llvm-mca/CodeRegionGenerator.h b/llvm/tools/llvm-mca/CodeRegionGenerator.h index d1b995d..68da567 100644 --- a/llvm/tools/llvm-mca/CodeRegionGenerator.h +++ b/llvm/tools/llvm-mca/CodeRegionGenerator.h @@ -20,6 +20,7 @@ #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCContext.h" #include "llvm/MC/MCParser/MCAsmLexer.h" +#include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/MCA/CustomBehaviour.h" @@ -77,6 +78,67 @@ public: /// region of type INSTRUMENATION_TYPE, then it will end the active /// one and begin a new one using the new data. void HandleComment(SMLoc Loc, StringRef CommentText) override; + + InstrumentManager &getInstrumentManager() { return IM; } +}; + +// This class provides the callbacks that occur when parsing input assembly. +class MCStreamerWrapper : public MCStreamer { +protected: + CodeRegions &Regions; + +public: + MCStreamerWrapper(MCContext &Context, mca::CodeRegions &R) + : MCStreamer(Context), Regions(R) {} + + // We only want to intercept the emission of new instructions. + void emitInstruction(const MCInst &Inst, + const MCSubtargetInfo & /* unused */) override { + Regions.addInstruction(Inst); + } + + bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override { + return true; + } + + void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, + Align ByteAlignment) override {} + void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr, + uint64_t Size = 0, Align ByteAlignment = Align(1), + SMLoc Loc = SMLoc()) override {} + void emitGPRel32Value(const MCExpr *Value) override {} + void beginCOFFSymbolDef(const MCSymbol *Symbol) override {} + void emitCOFFSymbolStorageClass(int StorageClass) override {} + void emitCOFFSymbolType(int Type) override {} + void endCOFFSymbolDef() override {} + + ArrayRef<MCInst> GetInstructionSequence(unsigned Index) const { + return Regions.getInstructionSequence(Index); + } +}; + +class InstrumentMCStreamer : public MCStreamerWrapper { + InstrumentManager &IM; + +public: + InstrumentMCStreamer(MCContext &Context, mca::InstrumentRegions &R, + InstrumentManager &IM) + : MCStreamerWrapper(Context, R), IM(IM) {} + + void emitInstruction(const MCInst &Inst, + const MCSubtargetInfo &MCSI) override { + MCStreamerWrapper::emitInstruction(Inst, MCSI); + + // We know that Regions is an InstrumentRegions by the constructor. + for (UniqueInstrument &I : IM.createInstruments(Inst)) { + StringRef InstrumentKind = I.get()->getDesc(); + // End InstrumentType region if one is open + if (Regions.isRegionActive(InstrumentKind)) + Regions.endRegion(InstrumentKind, Inst.getLoc()); + // Start new instrumentation region + Regions.beginRegion(InstrumentKind, Inst.getLoc(), std::move(I)); + } + } }; /// This abstract class is responsible for parsing the input given to @@ -121,19 +183,22 @@ public: /// generating a CodeRegions instance. class AsmCodeRegionGenerator : public virtual CodeRegionGenerator { const Target &TheTarget; - MCContext &Ctx; const MCAsmInfo &MAI; const MCSubtargetInfo &STI; const MCInstrInfo &MCII; unsigned AssemblerDialect; // This is set during parsing. +protected: + MCContext &Ctx; + public: AsmCodeRegionGenerator(const Target &T, MCContext &C, const MCAsmInfo &A, const MCSubtargetInfo &S, const MCInstrInfo &I) - : TheTarget(T), Ctx(C), MAI(A), STI(S), MCII(I), AssemblerDialect(0) {} + : TheTarget(T), MAI(A), STI(S), MCII(I), AssemblerDialect(0), Ctx(C) {} virtual MCACommentConsumer *getCommentConsumer() = 0; virtual CodeRegions &getRegions() = 0; + virtual MCStreamerWrapper *getMCStreamer() = 0; unsigned getAssemblerDialect() const { return AssemblerDialect; } Expected<const CodeRegions &> @@ -143,16 +208,18 @@ public: class AsmAnalysisRegionGenerator final : public AnalysisRegionGenerator, public AsmCodeRegionGenerator { AnalysisRegionCommentConsumer CC; + MCStreamerWrapper Streamer; public: AsmAnalysisRegionGenerator(const Target &T, llvm::SourceMgr &SM, MCContext &C, const MCAsmInfo &A, const MCSubtargetInfo &S, const MCInstrInfo &I) : AnalysisRegionGenerator(SM), AsmCodeRegionGenerator(T, C, A, S, I), - CC(Regions) {} + CC(Regions), Streamer(Ctx, Regions) {} MCACommentConsumer *getCommentConsumer() override { return &CC; }; CodeRegions &getRegions() override { return Regions; }; + MCStreamerWrapper *getMCStreamer() override { return &Streamer; } Expected<const AnalysisRegions &> parseAnalysisRegions(const std::unique_ptr<MCInstPrinter> &IP) override { @@ -172,6 +239,7 @@ public: class AsmInstrumentRegionGenerator final : public InstrumentRegionGenerator, public AsmCodeRegionGenerator { InstrumentRegionCommentConsumer CC; + InstrumentMCStreamer Streamer; public: AsmInstrumentRegionGenerator(const Target &T, llvm::SourceMgr &SM, @@ -179,10 +247,11 @@ public: const MCSubtargetInfo &S, const MCInstrInfo &I, InstrumentManager &IM) : InstrumentRegionGenerator(SM), AsmCodeRegionGenerator(T, C, A, S, I), - CC(SM, Regions, IM) {} + CC(SM, Regions, IM), Streamer(Ctx, Regions, IM) {} MCACommentConsumer *getCommentConsumer() override { return &CC; }; CodeRegions &getRegions() override { return Regions; }; + MCStreamerWrapper *getMCStreamer() override { return &Streamer; } Expected<const InstrumentRegions &> parseInstrumentRegions(const std::unique_ptr<MCInstPrinter> &IP) override { |