Path: blob/21.2-virgl/src/gallium/frontends/clover/llvm/compat.hpp
4573 views
//1// Copyright 2016 Francisco Jerez2//3// Permission is hereby granted, free of charge, to any person obtaining a4// copy of this software and associated documentation files (the "Software"),5// to deal in the Software without restriction, including without limitation6// the rights to use, copy, modify, merge, publish, distribute, sublicense,7// and/or sell copies of the Software, and to permit persons to whom the8// Software is furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR17// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19// OTHER DEALINGS IN THE SOFTWARE.20//2122///23/// \file24/// Some thin wrappers around the Clang/LLVM API used to preserve25/// compatibility with older API versions while keeping the ifdef clutter low26/// in the rest of the clover::llvm subtree. In case of an API break please27/// consider whether it's possible to preserve backwards compatibility by28/// introducing a new one-liner inline function or typedef here under the29/// compat namespace in order to keep the running code free from preprocessor30/// conditionals.31///3233#ifndef CLOVER_LLVM_COMPAT_HPP34#define CLOVER_LLVM_COMPAT_HPP3536#include "util/algorithm.hpp"3738#include <llvm/Config/llvm-config.h>3940#include <llvm/ADT/Triple.h>41#include <llvm/Analysis/TargetLibraryInfo.h>42#include <llvm/IR/LegacyPassManager.h>43#include <llvm/IR/LLVMContext.h>44#include <llvm/IR/Type.h>45#include <llvm/Linker/Linker.h>46#include <llvm/Target/TargetMachine.h>47#include <llvm/Transforms/IPO.h>48#include <llvm/Transforms/Utils/Cloning.h>4950#include <clang/Basic/TargetInfo.h>51#include <clang/Frontend/CompilerInstance.h>52#include <clang/Lex/PreprocessorOptions.h>5354#if LLVM_VERSION_MAJOR >= 1055#include <llvm/Support/CodeGen.h>56#endif5758namespace clover {59namespace llvm {60namespace compat {6162#if LLVM_VERSION_MAJOR >= 1063const auto CGFT_ObjectFile = ::llvm::CGFT_ObjectFile;64const auto CGFT_AssemblyFile = ::llvm::CGFT_AssemblyFile;65typedef ::llvm::CodeGenFileType CodeGenFileType;66#else67const auto CGFT_ObjectFile = ::llvm::TargetMachine::CGFT_ObjectFile;68const auto CGFT_AssemblyFile =69::llvm::TargetMachine::CGFT_AssemblyFile;70typedef ::llvm::TargetMachine::CodeGenFileType CodeGenFileType;71#endif7273#if LLVM_VERSION_MAJOR >= 1074const clang::InputKind ik_opencl = clang::Language::OpenCL;75#else76const clang::InputKind ik_opencl = clang::InputKind::OpenCL;77#endif7879template<typename T> inline bool80create_compiler_invocation_from_args(clang::CompilerInvocation &cinv,81T copts,82clang::DiagnosticsEngine &diag)83{84#if LLVM_VERSION_MAJOR >= 1085return clang::CompilerInvocation::CreateFromArgs(86cinv, copts, diag);87#else88return clang::CompilerInvocation::CreateFromArgs(89cinv, copts.data(), copts.data() + copts.size(), diag);90#endif91}9293static inline void94compiler_set_lang_defaults(std::unique_ptr<clang::CompilerInstance> &c,95clang::InputKind ik, const ::llvm::Triple& triple,96clang::LangStandard::Kind d)97{98c->getInvocation().setLangDefaults(c->getLangOpts(), ik, triple,99#if LLVM_VERSION_MAJOR >= 12100c->getPreprocessorOpts().Includes,101#else102c->getPreprocessorOpts(),103#endif104d);105}106107static inline bool108is_scalable_vector(const ::llvm::Type *type)109{110#if LLVM_VERSION_MAJOR >= 11111return ::llvm::isa<::llvm::ScalableVectorType>(type);112#else113return false;114#endif115}116117static inline bool118is_fixed_vector(const ::llvm::Type *type)119{120#if LLVM_VERSION_MAJOR >= 11121return ::llvm::isa<::llvm::FixedVectorType>(type);122#else123return type->isVectorTy();124#endif125}126127static inline unsigned128get_fixed_vector_elements(const ::llvm::Type *type)129{130#if LLVM_VERSION_MAJOR >= 11131return ::llvm::cast<::llvm::FixedVectorType>(type)->getNumElements();132#else133return ((::llvm::VectorType*)type)->getNumElements();134#endif135}136}137}138}139140#endif141142143