llvm-project/llvm/tools/llvm-mca/CodeRegionGenerator.cpp
Peter Waller 1de0535e84
[llvm-mca] Abort on parse error without -skip-unsupported-instructions (#90474)
[llvm-mca] Abort on parse error without -skip-unsupported-instructions

Prior to this patch, llvm-mca would continue executing after parse
errors. These errors can lead to some confusion since some analysis
results are printed on the standard output, and they're printed after
the errors, which could otherwise be easy to miss.

However it is still useful to be able to continue analysis after errors;
so extend the recently added -skip-unsupported-instructions to support
this.

Two tests which have parse errors for some of the 'RUN' branches are
updated to use -skip-unsupported-instructions so they can remain as-is.

Add a description of -skip-unsupported-instructions to the llvm-mca
command guide, and add it to the llvm-mca --help output:

```
  --skip-unsupported-instructions=<value> - Force analysis to continue in the presence of unsupported instructions
    =none                                 -   Exit with an error when an instruction is unsupported for any reason (default)
    =lack-sched                           -   Skip instructions on input which lack scheduling information
    =parse-failure                        -   Skip lines on the input which fail to parse for any reason
    =any                                  -   Skip instructions or lines on input which are unsupported for any reason
```

Tests within this patch are intended to cover each of the cases.

Reason        | Flag | Comment
--------------|------|-------
none          | none | Usual case, existing test suite
lack-sched    | none | Advises user to use -skip-unsupported-instructions=lack-sched, tested in llvm/test/tools/llvm-mca/X86/BtVer2/unsupported-instruction.s
parse-failure | none | Advises user to use -skip-unsupported-instructions=parse-failure, tested in llvm/test/tools/llvm-mca/bad-input.s
any           | none | (N/A, covered above)
lack-sched    | any  | Continues, prints warnings, tested in llvm/test/tools/llvm-mca/X86/BtVer2/unsupported-instruction.s
parse-failure | any  | Continues, prints errors, tested in llvm/test/tools/llvm-mca/bad-input.s
lack-sched    | parse-failure | Advises user to use -skip-unsupported-instructions=lack-sched, tested in llvm/test/tools/llvm-mca/X86/BtVer2/unsupported-instruction.s
parse-failure | lack-sched    | Advises user to use -skip-unsupported-instructions=parse-failure, tested in llvm/test/tools/llvm-mca/bad-input.s
none          | * | This would be any test case with skip-unsupported-instructions, coverage added in llvm/test/tools/llvm-mca/X86/BtVer2/simple-test.s
any           | * | (Logically covered by the other cases)
2024-05-07 09:13:44 +01:00

184 lines
6.5 KiB
C++

//===----------------------- CodeRegionGenerator.cpp ------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file defines classes responsible for generating llvm-mca
/// CodeRegions from various types of input. llvm-mca only analyzes CodeRegions,
/// so the classes here provide the input-to-CodeRegions translation.
//
//===----------------------------------------------------------------------===//
#include "CodeRegionGenerator.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/SMLoc.h"
#include <memory>
namespace llvm {
namespace mca {
// This virtual dtor serves as the anchor for the CodeRegionGenerator class.
CodeRegionGenerator::~CodeRegionGenerator() {}
Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions(
const std::unique_ptr<MCInstPrinter> &IP, bool SkipFailures) {
MCTargetOptions Opts;
Opts.PreserveAsmComments = false;
CodeRegions &Regions = getRegions();
MCStreamerWrapper *Str = getMCStreamer();
// Need to initialize an MCTargetStreamer otherwise
// certain asm directives will cause a segfault.
// Using nulls() so that anything emitted by the MCTargetStreamer
// doesn't show up in the llvm-mca output.
raw_ostream &OSRef = nulls();
formatted_raw_ostream FOSRef(OSRef);
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));
MCAsmLexer &Lexer = Parser->getLexer();
MCACommentConsumer *CCP = getCommentConsumer();
Lexer.setCommentConsumer(CCP);
// Enable support for MASM literal numbers (example: 05h, 101b).
Lexer.setLexMasmIntegers(true);
std::unique_ptr<MCTargetAsmParser> TAP(
TheTarget.createMCAsmParser(STI, *Parser, MCII, Opts));
if (!TAP)
return make_error<StringError>(
"This target does not support assembly parsing.",
inconvertibleErrorCode());
Parser->setTargetParser(*TAP);
// Parser->Run() confusingly returns true on errors, in which case the errors
// were already shown to the user. SkipFailures implies continuing in the
// presence of any kind of failure within the parser, in which case failing
// input lines are not represented, but the rest of the input remains.
if (Parser->Run(false) && !SkipFailures) {
const char *Message = "Assembly input parsing had errors, use "
"-skip-unsupported-instructions=parse-failure "
"to drop failing lines from the input.";
return make_error<StringError>(Message, inconvertibleErrorCode());
}
if (CCP->hadErr())
return make_error<StringError>("There was an error parsing comments.",
inconvertibleErrorCode());
// Set the assembler dialect from the input. llvm-mca will use this as the
// default dialect when printing reports.
AssemblerDialect = Parser->getAssemblerDialect();
return Regions;
}
void AnalysisRegionCommentConsumer::HandleComment(SMLoc Loc,
StringRef CommentText) {
// Skip empty comments.
StringRef Comment(CommentText);
if (Comment.empty())
return;
// Skip spaces and tabs.
unsigned Position = Comment.find_first_not_of(" \t");
if (Position >= Comment.size())
// We reached the end of the comment. Bail out.
return;
Comment = Comment.drop_front(Position);
if (Comment.consume_front("LLVM-MCA-END")) {
// Skip spaces and tabs.
Position = Comment.find_first_not_of(" \t");
if (Position < Comment.size())
Comment = Comment.drop_front(Position);
Regions.endRegion(Comment, Loc);
return;
}
// Try to parse the LLVM-MCA-BEGIN comment.
if (!Comment.consume_front("LLVM-MCA-BEGIN"))
return;
// Skip spaces and tabs.
Position = Comment.find_first_not_of(" \t");
if (Position < Comment.size())
Comment = Comment.drop_front(Position);
// Use the rest of the string as a descriptor for this code snippet.
Regions.beginRegion(Comment, Loc);
}
void InstrumentRegionCommentConsumer::HandleComment(SMLoc Loc,
StringRef CommentText) {
// Skip empty comments.
StringRef Comment(CommentText);
if (Comment.empty())
return;
// Skip spaces and tabs.
unsigned Position = Comment.find_first_not_of(" \t");
if (Position >= Comment.size())
// We reached the end of the comment. Bail out.
return;
Comment = Comment.drop_front(Position);
// Bail out if not an MCA style comment
if (!Comment.consume_front("LLVM-MCA-"))
return;
// Skip AnalysisRegion comments
if (Comment.consume_front("BEGIN") || Comment.consume_front("END"))
return;
if (IM.shouldIgnoreInstruments())
return;
auto [InstrumentKind, Data] = Comment.split(" ");
// An error if not of the form LLVM-MCA-TARGET-KIND
if (!IM.supportsInstrumentType(InstrumentKind)) {
if (InstrumentKind.empty())
SM.PrintMessage(
Loc, llvm::SourceMgr::DK_Error,
"No instrumentation kind was provided in LLVM-MCA comment");
else
SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
"Unknown instrumentation type in LLVM-MCA comment: " +
InstrumentKind);
FoundError = true;
return;
}
UniqueInstrument I = IM.createInstrument(InstrumentKind, Data);
if (!I) {
if (Data.empty())
SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
"Failed to create " + InstrumentKind +
" instrument with no data");
else
SM.PrintMessage(Loc, llvm::SourceMgr::DK_Error,
"Failed to create " + InstrumentKind +
" instrument with data: " + Data);
FoundError = true;
return;
}
// End InstrumentType region if one is open
if (Regions.isRegionActive(InstrumentKind))
Regions.endRegion(InstrumentKind, Loc);
// Start new instrumentation region
Regions.beginRegion(InstrumentKind, Loc, std::move(I));
}
} // namespace mca
} // namespace llvm