1 //===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
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 // This class implements functionality shared by both MCJIT C API tests, and
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
15 #define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/MC/TargetRegistry.h"
23 #include "llvm/PassRegistry.h"
24 #include "llvm/Support/Host.h"
25 #include "llvm/Support/TargetSelect.h"
27 // Used to skip tests on unsupported architectures and operating systems.
28 // To skip a test, add this macro at the top of a test-case in a suite that
29 // inherits from MCJITTestBase. See MCJITTest.cpp for examples.
30 #define SKIP_UNSUPPORTED_PLATFORM \
32 if (!ArchSupportsMCJIT() || !OSSupportsMCJIT() || !HostCanBeTargeted()) \
38 class MCJITTestAPICommon
{
41 : HostTriple(sys::getProcessTriple())
43 InitializeNativeTarget();
44 InitializeNativeTargetAsmPrinter();
46 // FIXME: It isn't at all clear why this is necesasry, but without it we
47 // fail to initialize the AssumptionCacheTracker.
48 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
51 // On Windows, generate ELF objects by specifying "-elf" in triple
54 HostTriple
= Triple::normalize(HostTriple
);
57 bool HostCanBeTargeted() {
59 return TargetRegistry::lookupTarget(HostTriple
, Error
) != nullptr;
62 /// Returns true if the host architecture is known to support MCJIT
63 bool ArchSupportsMCJIT() {
64 Triple
Host(HostTriple
);
65 // If ARCH is not supported, bail
66 if (!is_contained(SupportedArchs
, Host
.getArch()))
69 // If ARCH is supported and has no specific sub-arch support
70 if (!is_contained(HasSubArchs
, Host
.getArch()))
73 // If ARCH has sub-arch support, find it
74 SmallVectorImpl
<std::string
>::const_iterator I
= SupportedSubArchs
.begin();
75 for(; I
!= SupportedSubArchs
.end(); ++I
)
76 if (Host
.getArchName().startswith(*I
))
82 /// Returns true if the host OS is known to support MCJIT
83 bool OSSupportsMCJIT() {
84 Triple
Host(HostTriple
);
86 if (find(UnsupportedEnvironments
, Host
.getEnvironment()) !=
87 UnsupportedEnvironments
.end())
90 if (!is_contained(UnsupportedOSs
, Host
.getOS()))
96 std::string HostTriple
;
97 SmallVector
<Triple::ArchType
, 4> SupportedArchs
;
98 SmallVector
<Triple::ArchType
, 1> HasSubArchs
;
99 SmallVector
<std::string
, 2> SupportedSubArchs
; // We need to own the memory
100 SmallVector
<Triple::OSType
, 4> UnsupportedOSs
;
101 SmallVector
<Triple::EnvironmentType
, 1> UnsupportedEnvironments
;