Skip to content

Commit e5d59db

Browse files
holland11adibiagio
authored andcommitted
[MCA] llvm-mca MCTargetStreamer segfault fix
In order to create the code regions for llvm-mca to analyze, llvm-mca creates an AsmCodeRegionGenerator and calls AsmCodeRegionGenerator::parseCodeRegions(). Within this function, both an MCAsmParser and MCTargetAsmParser are created so that MCAsmParser::Run() can be used to create the code regions for us. These parser classes were created for llvm-mc so they are designed to emit code with an MCStreamer and MCTargetStreamer that are expected to be setup and passed into the MCAsmParser constructor. Because llvm-mca doesn’t want to emit any code, an MCStreamerWrapper class gets created instead and passed into the MCAsmParser constructor. This wrapper inherits from MCStreamer and overrides many of the emit methods to just do nothing. The exception is the emitInstruction() method which calls Regions.addInstruction(Inst). This works well and allows llvm-mca to utilize llvm-mc’s MCAsmParser to build our code regions, however there are a few directives which rely on the MCTargetStreamer. llvm-mc assumes that the MCStreamer that gets passed into the MCAsmParser’s constructor has a valid pointer to an MCTargetStreamer. Because llvm-mca doesn’t setup an MCTargetStreamer, when the parser encounters one of those directives, a segfault will occur. In x86, each one of these 7 directives will cause this segfault if they exist in the input assembly to llvm-mca: .cv_fpo_proc .cv_fpo_setframe .cv_fpo_pushreg .cv_fpo_stackalloc .cv_fpo_stackalign .cv_fpo_endprologue .cv_fpo_endproc I haven’t looked at other targets, but I wouldn’t be surprised if some of the other ones also have certain directives which could result in this same segfault. My proposed solution is to simply initialize an MCTargetStreamer after we initialize the MCStreamerWrapper. The MCTargetStreamer requires an ostream object, but we don’t actually want any of these directives to be emitted anywhere, so I use an ostream created with the nulls() function. Since this needs to happen after the MCStreamerWrapper has been initialized, it needs to happen within the AsmCodeRegionGenerator::parseCodeRegions() function. The MCTargetStreamer also needs an MCInstPrinter which is easiest to initialize within the main() function of llvm-mca. So this MCInstPrinter gets constructed within main() then passed into the parseCodeRegions() function as a parameter. (If you feel like it would be appropriate and possible to create the MCInstPrinter within the parseCodeRegions() function, then feel free to modify my solution. That would stop us from having to pass it into the function and would limit its scope / lifetime.) My solution stops the segfault from happening and still passes all of the current (expected) llvm-mca tests. I also added a new test for x86 that checks for this segfault on an input that includes one of the .cv_fpo directives (this test fails without my solution, but passes with it). As far as I can tell, all of the functions that I modified are only called from within llvm-mca so there shouldn’t be any worries about breaking other tools. Differential Revision: https://reviews.llvm.org/D102709
1 parent 449d14e commit e5d59db

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# RUN: llvm-mca -mtriple=x86_64-unknown-unknown -resource-pressure=false -instruction-info=false < %s | FileCheck %s
2+
3+
.cv_fpo_pushreg ebx
4+
add %eax, %eax
5+
add %ebx, %ebx
6+
add %ecx, %ecx
7+
add %edx, %edx
8+
9+
# CHECK: Iterations: 100

llvm/tools/llvm-mca/CodeRegionGenerator.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,21 @@ void MCACommentConsumer::HandleComment(SMLoc Loc, StringRef CommentText) {
106106
Regions.beginRegion(Comment, Loc);
107107
}
108108

109-
Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions() {
109+
Expected<const CodeRegions &> AsmCodeRegionGenerator::parseCodeRegions(
110+
const std::unique_ptr<MCInstPrinter> &IP) {
110111
MCTargetOptions Opts;
111112
Opts.PreserveAsmComments = false;
112113
MCStreamerWrapper Str(Ctx, Regions);
113114

115+
// Need to initialize an MCTargetStreamer otherwise
116+
// certain asm directives will cause a segfault.
117+
// Using nulls() so that anything emitted by the MCTagetStreamer
118+
// doesn't show up in the llvm-mca output.
119+
raw_ostream &OSRef = nulls();
120+
formatted_raw_ostream FOSRef(OSRef);
121+
TheTarget.createAsmTargetStreamer(Str, FOSRef, IP.get(),
122+
/*IsVerboseAsm=*/true);
123+
114124
// Create a MCAsmParser and setup the lexer to recognize llvm-mca ASM
115125
// comments.
116126
std::unique_ptr<MCAsmParser> Parser(

llvm/tools/llvm-mca/CodeRegionGenerator.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class CodeRegionGenerator {
3939
public:
4040
CodeRegionGenerator(SourceMgr &SM) : Regions(SM) {}
4141
virtual ~CodeRegionGenerator();
42-
virtual Expected<const CodeRegions &> parseCodeRegions() = 0;
42+
virtual Expected<const CodeRegions &>
43+
parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) = 0;
4344
};
4445

4546
/// This class is responsible for parsing input ASM and generating
@@ -60,7 +61,8 @@ class AsmCodeRegionGenerator final : public CodeRegionGenerator {
6061
AssemblerDialect(0) {}
6162

6263
unsigned getAssemblerDialect() const { return AssemblerDialect; }
63-
Expected<const CodeRegions &> parseCodeRegions() override;
64+
Expected<const CodeRegions &>
65+
parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) override;
6466
};
6567

6668
} // namespace mca

llvm/tools/llvm-mca/llvm-mca.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,28 @@ int main(int argc, char **argv) {
389389
std::unique_ptr<MCInstrAnalysis> MCIA(
390390
TheTarget->createMCInstrAnalysis(MCII.get()));
391391

392+
// Need to initialize an MCInstPrinter as it is
393+
// required for initializing the MCTargetStreamer
394+
// which needs to happen within the CRG.parseCodeRegions() call below.
395+
// Without an MCTargetStreamer, certain assembly directives can trigger a
396+
// segfault. (For example, the .cv_fpo_proc directive on x86 will segfault if
397+
// we don't initialize the MCTargetStreamer.)
398+
unsigned IPtempOutputAsmVariant =
399+
OutputAsmVariant == -1 ? 0 : OutputAsmVariant;
400+
std::unique_ptr<MCInstPrinter> IPtemp(TheTarget->createMCInstPrinter(
401+
Triple(TripleName), IPtempOutputAsmVariant, *MAI, *MCII, *MRI));
402+
if (!IPtemp) {
403+
WithColor::error()
404+
<< "unable to create instruction printer for target triple '"
405+
<< TheTriple.normalize() << "' with assembly variant "
406+
<< IPtempOutputAsmVariant << ".\n";
407+
return 1;
408+
}
409+
392410
// Parse the input and create CodeRegions that llvm-mca can analyze.
393411
mca::AsmCodeRegionGenerator CRG(*TheTarget, SrcMgr, Ctx, *MAI, *STI, *MCII);
394-
Expected<const mca::CodeRegions &> RegionsOrErr = CRG.parseCodeRegions();
412+
Expected<const mca::CodeRegions &> RegionsOrErr =
413+
CRG.parseCodeRegions(std::move(IPtemp));
395414
if (!RegionsOrErr) {
396415
if (auto Err =
397416
handleErrors(RegionsOrErr.takeError(), [](const StringError &E) {

0 commit comments

Comments
 (0)