1 //===- unittests/Passes/PassBuilderBindingsTest.cpp -----------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm-c/Core.h"
10 #include "llvm-c/Transforms/PassBuilder.h"
11 #include "llvm-c/Types.h"
12 #include "gtest/gtest.h"
17 class PassBuilderCTest
: public testing::Test
{
18 void SetUp() override
{
19 char *Triple
= LLVMGetDefaultTargetTriple();
20 if (strlen(Triple
) == 0) {
22 LLVMDisposeMessage(Triple
);
25 LLVMInitializeAllTargetInfos();
28 if (LLVMGetTargetFromTriple(Triple
, &Target
, &Err
)) {
29 FAIL() << "Failed to create target from default triple (" << Triple
32 TM
= LLVMCreateTargetMachine(Target
, Triple
, "generic", "",
33 LLVMCodeGenLevelDefault
, LLVMRelocDefault
,
34 LLVMCodeModelDefault
);
35 LLVMDisposeMessage(Triple
);
36 Context
= LLVMContextCreate();
37 Module
= LLVMModuleCreateWithNameInContext("test", Context
);
39 LLVMFunctionType(LLVMVoidTypeInContext(Context
), nullptr, 0, 0);
40 Function
= LLVMAddFunction(Module
, "test", FT
);
43 void TearDown() override
{
44 char *Triple
= LLVMGetDefaultTargetTriple();
45 if (strlen(Triple
) == 0) {
46 LLVMDisposeMessage(Triple
);
47 return; // Skipped, so nothing to tear down
49 LLVMDisposeMessage(Triple
);
50 LLVMDisposeTargetMachine(TM
);
51 LLVMDisposeModule(Module
);
52 LLVMContextDispose(Context
);
56 LLVMTargetMachineRef TM
;
58 LLVMValueRef Function
;
59 LLVMContextRef Context
;
62 TEST_F(PassBuilderCTest
, Basic
) {
63 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
64 LLVMPassBuilderOptionsSetLoopUnrolling(Options
, 1);
65 LLVMPassBuilderOptionsSetVerifyEach(Options
, 1);
66 LLVMPassBuilderOptionsSetDebugLogging(Options
, 0);
67 LLVMPassBuilderOptionsSetAAPipeline(Options
, "basic-aa");
68 if (LLVMErrorRef E
= LLVMRunPasses(Module
, "default<O2>", TM
, Options
)) {
69 char *Msg
= LLVMGetErrorMessage(E
);
70 LLVMDisposePassBuilderOptions(Options
);
71 FAIL() << "Failed to run passes: " << Msg
;
73 LLVMDisposePassBuilderOptions(Options
);
76 TEST_F(PassBuilderCTest
, InvalidPassIsError
) {
77 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
78 LLVMErrorRef E1
= LLVMRunPasses(Module
, "", TM
, Options
);
79 LLVMErrorRef E2
= LLVMRunPasses(Module
, "does-not-exist-pass", TM
, Options
);
84 LLVMDisposePassBuilderOptions(Options
);
87 TEST_F(PassBuilderCTest
, Function
) {
88 LLVMPassBuilderOptionsRef Options
= LLVMCreatePassBuilderOptions();
90 LLVMRunPassesOnFunction(Function
, "no-op-function", TM
, Options
)) {
91 char *Msg
= LLVMGetErrorMessage(E
);
92 LLVMDisposePassBuilderOptions(Options
);
93 FAIL() << "Failed to run passes on function: " << Msg
;
95 LLVMDisposePassBuilderOptions(Options
);