blob: 0f554439ad42a8f66fb16fab9153089100f0e057 [file] [log] [blame]
Andrew Kaylor31be5ef2013-04-29 17:49:40 +00001//===- MCJITTestBase.h - Common base class for MCJIT Unit tests ----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Andrew Kaylor31be5ef2013-04-29 17:49:40 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class implements functionality shared by both MCJIT C API tests, and
10// the C++ API tests.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
15#define LLVM_UNITTESTS_EXECUTIONENGINE_MCJIT_MCJITTESTAPICOMMON_H
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000016
David Majnemer0d955d02016-08-11 22:21:41 +000017#include "llvm/ADT/STLExtras.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000018#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Triple.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000020#include "llvm/IR/LegacyPassManager.h"
21#include "llvm/InitializePasses.h"
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000022#include "llvm/Support/Host.h"
23#include "llvm/Support/TargetSelect.h"
24
25// Used to skip tests on unsupported architectures and operating systems.
26// To skip a test, add this macro at the top of a test-case in a suite that
27// inherits from MCJITTestBase. See MCJITTest.cpp for examples.
28#define SKIP_UNSUPPORTED_PLATFORM \
29 do \
30 if (!ArchSupportsMCJIT() || !OSSupportsMCJIT()) \
31 return; \
32 while(0)
33
34namespace llvm {
35
36class MCJITTestAPICommon {
37protected:
38 MCJITTestAPICommon()
39 : HostTriple(sys::getProcessTriple())
40 {
41 InitializeNativeTarget();
42 InitializeNativeTargetAsmPrinter();
43
Daniel Jasperaec2fa32016-12-19 08:22:17 +000044 // FIXME: It isn't at all clear why this is necesasry, but without it we
45 // fail to initialize the AssumptionCacheTracker.
46 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
47
Nico Weber712e8d22018-04-29 00:45:03 +000048#ifdef _WIN32
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000049 // On Windows, generate ELF objects by specifying "-elf" in triple
50 HostTriple += "-elf";
Nico Weber712e8d22018-04-29 00:45:03 +000051#endif // _WIN32
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000052 HostTriple = Triple::normalize(HostTriple);
53 }
54
55 /// Returns true if the host architecture is known to support MCJIT
56 bool ArchSupportsMCJIT() {
57 Triple Host(HostTriple);
Renato Golincf6979d2013-05-19 20:10:10 +000058 // If ARCH is not supported, bail
David Majnemer0d955d02016-08-11 22:21:41 +000059 if (!is_contained(SupportedArchs, Host.getArch()))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000060 return false;
Renato Golincf6979d2013-05-19 20:10:10 +000061
62 // If ARCH is supported and has no specific sub-arch support
David Majnemer0d955d02016-08-11 22:21:41 +000063 if (!is_contained(HasSubArchs, Host.getArch()))
Renato Golincf6979d2013-05-19 20:10:10 +000064 return true;
65
66 // If ARCH has sub-arch support, find it
67 SmallVectorImpl<std::string>::const_iterator I = SupportedSubArchs.begin();
68 for(; I != SupportedSubArchs.end(); ++I)
Malcolm Parsons06ac79c2016-11-02 16:43:50 +000069 if (Host.getArchName().startswith(*I))
Renato Golincf6979d2013-05-19 20:10:10 +000070 return true;
71
72 return false;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000073 }
74
75 /// Returns true if the host OS is known to support MCJIT
76 bool OSSupportsMCJIT() {
77 Triple Host(HostTriple);
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000078
David Majnemer0d955d02016-08-11 22:21:41 +000079 if (find(UnsupportedEnvironments, Host.getEnvironment()) !=
80 UnsupportedEnvironments.end())
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000081 return false;
82
David Majnemer0d955d02016-08-11 22:21:41 +000083 if (!is_contained(UnsupportedOSs, Host.getOS()))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000084 return true;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000085
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000086 return false;
87 }
88
89 std::string HostTriple;
90 SmallVector<Triple::ArchType, 4> SupportedArchs;
Renato Golincf6979d2013-05-19 20:10:10 +000091 SmallVector<Triple::ArchType, 1> HasSubArchs;
92 SmallVector<std::string, 2> SupportedSubArchs; // We need to own the memory
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000093 SmallVector<Triple::OSType, 4> UnsupportedOSs;
Saleem Abdulrasoold3bafe32014-03-31 23:42:23 +000094 SmallVector<Triple::EnvironmentType, 1> UnsupportedEnvironments;
Andrew Kaylor31be5ef2013-04-29 17:49:40 +000095};
96
97} // namespace llvm
98
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000099#endif
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000100