1//===- LLVMIntrinsicGen.cpp - TableGen utility for converting intrinsics --===//
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// This is a TableGen generator that converts TableGen definitions for LLVM
10// intrinsics to TableGen definitions for MLIR operations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/TableGen/GenInfo.h"
15
16#include "llvm/ADT/SmallBitVector.h"
17#include "llvm/ADT/StringSwitch.h"
18#include "llvm/CodeGenTypes/MachineValueType.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/PrettyStackTrace.h"
21#include "llvm/Support/Regex.h"
22#include "llvm/Support/Signals.h"
23#include "llvm/TableGen/Error.h"
24#include "llvm/TableGen/Main.h"
25#include "llvm/TableGen/Record.h"
26#include "llvm/TableGen/TableGenBackend.h"
27
28using llvm::Record;
29using llvm::RecordKeeper;
30using llvm::Regex;
31using namespace mlir;
32
33static llvm::cl::OptionCategory intrinsicGenCat("Intrinsics Generator Options");
34
35static llvm::cl::opt<std::string>
36 nameFilter("llvmir-intrinsics-filter",
37 llvm::cl::desc("Only keep the intrinsics with the specified "
38 "substring in their record name"),
39 llvm::cl::cat(intrinsicGenCat));
40
41static llvm::cl::opt<std::string>
42 opBaseClass("dialect-opclass-base",
43 llvm::cl::desc("The base class for the ops in the dialect we "
44 "are planning to emit"),
45 llvm::cl::init(Val: "LLVM_IntrOp"), llvm::cl::cat(intrinsicGenCat));
46
47static llvm::cl::opt<std::string> accessGroupRegexp(
48 "llvmir-intrinsics-access-group-regexp",
49 llvm::cl::desc("Mark intrinsics that match the specified "
50 "regexp as taking an access group metadata"),
51 llvm::cl::cat(intrinsicGenCat));
52
53static llvm::cl::opt<std::string> aliasAnalysisRegexp(
54 "llvmir-intrinsics-alias-analysis-regexp",
55 llvm::cl::desc("Mark intrinsics that match the specified "
56 "regexp as taking alias.scopes, noalias, and tbaa metadata"),
57 llvm::cl::cat(intrinsicGenCat));
58
59// Used to represent the indices of overloadable operands/results.
60using IndicesTy = llvm::SmallBitVector;
61
62/// Return a CodeGen value type entry from a type record.
63static llvm::MVT::SimpleValueType getValueType(const Record *rec) {
64 return StringSwitch<llvm::MVT::SimpleValueType>(
65 rec->getValueAsDef(FieldName: "VT")->getValueAsString(FieldName: "LLVMName"))
66#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
67 .Case(#Ty, llvm::MVT::Ty)
68#include "llvm/CodeGen/GenVT.inc"
69#undef GET_VT_ATTR
70 .Case(S: "INVALID_SIMPLE_VALUE_TYPE", Value: llvm::MVT::INVALID_SIMPLE_VALUE_TYPE);
71}
72
73/// Return the indices of the definitions in a list of definitions that
74/// represent overloadable types
75static IndicesTy getOverloadableTypeIdxs(const Record &record,
76 const char *listName) {
77 auto results = record.getValueAsListOfDefs(FieldName: listName);
78 IndicesTy overloadedOps(results.size());
79 for (const auto &r : llvm::enumerate(First&: results)) {
80 // Partially dependent types are assigned their own overload index and
81 // therefore count as overloaded types.
82 if (r.value()->isSubClassOf(Name: "LLVMPartiallyDependentType")) {
83 overloadedOps.set(r.index());
84 continue;
85 }
86 llvm::MVT::SimpleValueType vt = getValueType(rec: r.value());
87 switch (vt) {
88 case llvm::MVT::iAny:
89 case llvm::MVT::fAny:
90 case llvm::MVT::Any:
91 case llvm::MVT::pAny:
92 case llvm::MVT::vAny:
93 overloadedOps.set(r.index());
94 break;
95 default:
96 continue;
97 }
98 }
99 return overloadedOps;
100}
101
102namespace {
103/// A wrapper for LLVM's Tablegen class `Intrinsic` that provides accessors to
104/// the fields of the record.
105class LLVMIntrinsic {
106public:
107 LLVMIntrinsic(const Record &record) : record(record) {}
108
109 /// Get the name of the operation to be used in MLIR. Uses the appropriate
110 /// field if not empty, constructs a name by replacing underscores with dots
111 /// in the record name otherwise.
112 std::string getOperationName() const {
113 StringRef name = record.getValueAsString(FieldName: fieldName);
114 if (!name.empty())
115 return name.str();
116
117 name = record.getName();
118 assert(name.starts_with("int_") &&
119 "LLVM intrinsic names are expected to start with 'int_'");
120 name = name.drop_front(N: 4);
121 SmallVector<StringRef, 8> chunks;
122 StringRef targetPrefix = record.getValueAsString(FieldName: "TargetPrefix");
123 name.split(A&: chunks, Separator: '_');
124 auto *chunksBegin = chunks.begin();
125 // Remove the target prefix from target specific intrinsics.
126 if (!targetPrefix.empty()) {
127 assert(targetPrefix == *chunksBegin &&
128 "Intrinsic has TargetPrefix, but "
129 "record name doesn't begin with it");
130 assert(chunks.size() >= 2 &&
131 "Intrinsic has TargetPrefix, but "
132 "chunks has only one element meaning the intrinsic name is empty");
133 ++chunksBegin;
134 }
135 return llvm::join(Begin: chunksBegin, End: chunks.end(), Separator: ".");
136 }
137
138 /// Get the name of the record without the "intrinsic" prefix.
139 StringRef getProperRecordName() const {
140 StringRef name = record.getName();
141 assert(name.starts_with("int_") &&
142 "LLVM intrinsic names are expected to start with 'int_'");
143 return name.drop_front(N: 4);
144 }
145
146 /// Get the number of operands.
147 unsigned getNumOperands() const {
148 auto operands = record.getValueAsListOfDefs(FieldName: fieldOperands);
149 assert(llvm::all_of(
150 operands,
151 [](const Record *r) { return r->isSubClassOf("LLVMType"); }) &&
152 "expected operands to be of LLVM type");
153 return operands.size();
154 }
155
156 /// Get the number of results. Note that LLVM does not support multi-value
157 /// operations so, in fact, multiple results will be returned as a value of
158 /// structure type.
159 unsigned getNumResults() const {
160 auto results = record.getValueAsListOfDefs(FieldName: fieldResults);
161 for (const Record *r : results) {
162 (void)r;
163 assert(r->isSubClassOf("LLVMType") &&
164 "expected operands to be of LLVM type");
165 }
166 return results.size();
167 }
168
169 /// Return true if the intrinsic may have side effects, i.e. does not have the
170 /// `IntrNoMem` property or has the `IntrHasSideEffects` property.
171 bool hasSideEffects() const {
172 return llvm::none_of(
173 Range: record.getValueAsListOfDefs(FieldName: fieldTraits),
174 P: [](const Record *r) { return r->getName() == "IntrNoMem"; }) ||
175 llvm::any_of(Range: record.getValueAsListOfDefs(FieldName: fieldTraits),
176 P: [](const Record *r) {
177 return r->getName() == "IntrHasSideEffects";
178 });
179 }
180
181 /// Return true if the intrinsic is commutative, i.e. has the respective
182 /// property.
183 bool isCommutative() const {
184 return llvm::any_of(
185 Range: record.getValueAsListOfDefs(FieldName: fieldTraits),
186 P: [](const Record *r) { return r->getName() == "Commutative"; });
187 }
188
189 IndicesTy getOverloadableOperandsIdxs() const {
190 return getOverloadableTypeIdxs(record, listName: fieldOperands);
191 }
192
193 IndicesTy getOverloadableResultsIdxs() const {
194 return getOverloadableTypeIdxs(record, listName: fieldResults);
195 }
196
197private:
198 /// Names of the fields in the Intrinsic LLVM Tablegen class.
199 const char *fieldName = "LLVMName";
200 const char *fieldOperands = "ParamTypes";
201 const char *fieldResults = "RetTypes";
202 const char *fieldTraits = "IntrProperties";
203
204 const Record &record;
205};
206} // namespace
207
208/// Prints the elements in "range" separated by commas and surrounded by "[]".
209template <typename Range>
210static void printBracketedRange(const Range &range, llvm::raw_ostream &os) {
211 os << '[';
212 llvm::interleaveComma(range, os);
213 os << ']';
214}
215
216/// Emits ODS (TableGen-based) code for `record` representing an LLVM intrinsic.
217/// Returns true on error, false on success.
218static bool emitIntrinsic(const Record &record, llvm::raw_ostream &os) {
219 LLVMIntrinsic intr(record);
220
221 Regex accessGroupMatcher(accessGroupRegexp);
222 bool requiresAccessGroup =
223 !accessGroupRegexp.empty() && accessGroupMatcher.match(String: record.getName());
224
225 Regex aliasAnalysisMatcher(aliasAnalysisRegexp);
226 bool requiresAliasAnalysis = !aliasAnalysisRegexp.empty() &&
227 aliasAnalysisMatcher.match(String: record.getName());
228
229 // Prepare strings for traits, if any.
230 SmallVector<StringRef, 2> traits;
231 if (intr.isCommutative())
232 traits.push_back(Elt: "Commutative");
233 if (!intr.hasSideEffects())
234 traits.push_back(Elt: "NoMemoryEffect");
235
236 // Prepare strings for operands.
237 SmallVector<StringRef, 8> operands(intr.getNumOperands(), "LLVM_Type");
238 if (requiresAccessGroup)
239 operands.push_back(
240 Elt: "OptionalAttr<LLVM_AccessGroupArrayAttr>:$access_groups");
241 if (requiresAliasAnalysis) {
242 operands.push_back(Elt: "OptionalAttr<LLVM_AliasScopeArrayAttr>:$alias_scopes");
243 operands.push_back(
244 Elt: "OptionalAttr<LLVM_AliasScopeArrayAttr>:$noalias_scopes");
245 operands.push_back(Elt: "OptionalAttr<LLVM_TBAATagArrayAttr>:$tbaa");
246 }
247
248 // Emit the definition.
249 os << "def LLVM_" << intr.getProperRecordName() << " : " << opBaseClass
250 << "<\"" << intr.getOperationName() << "\", ";
251 printBracketedRange(range: intr.getOverloadableResultsIdxs().set_bits(), os);
252 os << ", ";
253 printBracketedRange(range: intr.getOverloadableOperandsIdxs().set_bits(), os);
254 os << ", ";
255 printBracketedRange(range: traits, os);
256 os << ", " << intr.getNumResults() << ", "
257 << (requiresAccessGroup ? "1" : "0") << ", "
258 << (requiresAliasAnalysis ? "1" : "0") << ">, Arguments<(ins"
259 << (operands.empty() ? "" : " ");
260 llvm::interleaveComma(c: operands, os);
261 os << ")>;\n\n";
262
263 return false;
264}
265
266/// Traverses the list of TableGen definitions derived from the "Intrinsic"
267/// class and generates MLIR ODS definitions for those intrinsics that have
268/// the name matching the filter.
269static bool emitIntrinsics(const RecordKeeper &records, llvm::raw_ostream &os) {
270 llvm::emitSourceFileHeader(Desc: "Operations for LLVM intrinsics", OS&: os, Record: records);
271 os << "include \"mlir/Dialect/LLVMIR/LLVMOpBase.td\"\n";
272 os << "include \"mlir/Interfaces/SideEffectInterfaces.td\"\n\n";
273
274 auto defs = records.getAllDerivedDefinitions(ClassName: "Intrinsic");
275 for (const Record *r : defs) {
276 if (!nameFilter.empty() && !r->getName().contains(Other: nameFilter))
277 continue;
278 if (emitIntrinsic(record: *r, os))
279 return true;
280 }
281
282 return false;
283}
284
285static mlir::GenRegistration genLLVMIRIntrinsics("gen-llvmir-intrinsics",
286 "Generate LLVM IR intrinsics",
287 emitIntrinsics);
288