18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/TypeSwitch.h"
20 #include "llvm/BinaryFormat/Dwarf.h"
21 #include "llvm/IR/DebugInfoMetadata.h"
31 SmallVector<uint64_t> &args);
36 ArrayRef<uint64_t> args);
38 #include "mlir/Dialect/LLVMIR/LLVMAttrInterfaces.cpp.inc"
39 #include "mlir/Dialect/LLVMIR/LLVMOpsEnums.cpp.inc"
40 #define GET_ATTRDEF_CLASSES
41 #include "mlir/Dialect/LLVMIR/LLVMOpsAttrDefs.cpp.inc"
47 void LLVMDialect::registerAttributes() {
49 #define GET_ATTRDEF_LIST
50 #include "mlir/Dialect/LLVMIR/LLVMOpsAttrDefs.cpp.inc"
61 Attribute id, AliasScopeDomainAttr domain,
62 StringAttr description) {
65 if (!llvm::isa<StringAttr, DistinctAttr>(
id))
67 <<
"id of an alias scope must be a StringAttr or a DistrinctAttr";
78 DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
79 DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr, DIGenericSubrangeAttr,
80 DIGlobalVariableAttr, DIImportedEntityAttr, DILabelAttr,
81 DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
82 DIModuleAttr, DINamespaceAttr, DINullTypeAttr, DIAnnotationAttr,
83 DIStringTypeAttr, DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
92 return llvm::isa<DICommonBlockAttr, DICompileUnitAttr, DICompositeTypeAttr,
102 return llvm::isa<DILexicalBlockAttr, DILexicalBlockFileAttr,
103 DISubprogramAttr>(attr);
111 return llvm::isa<DILocalVariableAttr, DIGlobalVariableAttr>(attr);
119 return llvm::isa<DINullTypeAttr, DIBasicTypeAttr, DICompositeTypeAttr,
120 DIDerivedTypeAttr, DIStringTypeAttr, DISubroutineTypeAttr>(
129 return llvm::isa<TBAATypeDescriptorAttr, TBAARootAttr>(attr);
138 if (memInfoArgs.empty())
140 ModRefInfo::ModRef, ModRefInfo::ModRef);
141 if (memInfoArgs.size() == 3)
147 bool MemoryEffectsAttr::isReadWrite() {
148 if (this->getArgMem() != ModRefInfo::ModRef)
150 if (this->getInaccessibleMem() != ModRefInfo::ModRef)
152 if (this->getOther() != ModRefInfo::ModRef)
162 return get(context, ArrayRef<DIExpressionElemAttr>({}));
166 SmallVector<uint64_t> &args) {
167 auto operandParser = [&]() -> LogicalResult {
168 uint64_t operand = 0;
169 if (!args.empty() && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
173 operand = llvm::dwarf::getAttributeEncoding(keyword);
177 <<
"encountered unknown attribute encoding \"" << keyword
189 <<
"expected integer operand";
193 args.push_back(operand);
202 ArrayRef<uint64_t> args) {
204 llvm::interleaveComma(args, printer, [&](uint64_t operand) {
205 if (i > 0 && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
206 if (
const StringRef keyword =
207 llvm::dwarf::AttributeEncodingString(operand);
223 DIRecursiveTypeAttrInterface
226 getContext(), recId, getIsRecSelf(), getTag(), getName(), getFile(),
227 getLine(), getScope(), getBaseType(), getFlags(), getSizeInBits(),
228 getAlignInBits(), getElements(), getDataLocation(), getRank(),
229 getAllocated(), getAssociated());
232 DIRecursiveTypeAttrInterface
235 0, {}, {}, 0, {}, {}, DIFlags(), 0, 0, {}, {},
243 DIRecursiveTypeAttrInterface DISubprogramAttr::withRecId(
DistinctAttr recId) {
245 getCompileUnit(), getScope(), getName(),
246 getLinkageName(), getFile(), getLine(),
247 getScopeLine(), getSubprogramFlags(),
getType(),
248 getRetainedNodes(), getAnnotations());
251 DIRecursiveTypeAttrInterface DISubprogramAttr::getRecSelf(
DistinctAttr recId) {
253 {}, {}, {}, {}, {}, {}, 0, 0, {}, {}, {}, {});
262 IntegerType widthType;
267 unsigned bitWidth = widthType.getWidth();
268 APInt lower(bitWidth, 0);
269 APInt upper(bitWidth, 0);
274 lower = lower.sextOrTrunc(bitWidth);
275 upper = upper.sextOrTrunc(bitWidth);
281 printer <<
"<i" << getLower().getBitWidth() <<
", " << getLower() <<
", "
282 << getUpper() <<
">";
287 APInt lower, APInt upper) {
288 if (lower.getBitWidth() != upper.getBitWidth())
290 <<
"expected lower and upper to have matching bitwidths but got "
291 << lower.getBitWidth() <<
" vs. " << upper.getBitWidth();
302 llvm::map_to_vector(features, [&](StringRef feature) {
311 return Base::getChecked(
emitError, context,
312 llvm::map_to_vector(features, [&](StringRef feature) {
318 StringRef targetFeatures) {
319 SmallVector<StringRef> features;
320 targetFeatures.split(features,
',', -1,
322 return get(context, features);
328 SmallVector<StringRef> features;
329 targetFeatures.split(features,
',', -1,
331 ArrayRef featuresRef(features);
332 return getChecked(
emitError, context, featuresRef);
338 for (StringAttr featureAttr : features) {
339 if (!featureAttr || featureAttr.empty())
340 return emitError() <<
"target features can not be null or empty";
341 auto feature = featureAttr.strref();
342 if (feature[0] !=
'+' && feature[0] !=
'-')
343 return emitError() <<
"target features must start with '+' or '-'";
344 if (feature.contains(
','))
345 return emitError() <<
"target features can not contain ','";
354 return llvm::is_contained(getFeatures(), feature);
360 return llvm::is_contained(getFeatures(), feature);
363 std::string TargetFeaturesAttr::getFeaturesString()
const {
364 std::string featuresString;
365 llvm::raw_string_ostream ss(featuresString);
367 getFeatures(), ss, [&](
auto &feature) { ss << feature.strref(); },
",");
368 return featuresString;
371 TargetFeaturesAttr TargetFeaturesAttr::featuresAt(
Operation *op) {
375 return parentFunction.getOperation()->getAttrOfType<TargetFeaturesAttr>(
static MLIRContext * getContext(OpFoldResult val)
static void printExpressionArg(AsmPrinter &printer, uint64_t opcode, ArrayRef< uint64_t > args)
Prints DWARF expression arguments with respect to the specific DWARF operation.
static ParseResult parseExpressionArg(AsmParser &parser, uint64_t opcode, SmallVector< uint64_t > &args)
Parses DWARF expression arguments with respect to the DWARF operation opcode.
static bool contains(SMRange range, SMLoc loc)
Returns true if the given range contains the given source location.
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
auto getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
MLIRContext * getContext() const
Return the context this attribute belongs to.
An attribute that associates a referenced attribute with a unique identifier.
This class represents a diagnostic that is inflight and set to be reported.
This class represents a LLVM attribute that describes a local debug info scope.
static bool classof(Attribute attr)
Support LLVM type casting.
static bool classof(Attribute attr)
static bool classof(Attribute attr)
Support LLVM type casting.
static bool classof(Attribute attr)
Support LLVM type casting.
static bool classof(Attribute attr)
Support LLVM type casting.
static bool classof(Attribute attr)
Support LLVM type casting.
MLIRContext is the top-level object for a collection of MLIR operations.
Operation is the basic unit of execution within MLIR.
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...