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"
60 DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
61 DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr, DIGenericSubrangeAttr,
62 DIGlobalVariableAttr, DIImportedEntityAttr, DILabelAttr,
63 DILexicalBlockAttr, DILexicalBlockFileAttr, DILocalVariableAttr,
64 DIModuleAttr, DINamespaceAttr, DINullTypeAttr, DIAnnotationAttr,
65 DIStringTypeAttr, DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
74 return llvm::isa<DICommonBlockAttr, DICompileUnitAttr, DICompositeTypeAttr,
84 return llvm::isa<DILexicalBlockAttr, DILexicalBlockFileAttr,
85 DISubprogramAttr>(attr);
93 return llvm::isa<DILocalVariableAttr, DIGlobalVariableAttr>(attr);
101 return llvm::isa<DINullTypeAttr, DIBasicTypeAttr, DICompositeTypeAttr,
102 DIDerivedTypeAttr, DIStringTypeAttr, DISubroutineTypeAttr>(
111 return llvm::isa<TBAATypeDescriptorAttr, TBAARootAttr>(attr);
120 if (memInfoArgs.empty())
122 ModRefInfo::ModRef, ModRefInfo::ModRef);
123 if (memInfoArgs.size() == 3)
129 bool MemoryEffectsAttr::isReadWrite() {
130 if (this->getArgMem() != ModRefInfo::ModRef)
132 if (this->getInaccessibleMem() != ModRefInfo::ModRef)
134 if (this->getOther() != ModRefInfo::ModRef)
144 return get(context, ArrayRef<DIExpressionElemAttr>({}));
148 SmallVector<uint64_t> &args) {
149 auto operandParser = [&]() -> LogicalResult {
150 uint64_t operand = 0;
151 if (!args.empty() && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
155 operand = llvm::dwarf::getAttributeEncoding(keyword);
159 <<
"encountered unknown attribute encoding \"" << keyword
171 <<
"expected integer operand";
175 args.push_back(operand);
184 ArrayRef<uint64_t> args) {
186 llvm::interleaveComma(args, printer, [&](uint64_t operand) {
187 if (i > 0 && opcode == llvm::dwarf::DW_OP_LLVM_convert) {
188 if (
const StringRef keyword =
189 llvm::dwarf::AttributeEncodingString(operand);
205 DIRecursiveTypeAttrInterface
208 getContext(), recId, getIsRecSelf(), getTag(), getName(), getFile(),
209 getLine(), getScope(), getBaseType(), getFlags(), getSizeInBits(),
210 getAlignInBits(), getElements(), getDataLocation(), getRank(),
211 getAllocated(), getAssociated());
214 DIRecursiveTypeAttrInterface
217 0, {}, {}, 0, {}, {}, DIFlags(), 0, 0, {}, {},
225 DIRecursiveTypeAttrInterface DISubprogramAttr::withRecId(
DistinctAttr recId) {
227 getCompileUnit(), getScope(), getName(),
228 getLinkageName(), getFile(), getLine(),
229 getScopeLine(), getSubprogramFlags(),
getType(),
230 getRetainedNodes(), getAnnotations());
233 DIRecursiveTypeAttrInterface DISubprogramAttr::getRecSelf(
DistinctAttr recId) {
235 {}, {}, {}, {}, {}, 0, 0, {}, {}, {}, {}, {});
244 IntegerType widthType;
249 unsigned bitWidth = widthType.getWidth();
250 APInt lower(bitWidth, 0);
251 APInt upper(bitWidth, 0);
257 lower = lower.sextOrTrunc(bitWidth);
259 upper = upper.sextOrTrunc(bitWidth);
265 printer <<
"<i" << getLower().getBitWidth() <<
", " << getLower() <<
", "
266 << getUpper() <<
">";
271 APInt lower, APInt upper) {
272 if (lower.getBitWidth() != upper.getBitWidth())
274 <<
"expected lower and upper to have matching bitwidths but got "
275 << lower.getBitWidth() <<
" vs. " << upper.getBitWidth();
286 llvm::map_to_vector(features, [&](StringRef feature) {
292 StringRef targetFeatures) {
293 SmallVector<StringRef> features;
294 targetFeatures.split(features,
',', -1,
296 return get(context, features);
302 for (StringAttr featureAttr : features) {
303 if (!featureAttr || featureAttr.empty())
304 return emitError() <<
"target features can not be null or empty";
305 auto feature = featureAttr.strref();
306 if (feature[0] !=
'+' && feature[0] !=
'-')
307 return emitError() <<
"target features must start with '+' or '-'";
308 if (feature.contains(
','))
309 return emitError() <<
"target features can not contain ','";
318 return llvm::is_contained(getFeatures(), feature);
324 return llvm::is_contained(getFeatures(), feature);
327 std::string TargetFeaturesAttr::getFeaturesString()
const {
328 std::string featuresString;
329 llvm::raw_string_ostream ss(featuresString);
331 getFeatures(), ss, [&](
auto &feature) { ss << feature.strref(); },
",");
332 return featuresString;
335 TargetFeaturesAttr TargetFeaturesAttr::featuresAt(
Operation *op) {
339 return parentFunction.getOperation()->getAttrOfType<TargetFeaturesAttr>(
static MLIRContext * getContext(OpFoldResult val)
static LogicalResult parseExpressionArg(AsmParser &parser, uint64_t opcode, SmallVector< uint64_t > &args)
Parses DWARF expression arguments with respect to the DWARF operation opcode.
static void printExpressionArg(AsmPrinter &printer, uint64_t opcode, ArrayRef< uint64_t > args)
Prints DWARF expression arguments with respect to the specific DWARF operation.
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,...