1 //===- Builders.cpp - Helpers for constructing MLIR Classes ---------------===//
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 #include "mlir/IR/Builders.h"
10 #include "mlir/IR/AffineExpr.h"
11 #include "mlir/IR/AffineMap.h"
12 #include "mlir/IR/Dialect.h"
13 #include "mlir/IR/IntegerSet.h"
14 #include "mlir/IR/Matchers.h"
15 #include "mlir/IR/Module.h"
16 #include "mlir/IR/StandardTypes.h"
17 #include "llvm/Support/raw_ostream.h"
18 using namespace mlir;
19 
20 Builder::Builder(ModuleOp module) : context(module.getContext()) {}
21 
22 Identifier Builder::getIdentifier(StringRef str) {
23   return Identifier::get(str, context);
24 }
25 
26 //===----------------------------------------------------------------------===//
27 // Locations.
28 //===----------------------------------------------------------------------===//
29 
30 Location Builder::getUnknownLoc() { return UnknownLoc::get(context); }
31 
32 Location Builder::getFileLineColLoc(Identifier filename, unsigned line,
33                                     unsigned column) {
34   return FileLineColLoc::get(filename, line, column, context);
35 }
36 
37 Location Builder::getFusedLoc(ArrayRef<Location> locs, Attribute metadata) {
38   return FusedLoc::get(locs, metadata, context);
39 }
40 
41 //===----------------------------------------------------------------------===//
42 // Types.
43 //===----------------------------------------------------------------------===//
44 
45 FloatType Builder::getBF16Type() { return FloatType::getBF16(context); }
46 
47 FloatType Builder::getF16Type() { return FloatType::getF16(context); }
48 
49 FloatType Builder::getF32Type() { return FloatType::getF32(context); }
50 
51 FloatType Builder::getF64Type() { return FloatType::getF64(context); }
52 
53 IndexType Builder::getIndexType() { return IndexType::get(context); }
54 
55 IntegerType Builder::getI1Type() { return IntegerType::get(1, context); }
56 
57 IntegerType Builder::getIntegerType(unsigned width) {
58   return IntegerType::get(width, context);
59 }
60 
61 IntegerType Builder::getIntegerType(unsigned width, bool isSigned) {
62   return IntegerType::get(
63       width, isSigned ? IntegerType::Signed : IntegerType::Unsigned, context);
64 }
65 
66 FunctionType Builder::getFunctionType(ArrayRef<Type> inputs,
67                                       ArrayRef<Type> results) {
68   return FunctionType::get(inputs, results, context);
69 }
70 
71 TupleType Builder::getTupleType(ArrayRef<Type> elementTypes) {
72   return TupleType::get(elementTypes, context);
73 }
74 
75 NoneType Builder::getNoneType() { return NoneType::get(context); }
76 
77 //===----------------------------------------------------------------------===//
78 // Attributes.
79 //===----------------------------------------------------------------------===//
80 
81 NamedAttribute Builder::getNamedAttr(StringRef name, Attribute val) {
82   return NamedAttribute(getIdentifier(name), val);
83 }
84 
85 UnitAttr Builder::getUnitAttr() { return UnitAttr::get(context); }
86 
87 BoolAttr Builder::getBoolAttr(bool value) {
88   return BoolAttr::get(value, context);
89 }
90 
91 DictionaryAttr Builder::getDictionaryAttr(ArrayRef<NamedAttribute> value) {
92   return DictionaryAttr::get(value, context);
93 }
94 
95 IntegerAttr Builder::getIndexAttr(int64_t value) {
96   return IntegerAttr::get(getIndexType(), APInt(64, value));
97 }
98 
99 IntegerAttr Builder::getI64IntegerAttr(int64_t value) {
100   return IntegerAttr::get(getIntegerType(64), APInt(64, value));
101 }
102 
103 DenseIntElementsAttr Builder::getI32VectorAttr(ArrayRef<int32_t> values) {
104   return DenseIntElementsAttr::get(
105       VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(32)),
106       values);
107 }
108 
109 DenseIntElementsAttr Builder::getI64VectorAttr(ArrayRef<int64_t> values) {
110   return DenseIntElementsAttr::get(
111       VectorType::get(static_cast<int64_t>(values.size()), getIntegerType(64)),
112       values);
113 }
114 
115 DenseIntElementsAttr Builder::getI32TensorAttr(ArrayRef<int32_t> values) {
116   return DenseIntElementsAttr::get(
117       RankedTensorType::get(static_cast<int64_t>(values.size()),
118                             getIntegerType(32)),
119       values);
120 }
121 
122 DenseIntElementsAttr Builder::getI64TensorAttr(ArrayRef<int64_t> values) {
123   return DenseIntElementsAttr::get(
124       RankedTensorType::get(static_cast<int64_t>(values.size()),
125                             getIntegerType(64)),
126       values);
127 }
128 
129 IntegerAttr Builder::getI32IntegerAttr(int32_t value) {
130   return IntegerAttr::get(getIntegerType(32), APInt(32, value));
131 }
132 
133 IntegerAttr Builder::getSI32IntegerAttr(int32_t value) {
134   return IntegerAttr::get(getIntegerType(32, /*isSigned=*/true),
135                           APInt(32, value, /*isSigned=*/true));
136 }
137 
138 IntegerAttr Builder::getUI32IntegerAttr(uint32_t value) {
139   return IntegerAttr::get(getIntegerType(32, /*isSigned=*/false),
140                           APInt(32, (uint64_t)value, /*isSigned=*/false));
141 }
142 
143 IntegerAttr Builder::getI16IntegerAttr(int16_t value) {
144   return IntegerAttr::get(getIntegerType(16), APInt(16, value));
145 }
146 
147 IntegerAttr Builder::getI8IntegerAttr(int8_t value) {
148   return IntegerAttr::get(getIntegerType(8), APInt(8, value));
149 }
150 
151 IntegerAttr Builder::getIntegerAttr(Type type, int64_t value) {
152   if (type.isIndex())
153     return IntegerAttr::get(type, APInt(64, value));
154   return IntegerAttr::get(
155       type, APInt(type.getIntOrFloatBitWidth(), value, type.isSignedInteger()));
156 }
157 
158 IntegerAttr Builder::getIntegerAttr(Type type, const APInt &value) {
159   return IntegerAttr::get(type, value);
160 }
161 
162 FloatAttr Builder::getF64FloatAttr(double value) {
163   return FloatAttr::get(getF64Type(), APFloat(value));
164 }
165 
166 FloatAttr Builder::getF32FloatAttr(float value) {
167   return FloatAttr::get(getF32Type(), APFloat(value));
168 }
169 
170 FloatAttr Builder::getF16FloatAttr(float value) {
171   return FloatAttr::get(getF16Type(), value);
172 }
173 
174 FloatAttr Builder::getFloatAttr(Type type, double value) {
175   return FloatAttr::get(type, value);
176 }
177 
178 FloatAttr Builder::getFloatAttr(Type type, const APFloat &value) {
179   return FloatAttr::get(type, value);
180 }
181 
182 StringAttr Builder::getStringAttr(StringRef bytes) {
183   return StringAttr::get(bytes, context);
184 }
185 
186 ArrayAttr Builder::getArrayAttr(ArrayRef<Attribute> value) {
187   return ArrayAttr::get(value, context);
188 }
189 
190 FlatSymbolRefAttr Builder::getSymbolRefAttr(Operation *value) {
191   auto symName =
192       value->getAttrOfType<StringAttr>(SymbolTable::getSymbolAttrName());
193   assert(symName && "value does not have a valid symbol name");
194   return getSymbolRefAttr(symName.getValue());
195 }
196 FlatSymbolRefAttr Builder::getSymbolRefAttr(StringRef value) {
197   return SymbolRefAttr::get(value, getContext());
198 }
199 SymbolRefAttr
200 Builder::getSymbolRefAttr(StringRef value,
201                           ArrayRef<FlatSymbolRefAttr> nestedReferences) {
202   return SymbolRefAttr::get(value, nestedReferences, getContext());
203 }
204 
205 ArrayAttr Builder::getBoolArrayAttr(ArrayRef<bool> values) {
206   auto attrs = llvm::to_vector<8>(llvm::map_range(
207       values, [this](bool v) -> Attribute { return getBoolAttr(v); }));
208   return getArrayAttr(attrs);
209 }
210 
211 ArrayAttr Builder::getI32ArrayAttr(ArrayRef<int32_t> values) {
212   auto attrs = llvm::to_vector<8>(llvm::map_range(
213       values, [this](int32_t v) -> Attribute { return getI32IntegerAttr(v); }));
214   return getArrayAttr(attrs);
215 }
216 ArrayAttr Builder::getI64ArrayAttr(ArrayRef<int64_t> values) {
217   auto attrs = llvm::to_vector<8>(llvm::map_range(
218       values, [this](int64_t v) -> Attribute { return getI64IntegerAttr(v); }));
219   return getArrayAttr(attrs);
220 }
221 
222 ArrayAttr Builder::getIndexArrayAttr(ArrayRef<int64_t> values) {
223   auto attrs = llvm::to_vector<8>(
224       llvm::map_range(values, [this](int64_t v) -> Attribute {
225         return getIntegerAttr(IndexType::get(getContext()), v);
226       }));
227   return getArrayAttr(attrs);
228 }
229 
230 ArrayAttr Builder::getF32ArrayAttr(ArrayRef<float> values) {
231   auto attrs = llvm::to_vector<8>(llvm::map_range(
232       values, [this](float v) -> Attribute { return getF32FloatAttr(v); }));
233   return getArrayAttr(attrs);
234 }
235 
236 ArrayAttr Builder::getF64ArrayAttr(ArrayRef<double> values) {
237   auto attrs = llvm::to_vector<8>(llvm::map_range(
238       values, [this](double v) -> Attribute { return getF64FloatAttr(v); }));
239   return getArrayAttr(attrs);
240 }
241 
242 ArrayAttr Builder::getStrArrayAttr(ArrayRef<StringRef> values) {
243   auto attrs = llvm::to_vector<8>(llvm::map_range(
244       values, [this](StringRef v) -> Attribute { return getStringAttr(v); }));
245   return getArrayAttr(attrs);
246 }
247 
248 ArrayAttr Builder::getAffineMapArrayAttr(ArrayRef<AffineMap> values) {
249   auto attrs = llvm::to_vector<8>(llvm::map_range(
250       values, [](AffineMap v) -> Attribute { return AffineMapAttr::get(v); }));
251   return getArrayAttr(attrs);
252 }
253 
254 Attribute Builder::getZeroAttr(Type type) {
255   switch (type.getKind()) {
256   case StandardTypes::BF16:
257   case StandardTypes::F16:
258   case StandardTypes::F32:
259   case StandardTypes::F64:
260     return getFloatAttr(type, 0.0);
261   case StandardTypes::Integer: {
262     auto width = type.cast<IntegerType>().getWidth();
263     if (width == 1)
264       return getBoolAttr(false);
265     return getIntegerAttr(type, APInt(width, 0));
266   }
267   case StandardTypes::Vector:
268   case StandardTypes::RankedTensor: {
269     auto vtType = type.cast<ShapedType>();
270     auto element = getZeroAttr(vtType.getElementType());
271     if (!element)
272       return {};
273     return DenseElementsAttr::get(vtType, element);
274   }
275   default:
276     break;
277   }
278   return {};
279 }
280 
281 //===----------------------------------------------------------------------===//
282 // Affine Expressions, Affine Maps, and Integer Sets.
283 //===----------------------------------------------------------------------===//
284 
285 AffineExpr Builder::getAffineDimExpr(unsigned position) {
286   return mlir::getAffineDimExpr(position, context);
287 }
288 
289 AffineExpr Builder::getAffineSymbolExpr(unsigned position) {
290   return mlir::getAffineSymbolExpr(position, context);
291 }
292 
293 AffineExpr Builder::getAffineConstantExpr(int64_t constant) {
294   return mlir::getAffineConstantExpr(constant, context);
295 }
296 
297 AffineMap Builder::getEmptyAffineMap() { return AffineMap::get(context); }
298 
299 AffineMap Builder::getConstantAffineMap(int64_t val) {
300   return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/0,
301                         getAffineConstantExpr(val));
302 }
303 
304 AffineMap Builder::getDimIdentityMap() {
305   return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, getAffineDimExpr(0));
306 }
307 
308 AffineMap Builder::getMultiDimIdentityMap(unsigned rank) {
309   SmallVector<AffineExpr, 4> dimExprs;
310   dimExprs.reserve(rank);
311   for (unsigned i = 0; i < rank; ++i)
312     dimExprs.push_back(getAffineDimExpr(i));
313   return AffineMap::get(/*dimCount=*/rank, /*symbolCount=*/0, dimExprs,
314                         context);
315 }
316 
317 AffineMap Builder::getSymbolIdentityMap() {
318   return AffineMap::get(/*dimCount=*/0, /*symbolCount=*/1,
319                         getAffineSymbolExpr(0));
320 }
321 
322 AffineMap Builder::getSingleDimShiftAffineMap(int64_t shift) {
323   // expr = d0 + shift.
324   auto expr = getAffineDimExpr(0) + shift;
325   return AffineMap::get(/*dimCount=*/1, /*symbolCount=*/0, expr);
326 }
327 
328 AffineMap Builder::getShiftedAffineMap(AffineMap map, int64_t shift) {
329   SmallVector<AffineExpr, 4> shiftedResults;
330   shiftedResults.reserve(map.getNumResults());
331   for (auto resultExpr : map.getResults())
332     shiftedResults.push_back(resultExpr + shift);
333   return AffineMap::get(map.getNumDims(), map.getNumSymbols(), shiftedResults,
334                         context);
335 }
336 
337 //===----------------------------------------------------------------------===//
338 // OpBuilder
339 //===----------------------------------------------------------------------===//
340 
341 OpBuilder::Listener::~Listener() {}
342 
343 /// Insert the given operation at the current insertion point and return it.
344 Operation *OpBuilder::insert(Operation *op) {
345   if (block)
346     block->getOperations().insert(insertPoint, op);
347 
348   if (listener)
349     listener->notifyOperationInserted(op);
350   return op;
351 }
352 
353 /// Add new block with 'argTypes' arguments and set the insertion point to the
354 /// end of it. The block is inserted at the provided insertion point of
355 /// 'parent'.
356 Block *OpBuilder::createBlock(Region *parent, Region::iterator insertPt,
357                               TypeRange argTypes) {
358   assert(parent && "expected valid parent region");
359   if (insertPt == Region::iterator())
360     insertPt = parent->end();
361 
362   Block *b = new Block();
363   b->addArguments(argTypes);
364   parent->getBlocks().insert(insertPt, b);
365   setInsertionPointToEnd(b);
366 
367   if (listener)
368     listener->notifyBlockCreated(b);
369   return b;
370 }
371 
372 /// Add new block with 'argTypes' arguments and set the insertion point to the
373 /// end of it.  The block is placed before 'insertBefore'.
374 Block *OpBuilder::createBlock(Block *insertBefore, TypeRange argTypes) {
375   assert(insertBefore && "expected valid insertion block");
376   return createBlock(insertBefore->getParent(), Region::iterator(insertBefore),
377                      argTypes);
378 }
379 
380 /// Create an operation given the fields represented as an OperationState.
381 Operation *OpBuilder::createOperation(const OperationState &state) {
382   return insert(Operation::create(state));
383 }
384 
385 /// Attempts to fold the given operation and places new results within
386 /// 'results'. Returns success if the operation was folded, failure otherwise.
387 /// Note: This function does not erase the operation on a successful fold.
388 LogicalResult OpBuilder::tryFold(Operation *op,
389                                  SmallVectorImpl<Value> &results) {
390   results.reserve(op->getNumResults());
391   auto cleanupFailure = [&] {
392     results.assign(op->result_begin(), op->result_end());
393     return failure();
394   };
395 
396   // If this operation is already a constant, there is nothing to do.
397   if (matchPattern(op, m_Constant()))
398     return cleanupFailure();
399 
400   // Check to see if any operands to the operation is constant and whether
401   // the operation knows how to constant fold itself.
402   SmallVector<Attribute, 4> constOperands(op->getNumOperands());
403   for (unsigned i = 0, e = op->getNumOperands(); i != e; ++i)
404     matchPattern(op->getOperand(i), m_Constant(&constOperands[i]));
405 
406   // Try to fold the operation.
407   SmallVector<OpFoldResult, 4> foldResults;
408   if (failed(op->fold(constOperands, foldResults)) || foldResults.empty())
409     return cleanupFailure();
410 
411   // A temporary builder used for creating constants during folding.
412   OpBuilder cstBuilder(context);
413   SmallVector<Operation *, 1> generatedConstants;
414 
415   // Populate the results with the folded results.
416   Dialect *dialect = op->getDialect();
417   for (auto &it : llvm::enumerate(foldResults)) {
418     // Normal values get pushed back directly.
419     if (auto value = it.value().dyn_cast<Value>()) {
420       results.push_back(value);
421       continue;
422     }
423 
424     // Otherwise, try to materialize a constant operation.
425     if (!dialect)
426       return cleanupFailure();
427 
428     // Ask the dialect to materialize a constant operation for this value.
429     Attribute attr = it.value().get<Attribute>();
430     auto *constOp = dialect->materializeConstant(
431         cstBuilder, attr, op->getResult(it.index()).getType(), op->getLoc());
432     if (!constOp) {
433       // Erase any generated constants.
434       for (Operation *cst : generatedConstants)
435         cst->erase();
436       return cleanupFailure();
437     }
438     assert(matchPattern(constOp, m_Constant()));
439 
440     generatedConstants.push_back(constOp);
441     results.push_back(constOp->getResult(0));
442   }
443 
444   // If we were successful, insert any generated constants.
445   for (Operation *cst : generatedConstants)
446     insert(cst);
447 
448   return success();
449 }
450