18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/STLExtras.h"
30 return dyn_cast_or_null<GlobalOp>(
35 return dyn_cast_or_null<LLVMFuncOp>(
40 return dyn_cast_or_null<AliasOp>(
45 return dyn_cast_or_null<IFuncOp>(
54 auto global = dyn_cast_or_null<GlobalOp>(symbol);
55 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
56 auto alias = dyn_cast_or_null<AliasOp>(symbol);
57 auto ifunc = dyn_cast_or_null<IFuncOp>(symbol);
59 if (!global && !function && !alias && !ifunc)
60 return emitOpError(
"must reference a global defined by 'llvm.mlir.global', "
61 "'llvm.mlir.alias' or 'llvm.func' or 'llvm.mlir.ifunc'");
63 LLVMPointerType type =
getType();
64 if ((global && global.getAddrSpace() != type.getAddressSpace()) ||
65 (alias && alias.getAddrSpace() != type.getAddressSpace()))
66 return emitOpError(
"pointer address space must match address space of the "
67 "referenced global or alias");
74 return getGlobalNameAttr();
95 getFunctionNameAttr());
96 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
97 auto alias = dyn_cast_or_null<AliasOp>(symbol);
99 if (!function && !alias)
101 "must reference a global defined by 'llvm.func' or 'llvm.mlir.alias'");
104 if (alias.getInitializer()
105 .walk([&](AddressOfOp addrOp) {
106 if (addrOp.getGlobal(symbolTable))
107 return WalkResult::interrupt();
108 return WalkResult::advance();
111 return emitOpError(
"must reference an alias to a function");
114 if ((function && function.getLinkage() == LLVM::Linkage::ExternWeak) ||
115 (alias && alias.getLinkage() == LLVM::Linkage::ExternWeak))
117 "target function with 'extern_weak' linkage not allowed");
125 return DSOLocalEquivalentAttr::get(
getContext(), getFunctionNameAttr());
151OpFoldResult LLVM::MetadataAsValueOp::fold(FoldAdaptor) {
152 return getMetadataAttr();
159LogicalResult LLVM::ZeroOp::verify() {
160 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(
getType()))
161 if (!targetExtType.hasProperty(LLVM::LLVMTargetExtType::HasZeroInit))
163 <<
"target extension type does not support zero-initializer";
185 if (
auto vecType = dyn_cast<VectorType>(t)) {
186 assert(!vecType.isScalable() &&
187 "number of elements of a scalable vector type is unknown");
188 return vecType.getNumElements() *
getNumElements(vecType.getElementType());
190 if (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
191 return arrayType.getNumElements() *
199 while (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(type))
200 type = arrayType.getElementType();
201 if (
auto vecType = dyn_cast<VectorType>(type))
202 return vecType.getElementType();
203 if (
auto tenType = dyn_cast<TensorType>(type))
204 return tenType.getElementType();
211 if (
auto vecType = dyn_cast<VectorType>(t)) {
212 if (vecType.isScalable())
216 if (
auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
224 LLVM::LLVMArrayType arrayType,
226 if (arrayType.getNumElements() != arrayAttr.size())
227 return op.emitOpError()
228 <<
"array attribute size does not match array type size in "
230 << dim <<
": " << arrayAttr.size() <<
" vs. "
231 << arrayType.getNumElements();
236 if (
auto subArrayType =
237 dyn_cast<LLVM::LLVMArrayType>(arrayType.getElementType())) {
238 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr))
239 if (elementsVerified.insert(elementAttr).second) {
240 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(elementAttr))
242 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
244 return op.emitOpError()
245 <<
"nested attribute for sub-array in dimension " << dim
246 <<
" at index " << idx
247 <<
" must be a zero, or undef, or array attribute";
261 Type elementType = arrayType.getElementType();
262 if (isa<LLVM::LLVMPointerType>(elementType)) {
263 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr)) {
265 LLVM::PoisonAttr>(elementAttr))
267 return op.emitOpError()
268 <<
"pointer array element at index " << idx
269 <<
" must be a flat symbol reference, zero, undef, or poison";
273 auto structType = dyn_cast<LLVM::LLVMStructType>(elementType);
275 return op.emitOpError() <<
"for array with an array attribute must have a "
276 "struct element type";
280 size_t numStructElements = structType.getBody().size();
281 for (
auto [idx, elementAttr] : llvm::enumerate(arrayAttr)) {
282 if (elementsVerified.insert(elementAttr).second) {
283 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(elementAttr))
285 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
287 return op.emitOpError()
288 <<
"nested attribute for struct element at index " << idx
289 <<
" must be a zero, or undef, or array attribute";
290 if (subArrayAttr.size() != numStructElements)
291 return op.emitOpError()
292 <<
"nested array attribute size for struct element at index "
293 << idx <<
" must match struct size: " << subArrayAttr.size()
294 <<
" vs. " << numStructElements;
301LogicalResult LLVM::ConstantOp::verify() {
302 if (StringAttr sAttr = llvm::dyn_cast<StringAttr>(getValue())) {
303 auto arrayType = llvm::dyn_cast<LLVMArrayType>(
getType());
304 if (!arrayType || arrayType.getNumElements() != sAttr.getValue().size() ||
305 !arrayType.getElementType().isInteger(8)) {
306 return emitOpError() <<
"expected array type of "
307 << sAttr.getValue().size()
308 <<
" i8 elements for the string constant";
312 if (
auto structType = dyn_cast<LLVMStructType>(
getType())) {
313 auto arrayAttr = dyn_cast<ArrayAttr>(getValue());
315 return emitOpError() <<
"expected array attribute for struct type";
318 if (arrayAttr.size() != elementTypes.size()) {
319 return emitOpError() <<
"expected array attribute of size "
320 << elementTypes.size();
322 for (
auto [i, attr, type] : llvm::enumerate(arrayAttr, elementTypes)) {
323 if (!type.isSignlessIntOrIndexOrFloat()) {
324 return emitOpError() <<
"expected struct element types to be floating "
325 "point type or integer type";
327 if (!isa<FloatAttr, IntegerAttr>(attr)) {
328 return emitOpError() <<
"expected element of array attribute to be "
329 "floating point or integer";
331 if (cast<TypedAttr>(attr).
getType() != type)
333 <<
"struct element at index " << i <<
" is of wrong type";
338 if (
auto targetExtType = dyn_cast<LLVMTargetExtType>(
getType()))
339 return emitOpError() <<
"does not support target extension type.";
350 auto verifyFloatSemantics =
351 [
this](
const llvm::fltSemantics &attributeFloatSemantics,
352 Type constantElementType) -> LogicalResult {
353 if (
auto floatType = dyn_cast<FloatType>(constantElementType)) {
354 if (&floatType.getFloatSemantics() != &attributeFloatSemantics) {
356 <<
"attribute and type have different float semantics";
360 unsigned floatWidth = APFloat::getSizeInBits(attributeFloatSemantics);
361 if (isa<IntegerType>(constantElementType)) {
362 if (!constantElementType.isInteger(floatWidth))
363 return emitOpError() <<
"expected integer type of width " << floatWidth;
378 auto verifyIntegerSemantics = [
this](
Type attributeIntType,
379 Type constantElementType,
380 StringRef description) -> LogicalResult {
381 if (attributeIntType != constantElementType)
382 return emitOpError() <<
"attribute and type have different integer "
383 << description <<
"s: " << attributeIntType
384 <<
" vs. " << constantElementType;
389 if (
auto intAttr = dyn_cast<IntegerAttr>(getValue())) {
390 if (!llvm::isa<IntegerType>(
getType()))
391 return emitOpError() <<
"expected integer type";
392 return verifyIntegerSemantics(intAttr.getType(),
getType(),
"type");
393 }
else if (
auto floatAttr = dyn_cast<FloatAttr>(getValue())) {
394 return verifyFloatSemantics(floatAttr.getValue().getSemantics(),
getType());
395 }
else if (
auto elementsAttr = dyn_cast<ElementsAttr>(getValue())) {
399 auto verifyElementTypes = [&](ElementsAttr attr) -> LogicalResult {
402 if (
auto floatType = dyn_cast<FloatType>(attrElmType))
403 return verifyFloatSemantics(floatType.getFloatSemantics(),
406 if (isa<IntegerType, IndexType>(attrElmType)) {
407 if (!isa<IntegerType>(resultElmType))
409 "expected integer element type for integer elements attribute");
410 return verifyIntegerSemantics(attrElmType, resultElmType,
419 auto splatElementsAttr = dyn_cast<SplatElementsAttr>(getValue());
420 if (!splatElementsAttr)
422 <<
"scalable vector type requires a splat attribute";
423 return verifyElementTypes(splatElementsAttr);
425 if (!isa<VectorType, LLVM::LLVMArrayType>(
getType()))
426 return emitOpError() <<
"expected vector or array type";
429 int64_t attrNumElements = elementsAttr.getNumElements();
432 <<
"type and attribute have a different number of elements: "
436 return verifyElementTypes(elementsAttr);
437 }
else if (
auto arrayAttr = dyn_cast<ArrayAttr>(getValue())) {
440 auto arrayType = dyn_cast<LLVM::LLVMArrayType>(
getType());
443 <<
"expected array or struct type for array attribute";
450 <<
"only supports integer, float, string or elements attributes";
454bool LLVM::ConstantOp::isBuildableWith(
Attribute value,
Type type) {
456 auto typedAttr = dyn_cast<TypedAttr>(value);
463 return isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
468 if (isBuildableWith(value, type))
469 return LLVM::ConstantOp::create(builder, loc, cast<TypedAttr>(value));
474OpFoldResult LLVM::ConstantOp::fold(FoldAdaptor) {
return getValue(); }
483 getBlockAddr().getFunction());
484 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
487 return emitOpError(
"must reference a function defined by 'llvm.func'");
497BlockTagOp BlockAddressOp::getBlockTagOp() {
502 auto funcOp = dyn_cast<LLVMFuncOp>(sym);
505 BlockTagOp blockTagOp =
nullptr;
506 funcOp.walk([&](LLVM::BlockTagOp labelOp) {
507 if (labelOp.getTag() == getBlockAddr().getTag()) {
508 blockTagOp = labelOp;
516LogicalResult BlockAddressOp::verify() {
517 if (!getBlockTagOp())
519 "expects an existing block label target in the referenced function");
526OpFoldResult BlockAddressOp::fold(FoldAdaptor) {
return getBlockAddr(); }
528#define GET_OP_CLASSES
529#include "mlir/Dialect/LLVMIR/LLVMConstantOps.cpp.inc"
static LogicalResult verifyStructArrayConstant(LLVM::ConstantOp op, LLVM::LLVMArrayType arrayType, ArrayAttr arrayAttr, int dim)
Verifies the constant array represented by arrayAttr matches the provided arrayType.
static bool hasScalableVectorType(Type t)
Check if the given type is a scalable vector type or a vector/array type that contains a nested scala...
static int64_t getNumElements(Type t)
Compute the total number of elements in the given type, also taking into account nested types.
Attributes are known-constant values of operations.
This class is a general helper class for creating context-global objects like types,...
TypedAttr getZeroAttr(Type type)
A symbol reference with a reference path containing a single element.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
This class helps build Operations.
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
This class represents a collection of SymbolTables.
virtual Operation * lookupSymbolIn(Operation *symbolTableOp, StringAttr symbol)
Look up a symbol with the specified name within the specified symbol table operation,...
static Operation * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
static WalkResult advance()
static WalkResult interrupt()
Operation * parentLLVMModule(Operation *op)
Lookup parent Module satisfying LLVM conditions on the Module Operation.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Type getConstantElementType(Type type)
Determines the element type of type the way the llvm.mlir.constant verifier does, i....
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.