1 //===- BuiltinDialect.cpp - MLIR Builtin Dialect --------------------------===// 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 contains the Builtin dialect that contains all of the attributes, 10 // operations, and types that are necessary for the validity of the IR. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/IR/BuiltinDialect.h" 15 #include "mlir/IR/BlockAndValueMapping.h" 16 #include "mlir/IR/Builders.h" 17 #include "mlir/IR/BuiltinOps.h" 18 #include "mlir/IR/FunctionImplementation.h" 19 #include "mlir/IR/OpImplementation.h" 20 #include "mlir/IR/StandardTypes.h" 21 #include "llvm/ADT/MapVector.h" 22 23 using namespace mlir; 24 25 //===----------------------------------------------------------------------===// 26 // Builtin Dialect 27 //===----------------------------------------------------------------------===// 28 29 namespace { 30 struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface { 31 using OpAsmDialectInterface::OpAsmDialectInterface; 32 33 LogicalResult getAlias(Attribute attr, raw_ostream &os) const override { 34 if (attr.isa<AffineMapAttr>()) { 35 os << "map"; 36 return success(); 37 } 38 if (attr.isa<IntegerSetAttr>()) { 39 os << "set"; 40 return success(); 41 } 42 if (attr.isa<LocationAttr>()) { 43 os << "loc"; 44 return success(); 45 } 46 return failure(); 47 } 48 }; 49 } // end anonymous namespace. 50 51 void BuiltinDialect::initialize() { 52 addTypes<ComplexType, BFloat16Type, Float16Type, Float32Type, Float64Type, 53 FunctionType, IndexType, IntegerType, MemRefType, UnrankedMemRefType, 54 NoneType, OpaqueType, RankedTensorType, TupleType, 55 UnrankedTensorType, VectorType>(); 56 addAttributes<AffineMapAttr, ArrayAttr, DenseIntOrFPElementsAttr, 57 DenseStringElementsAttr, DictionaryAttr, FloatAttr, 58 SymbolRefAttr, IntegerAttr, IntegerSetAttr, OpaqueAttr, 59 OpaqueElementsAttr, SparseElementsAttr, StringAttr, TypeAttr, 60 UnitAttr>(); 61 addAttributes<CallSiteLoc, FileLineColLoc, FusedLoc, NameLoc, OpaqueLoc, 62 UnknownLoc>(); 63 addOperations< 64 #define GET_OP_LIST 65 #include "mlir/IR/BuiltinOps.cpp.inc" 66 >(); 67 addInterfaces<BuiltinOpAsmDialectInterface>(); 68 } 69 70 //===----------------------------------------------------------------------===// 71 // FuncOp 72 //===----------------------------------------------------------------------===// 73 74 FuncOp FuncOp::create(Location location, StringRef name, FunctionType type, 75 ArrayRef<NamedAttribute> attrs) { 76 OperationState state(location, "func"); 77 OpBuilder builder(location->getContext()); 78 FuncOp::build(builder, state, name, type, attrs); 79 return cast<FuncOp>(Operation::create(state)); 80 } 81 FuncOp FuncOp::create(Location location, StringRef name, FunctionType type, 82 iterator_range<dialect_attr_iterator> attrs) { 83 SmallVector<NamedAttribute, 8> attrRef(attrs); 84 return create(location, name, type, llvm::makeArrayRef(attrRef)); 85 } 86 FuncOp FuncOp::create(Location location, StringRef name, FunctionType type, 87 ArrayRef<NamedAttribute> attrs, 88 ArrayRef<MutableDictionaryAttr> argAttrs) { 89 FuncOp func = create(location, name, type, attrs); 90 func.setAllArgAttrs(argAttrs); 91 return func; 92 } 93 94 void FuncOp::build(OpBuilder &builder, OperationState &state, StringRef name, 95 FunctionType type, ArrayRef<NamedAttribute> attrs, 96 ArrayRef<MutableDictionaryAttr> argAttrs) { 97 state.addAttribute(SymbolTable::getSymbolAttrName(), 98 builder.getStringAttr(name)); 99 state.addAttribute(getTypeAttrName(), TypeAttr::get(type)); 100 state.attributes.append(attrs.begin(), attrs.end()); 101 state.addRegion(); 102 103 if (argAttrs.empty()) 104 return; 105 assert(type.getNumInputs() == argAttrs.size()); 106 SmallString<8> argAttrName; 107 for (unsigned i = 0, e = type.getNumInputs(); i != e; ++i) 108 if (auto argDict = argAttrs[i].getDictionary(builder.getContext())) 109 state.addAttribute(getArgAttrName(i, argAttrName), argDict); 110 } 111 112 static ParseResult parseFuncOp(OpAsmParser &parser, OperationState &result) { 113 auto buildFuncType = [](Builder &builder, ArrayRef<Type> argTypes, 114 ArrayRef<Type> results, impl::VariadicFlag, 115 std::string &) { 116 return builder.getFunctionType(argTypes, results); 117 }; 118 119 return impl::parseFunctionLikeOp(parser, result, /*allowVariadic=*/false, 120 buildFuncType); 121 } 122 123 static void print(FuncOp op, OpAsmPrinter &p) { 124 FunctionType fnType = op.getType(); 125 impl::printFunctionLikeOp(p, op, fnType.getInputs(), /*isVariadic=*/false, 126 fnType.getResults()); 127 } 128 129 static LogicalResult verify(FuncOp op) { 130 // If this function is external there is nothing to do. 131 if (op.isExternal()) 132 return success(); 133 134 // Verify that the argument list of the function and the arg list of the entry 135 // block line up. The trait already verified that the number of arguments is 136 // the same between the signature and the block. 137 auto fnInputTypes = op.getType().getInputs(); 138 Block &entryBlock = op.front(); 139 for (unsigned i = 0, e = entryBlock.getNumArguments(); i != e; ++i) 140 if (fnInputTypes[i] != entryBlock.getArgument(i).getType()) 141 return op.emitOpError("type of entry block argument #") 142 << i << '(' << entryBlock.getArgument(i).getType() 143 << ") must match the type of the corresponding argument in " 144 << "function signature(" << fnInputTypes[i] << ')'; 145 146 return success(); 147 } 148 149 /// Clone the internal blocks from this function into dest and all attributes 150 /// from this function to dest. 151 void FuncOp::cloneInto(FuncOp dest, BlockAndValueMapping &mapper) { 152 // Add the attributes of this function to dest. 153 llvm::MapVector<Identifier, Attribute> newAttrs; 154 for (auto &attr : dest.getAttrs()) 155 newAttrs.insert(attr); 156 for (auto &attr : getAttrs()) 157 newAttrs.insert(attr); 158 dest.getOperation()->setAttrs( 159 DictionaryAttr::get(newAttrs.takeVector(), getContext())); 160 161 // Clone the body. 162 getBody().cloneInto(&dest.getBody(), mapper); 163 } 164 165 /// Create a deep copy of this function and all of its blocks, remapping 166 /// any operands that use values outside of the function using the map that is 167 /// provided (leaving them alone if no entry is present). Replaces references 168 /// to cloned sub-values with the corresponding value that is copied, and adds 169 /// those mappings to the mapper. 170 FuncOp FuncOp::clone(BlockAndValueMapping &mapper) { 171 FunctionType newType = getType(); 172 173 // If the function has a body, then the user might be deleting arguments to 174 // the function by specifying them in the mapper. If so, we don't add the 175 // argument to the input type vector. 176 bool isExternalFn = isExternal(); 177 if (!isExternalFn) { 178 SmallVector<Type, 4> inputTypes; 179 inputTypes.reserve(newType.getNumInputs()); 180 for (unsigned i = 0, e = getNumArguments(); i != e; ++i) 181 if (!mapper.contains(getArgument(i))) 182 inputTypes.push_back(newType.getInput(i)); 183 newType = FunctionType::get(inputTypes, newType.getResults(), getContext()); 184 } 185 186 // Create the new function. 187 FuncOp newFunc = cast<FuncOp>(getOperation()->cloneWithoutRegions()); 188 newFunc.setType(newType); 189 190 /// Set the argument attributes for arguments that aren't being replaced. 191 for (unsigned i = 0, e = getNumArguments(), destI = 0; i != e; ++i) 192 if (isExternalFn || !mapper.contains(getArgument(i))) 193 newFunc.setArgAttrs(destI++, getArgAttrs(i)); 194 195 /// Clone the current function into the new one and return it. 196 cloneInto(newFunc, mapper); 197 return newFunc; 198 } 199 FuncOp FuncOp::clone() { 200 BlockAndValueMapping mapper; 201 return clone(mapper); 202 } 203 204 //===----------------------------------------------------------------------===// 205 // ModuleOp 206 //===----------------------------------------------------------------------===// 207 208 void ModuleOp::build(OpBuilder &builder, OperationState &state, 209 Optional<StringRef> name) { 210 ensureTerminator(*state.addRegion(), builder, state.location); 211 if (name) { 212 state.attributes.push_back(builder.getNamedAttr( 213 mlir::SymbolTable::getSymbolAttrName(), builder.getStringAttr(*name))); 214 } 215 } 216 217 /// Construct a module from the given context. 218 ModuleOp ModuleOp::create(Location loc, Optional<StringRef> name) { 219 OpBuilder builder(loc->getContext()); 220 return builder.create<ModuleOp>(loc, name); 221 } 222 223 static LogicalResult verify(ModuleOp op) { 224 // Check that none of the attributes are non-dialect attributes, except for 225 // the symbol related attributes. 226 for (auto attr : op.getAttrs()) { 227 if (!attr.first.strref().contains('.') && 228 !llvm::is_contained( 229 ArrayRef<StringRef>{mlir::SymbolTable::getSymbolAttrName(), 230 mlir::SymbolTable::getVisibilityAttrName()}, 231 attr.first.strref())) 232 return op.emitOpError() 233 << "can only contain dialect-specific attributes, found: '" 234 << attr.first << "'"; 235 } 236 237 return success(); 238 } 239 240 //===----------------------------------------------------------------------===// 241 // TableGen'd op method definitions 242 //===----------------------------------------------------------------------===// 243 244 #define GET_OP_CLASSES 245 #include "mlir/IR/BuiltinOps.cpp.inc" 246