mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-22 18:54:02 +01:00
81da9f4819
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
72 lines
2.5 KiB
C++
72 lines
2.5 KiB
C++
//===----------------------- CodeRegionGenerator.h --------------*- 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 declares 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.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
|
|
#define LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
|
|
|
|
#include "CodeRegion.h"
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
#include "llvm/MC/MCContext.h"
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
|
#include "llvm/Support/Error.h"
|
|
#include "llvm/Support/SourceMgr.h"
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
#include <memory>
|
|
|
|
namespace llvm {
|
|
namespace mca {
|
|
|
|
/// This class is responsible for parsing the input given to the llvm-mca
|
|
/// driver, and converting that into a CodeRegions instance.
|
|
class CodeRegionGenerator {
|
|
protected:
|
|
CodeRegions Regions;
|
|
CodeRegionGenerator(const CodeRegionGenerator &) = delete;
|
|
CodeRegionGenerator &operator=(const CodeRegionGenerator &) = delete;
|
|
|
|
public:
|
|
CodeRegionGenerator(SourceMgr &SM) : Regions(SM) {}
|
|
virtual ~CodeRegionGenerator();
|
|
virtual Expected<const CodeRegions &>
|
|
parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) = 0;
|
|
};
|
|
|
|
/// This class is responsible for parsing input ASM and generating
|
|
/// a CodeRegions instance.
|
|
class AsmCodeRegionGenerator final : public CodeRegionGenerator {
|
|
const Target &TheTarget;
|
|
MCContext &Ctx;
|
|
const MCAsmInfo &MAI;
|
|
const MCSubtargetInfo &STI;
|
|
const MCInstrInfo &MCII;
|
|
unsigned AssemblerDialect; // This is set during parsing.
|
|
|
|
public:
|
|
AsmCodeRegionGenerator(const Target &T, SourceMgr &SM, MCContext &C,
|
|
const MCAsmInfo &A, const MCSubtargetInfo &S,
|
|
const MCInstrInfo &I)
|
|
: CodeRegionGenerator(SM), TheTarget(T), Ctx(C), MAI(A), STI(S), MCII(I),
|
|
AssemblerDialect(0) {}
|
|
|
|
unsigned getAssemblerDialect() const { return AssemblerDialect; }
|
|
Expected<const CodeRegions &>
|
|
parseCodeRegions(const std::unique_ptr<MCInstPrinter> &IP) override;
|
|
};
|
|
|
|
} // namespace mca
|
|
} // namespace llvm
|
|
|
|
#endif // LLVM_TOOLS_LLVM_MCA_CODEREGION_GENERATOR_H
|