Skip to content

[NewPM] Set diagnostic handler in MachineModuleAnalysis #88229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion llvm/lib/CodeGen/MachineModuleInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
MMI.initialize();
MMI.TheModule = &M;
// FIXME: Do this for new pass manager.
LLVMContext &Ctx = M.getContext();
MMI.getContext().setDiagnosticHandler(
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
Expand All @@ -240,6 +239,17 @@ AnalysisKey MachineModuleAnalysis::Key;
MachineModuleAnalysis::Result
MachineModuleAnalysis::run(Module &M, ModuleAnalysisManager &) {
MMI.TheModule = &M;
LLVMContext &Ctx = M.getContext();
MMI.getContext().setDiagnosticHandler(
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
const SourceMgr &SrcMgr,
std::vector<const MDNode *> &LocInfos) {
unsigned LocCookie = 0;
if (IsInlineAsm)
LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
Ctx.diagnose(
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
});
MMI.DbgInfoAvailable =
!DisableDebugInfoPrinting && !M.debug_compile_units().empty();
return Result(MMI);
Expand Down
50 changes: 49 additions & 1 deletion llvm/unittests/CodeGen/PassManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ struct TestMachineModulePass : public PassInfoMixin<TestMachineModulePass> {
std::vector<int> &Counts;
};

struct ReportWarningPass : public PassInfoMixin<ReportWarningPass> {
PreservedAnalyses run(MachineFunction &MF,
MachineFunctionAnalysisManager &MFAM) {
auto &Ctx = MF.getContext();
Ctx.reportWarning(SMLoc(), "Test warning message.");
return PreservedAnalyses::all();
}
};

std::unique_ptr<Module> parseIR(LLVMContext &Context, const char *IR) {
SMDiagnostic Err;
return parseAssemblyString(IR, Err, Context);
Expand Down Expand Up @@ -176,10 +185,10 @@ TEST_F(PassManagerTest, Basic) {
MachineModuleInfo MMI(LLVMTM);

LoopAnalysisManager LAM;
MachineFunctionAnalysisManager MFAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;
MachineFunctionAnalysisManager MFAM;
PassBuilder PB(TM.get());
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
Expand All @@ -204,10 +213,49 @@ TEST_F(PassManagerTest, Basic) {
MFPM.addPass(TestMachineFunctionPass(Count, Counts));
MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));

testing::internal::CaptureStderr();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL CaptureStderr, this is good to know

I'd separate the ReportWarningPass run out to its own test that doesn't check Counts

MPM.run(*M, MAM);
std::string Output = testing::internal::GetCapturedStderr();

EXPECT_EQ((std::vector<int>{10, 16, 18, 20, 30, 36, 38, 40}), Counts);
EXPECT_EQ(40, Count);
}

TEST_F(PassManagerTest, DiagnosticHandler) {
if (!TM)
GTEST_SKIP();

LLVMTargetMachine *LLVMTM = static_cast<LLVMTargetMachine *>(TM.get());
M->setDataLayout(TM->createDataLayout());

MachineModuleInfo MMI(LLVMTM);

LoopAnalysisManager LAM;
MachineFunctionAnalysisManager MFAM;
FunctionAnalysisManager FAM;
CGSCCAnalysisManager CGAM;
ModuleAnalysisManager MAM;
PassBuilder PB(TM.get());
PB.registerModuleAnalyses(MAM);
PB.registerCGSCCAnalyses(CGAM);
PB.registerFunctionAnalyses(FAM);
PB.registerLoopAnalyses(LAM);
PB.registerMachineFunctionAnalyses(MFAM);
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM, &MFAM);

MAM.registerPass([&] { return MachineModuleAnalysis(MMI); });

ModulePassManager MPM;
FunctionPassManager FPM;
MachineFunctionPassManager MFPM;
MFPM.addPass(ReportWarningPass());
MPM.addPass(createModuleToMachineFunctionPassAdaptor(std::move(MFPM)));
testing::internal::CaptureStderr();
MPM.run(*M, MAM);
std::string Output = testing::internal::GetCapturedStderr();

EXPECT_TRUE(Output.find("warning: <unknown>:0: Test warning message.") !=
std::string::npos);
}

} // namespace