2021-05-17 20:03:47 +02:00
|
|
|
//===- unittests/Passes/PassBuilderBindingsTest.cpp -----------------------===//
|
|
|
|
//
|
|
|
|
// 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-c/Core.h"
|
|
|
|
#include "llvm-c/Transforms/PassBuilder.h"
|
|
|
|
#include "llvm-c/Types.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
class PassBuilderCTest : public testing::Test {
|
|
|
|
void SetUp() override {
|
2021-05-18 18:33:50 +02:00
|
|
|
LLVMInitializeAllTargetInfos();
|
2021-05-17 22:56:49 +02:00
|
|
|
char *Triple = LLVMGetDefaultTargetTriple();
|
2021-05-17 20:03:47 +02:00
|
|
|
char *Err;
|
|
|
|
LLVMTargetRef Target;
|
|
|
|
if (LLVMGetTargetFromTriple(Triple, &Target, &Err)) {
|
2021-05-18 18:33:50 +02:00
|
|
|
FAIL() << "Failed to create target from default triple (" << Triple
|
|
|
|
<< "): " << Err;
|
2021-05-17 20:03:47 +02:00
|
|
|
}
|
|
|
|
TM = LLVMCreateTargetMachine(Target, Triple, "generic", "",
|
|
|
|
LLVMCodeGenLevelDefault, LLVMRelocDefault,
|
|
|
|
LLVMCodeModelDefault);
|
2021-05-17 22:56:49 +02:00
|
|
|
LLVMDisposeMessage(Triple);
|
2021-05-17 20:03:47 +02:00
|
|
|
Context = LLVMContextCreate();
|
|
|
|
Module = LLVMModuleCreateWithNameInContext("test", Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
LLVMDisposeTargetMachine(TM);
|
|
|
|
LLVMDisposeModule(Module);
|
|
|
|
LLVMContextDispose(Context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
LLVMTargetMachineRef TM;
|
|
|
|
LLVMModuleRef Module;
|
|
|
|
LLVMContextRef Context;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(PassBuilderCTest, Basic) {
|
|
|
|
LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions();
|
|
|
|
LLVMPassBuilderOptionsSetLoopUnrolling(Options, 1);
|
|
|
|
LLVMPassBuilderOptionsSetVerifyEach(Options, 1);
|
|
|
|
LLVMPassBuilderOptionsSetDebugLogging(Options, 0);
|
|
|
|
if (LLVMErrorRef E = LLVMRunPasses(Module, "default<O2>", TM, Options)) {
|
|
|
|
char *Msg = LLVMGetErrorMessage(E);
|
|
|
|
LLVMConsumeError(E);
|
|
|
|
LLVMDisposePassBuilderOptions(Options);
|
|
|
|
FAIL() << "Failed to run passes: " << Msg;
|
|
|
|
}
|
|
|
|
LLVMDisposePassBuilderOptions(Options);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PassBuilderCTest, InvalidPassIsError) {
|
|
|
|
LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions();
|
|
|
|
LLVMErrorRef E1 = LLVMRunPasses(Module, "", TM, Options);
|
|
|
|
LLVMErrorRef E2 = LLVMRunPasses(Module, "does-not-exist-pass", TM, Options);
|
|
|
|
ASSERT_TRUE(E1);
|
|
|
|
ASSERT_TRUE(E2);
|
|
|
|
LLVMConsumeError(E1);
|
|
|
|
LLVMConsumeError(E2);
|
|
|
|
LLVMDisposePassBuilderOptions(Options);
|
|
|
|
}
|