Skip to content

Commit 98b88de

Browse files
[LLVM-C] Test TargetMachine creation via the C API
1 parent f2367cb commit 98b88de

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

llvm/unittests/Target/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ foreach(t ${LLVM_TARGETS_TO_BUILD})
33
add_subdirectory(${t})
44
endif()
55
endforeach()
6+
7+
set(LLVM_LINK_COMPONENTS Target AllTargetsCodeGens)
8+
9+
add_llvm_unittest(TargetMachineCTests
10+
TargetMachineOptionsTest.cpp
11+
)
12+
13+
set_property(TARGET TargetMachineCTests
14+
PROPERTY FOLDER "Tests/UnitTests/TargetTests")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===-- llvm/unittests/Target/TargetMachineOptionsTest.cpp ----------
2+
//-----===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
///
10+
/// \file
11+
/// This file contains unit tests for the opaque structure describing options
12+
/// for TargetMachine creation via the C API.
13+
///
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "llvm-c/Core.h"
17+
#include "llvm-c/TargetMachine.h"
18+
#include "llvm/Config/llvm-config.h"
19+
#include "gtest/gtest.h"
20+
21+
namespace llvm {
22+
23+
TEST(TargetMachineCTest, TargetMachineOptions) {
24+
auto *Options = LLVMCreateTargetMachineOptions();
25+
26+
LLVMTargetMachineOptionsSetCPU(Options, "cortex-a53");
27+
LLVMTargetMachineOptionsSetFeatures(Options, "+neon");
28+
LLVMTargetMachineOptionsSetABI(Options, "aapcs");
29+
LLVMTargetMachineOptionsSetCodeGenOptLevel(Options, LLVMCodeGenLevelNone);
30+
LLVMTargetMachineOptionsSetRelocMode(Options, LLVMRelocStatic);
31+
LLVMTargetMachineOptionsSetCodeModel(Options, LLVMCodeModelKernel);
32+
33+
LLVMDisposeTargetMachineOptions(Options);
34+
}
35+
36+
TEST(TargetMachineCTest, TargetMachineCreation) {
37+
LLVMInitializeAllTargets();
38+
LLVMInitializeAllTargetInfos();
39+
LLVMInitializeAllTargetMCs();
40+
41+
// Get the default target to keep the test as generic as possible. This may
42+
// not be a target for which we can generate code; in that case we give up.
43+
44+
auto *Triple = LLVMGetDefaultTargetTriple();
45+
if (strlen(Triple) == 0) {
46+
LLVMDisposeMessage(Triple);
47+
GTEST_SKIP();
48+
}
49+
50+
LLVMTargetRef Target = nullptr;
51+
char *Error = nullptr;
52+
if (LLVMGetTargetFromTriple(Triple, &Target, &Error))
53+
FAIL() << "Failed to create target from default triple (" << Triple
54+
<< "): " << Error;
55+
56+
ASSERT_NE(Target, nullptr);
57+
58+
if (!LLVMTargetHasTargetMachine(Target))
59+
GTEST_SKIP() << "Default target doesn't support code generation";
60+
61+
// We don't know which target we're creating a machine for, so don't set any
62+
// non-default options; they might cause fatal errors.
63+
64+
auto *Options = LLVMCreateTargetMachineOptions();
65+
auto *TM = LLVMCreateTargetMachineWithOptions(Target, Triple, Options);
66+
ASSERT_NE(TM, nullptr);
67+
68+
LLVMDisposeMessage(Triple);
69+
LLVMDisposeTargetMachineOptions(Options);
70+
LLVMDisposeTargetMachine(TM);
71+
}
72+
73+
} // namespace llvm

0 commit comments

Comments
 (0)