-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
LLVMAttrs.cpp
269 lines (231 loc) · 9.97 KB
/
LLVMAttrs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//===- LLVMAttrs.cpp - LLVM Attributes registration -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines the attribute details for the LLVM IR dialect in MLIR.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/Interfaces/FunctionInterfaces.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include <optional>
using namespace mlir;
using namespace mlir::LLVM;
/// Parses DWARF expression arguments with respect to the DWARF operation
/// opcode. Some DWARF expression operations have a specific number of operands
/// and may appear in a textual form.
static LogicalResult parseExpressionArg(AsmParser &parser, uint64_t opcode,
SmallVector<uint64_t> &args);
/// Prints DWARF expression arguments with respect to the specific DWARF
/// operation. Some operands are printed in their textual form.
static void printExpressionArg(AsmPrinter &printer, uint64_t opcode,
ArrayRef<uint64_t> args);
#include "mlir/Dialect/LLVMIR/LLVMAttrInterfaces.cpp.inc"
#include "mlir/Dialect/LLVMIR/LLVMOpsEnums.cpp.inc"
#define GET_ATTRDEF_CLASSES
#include "mlir/Dialect/LLVMIR/LLVMOpsAttrDefs.cpp.inc"
//===----------------------------------------------------------------------===//
// LLVMDialect registration
//===----------------------------------------------------------------------===//
void LLVMDialect::registerAttributes() {
addAttributes<
#define GET_ATTRDEF_LIST
#include "mlir/Dialect/LLVMIR/LLVMOpsAttrDefs.cpp.inc"
>();
}
//===----------------------------------------------------------------------===//
// DINodeAttr
//===----------------------------------------------------------------------===//
bool DINodeAttr::classof(Attribute attr) {
return llvm::isa<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
DISubroutineTypeAttr>(attr);
}
//===----------------------------------------------------------------------===//
// DIScopeAttr
//===----------------------------------------------------------------------===//
bool DIScopeAttr::classof(Attribute attr) {
return llvm::isa<DICompileUnitAttr, DICompositeTypeAttr, DIFileAttr,
DILocalScopeAttr, DIModuleAttr, DINamespaceAttr>(attr);
}
//===----------------------------------------------------------------------===//
// DILocalScopeAttr
//===----------------------------------------------------------------------===//
bool DILocalScopeAttr::classof(Attribute attr) {
return llvm::isa<DILexicalBlockAttr, DILexicalBlockFileAttr,
DISubprogramAttr>(attr);
}
//===----------------------------------------------------------------------===//
// DITypeAttr
//===----------------------------------------------------------------------===//
bool DITypeAttr::classof(Attribute attr) {
return llvm::isa<DINullTypeAttr, DIBasicTypeAttr, DICompositeTypeAttr,
DIDerivedTypeAttr, DISubroutineTypeAttr>(attr);
}
//===----------------------------------------------------------------------===//
// TBAANodeAttr
//===----------------------------------------------------------------------===//
bool TBAANodeAttr::classof(Attribute attr) {
return llvm::isa<TBAATypeDescriptorAttr, TBAARootAttr>(attr);
}
//===----------------------------------------------------------------------===//
// MemoryEffectsAttr
//===----------------------------------------------------------------------===//
MemoryEffectsAttr MemoryEffectsAttr::get(MLIRContext *context,
ArrayRef<ModRefInfo> memInfoArgs) {
if (memInfoArgs.empty())
return MemoryEffectsAttr::get(context, ModRefInfo::ModRef,
ModRefInfo::ModRef, ModRefInfo::ModRef);
if (memInfoArgs.size() == 3)
return MemoryEffectsAttr::get(context, memInfoArgs[0], memInfoArgs[1],
memInfoArgs[2]);
return {};
}
bool MemoryEffectsAttr::isReadWrite() {
if (this->getArgMem() != ModRefInfo::ModRef)
return false;
if (this->getInaccessibleMem() != ModRefInfo::ModRef)
return false;
if (this->getOther() != ModRefInfo::ModRef)
return false;
return true;
}
//===----------------------------------------------------------------------===//
// DIExpression
//===----------------------------------------------------------------------===//
DIExpressionAttr DIExpressionAttr::get(MLIRContext *context) {
return get(context, ArrayRef<DIExpressionElemAttr>({}));
}
LogicalResult parseExpressionArg(AsmParser &parser, uint64_t opcode,
SmallVector<uint64_t> &args) {
auto operandParser = [&]() -> LogicalResult {
uint64_t operand = 0;
if (!args.empty() && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
// Attempt to parse a keyword.
StringRef keyword;
if (succeeded(parser.parseOptionalKeyword(&keyword))) {
operand = llvm::dwarf::getAttributeEncoding(keyword);
if (operand == 0) {
// The keyword is invalid.
return parser.emitError(parser.getCurrentLocation())
<< "encountered unknown attribute encoding \"" << keyword
<< "\"";
}
}
}
// operand should be non-zero if a keyword was parsed. Otherwise, the
// operand MUST be an integer.
if (operand == 0) {
// Parse the next operand as an integer.
if (parser.parseInteger(operand)) {
return parser.emitError(parser.getCurrentLocation())
<< "expected integer operand";
}
}
args.push_back(operand);
return success();
};
// Parse operands as a comma-separated list.
return parser.parseCommaSeparatedList(operandParser);
}
void printExpressionArg(AsmPrinter &printer, uint64_t opcode,
ArrayRef<uint64_t> args) {
size_t i = 0;
llvm::interleaveComma(args, printer, [&](uint64_t operand) {
if (i > 0 && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
if (const StringRef keyword =
llvm::dwarf::AttributeEncodingString(operand);
!keyword.empty()) {
printer << keyword;
return;
}
}
// All operands are expected to be printed as integers.
printer << operand;
i++;
});
}
//===----------------------------------------------------------------------===//
// DICompositeTypeAttr
//===----------------------------------------------------------------------===//
DIRecursiveTypeAttrInterface
DICompositeTypeAttr::withRecId(DistinctAttr recId) {
return DICompositeTypeAttr::get(getContext(), getTag(), recId, getName(),
getFile(), getLine(), getScope(),
getBaseType(), getFlags(), getSizeInBits(),
getAlignInBits(), getElements());
}
DIRecursiveTypeAttrInterface
DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
return DICompositeTypeAttr::get(recId.getContext(), 0, recId, {}, {}, 0, {},
{}, DIFlags(), 0, 0, {});
}
//===----------------------------------------------------------------------===//
// TargetFeaturesAttr
//===----------------------------------------------------------------------===//
TargetFeaturesAttr TargetFeaturesAttr::get(MLIRContext *context,
llvm::ArrayRef<StringRef> features) {
return Base::get(context,
llvm::map_to_vector(features, [&](StringRef feature) {
return StringAttr::get(context, feature);
}));
}
TargetFeaturesAttr TargetFeaturesAttr::get(MLIRContext *context,
StringRef targetFeatures) {
SmallVector<StringRef> features;
targetFeatures.split(features, ',', /*MaxSplit=*/-1,
/*KeepEmpty=*/false);
return get(context, features);
}
LogicalResult
TargetFeaturesAttr::verify(function_ref<InFlightDiagnostic()> emitError,
llvm::ArrayRef<StringAttr> features) {
for (StringAttr featureAttr : features) {
if (!featureAttr || featureAttr.empty())
return emitError() << "target features can not be null or empty";
auto feature = featureAttr.strref();
if (feature[0] != '+' && feature[0] != '-')
return emitError() << "target features must start with '+' or '-'";
if (feature.contains(','))
return emitError() << "target features can not contain ','";
}
return success();
}
bool TargetFeaturesAttr::contains(StringAttr feature) const {
if (nullOrEmpty())
return false;
// Note: Using StringAttr does pointer comparisons.
return llvm::is_contained(getFeatures(), feature);
}
bool TargetFeaturesAttr::contains(StringRef feature) const {
if (nullOrEmpty())
return false;
return llvm::is_contained(getFeatures(), feature);
}
std::string TargetFeaturesAttr::getFeaturesString() const {
std::string featuresString;
llvm::raw_string_ostream ss(featuresString);
llvm::interleave(
getFeatures(), ss, [&](auto &feature) { ss << feature.strref(); }, ",");
return ss.str();
}
TargetFeaturesAttr TargetFeaturesAttr::featuresAt(Operation *op) {
auto parentFunction = op->getParentOfType<FunctionOpInterface>();
if (!parentFunction)
return {};
return parentFunction.getOperation()->getAttrOfType<TargetFeaturesAttr>(
getAttributeName());
}