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
13#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
14
15#include "mlir/Dialect/LLVMIR/LLVMModuleOps.h"
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/DialectImplementation.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 Val: symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this), name: getGlobalNameAttr()));
32}
33
34LLVMFuncOp AddressOfOp::getFunction(SymbolTableCollection &symbolTable) {
35 return dyn_cast_or_null<LLVMFuncOp>(
36 Val: symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this), name: getGlobalNameAttr()));
37}
38
39AliasOp AddressOfOp::getAlias(SymbolTableCollection &symbolTable) {
40 return dyn_cast_or_null<AliasOp>(
41 Val: symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this), name: getGlobalNameAttr()));
42}
43
44IFuncOp AddressOfOp::getIFunc(SymbolTableCollection &symbolTable) {
45 return dyn_cast_or_null<IFuncOp>(
46 Val: symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this), name: getGlobalNameAttr()));
47}
48
49LogicalResult
50AddressOfOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
51 Operation *symbol =
52 symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this), name: getGlobalNameAttr());
53
54 auto global = dyn_cast_or_null<GlobalOp>(Val: symbol);
55 auto function = dyn_cast_or_null<LLVMFuncOp>(Val: symbol);
56 auto alias = dyn_cast_or_null<AliasOp>(Val: symbol);
57 auto ifunc = dyn_cast_or_null<IFuncOp>(Val: symbol);
58
59 if (!global && !function && !alias && !ifunc)
60 return emitOpError(message: "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(message: "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>(Val: symbolTable.lookupSymbolIn(
84 symbolTableOp: parentLLVMModule(op: *this), name: getFunctionNameAttr()));
85}
86
87AliasOp DSOLocalEquivalentOp::getAlias(SymbolTableCollection &symbolTable) {
88 return dyn_cast_or_null<AliasOp>(Val: symbolTable.lookupSymbolIn(
89 symbolTableOp: parentLLVMModule(op: *this), name: getFunctionNameAttr()));
90}
91
92LogicalResult
93DSOLocalEquivalentOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
94 Operation *symbol = symbolTable.lookupSymbolIn(symbolTableOp: parentLLVMModule(op: *this),
95 name: getFunctionNameAttr());
96 auto function = dyn_cast_or_null<LLVMFuncOp>(Val: symbol);
97 auto alias = dyn_cast_or_null<AliasOp>(Val: symbol);
98
99 if (!function && !alias)
100 return emitOpError(
101 message: "must reference a global defined by 'llvm.func' or 'llvm.mlir.alias'");
102
103 if (alias) {
104 if (alias.getInitializer()
105 .walk(callback: [&](AddressOfOp addrOp) {
106 if (addrOp.getGlobal(symbolTable))
107 return WalkResult::interrupt();
108 return WalkResult::advance();
109 })
110 .wasInterrupted())
111 return emitOpError(message: "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 message: "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(context: getContext(), sym: 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(ctx: 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(ctx: 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>(Val: getType()))
161 if (!targetExtType.hasProperty(Prop: 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) {
171 OpFoldResult result = Builder(getContext()).getZeroAttr(type: getType());
172 if (result)
173 return result;
174 return LLVM::ZeroAttr::get(ctx: 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.
184static int64_t getNumElements(Type t) {
185 if (auto vecType = dyn_cast<VectorType>(Val&: t)) {
186 assert(!vecType.isScalable() &&
187 "number of elements of a scalable vector type is unknown");
188 return vecType.getNumElements() * getNumElements(t: vecType.getElementType());
189 }
190 if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(Val&: t))
191 return arrayType.getNumElements() *
192 getNumElements(t: 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.
198Type LLVM::getConstantElementType(Type type) {
199 while (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(Val&: type))
200 type = arrayType.getElementType();
201 if (auto vecType = dyn_cast<VectorType>(Val&: type))
202 return vecType.getElementType();
203 if (auto tenType = dyn_cast<TensorType>(Val&: 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.
210static bool hasScalableVectorType(Type t) {
211 if (auto vecType = dyn_cast<VectorType>(Val&: t)) {
212 if (vecType.isScalable())
213 return true;
214 return hasScalableVectorType(t: vecType.getElementType());
215 }
216 if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(Val&: t))
217 return hasScalableVectorType(t: 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>(Val: arrayType.getElementType())) {
238 for (auto [idx, elementAttr] : llvm::enumerate(First&: arrayAttr))
239 if (elementsVerified.insert(V: elementAttr).second) {
240 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(Val: elementAttr))
241 continue;
242 auto subArrayAttr = dyn_cast<ArrayAttr>(Val: 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(Result: verifyStructArrayConstant(op, arrayType: subArrayType, arrayAttr: subArrayAttr,
249 dim: 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>(Val: elementType)) {
263 for (auto [idx, elementAttr] : llvm::enumerate(First&: arrayAttr)) {
264 if (isa<FlatSymbolRefAttr, LLVM::ZeroAttr, LLVM::UndefAttr,
265 LLVM::PoisonAttr>(Val: 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>(Val&: 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(First&: arrayAttr)) {
282 if (elementsVerified.insert(V: elementAttr).second) {
283 if (isa<LLVM::ZeroAttr, LLVM::UndefAttr>(Val: elementAttr))
284 continue;
285 auto subArrayAttr = dyn_cast<ArrayAttr>(Val: 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>(Val: getValue())) {
303 auto arrayType = llvm::dyn_cast<LLVMArrayType>(Val: getType());
304 if (!arrayType || arrayType.getNumElements() != sAttr.getValue().size() ||
305 !arrayType.getElementType().isInteger(width: 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>(Val: getType())) {
313 auto arrayAttr = dyn_cast<ArrayAttr>(Val: 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(First&: arrayAttr, Rest&: 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>(Val: attr)) {
328 return emitOpError() << "expected element of array attribute to be "
329 "floating point or integer";
330 }
331 if (cast<TypedAttr>(Val: 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>(Val: 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>(Val&: 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(Sem: attributeFloatSemantics);
361 if (isa<IntegerType>(Val: constantElementType)) {
362 if (!constantElementType.isInteger(width: 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>(Val: getValue())) {
390 if (!llvm::isa<IntegerType>(Val: getType()))
391 return emitOpError() << "expected integer type";
392 return verifyIntegerSemantics(intAttr.getType(), getType(), "type");
393 } else if (auto floatAttr = dyn_cast<FloatAttr>(Val: getValue())) {
394 return verifyFloatSemantics(floatAttr.getValue().getSemantics(), getType());
395 } else if (auto elementsAttr = dyn_cast<ElementsAttr>(Val: 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(type: attr.getType());
401 Type resultElmType = LLVM::getConstantElementType(type: getType());
402 if (auto floatType = dyn_cast<FloatType>(Val&: attrElmType))
403 return verifyFloatSemantics(floatType.getFloatSemantics(),
404 resultElmType);
405
406 if (isa<IntegerType, IndexType>(Val: attrElmType)) {
407 if (!isa<IntegerType>(Val: resultElmType))
408 return emitOpError(
409 message: "expected integer element type for integer elements attribute");
410 return verifyIntegerSemantics(attrElmType, resultElmType,
411 "element type");
412 }
413 return success();
414 };
415
416 if (hasScalableVectorType(t: getType())) {
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>(Val: 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>(Val: 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(t: getType()) != attrNumElements) {
431 return emitOpError()
432 << "type and attribute have a different number of elements: "
433 << getNumElements(t: getType()) << " vs. " << attrNumElements;
434 }
435
436 return verifyElementTypes(elementsAttr);
437 } else if (auto arrayAttr = dyn_cast<ArrayAttr>(Val: getValue())) {
438
439 // The case where the constant is LLVMStructType has already been handled.
440 auto arrayType = dyn_cast<LLVM::LLVMArrayType>(Val: 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(op: *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>(Val&: 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>(Val: 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, location: loc, value: cast<TypedAttr>(Val&: 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(symbolTableOp: parentLLVMModule(op: *this),
483 name: getBlockAddr().getFunction());
484 auto function = dyn_cast_or_null<LLVMFuncOp>(Val: symbol);
485
486 if (!function)
487 return emitOpError(message: "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>(Val: symbolTable.lookupSymbolIn(
494 symbolTableOp: parentLLVMModule(op: *this), name: getBlockAddr().getFunction()));
495}
496
497BlockTagOp BlockAddressOp::getBlockTagOp() {
498 Operation *sym = mlir::SymbolTable::lookupNearestSymbolFrom(
499 from: parentLLVMModule(op: *this), symbol: getBlockAddr().getFunction());
500 if (!sym)
501 return nullptr;
502 auto funcOp = dyn_cast<LLVMFuncOp>(Val: sym);
503 if (!funcOp)
504 return nullptr;
505 BlockTagOp blockTagOp = nullptr;
506 funcOp.walk(callback: [&](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 message: "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"
530