| 1 | //===- FuncToEmitC.cpp - Func to EmitC Patterns -----------------*- C++ -*-===// |
| 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 implements patterns to convert the Func dialect to the EmitC |
| 10 | // dialect. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Conversion/FuncToEmitC/FuncToEmitC.h" |
| 15 | |
| 16 | #include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h" |
| 17 | #include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 18 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 19 | #include "mlir/IR/BuiltinAttributes.h" |
| 20 | #include "mlir/IR/SymbolTable.h" |
| 21 | #include "mlir/Transforms/DialectConversion.h" |
| 22 | #include "llvm/ADT/StringExtras.h" |
| 23 | #include "llvm/Support/LogicalResult.h" |
| 24 | |
| 25 | using namespace mlir; |
| 26 | |
| 27 | namespace { |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Multi-return struct helpers |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | // Looks up or creates an `emitc.class` named after `types` in the nearest |
| 34 | // enclosing symbol table of `op`, suitable for packing those types as plain |
| 35 | // struct fields (field0, field1, ...). If the class already exists it is |
| 36 | // verified to have exactly the right fields and no methods. Returns the |
| 37 | // corresponding !emitc.opaque<"struct ..."> type on success. |
| 38 | static FailureOr<emitc::OpaqueType> |
| 39 | getOrCreateMultiReturnType(ConversionPatternRewriter &rewriter, Location loc, |
| 40 | Operation *op, TypeRange types) { |
| 41 | // Build the struct name from the types, e.g. "return_i32_i32". Each type is |
| 42 | // printed and non-alphanumeric characters are replaced with '_'. |
| 43 | std::string structName = "return" ; |
| 44 | for (Type type : types) { |
| 45 | std::string typeName; |
| 46 | llvm::raw_string_ostream os(typeName); |
| 47 | type.print(os); |
| 48 | std::replace_if( |
| 49 | first: typeName.begin(), last: typeName.end(), |
| 50 | pred: [](char c) { return !llvm::isAlnum(C: c); }, new_value: '_'); |
| 51 | structName += "_" + typeName; |
| 52 | } |
| 53 | |
| 54 | // Find the enclosing symbol table and the direct child op within it that |
| 55 | // contains `op`; the class will be inserted immediately before that child. |
| 56 | Operation *symbolTableOp = SymbolTable::getNearestSymbolTable(from: op); |
| 57 | Operation *insertBefore = op; |
| 58 | while (insertBefore->getParentOp() != symbolTableOp) |
| 59 | insertBefore = insertBefore->getParentOp(); |
| 60 | |
| 61 | if (Operation *sym = SymbolTable::lookupSymbolIn(op: symbolTableOp, symbol: structName)) { |
| 62 | auto classOp = dyn_cast<emitc::ClassOp>(Val: sym); |
| 63 | if (!classOp) |
| 64 | return emitError(loc) << "symbol '" << structName |
| 65 | << "' exists but is not an emitc.class" ; |
| 66 | |
| 67 | if (classOp.getClassType() != emitc::ClassType::struct_) |
| 68 | return emitError(loc) |
| 69 | << "existing class '" << structName << "' is not a struct" ; |
| 70 | |
| 71 | SmallVector<emitc::FieldOp> fields; |
| 72 | for (Operation &bodyOp : classOp.getBody().front()) { |
| 73 | if (isa<emitc::FuncOp>(Val: bodyOp)) |
| 74 | return emitError(loc) << "existing class '" << structName |
| 75 | << "' has methods; expected a plain struct" ; |
| 76 | if (auto fieldOp = dyn_cast<emitc::FieldOp>(Val&: bodyOp)) |
| 77 | fields.push_back(Elt: fieldOp); |
| 78 | } |
| 79 | if (fields.size() != types.size()) |
| 80 | return emitError(loc) << "existing class '" << structName |
| 81 | << "' has wrong number of fields" ; |
| 82 | for (auto [i, fieldOp] : llvm::enumerate(First&: fields)) { |
| 83 | if (fieldOp.getSymName() != "field" + std::to_string(val: i)) |
| 84 | return emitError(loc) << "existing class '" << structName |
| 85 | << "': unexpected field name at index " << i; |
| 86 | if (fieldOp.getTypeAttr().getValue() != types[i]) |
| 87 | return emitError(loc) << "existing class '" << structName |
| 88 | << "': wrong type for field " << i; |
| 89 | } |
| 90 | } else { |
| 91 | // Create the ClassOp before `insertBefore`, then restore the insertion |
| 92 | // point. |
| 93 | auto savedIP = rewriter.saveInsertionPoint(); |
| 94 | rewriter.setInsertionPoint(insertBefore); |
| 95 | |
| 96 | emitc::ClassOp classOp = emitc::ClassOp::create( |
| 97 | builder&: rewriter, location: loc, sym_name: structName, /*sym_visibility=*/nullptr, |
| 98 | /*final_specifier=*/false, class_type: emitc::ClassType::struct_); |
| 99 | rewriter.createBlock(parent: &classOp.getBody()); |
| 100 | rewriter.setInsertionPointToStart(&classOp.getBody().front()); |
| 101 | |
| 102 | for (auto [i, type] : llvm::enumerate(First&: types)) { |
| 103 | auto fieldName = rewriter.getStringAttr(bytes: "field" + std::to_string(val: i)); |
| 104 | emitc::FieldOp::create(builder&: rewriter, location: loc, sym_name: fieldName, |
| 105 | /*sym_visibility=*/nullptr, type: TypeAttr::get(type), |
| 106 | initial_value: nullptr); |
| 107 | } |
| 108 | |
| 109 | rewriter.restoreInsertionPoint(ip: savedIP); |
| 110 | } |
| 111 | return emitc::OpaqueType::get(context: rewriter.getContext(), value: "struct " + structName); |
| 112 | } |
| 113 | |
| 114 | // Packs multiple SSA values into an emitc.class struct variable and loads the |
| 115 | // result as a single SSA value of the opaque struct type. |
| 116 | static Value packValuesIntoStruct(ConversionPatternRewriter &rewriter, |
| 117 | Location loc, ValueRange values, |
| 118 | emitc::OpaqueType structType) { |
| 119 | MLIRContext *ctx = rewriter.getContext(); |
| 120 | auto noInit = emitc::OpaqueAttr::get(context: ctx, value: "" ); |
| 121 | Value structLv = |
| 122 | emitc::VariableOp::create(builder&: rewriter, location: loc, |
| 123 | resultType0: emitc::LValueType::get(valueType: structType), value: noInit) |
| 124 | .getResult(); |
| 125 | for (auto [i, val] : llvm::enumerate(First&: values)) { |
| 126 | Value fieldLv = |
| 127 | emitc::MemberOp::create( |
| 128 | builder&: rewriter, location: loc, resultType0: emitc::LValueType::get(valueType: val.getType()), |
| 129 | member: rewriter.getStringAttr(bytes: "field" + std::to_string(val: i)), operand: structLv) |
| 130 | .getResult(); |
| 131 | emitc::AssignOp::create(builder&: rewriter, location: loc, var: fieldLv, value: val); |
| 132 | } |
| 133 | return emitc::LoadOp::create(builder&: rewriter, location: loc, result: structType, operand: structLv).getResult(); |
| 134 | } |
| 135 | |
| 136 | /// Implement the interface to convert Func to EmitC. |
| 137 | struct FuncToEmitCDialectInterface : public ConvertToEmitCPatternInterface { |
| 138 | FuncToEmitCDialectInterface(Dialect *dialect) |
| 139 | : ConvertToEmitCPatternInterface(dialect) {} |
| 140 | |
| 141 | /// Hook for derived dialect interface to provide conversion patterns |
| 142 | /// and mark dialect legal for the conversion target. |
| 143 | void populateConvertToEmitCConversionPatterns( |
| 144 | ConversionTarget &target, TypeConverter &typeConverter, |
| 145 | RewritePatternSet &patterns, std::optional<bool> lowerToCpp) const final { |
| 146 | populateFuncToEmitCPatterns(typeConverter, patterns, |
| 147 | lowerToCpp: lowerToCpp.value_or(u: true)); |
| 148 | } |
| 149 | }; |
| 150 | } // namespace |
| 151 | |
| 152 | void mlir::registerConvertFuncToEmitCInterface(DialectRegistry ®istry) { |
| 153 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, func::FuncDialect *dialect) { |
| 154 | dialect->addInterfaces<FuncToEmitCDialectInterface>(); |
| 155 | }); |
| 156 | } |
| 157 | |
| 158 | //===----------------------------------------------------------------------===// |
| 159 | // Conversion Patterns |
| 160 | //===----------------------------------------------------------------------===// |
| 161 | |
| 162 | namespace { |
| 163 | class CallOpConversion final : public OpConversionPattern<func::CallOp> { |
| 164 | public: |
| 165 | CallOpConversion(const TypeConverter &typeConverter, MLIRContext *ctx, |
| 166 | bool lowerToCpp) |
| 167 | : OpConversionPattern<func::CallOp>(typeConverter, ctx), |
| 168 | lowerToCpp(lowerToCpp) {} |
| 169 | |
| 170 | LogicalResult |
| 171 | matchAndRewrite(func::CallOp callOp, OpAdaptor adaptor, |
| 172 | ConversionPatternRewriter &rewriter) const override { |
| 173 | // Do not convert multiple-return functions if lowering target is Cpp. |
| 174 | // The translator will emit the return values as an std::tuple. |
| 175 | if (callOp.getNumResults() > 1 && lowerToCpp) |
| 176 | return rewriter.notifyMatchFailure( |
| 177 | arg&: callOp, msg: "only functions with zero or one result can be converted" ); |
| 178 | |
| 179 | SmallVector<Type> convertedResultTypes; |
| 180 | for (Type t : callOp.getResultTypes()) { |
| 181 | Type resultType = getTypeConverter()->convertType(t); |
| 182 | if (!resultType) |
| 183 | return rewriter.notifyMatchFailure(arg&: callOp, |
| 184 | msg: "result type conversion failed" ); |
| 185 | if (isa<emitc::ArrayType>(Val: resultType)) |
| 186 | return rewriter.notifyMatchFailure( |
| 187 | arg&: callOp, msg: "function calls returning arrays are not supported" ); |
| 188 | convertedResultTypes.push_back(Elt: resultType); |
| 189 | } |
| 190 | |
| 191 | if (callOp.getNumResults() <= 1) { |
| 192 | auto newCall = rewriter.replaceOpWithNewOp<emitc::CallOp>( |
| 193 | op: callOp, args: callOp.getCalleeAttr(), args&: convertedResultTypes, |
| 194 | args: adaptor.getOperands()); |
| 195 | newCall.setArgAttrsAttr(callOp.getArgAttrsAttr()); |
| 196 | newCall.setResAttrsAttr(callOp.getResAttrsAttr()); |
| 197 | newCall->setDiscardableAttrs(callOp->getDiscardableAttrDictionary()); |
| 198 | return success(); |
| 199 | } |
| 200 | |
| 201 | // Multi-result call: determine the struct type. |
| 202 | Location loc = callOp.getLoc(); |
| 203 | |
| 204 | auto structType = |
| 205 | getOrCreateMultiReturnType(rewriter, loc, op: callOp, types: convertedResultTypes); |
| 206 | if (failed(Result: structType)) |
| 207 | return rewriter.notifyMatchFailure(arg&: callOp, |
| 208 | msg: "incompatible multi-return struct" ); |
| 209 | |
| 210 | // Emit a call returning the packed struct. |
| 211 | Value structVal = |
| 212 | emitc::CallOp::create(builder&: rewriter, location: loc, callee: callOp.getCalleeAttr(), |
| 213 | results: TypeRange{*structType}, operands: adaptor.getOperands()) |
| 214 | .getResult(i: 0); |
| 215 | |
| 216 | // Unpack struct fields to replace the original multiple results. |
| 217 | SmallVector<Value> results; |
| 218 | for (auto [i, result] : llvm::enumerate(First: callOp.getResults())) { |
| 219 | if (result.use_empty()) { |
| 220 | results.push_back(Elt: Value()); // No replacement needed. |
| 221 | continue; |
| 222 | } |
| 223 | Type fieldType = convertedResultTypes[i]; |
| 224 | StringAttr fieldName = |
| 225 | rewriter.getStringAttr(bytes: "field" + std::to_string(val: i)); |
| 226 | Value fieldValue = emitc::MemberOp::create(builder&: rewriter, location: loc, resultType0: fieldType, |
| 227 | member: fieldName, operand: structVal) |
| 228 | .getResult(); |
| 229 | results.push_back(Elt: fieldValue); |
| 230 | } |
| 231 | |
| 232 | rewriter.replaceOp(op: callOp, newValues: results); |
| 233 | return success(); |
| 234 | } |
| 235 | |
| 236 | private: |
| 237 | bool lowerToCpp; |
| 238 | }; |
| 239 | |
| 240 | class FuncOpConversion final : public OpConversionPattern<func::FuncOp> { |
| 241 | public: |
| 242 | FuncOpConversion(const TypeConverter &typeConverter, MLIRContext *ctx, |
| 243 | bool lowerToCpp) |
| 244 | : OpConversionPattern<func::FuncOp>(typeConverter, ctx), |
| 245 | lowerToCpp(lowerToCpp) {} |
| 246 | |
| 247 | LogicalResult |
| 248 | matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor, |
| 249 | ConversionPatternRewriter &rewriter) const override { |
| 250 | FunctionType fnType = funcOp.getFunctionType(); |
| 251 | |
| 252 | // Do not convert multiple-return functions if lowering target is Cpp. |
| 253 | // The translator will emit the return values as an std::tuple. |
| 254 | if (fnType.getNumResults() > 1 && lowerToCpp) |
| 255 | return rewriter.notifyMatchFailure( |
| 256 | arg&: funcOp, msg: "only functions with zero or one result can be converted" ); |
| 257 | |
| 258 | TypeConverter::SignatureConversion signatureConverter( |
| 259 | fnType.getNumInputs()); |
| 260 | for (const auto &argType : enumerate(First: fnType.getInputs())) { |
| 261 | auto convertedType = getTypeConverter()->convertType(t: argType.value()); |
| 262 | if (!convertedType) |
| 263 | return rewriter.notifyMatchFailure(arg&: funcOp, |
| 264 | msg: "argument type conversion failed" ); |
| 265 | signatureConverter.addInputs(origInputNo: argType.index(), types: convertedType); |
| 266 | } |
| 267 | |
| 268 | SmallVector<Type> convertedResultTypes; |
| 269 | for (Type t : fnType.getResults()) { |
| 270 | Type resultType = getTypeConverter()->convertType(t); |
| 271 | if (!resultType) |
| 272 | return rewriter.notifyMatchFailure(arg&: funcOp, |
| 273 | msg: "result type conversion failed" ); |
| 274 | if (isa<emitc::ArrayType>(Val: resultType)) |
| 275 | return rewriter.notifyMatchFailure( |
| 276 | arg&: funcOp, msg: "functions returning arrays are not supported" ); |
| 277 | convertedResultTypes.push_back(Elt: resultType); |
| 278 | } |
| 279 | |
| 280 | Type resultType; |
| 281 | if (fnType.getNumResults() == 1) { |
| 282 | resultType = convertedResultTypes[0]; |
| 283 | } else if (fnType.getNumResults() > 1) { |
| 284 | auto structTypeOrErr = getOrCreateMultiReturnType( |
| 285 | rewriter, loc: funcOp.getLoc(), op: funcOp, types: convertedResultTypes); |
| 286 | if (failed(Result: structTypeOrErr)) |
| 287 | return rewriter.notifyMatchFailure(arg&: funcOp, |
| 288 | msg: "incompatible multi-return struct" ); |
| 289 | resultType = *structTypeOrErr; |
| 290 | } |
| 291 | |
| 292 | // Create the converted `emitc.func` op. |
| 293 | emitc::FuncOp newFuncOp = emitc::FuncOp::create( |
| 294 | builder&: rewriter, location: funcOp.getLoc(), name: funcOp.getName(), |
| 295 | type: FunctionType::get(context: rewriter.getContext(), |
| 296 | inputs: signatureConverter.getConvertedTypes(), |
| 297 | results: resultType ? TypeRange(resultType) : TypeRange())); |
| 298 | |
| 299 | newFuncOp.setArgAttrsAttr(funcOp.getArgAttrsAttr()); |
| 300 | newFuncOp.setResAttrsAttr(funcOp.getResAttrsAttr()); |
| 301 | newFuncOp.setVisibility(funcOp.getVisibility()); |
| 302 | |
| 303 | // Copy over the discardable attributes. |
| 304 | for (const auto &namedAttr : |
| 305 | funcOp->getDiscardableAttrDictionary().getValue()) |
| 306 | newFuncOp->setDiscardableAttr(name: namedAttr.getName(), value: namedAttr.getValue()); |
| 307 | |
| 308 | // Add `extern` to specifiers if `func.func` is declaration only. |
| 309 | if (funcOp.isDeclaration()) { |
| 310 | ArrayAttr specifiers = rewriter.getStrArrayAttr(values: {"extern" }); |
| 311 | newFuncOp.setSpecifiersAttr(specifiers); |
| 312 | } |
| 313 | |
| 314 | // Add `static` to specifiers if `func.func` is private but not a |
| 315 | // declaration. |
| 316 | if (funcOp.isPrivate() && !funcOp.isDeclaration()) { |
| 317 | ArrayAttr specifiers = rewriter.getStrArrayAttr(values: {"static" }); |
| 318 | newFuncOp.setSpecifiersAttr(specifiers); |
| 319 | } |
| 320 | |
| 321 | if (!funcOp.isDeclaration()) { |
| 322 | rewriter.inlineRegionBefore(region&: funcOp.getBody(), parent&: newFuncOp.getBody(), |
| 323 | before: newFuncOp.end()); |
| 324 | if (failed(Result: rewriter.convertRegionTypes( |
| 325 | region: &newFuncOp.getBody(), converter: *getTypeConverter(), entryConversion: &signatureConverter))) |
| 326 | return failure(); |
| 327 | } |
| 328 | rewriter.eraseOp(op: funcOp); |
| 329 | |
| 330 | return success(); |
| 331 | } |
| 332 | |
| 333 | private: |
| 334 | bool lowerToCpp; |
| 335 | }; |
| 336 | |
| 337 | class ReturnOpConversion final : public OpConversionPattern<func::ReturnOp> { |
| 338 | public: |
| 339 | ReturnOpConversion(const TypeConverter &typeConverter, MLIRContext *ctx, |
| 340 | bool lowerToCpp) |
| 341 | : OpConversionPattern<func::ReturnOp>(typeConverter, ctx), |
| 342 | lowerToCpp(lowerToCpp) {} |
| 343 | |
| 344 | LogicalResult |
| 345 | matchAndRewrite(func::ReturnOp returnOp, OpAdaptor adaptor, |
| 346 | ConversionPatternRewriter &rewriter) const override { |
| 347 | // Do not convert multiple-return functions if lowering target is Cpp. |
| 348 | // The translator will emit the return values as an std::tuple. |
| 349 | if (returnOp.getNumOperands() > 1 && lowerToCpp) |
| 350 | return rewriter.notifyMatchFailure( |
| 351 | arg&: returnOp, msg: "only zero or one operand is supported" ); |
| 352 | |
| 353 | if (llvm::any_of(Range: adaptor.getOperands(), P: [](Value operand) { |
| 354 | return isa<emitc::ArrayType>(Val: operand.getType()); |
| 355 | })) |
| 356 | return rewriter.notifyMatchFailure(arg&: returnOp, |
| 357 | msg: "returning arrays is not supported" ); |
| 358 | |
| 359 | if (returnOp.getNumOperands() <= 1) { |
| 360 | rewriter.replaceOpWithNewOp<emitc::ReturnOp>( |
| 361 | op: returnOp, |
| 362 | args: returnOp.getNumOperands() ? adaptor.getOperands()[0] : nullptr); |
| 363 | return success(); |
| 364 | } |
| 365 | |
| 366 | // Multi-operand return: pack values into a struct. |
| 367 | Location loc = returnOp.getLoc(); |
| 368 | |
| 369 | auto structType = getOrCreateMultiReturnType( |
| 370 | rewriter, loc, op: returnOp, types: adaptor.getOperands().getTypes()); |
| 371 | if (failed(Result: structType)) |
| 372 | return rewriter.notifyMatchFailure(arg&: returnOp, |
| 373 | msg: "incompatible multi-return struct" ); |
| 374 | |
| 375 | Value structVal = |
| 376 | packValuesIntoStruct(rewriter, loc, values: adaptor.getOperands(), structType: *structType); |
| 377 | rewriter.replaceOpWithNewOp<emitc::ReturnOp>(op: returnOp, args&: structVal); |
| 378 | return success(); |
| 379 | } |
| 380 | |
| 381 | private: |
| 382 | bool lowerToCpp; |
| 383 | }; |
| 384 | } // namespace |
| 385 | |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | // Pattern population |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | |
| 390 | void mlir::populateFuncToEmitCPatterns(const TypeConverter &typeConverter, |
| 391 | RewritePatternSet &patterns, |
| 392 | bool lowerToCpp) { |
| 393 | MLIRContext *ctx = patterns.getContext(); |
| 394 | |
| 395 | patterns.add<CallOpConversion, FuncOpConversion, ReturnOpConversion>( |
| 396 | arg: typeConverter, args&: ctx, args&: lowerToCpp); |
| 397 | } |
| 398 | |