Files
TheRock/compiler/amd-llvm/llvm/unittests/IR/TimePassesTest.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

177 lines
5.1 KiB
C++

//===- unittests/IR/TimePassesTest.cpp - TimePassesHandler tests ----------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/PassTimingInfo.h"
#include "llvm/Pass.h"
#include "llvm/PassRegistry.h"
#include <gtest/gtest.h>
#include <llvm/ADT/SmallString.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/PassInstrumentation.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/PassTimingInfo.h>
#include <llvm/Support/raw_ostream.h>
using namespace llvm;
//===----------------------------------------------------------------------===//
// Define dummy passes for legacy pass manager run.
namespace llvm {
void initializePass1Pass(PassRegistry &);
void initializePass2Pass(PassRegistry &);
} // namespace llvm
namespace {
struct Pass1 : public ModulePass {
static char ID;
public:
Pass1() : ModulePass(ID) {}
bool runOnModule(Module &M) override { return false; }
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
}
StringRef getPassName() const override { return "Pass1"; }
};
char Pass1::ID;
struct Pass2 : public ModulePass {
static char ID;
public:
Pass2() : ModulePass(ID) {}
bool runOnModule(Module &M) override { return false; }
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesAll();
}
StringRef getPassName() const override { return "Pass2"; }
};
char Pass2::ID;
} // namespace
INITIALIZE_PASS(Pass1, "Pass1", "Pass1", false, false)
INITIALIZE_PASS(Pass2, "Pass2", "Pass2", false, false)
namespace {
TEST(TimePassesTest, LegacyCustomOut) {
PassInstrumentationCallbacks PIC;
PassInstrumentation PI(&PIC);
LLVMContext Context;
Module M("TestModule", Context);
SmallString<0> TimePassesStr;
raw_svector_ostream ReportStream(TimePassesStr);
// Setup pass manager
legacy::PassManager PM1;
PM1.add(new Pass1());
PM1.add(new Pass2());
// Enable time-passes and run passes.
TimePassesIsEnabled = true;
PM1.run(M);
// Generating report.
reportAndResetTimings(&ReportStream);
// There should be Pass1 and Pass2 in the report
EXPECT_FALSE(TimePassesStr.empty());
EXPECT_TRUE(TimePassesStr.str().contains("report"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass1"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
// Clear and generate report again.
TimePassesStr.clear();
reportAndResetTimings(&ReportStream);
// Since we did not run any passes since last print, report should be empty.
EXPECT_TRUE(TimePassesStr.empty());
// Now run just a single pass to populate timers again.
legacy::PassManager PM2;
PM2.add(new Pass2());
PM2.run(M);
// Generate report again.
reportAndResetTimings(&ReportStream);
// There should be Pass2 in this report and no Pass1.
EXPECT_FALSE(TimePassesStr.str().empty());
EXPECT_TRUE(TimePassesStr.str().contains("report"));
EXPECT_FALSE(TimePassesStr.str().contains("Pass1"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
// Reset flag to not affect other tests.
TimePassesIsEnabled = false;
}
class MyPass1 : public OptionalPassInfoMixin<MyPass1> {};
class MyPass2 : public OptionalPassInfoMixin<MyPass2> {};
TEST(TimePassesTest, CustomOut) {
PassInstrumentationCallbacks PIC;
PassInstrumentation PI(&PIC);
LLVMContext Context;
Module M("TestModule", Context);
MyPass1 Pass1;
MyPass2 Pass2;
SmallString<0> TimePassesStr;
raw_svector_ostream ReportStream(TimePassesStr);
// Setup time-passes handler and redirect output to the stream.
std::unique_ptr<TimePassesHandler> TimePasses =
std::make_unique<TimePassesHandler>(true);
TimePasses->setOutStream(ReportStream);
TimePasses->registerCallbacks(PIC);
// Pretending that passes are running to trigger the timers.
PI.runBeforePass(Pass1, M);
PI.runAfterPass(Pass1, M, PreservedAnalyses::all());
PI.runBeforePass(Pass2, M);
PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
// Generating report.
TimePasses->print();
// There should be Pass1 and Pass2 in the report
EXPECT_FALSE(TimePassesStr.empty());
EXPECT_TRUE(TimePassesStr.str().contains("report"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass1"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
// Clear and generate report again.
TimePassesStr.clear();
TimePasses->print();
// Since we did not run any passes since last print, report should be empty.
EXPECT_TRUE(TimePassesStr.empty());
// Now trigger just a single pass to populate timers again.
PI.runBeforePass(Pass2, M);
PI.runAfterPass(Pass2, M, PreservedAnalyses::all());
// Clear and generate report again.
TimePasses->print();
// There should be Pass2 in this report and no Pass1.
EXPECT_FALSE(TimePassesStr.str().empty());
EXPECT_TRUE(TimePassesStr.str().contains("report"));
EXPECT_FALSE(TimePassesStr.str().contains("Pass1"));
EXPECT_TRUE(TimePassesStr.str().contains("Pass2"));
}
} // end anonymous namespace