| 1 |
//===- unittests/Passes/PassBuilderBindingsTest.cpp -----------------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm-c/Core.h" |
| 10 |
#include "llvm-c/Transforms/PassBuilder.h" |
| 11 |
#include "llvm-c/Types.h" |
| 12 |
#include "gtest/gtest.h" |
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
using namespace llvm; |
| 16 |
|
| 17 |
class PassBuilderCTest : public testing::Test { |
| 18 |
void SetUp() override { |
| 19 |
char *Triple = LLVMGetDefaultTargetTriple(); |
| 20 |
if (strlen(Triple) == 0) { |
| 21 |
GTEST_SKIP(); |
| 22 |
LLVMDisposeMessage(Triple); |
| 23 |
return; |
| 24 |
} |
| 25 |
LLVMInitializeAllTargetInfos(); |
| 26 |
char *Err; |
| 27 |
LLVMTargetRef Target; |
| 28 |
if (LLVMGetTargetFromTriple(Triple, &Target, &Err)) { |
| 29 |
FAIL() << "Failed to create target from default triple (" << Triple |
| 30 |
<< "): " << Err; |
| 31 |
} |
| 32 |
TM = LLVMCreateTargetMachine(Target, Triple, "generic", "", |
| 33 |
LLVMCodeGenLevelDefault, LLVMRelocDefault, |
| 34 |
LLVMCodeModelDefault); |
| 35 |
LLVMDisposeMessage(Triple); |
| 36 |
Context = LLVMContextCreate(); |
| 37 |
Module = LLVMModuleCreateWithNameInContext("test", Context); |
| 38 |
LLVMTypeRef FT = |
| 39 |
LLVMFunctionType(LLVMVoidTypeInContext(Context), nullptr, 0, 0); |
| 40 |
Function = LLVMAddFunction(Module, "test", FT); |
| 41 |
} |
| 42 |
|
| 43 |
void TearDown() override { |
| 44 |
char *Triple = LLVMGetDefaultTargetTriple(); |
| 45 |
if (strlen(Triple) == 0) { |
| 46 |
LLVMDisposeMessage(Triple); |
| 47 |
return; // Skipped, so nothing to tear down |
| 48 |
} |
| 49 |
LLVMDisposeMessage(Triple); |
| 50 |
LLVMDisposeTargetMachine(TM); |
| 51 |
LLVMDisposeModule(Module); |
| 52 |
LLVMContextDispose(Context); |
| 53 |
} |
| 54 |
|
| 55 |
public: |
| 56 |
LLVMTargetMachineRef TM; |
| 57 |
LLVMModuleRef Module; |
| 58 |
LLVMValueRef Function; |
| 59 |
LLVMContextRef Context; |
| 60 |
}; |
| 61 |
|
| 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; |
| 72 |
} |
| 73 |
LLVMDisposePassBuilderOptions(Options); |
| 74 |
} |
| 75 |
|
| 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); |
| 80 |
ASSERT_TRUE(E1); |
| 81 |
ASSERT_TRUE(E2); |
| 82 |
LLVMConsumeError(E1); |
| 83 |
LLVMConsumeError(E2); |
| 84 |
LLVMDisposePassBuilderOptions(Options); |
| 85 |
} |
| 86 |
|
| 87 |
TEST_F(PassBuilderCTest, Function) { |
| 88 |
LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions(); |
| 89 |
if (LLVMErrorRef E = |
| 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; |
| 94 |
} |
| 95 |
LLVMDisposePassBuilderOptions(Options); |
| 96 |
} |