xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/clover/llvm/compat.hpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 //
2 // Copyright 2016 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 
23 ///
24 /// \file
25 /// Some thin wrappers around the Clang/LLVM API used to preserve
26 /// compatibility with older API versions while keeping the ifdef clutter low
27 /// in the rest of the clover::llvm subtree.  In case of an API break please
28 /// consider whether it's possible to preserve backwards compatibility by
29 /// introducing a new one-liner inline function or typedef here under the
30 /// compat namespace in order to keep the running code free from preprocessor
31 /// conditionals.
32 ///
33 
34 #ifndef CLOVER_LLVM_COMPAT_HPP
35 #define CLOVER_LLVM_COMPAT_HPP
36 
37 #include "util/algorithm.hpp"
38 
39 #include <llvm/Config/llvm-config.h>
40 
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/Support/CodeGen.h>
47 #include <llvm/Target/TargetMachine.h>
48 #include <llvm/Transforms/IPO.h>
49 #include <llvm/Transforms/Utils/Cloning.h>
50 
51 #include <clang/Basic/TargetInfo.h>
52 #include <clang/Frontend/CompilerInstance.h>
53 #include <clang/Lex/PreprocessorOptions.h>
54 
55 #if LLVM_VERSION_MAJOR >= 14
56 #include <llvm/MC/TargetRegistry.h>
57 #else
58 #include <llvm/Support/TargetRegistry.h>
59 #endif
60 
61 #if LLVM_VERSION_MAJOR >= 17
62 #include <llvm/TargetParser/Triple.h>
63 #else
64 #include <llvm/ADT/Triple.h>
65 #endif
66 
67 namespace clover {
68    namespace llvm {
69       namespace compat {
70 
71 #if LLVM_VERSION_MAJOR >= 18
72          const auto CGFT_ObjectFile = ::llvm::CodeGenFileType::ObjectFile;
73          const auto CGFT_AssemblyFile = ::llvm::CodeGenFileType::AssemblyFile;
74 #else
75          const auto CGFT_ObjectFile = ::llvm::CGFT_ObjectFile;
76          const auto CGFT_AssemblyFile = ::llvm::CGFT_AssemblyFile;
77 #endif
78          typedef ::llvm::CodeGenFileType CodeGenFileType;
79 
80          const clang::InputKind ik_opencl = clang::Language::OpenCL;
81 
82          template<typename T> inline bool
create_compiler_invocation_from_args(clang::CompilerInvocation & cinv,T copts,clang::DiagnosticsEngine & diag)83          create_compiler_invocation_from_args(clang::CompilerInvocation &cinv,
84                                               T copts,
85                                               clang::DiagnosticsEngine &diag)
86          {
87             return clang::CompilerInvocation::CreateFromArgs(
88                cinv, copts, diag);
89          }
90 
91          static inline void
compiler_set_lang_defaults(std::unique_ptr<clang::CompilerInstance> & c,clang::InputKind ik,const::llvm::Triple & triple,clang::LangStandard::Kind d)92          compiler_set_lang_defaults(std::unique_ptr<clang::CompilerInstance> &c,
93                                     clang::InputKind ik, const ::llvm::Triple& triple,
94                                     clang::LangStandard::Kind d)
95          {
96 #if LLVM_VERSION_MAJOR >= 15
97             c->getLangOpts().setLangDefaults(c->getLangOpts(), ik.getLanguage(), triple,
98 #else
99             c->getInvocation().setLangDefaults(c->getLangOpts(), ik, triple,
100 #endif
101 #if LLVM_VERSION_MAJOR >= 12
102                                                c->getPreprocessorOpts().Includes,
103 #else
104                                                c->getPreprocessorOpts(),
105 #endif
106                                                d);
107          }
108 
109          static inline unsigned
get_abi_type_alignment(::llvm::DataLayout dl,::llvm::Type * type)110          get_abi_type_alignment(::llvm::DataLayout dl, ::llvm::Type *type)
111          {
112 #if LLVM_VERSION_MAJOR >= 16
113             return dl.getABITypeAlign(type).value();
114 #else
115             return dl.getABITypeAlignment(type);
116 #endif
117          }
118 
119          static inline bool
is_scalable_vector(const::llvm::Type * type)120          is_scalable_vector(const ::llvm::Type *type)
121          {
122             return ::llvm::isa<::llvm::ScalableVectorType>(type);
123          }
124 
125          static inline bool
is_fixed_vector(const::llvm::Type * type)126          is_fixed_vector(const ::llvm::Type *type)
127          {
128             return ::llvm::isa<::llvm::FixedVectorType>(type);
129          }
130 
131          static inline unsigned
get_fixed_vector_elements(const::llvm::Type * type)132          get_fixed_vector_elements(const ::llvm::Type *type)
133          {
134             return ::llvm::cast<::llvm::FixedVectorType>(type)->getNumElements();
135          }
136       }
137    }
138 }
139 
140 #endif
141