| 1 | //===- IdiomRecognizer.cpp - recognizing and raising idioms to CIR --------===// |
| 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 pass is responsible for recognizing idioms (such as uses of functions |
| 10 | // and types to the C/C++ standard library) and replacing them with Clang IR |
| 11 | // operators for later optimization. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PassDetail.h" |
| 16 | #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" |
| 17 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 18 | #include "clang/CIR/Dialect/Passes.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | |
| 21 | #include <utility> |
| 22 | |
| 23 | using namespace mlir; |
| 24 | using namespace cir; |
| 25 | |
| 26 | namespace mlir { |
| 27 | #define GEN_PASS_DEF_IDIOMRECOGNIZER |
| 28 | #include "clang/CIR/Dialect/Passes.h.inc" |
| 29 | } // namespace mlir |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // True when the call's no builtin state forbids treating it as `name`. A |
| 34 | // builtin mark wins over a nobuiltin mark or a nobuiltins list. |
| 35 | bool isNoBuiltin(CallOp call, llvm::StringRef name) { |
| 36 | if (call->hasAttr(name: cir::CIRDialect::getBuiltinAttrName())) |
| 37 | return false; |
| 38 | if (call->hasAttr(name: cir::CIRDialect::getNoBuiltinAttrName())) |
| 39 | return true; |
| 40 | auto noBuiltins = call->getAttrOfType<mlir::ArrayAttr>( |
| 41 | name: cir::CIRDialect::getNoBuiltinsAttrName()); |
| 42 | if (!noBuiltins) |
| 43 | return false; |
| 44 | return noBuiltins.empty() || |
| 45 | llvm::any_of(Range&: noBuiltins, P: [name](mlir::Attribute entry) { |
| 46 | auto builtinName = mlir::dyn_cast<mlir::StringAttr>(Val&: entry); |
| 47 | return builtinName && builtinName.getValue() == name; |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | // Raises a direct cir.call to the first candidate in `TargetOps` that matches. |
| 52 | template <typename... TargetOps> class StdRecognizer { |
| 53 | template <typename TargetOp, size_t... Indices> |
| 54 | static TargetOp buildCall(cir::CIRBaseBuilderTy &builder, CallOp call, |
| 55 | std::index_sequence<Indices...>) { |
| 56 | return TargetOp::create(builder, call.getLoc(), |
| 57 | call->getResult(idx: 0).getType(), |
| 58 | call.getOperand(i: Indices)..., call.getCalleeAttr()); |
| 59 | } |
| 60 | |
| 61 | template <typename TargetOp> |
| 62 | static bool raiseOne(CallOp call, mlir::MLIRContext &context, |
| 63 | mlir::SymbolTableCollection &symbolTables) { |
| 64 | // A musttail call must stay a call, so it is never raised. |
| 65 | if (!call.getCallee() || call.getMusttail() || |
| 66 | !TargetOp::signatureMatches(call->getOperandTypes(), |
| 67 | call->getResultTypes())) |
| 68 | return false; |
| 69 | |
| 70 | if constexpr (TargetOp::hasKnownFuncKind()) { |
| 71 | // Only a free std function with the right name carries the tag, so |
| 72 | // members, static members, and operators never match. The shape of the |
| 73 | // call is checked here, so a variadic callee never matches. |
| 74 | cir::FuncOp callee = call.resolveCalleeInTable(symbolTable&: symbolTables); |
| 75 | if (!callee || callee.getFunctionType().isVarArg()) |
| 76 | return false; |
| 77 | auto funcIdentity = mlir::dyn_cast_if_present<cir::FuncIdentityAttr>( |
| 78 | Val: callee.getFuncInfoAttr()); |
| 79 | if (!funcIdentity || funcIdentity.getKind() != TargetOp::getFuncKind()) |
| 80 | return false; |
| 81 | } else { |
| 82 | // A C library function has no identity tag, so it is matched by callee |
| 83 | // symbol, which works because C names are unmangled. The symbol alone is |
| 84 | // not enough when builtins are disabled, so the recorded no builtin state |
| 85 | // gates the match. |
| 86 | if (*call.getCallee() != TargetOp::getFunctionName() || |
| 87 | isNoBuiltin(call, TargetOp::getFunctionName())) |
| 88 | return false; |
| 89 | // The library function is not variadic, so a variadic callee that only |
| 90 | // shares the name is not that function. This lookup runs only after the |
| 91 | // name matches. |
| 92 | cir::FuncOp callee = call.resolveCalleeInTable(symbolTable&: symbolTables); |
| 93 | if (callee && callee.getFunctionType().isVarArg()) |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | cir::CIRBaseBuilderTy builder(context); |
| 98 | builder.setInsertionPointAfter(call.getOperation()); |
| 99 | constexpr unsigned numArgs = TargetOp::getNumArgs(); |
| 100 | TargetOp op = |
| 101 | buildCall<TargetOp>(builder, call, std::make_index_sequence<numArgs>()); |
| 102 | // Preserve the call properties as auxiliary metadata so LoweringPrepare |
| 103 | // can reconstruct the original call. The raised operation does not need |
| 104 | // to declare the same set of inherent attributes. |
| 105 | // TODO: Model the call properties needed by raised operations as inherent |
| 106 | // attributes on CIR_StdOp instead of carrying them as discardable here. |
| 107 | call->getName().walkInherentAttrs( |
| 108 | op: call, visitor: [&](llvm::StringRef name, mlir::Attribute &attr) { |
| 109 | if (name != call.getCalleeAttrName().getValue()) |
| 110 | op->setDiscardableAttr(name, attr); |
| 111 | }); |
| 112 | for (mlir::NamedAttribute attr : call->getDiscardableAttrs()) |
| 113 | op->setDiscardableAttr(attr.getName(), attr.getValue()); |
| 114 | call.replaceAllUsesWith(op); |
| 115 | call.erase(); |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | public: |
| 120 | // Tries each candidate in order and stops at the first that raises. |
| 121 | static bool raise(CallOp call, mlir::MLIRContext &context, |
| 122 | mlir::SymbolTableCollection &symbolTables) { |
| 123 | return (raiseOne<TargetOps>(call, context, symbolTables) || ...); |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // The library calls the recognizer knows how to raise, tried in order. |
| 128 | using RecognizedStdOps = StdRecognizer<StdFindOp, StrLenOp>; |
| 129 | |
| 130 | struct IdiomRecognizerPass |
| 131 | : public impl::IdiomRecognizerBase<IdiomRecognizerPass> { |
| 132 | IdiomRecognizerPass() = default; |
| 133 | |
| 134 | void runOnOperation() override; |
| 135 | |
| 136 | void recognizeStandardLibraryCall(CallOp call, |
| 137 | mlir::SymbolTableCollection &symbolTables); |
| 138 | }; |
| 139 | } // namespace |
| 140 | |
| 141 | void IdiomRecognizerPass::recognizeStandardLibraryCall( |
| 142 | CallOp call, mlir::SymbolTableCollection &symbolTables) { |
| 143 | RecognizedStdOps::raise(call, context&: getContext(), symbolTables); |
| 144 | } |
| 145 | |
| 146 | void IdiomRecognizerPass::runOnOperation() { |
| 147 | // The facts this pass reads live on the operations, so it needs no AST |
| 148 | // and also works on parsed CIR assembly. |
| 149 | mlir::SymbolTableCollection symbolTables; |
| 150 | |
| 151 | getOperation()->walk(callback: [&](CallOp callOp) { |
| 152 | // Skip indirect calls. |
| 153 | std::optional<llvm::StringRef> callee = callOp.getCallee(); |
| 154 | if (!callee) |
| 155 | return; |
| 156 | |
| 157 | recognizeStandardLibraryCall(call: callOp, symbolTables); |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | std::unique_ptr<Pass> mlir::createIdiomRecognizerPass() { |
| 162 | return std::make_unique<IdiomRecognizerPass>(); |
| 163 | } |
| 164 | |