1 //===- OMPConstants.cpp - Helpers related to OpenMP code generation ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "llvm/Frontend/OpenMP/OMPConstants.h"
12
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/Type.h"
17
18 using namespace llvm;
19 using namespace omp;
20 using namespace types;
21
getOpenMPDirectiveKind(StringRef Str)22 Directive llvm::omp::getOpenMPDirectiveKind(StringRef Str) {
23 return llvm::StringSwitch<Directive>(Str)
24 #define OMP_DIRECTIVE(Enum, Str) .Case(Str, Enum)
25 #include "llvm/Frontend/OpenMP/OMPKinds.def"
26 .Default(OMPD_unknown);
27 }
28
getOpenMPDirectiveName(Directive Kind)29 StringRef llvm::omp::getOpenMPDirectiveName(Directive Kind) {
30 switch (Kind) {
31 #define OMP_DIRECTIVE(Enum, Str) \
32 case Enum: \
33 return Str;
34 #include "llvm/Frontend/OpenMP/OMPKinds.def"
35 }
36 llvm_unreachable("Invalid OpenMP directive kind");
37 }
38
39 /// Declarations for LLVM-IR types (simple, function and structure) are
40 /// generated below. Their names are defined and used in OpenMPKinds.def. Here
41 /// we provide the declarations, the initializeTypes function will provide the
42 /// values.
43 ///
44 ///{
45
46 #define OMP_TYPE(VarName, InitValue) Type *llvm::omp::types::VarName = nullptr;
47 #define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
48 FunctionType *llvm::omp::types::VarName = nullptr; \
49 PointerType *llvm::omp::types::VarName##Ptr = nullptr;
50 #define OMP_STRUCT_TYPE(VarName, StrName, ...) \
51 StructType *llvm::omp::types::VarName = nullptr; \
52 PointerType *llvm::omp::types::VarName##Ptr = nullptr;
53 #include "llvm/Frontend/OpenMP/OMPKinds.def"
54
55 ///}
56
initializeTypes(Module & M)57 void llvm::omp::types::initializeTypes(Module &M) {
58 if (Void)
59 return;
60
61 LLVMContext &Ctx = M.getContext();
62 // Create all simple and struct types exposed by the runtime and remember
63 // the llvm::PointerTypes of them for easy access later.
64 StructType *T;
65 #define OMP_TYPE(VarName, InitValue) VarName = InitValue;
66 #define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
67 VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
68 VarName##Ptr = PointerType::getUnqual(VarName);
69 #define OMP_STRUCT_TYPE(VarName, StructName, ...) \
70 T = M.getTypeByName(StructName); \
71 if (!T) \
72 T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
73 VarName = T; \
74 VarName##Ptr = PointerType::getUnqual(T);
75 #include "llvm/Frontend/OpenMP/OMPKinds.def"
76 }
77
uninitializeTypes()78 void llvm::omp::types::uninitializeTypes() {
79 #define OMP_TYPE(VarName, InitValue) VarName = nullptr;
80 #define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
81 VarName = nullptr; \
82 VarName##Ptr = nullptr;
83 #define OMP_STRUCT_TYPE(VarName, StrName, ...) \
84 VarName = nullptr; \
85 VarName##Ptr = nullptr;
86 #include "llvm/Frontend/OpenMP/OMPKinds.def"
87 }
88