1 //===========- OpenMPCommonGen.cpp - OpenMP common info generator -===========//
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 // OpenMPCommonGen generates utility information from the single OpenMP source
10 // of truth in llvm/lib/Frontend/OpenMP.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "mlir/TableGen/GenInfo.h"
15
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include "llvm/TableGen/DirectiveEmitter.h"
19 #include "llvm/TableGen/Record.h"
20
21 using llvm::Clause;
22 using llvm::ClauseVal;
23 using llvm::raw_ostream;
24 using llvm::RecordKeeper;
25 using llvm::Twine;
26
27 // LLVM has multiple places (Clang, Flang, MLIR) where information about
28 // the OpenMP directives, and clauses are needed. It is good software
29 // engineering to keep the common information in a single place to avoid
30 // duplication, reduce engineering effort and prevent mistakes.
31 // Currently that common place is llvm/include/llvm/Frontend/OpenMP/OMP.td.
32 // We plan to use this tablegen source to generate all the required
33 // declarations, functions etc.
34 //
35 // Some OpenMP clauses accept only a fixed set of values as inputs. These
36 // can be represented as a String Enum Attribute (StrEnumAttr) in MLIR ODS.
37 // The emitDecls function below currently generates these enumerations. The
38 // name of the enumeration is specified in the enumClauseValue field of
39 // Clause record in OMP.td. This name can be used to specify the type of the
40 // OpenMP operation's operand. The allowedClauseValues field provides the list
41 // of ClauseValues which are part of the enumeration.
emitDecls(const RecordKeeper & recordKeeper,raw_ostream & os)42 static bool emitDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
43 const auto &clauses = recordKeeper.getAllDerivedDefinitions("Clause");
44
45 for (const auto &r : clauses) {
46 Clause c{r};
47 const auto &clauseVals = c.getClauseVals();
48 if (clauseVals.size() <= 0)
49 continue;
50
51 const auto enumName = c.getEnumName();
52 assert(enumName.size() != 0 && "enumClauseValue field not set.");
53
54 std::vector<std::string> cvDefs;
55 for (const auto &cv : clauseVals) {
56 ClauseVal cval{cv};
57 if (!cval.isUserVisible())
58 continue;
59
60 const auto name = cval.getFormattedName();
61 std::string cvDef{(enumName + llvm::Twine(name)).str()};
62 os << "def " << cvDef << " : StrEnumAttrCase<\"" << name << "\">;\n";
63 cvDefs.push_back(cvDef);
64 }
65
66 os << "def " << enumName << ": StrEnumAttr<\n";
67 os << " \"Clause" << enumName << "\",\n";
68 os << " \"" << enumName << " Clause\",\n";
69 os << " [";
70 for (unsigned int i = 0; i < cvDefs.size(); i++) {
71 os << cvDefs[i];
72 if (i != cvDefs.size() - 1)
73 os << ",";
74 }
75 os << "]> {\n";
76 os << " let cppNamespace = \"::mlir::omp\";\n";
77 os << "}\n";
78 }
79 return false;
80 }
81
82 // Registers the generator to mlir-tblgen.
83 static mlir::GenRegistration
84 genDirectiveDecls("gen-directive-decl",
85 "Generate declarations for directives (OpenMP etc.)",
__anon4bcf8a380102(const RecordKeeper &records, raw_ostream &os) 86 [](const RecordKeeper &records, raw_ostream &os) {
87 return emitDecls(records, os);
88 });
89