MLIR 24.0.0git
LLVMConstantOps.cpp
Go to the documentation of this file.
1//===- LLVMConstantOps.cpp - LLVM dialect constant ops --------------------===//
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 file defines the ops of op partition `Constant` of the LLVM dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
16#include "mlir/IR/Builders.h"
18#include "llvm/ADT/APFloat.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/STLExtras.h"
21
22using namespace mlir;
23using namespace mlir::LLVM;
24
25//===----------------------------------------------------------------------===//
26// LLVM::AddressOfOp.
27//===----------------------------------------------------------------------===//
28
29GlobalOp AddressOfOp::getGlobal(SymbolTableCollection &symbolTable) {
30 return dyn_cast_or_null<GlobalOp>(
31 symbolTable.lookupSymbolIn(parentLLVMModule(*this), getGlobalNameAttr()));
32}
33
34LLVMFuncOp AddressOfOp::getFunction(SymbolTableCollection &symbolTable) {
35 return dyn_cast_or_null<LLVMFuncOp>(
36 symbolTable.lookupSymbolIn(parentLLVMModule(*this), getGlobalNameAttr()));
37}
38
39AliasOp AddressOfOp::getAlias(SymbolTableCollection &symbolTable) {
40 return dyn_cast_or_null<AliasOp>(
41 symbolTable.lookupSymbolIn(parentLLVMModule(*this), getGlobalNameAttr()));
42}
43
44IFuncOp AddressOfOp::getIFunc(SymbolTableCollection &symbolTable) {
45 return dyn_cast_or_null<IFuncOp>(
46 symbolTable.lookupSymbolIn(parentLLVMModule(*this), getGlobalNameAttr()));
47}
48
49LogicalResult
50AddressOfOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
51 Operation *symbol =
52 symbolTable.lookupSymbolIn(parentLLVMModule(*this), getGlobalNameAttr());
53
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);
58
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'");
62
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");
68
69 return success();
70}
71
72// AddressOfOp constant-folds to the global symbol name.
73OpFoldResult LLVM::AddressOfOp::fold(FoldAdaptor) {
74 return getGlobalNameAttr();
75}
76
77//===----------------------------------------------------------------------===//
78// LLVM::DSOLocalEquivalentOp
79//===----------------------------------------------------------------------===//
80
81LLVMFuncOp
82DSOLocalEquivalentOp::getFunction(SymbolTableCollection &symbolTable) {
83 return dyn_cast_or_null<LLVMFuncOp>(symbolTable.lookupSymbolIn(
84 parentLLVMModule(*this), getFunctionNameAttr()));
85}
86
87AliasOp DSOLocalEquivalentOp::getAlias(SymbolTableCollection &symbolTable) {
88 return dyn_cast_or_null<AliasOp>(symbolTable.lookupSymbolIn(
89 parentLLVMModule(*this), getFunctionNameAttr()));
90}
91
92LogicalResult
93DSOLocalEquivalentOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
94 Operation *symbol = symbolTable.lookupSymbolIn(parentLLVMModule(*this),
95 getFunctionNameAttr());
96 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
97 auto alias = dyn_cast_or_null<AliasOp>(symbol);
98
99 if (!function && !alias)
100 return emitOpError(
101 "must reference a global defined by 'llvm.func' or 'llvm.mlir.alias'");
102
103 if (alias) {
104 if (alias.getInitializer()
105 .walk([&](AddressOfOp addrOp) {
106 if (addrOp.getGlobal(symbolTable))
107 return WalkResult::interrupt();
108 return WalkResult::advance();
109 })
110 .wasInterrupted())
111 return emitOpError("must reference an alias to a function");
112 }
113
114 if ((function && function.getLinkage() == LLVM::Linkage::ExternWeak) ||
115 (alias && alias.getLinkage() == LLVM::Linkage::ExternWeak))
116 return emitOpError(
117 "target function with 'extern_weak' linkage not allowed");
118
119 return success();
120}
121
122/// Fold a dso_local_equivalent operation to a dedicated dso_local_equivalent
123/// attribute.
124OpFoldResult DSOLocalEquivalentOp::fold(FoldAdaptor) {
125 return DSOLocalEquivalentAttr::get(getContext(), getFunctionNameAttr());
126}
127
128//===----------------------------------------------------------------------===//
129// UndefOp.
130//===----------------------------------------------------------------------===//
131
132/// Fold an undef operation to a dedicated undef attribute.
133OpFoldResult LLVM::UndefOp::fold(FoldAdaptor) {
134 return LLVM::UndefAttr::get(getContext());
135}
136
137//===----------------------------------------------------------------------===//
138// PoisonOp.
139//===----------------------------------------------------------------------===//
140
141/// Fold a poison operation to a dedicated poison attribute.
142OpFoldResult LLVM::PoisonOp::fold(FoldAdaptor) {
143 return LLVM::PoisonAttr::get(getContext());
144}
145
146//===----------------------------------------------------------------------===//
147// MetadataAsValueOp.
148//===----------------------------------------------------------------------===//
149
150/// Fold a metadata-as-value operation to its wrapped metadata attribute.
151OpFoldResult LLVM::MetadataAsValueOp::fold(FoldAdaptor) {
152 return getMetadataAttr();
153}
154
155//===----------------------------------------------------------------------===//
156// ZeroOp.
157//===----------------------------------------------------------------------===//
158
159LogicalResult LLVM::ZeroOp::verify() {
160 if (auto targetExtType = dyn_cast<LLVMTargetExtType>(getType()))
161 if (!targetExtType.hasProperty(LLVM::LLVMTargetExtType::HasZeroInit))
162 return emitOpError()
163 << "target extension type does not support zero-initializer";
164
165 return success();
166}
167
168/// Fold a zero operation to a builtin zero attribute when possible and fall
169/// back to a dedicated zero attribute.
170OpFoldResult LLVM::ZeroOp::fold(FoldAdaptor) {
172 if (result)
173 return result;
174 return LLVM::ZeroAttr::get(getContext());
175}
176
177//===----------------------------------------------------------------------===//
178// ConstantOp.
179//===----------------------------------------------------------------------===//
180
181/// Compute the total number of elements in the given type, also taking into
182/// account nested types. Supported types are `VectorType` and `LLVMArrayType`.
183/// Everything else is treated as a scalar.
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());
189 }
190 if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
191 return arrayType.getNumElements() *
192 getNumElements(arrayType.getElementType());
193 return 1;
194}
195
196/// Determine the element type of `type`. Supported types are `VectorType`,
197/// `TensorType`, and `LLVMArrayType`. Everything else is treated as a scalar.
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();
205 return type;
206}
207
208/// Check if the given type is a scalable vector type or a vector/array type
209/// that contains a nested scalable vector type.
211 if (auto vecType = dyn_cast<VectorType>(t)) {
212 if (vecType.isScalable())
213 return true;
214 return hasScalableVectorType(vecType.getElementType());
215 }
216 if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
217 return hasScalableVectorType(arrayType.getElementType());
218 return false;
219}
220
221/// Verifies the constant array represented by `arrayAttr` matches the provided
222/// `arrayType`.
223static LogicalResult verifyStructArrayConstant(LLVM::ConstantOp op,
224 LLVM::LLVMArrayType arrayType,
225 ArrayAttr arrayAttr, int dim) {
226 if (arrayType.getNumElements() != arrayAttr.size())
227 return op.emitOpError()
228 << "array attribute size does not match array type size in "
229 "dimension "
230 << dim << ": " << arrayAttr.size() << " vs. "
231 << arrayType.getNumElements();
232
233 llvm::DenseSet<Attribute> elementsVerified;
234
235 // Recursively verify sub-dimensions for multidimensional arrays.
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))
241 continue;
242 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
243 if (!subArrayAttr)
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";
248 if (failed(verifyStructArrayConstant(op, subArrayType, subArrayAttr,
249 dim + 1)))
250 return failure();
251 }
252 return success();
253 }
254
255 // Forbid usages of ArrayAttr for simple array types that should use
256 // DenseElementsAttr instead. Note that there would be a use case for such
257 // array types when one element value is obtained via a ptr-to-int conversion
258 // from a symbol and cannot be represented in a DenseElementsAttr, but no MLIR
259 // user needs this so far, and it seems better to avoid people misusing the
260 // ArrayAttr for simple types.
261 Type elementType = arrayType.getElementType();
262 if (isa<LLVM::LLVMPointerType>(elementType)) {
263 for (auto [idx, elementAttr] : llvm::enumerate(arrayAttr)) {
264 if (isa<FlatSymbolRefAttr, LLVM::ZeroAttr, LLVM::UndefAttr,
265 LLVM::PoisonAttr>(elementAttr))
266 continue;
267 return op.emitOpError()
268 << "pointer array element at index " << idx
269 << " must be a flat symbol reference, zero, undef, or poison";
270 }
271 return success();
272 }
273 auto structType = dyn_cast<LLVM::LLVMStructType>(elementType);
274 if (!structType)
275 return op.emitOpError() << "for array with an array attribute must have a "
276 "struct element type";
277
278 // Shallow verification that leaf attributes are appropriate as struct initial
279 // value.
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))
284 continue;
285 auto subArrayAttr = dyn_cast<ArrayAttr>(elementAttr);
286 if (!subArrayAttr)
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;
295 }
296 }
297
298 return success();
299}
300
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";
309 }
310 return success();
311 }
312 if (auto structType = dyn_cast<LLVMStructType>(getType())) {
313 auto arrayAttr = dyn_cast<ArrayAttr>(getValue());
314 if (!arrayAttr)
315 return emitOpError() << "expected array attribute for struct type";
316
317 ArrayRef<Type> elementTypes = structType.getBody();
318 if (arrayAttr.size() != elementTypes.size()) {
319 return emitOpError() << "expected array attribute of size "
320 << elementTypes.size();
321 }
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";
326 }
327 if (!isa<FloatAttr, IntegerAttr>(attr)) {
328 return emitOpError() << "expected element of array attribute to be "
329 "floating point or integer";
330 }
331 if (cast<TypedAttr>(attr).getType() != type)
332 return emitOpError()
333 << "struct element at index " << i << " is of wrong type";
334 }
335
336 return success();
337 }
338 if (auto targetExtType = dyn_cast<LLVMTargetExtType>(getType()))
339 return emitOpError() << "does not support target extension type.";
340
341 // Check that an attribute whose element type has floating point semantics
342 // `attributeFloatSemantics` is compatible with a type whose element type
343 // is `constantElementType`.
344 //
345 // Requirement is that either
346 // 1) They have identical floating point types.
347 // 2) `constantElementType` is an integer type of the same width as the float
348 // attribute. This is to support builtin MLIR float types without LLVM
349 // equivalents, see comments in getLLVMConstant for more details.
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) {
355 return emitOpError()
356 << "attribute and type have different float semantics";
357 }
358 return success();
359 }
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;
364
365 return success();
366 }
367 return success();
368 };
369
370 // Check that an integer attribute whose element type is `attributeIntType`
371 // is compatible with a type whose element type is `constantElementType`.
372 //
373 // Contrary to floats, integers must match exactly. An integer attribute
374 // carries no information that the corresponding LLVM type cannot represent,
375 // so any difference in width, signedness, or in `index` versus a fixed-width
376 // integer indicates a malformed constant. Note that `index` never reaches
377 // this check as a constant type, since it is not LLVM dialect-compatible.
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;
385 return success();
386 };
387
388 // Verification of IntegerAttr, FloatAttr, ElementsAttr, ArrayAttr.
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())) {
396 // Check that the element type of the attribute is compatible with the
397 // element type of the constant. Shared by the scalable and the fixed-size
398 // paths, since element types must agree either way.
399 auto verifyElementTypes = [&](ElementsAttr attr) -> LogicalResult {
400 Type attrElmType = LLVM::getConstantElementType(attr.getType());
401 Type resultElmType = LLVM::getConstantElementType(getType());
402 if (auto floatType = dyn_cast<FloatType>(attrElmType))
403 return verifyFloatSemantics(floatType.getFloatSemantics(),
404 resultElmType);
405
406 if (isa<IntegerType, IndexType>(attrElmType)) {
407 if (!isa<IntegerType>(resultElmType))
408 return emitOpError(
409 "expected integer element type for integer elements attribute");
410 return verifyIntegerSemantics(attrElmType, resultElmType,
411 "element type");
412 }
413 return success();
414 };
415
417 // The exact number of elements of a scalable vector is unknown, so we
418 // allow only splat attributes.
419 auto splatElementsAttr = dyn_cast<SplatElementsAttr>(getValue());
420 if (!splatElementsAttr)
421 return emitOpError()
422 << "scalable vector type requires a splat attribute";
423 return verifyElementTypes(splatElementsAttr);
424 }
425 if (!isa<VectorType, LLVM::LLVMArrayType>(getType()))
426 return emitOpError() << "expected vector or array type";
427
428 // The number of elements of the attribute and the type must match.
429 int64_t attrNumElements = elementsAttr.getNumElements();
430 if (getNumElements(getType()) != attrNumElements) {
431 return emitOpError()
432 << "type and attribute have a different number of elements: "
433 << getNumElements(getType()) << " vs. " << attrNumElements;
434 }
435
436 return verifyElementTypes(elementsAttr);
437 } else if (auto arrayAttr = dyn_cast<ArrayAttr>(getValue())) {
438
439 // The case where the constant is LLVMStructType has already been handled.
440 auto arrayType = dyn_cast<LLVM::LLVMArrayType>(getType());
441 if (!arrayType)
442 return emitOpError()
443 << "expected array or struct type for array attribute";
444
445 // When the attribute is an ArrayAttr, check that its nesting matches the
446 // corresponding ArrayType or VectorType nesting.
447 return verifyStructArrayConstant(*this, arrayType, arrayAttr, /*dim=*/0);
448 } else {
449 return emitOpError()
450 << "only supports integer, float, string or elements attributes";
451 }
452}
453
454bool LLVM::ConstantOp::isBuildableWith(Attribute value, Type type) {
455 // The value's type must be the same as the provided type.
456 auto typedAttr = dyn_cast<TypedAttr>(value);
457 if (!typedAttr || typedAttr.getType() != type || !isCompatibleType(type))
458 return false;
459 // The value's type must be an LLVM compatible type.
460 if (!isCompatibleType(type))
461 return false;
462 // TODO: Add support for additional attributes kinds once needed.
463 return isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
464}
465
466ConstantOp LLVM::ConstantOp::materialize(OpBuilder &builder, Attribute value,
467 Type type, Location loc) {
468 if (isBuildableWith(value, type))
469 return LLVM::ConstantOp::create(builder, loc, cast<TypedAttr>(value));
470 return nullptr;
471}
472
473// Constant op constant-folds to its value.
474OpFoldResult LLVM::ConstantOp::fold(FoldAdaptor) { return getValue(); }
475
476//===----------------------------------------------------------------------===//
477// BlockAddressOp
478//===----------------------------------------------------------------------===//
479
480LogicalResult
481BlockAddressOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
482 Operation *symbol = symbolTable.lookupSymbolIn(parentLLVMModule(*this),
483 getBlockAddr().getFunction());
484 auto function = dyn_cast_or_null<LLVMFuncOp>(symbol);
485
486 if (!function)
487 return emitOpError("must reference a function defined by 'llvm.func'");
488
489 return success();
490}
491
492LLVMFuncOp BlockAddressOp::getFunction(SymbolTableCollection &symbolTable) {
493 return dyn_cast_or_null<LLVMFuncOp>(symbolTable.lookupSymbolIn(
494 parentLLVMModule(*this), getBlockAddr().getFunction()));
495}
496
497BlockTagOp BlockAddressOp::getBlockTagOp() {
499 parentLLVMModule(*this), getBlockAddr().getFunction());
500 if (!sym)
501 return nullptr;
502 auto funcOp = dyn_cast<LLVMFuncOp>(sym);
503 if (!funcOp)
504 return nullptr;
505 BlockTagOp blockTagOp = nullptr;
506 funcOp.walk([&](LLVM::BlockTagOp labelOp) {
507 if (labelOp.getTag() == getBlockAddr().getTag()) {
508 blockTagOp = labelOp;
509 return WalkResult::interrupt();
510 }
511 return WalkResult::advance();
512 });
513 return blockTagOp;
514}
515
516LogicalResult BlockAddressOp::verify() {
517 if (!getBlockTagOp())
518 return emitOpError(
519 "expects an existing block label target in the referenced function");
520
521 return success();
522}
523
524/// Fold a blockaddress operation to a dedicated blockaddress
525/// attribute.
526OpFoldResult BlockAddressOp::fold(FoldAdaptor) { return getBlockAddr(); }
527
528#define GET_OP_CLASSES
529#include "mlir/Dialect/LLVMIR/LLVMConstantOps.cpp.inc"
return success()
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.
ArrayAttr()
b getContext())
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
TypedAttr getZeroAttr(Type type)
Definition Builders.cpp:333
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...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:210
This class represents a single result from folding an operation.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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...
Definition Types.h:74
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
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.
Definition Utils.cpp:311