Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / Passes / PassBuilderBindings / PassBuilderBindingsTest.cpp
blobffa3fdaf6e7e6f51e4426e4679a252f1fdfb1c4e
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 //===----------------------------------------------------------------------===//
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>
15 using namespace llvm;
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;
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;
32 TM = LLVMCreateTargetMachine(Target, Triple, "generic", "",
33 LLVMCodeGenLevelDefault, LLVMRelocDefault,
34 LLVMCodeModelDefault);
35 LLVMDisposeMessage(Triple);
36 Context = LLVMContextCreate();
37 Module = LLVMModuleCreateWithNameInContext("test", Context);
40 void TearDown() override {
41 char *Triple = LLVMGetDefaultTargetTriple();
42 if (strlen(Triple) == 0) {
43 LLVMDisposeMessage(Triple);
44 return; // Skipped, so nothing to tear down
46 LLVMDisposeMessage(Triple);
47 LLVMDisposeTargetMachine(TM);
48 LLVMDisposeModule(Module);
49 LLVMContextDispose(Context);
52 public:
53 LLVMTargetMachineRef TM;
54 LLVMModuleRef Module;
55 LLVMContextRef Context;
58 TEST_F(PassBuilderCTest, Basic) {
59 LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions();
60 LLVMPassBuilderOptionsSetLoopUnrolling(Options, 1);
61 LLVMPassBuilderOptionsSetVerifyEach(Options, 1);
62 LLVMPassBuilderOptionsSetDebugLogging(Options, 0);
63 if (LLVMErrorRef E = LLVMRunPasses(Module, "default<O2>", TM, Options)) {
64 char *Msg = LLVMGetErrorMessage(E);
65 LLVMConsumeError(E);
66 LLVMDisposePassBuilderOptions(Options);
67 FAIL() << "Failed to run passes: " << Msg;
69 LLVMDisposePassBuilderOptions(Options);
72 TEST_F(PassBuilderCTest, InvalidPassIsError) {
73 LLVMPassBuilderOptionsRef Options = LLVMCreatePassBuilderOptions();
74 LLVMErrorRef E1 = LLVMRunPasses(Module, "", TM, Options);
75 LLVMErrorRef E2 = LLVMRunPasses(Module, "does-not-exist-pass", TM, Options);
76 ASSERT_TRUE(E1);
77 ASSERT_TRUE(E2);
78 LLVMConsumeError(E1);
79 LLVMConsumeError(E2);
80 LLVMDisposePassBuilderOptions(Options);