28 changes: 28 additions & 0 deletions llvm/unittests/ExecutionEngine/MCJIT/MCJITCAPITest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using namespace llvm;

static bool didCallAllocateCodeSection;
static bool didAllocateCompactUnwindSection;
static bool didCallPassRunListener;

static uint8_t *roundTripAllocateCodeSection(void *object, uintptr_t size,
unsigned alignment,
Expand Down Expand Up @@ -64,6 +65,12 @@ static void roundTripDestroy(void *object) {
delete static_cast<SectionMemoryManager*>(object);
}

static void passRunListenerCallback(LLVMContextRef C, LLVMPassRef P,
LLVMModuleRef M, LLVMValueRef F,
LLVMBasicBlockRef BB) {
didCallPassRunListener = true;
}

namespace {

// memory manager to test reserve allocation space callback
Expand Down Expand Up @@ -142,6 +149,7 @@ class MCJITCAPITest : public testing::Test, public MCJITTestAPICommon {
virtual void SetUp() {
didCallAllocateCodeSection = false;
didAllocateCompactUnwindSection = false;
didCallPassRunListener = false;
Module = 0;
Function = 0;
Engine = 0;
Expand Down Expand Up @@ -429,3 +437,23 @@ TEST_F(MCJITCAPITest, reserve_allocation_space) {
EXPECT_TRUE(MM->UsedCodeSize > 0);
EXPECT_TRUE(MM->UsedDataSizeRW > 0);
}

TEST_F(MCJITCAPITest, pass_run_listener) {
SKIP_UNSUPPORTED_PLATFORM;

buildSimpleFunction();
buildMCJITOptions();
buildMCJITEngine();
LLVMContextRef C = LLVMGetGlobalContext();
LLVMAddPassRunListener(C, passRunListenerCallback);
buildAndRunPasses();

union {
void *raw;
int (*usable)();
} functionPointer;
functionPointer.raw = LLVMGetPointerToGlobal(Engine, Function);

EXPECT_EQ(42, functionPointer.usable());
EXPECT_TRUE(didCallPassRunListener);
}