[clang] [CIR] Recognize strlen in the IdiomRecognizer pass (PR #210400)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 17 15:01:16 PDT 2026


================
@@ -45,9 +45,41 @@ template <> bool signatureMatches<StdFindOp>(CallOp call) {
          iterTy == call->getResult(0).getType();
 }
 
-// Raises a direct cir.call to `TargetOp` when the callee carries the
-// matching identity tag.
-template <typename TargetOp> class StdRecognizer {
+// strlen takes a pointer to an 8 bit character and returns size_t, an unsigned
+// fundamental integer. A _BitInt is not a character type even at width 8.
+template <> bool signatureMatches<StrLenOp>(CallOp call) {
+  if (call.getNumOperands() != StrLenOp::getNumArgs() ||
+      call->getNumResults() != 1)
+    return false;
+  auto ptrTy = mlir::dyn_cast<cir::PointerType>(call.getOperand(0).getType());
+  return ptrTy && cir::isChar8Type(ptrTy.getPointee()) &&
+         cir::isFundamentalUIntType(call->getResult(0).getType());
+}
+
+// Returns true when the recorded no builtin state forbids treating the call
+// as the C library function `name`. The call carries a nobuiltin mark and
+// the caller a nobuiltins list, where an empty list disables them all.
+bool isNoBuiltin(CallOp call, llvm::StringRef name) {
+  if (call->hasAttr(cir::CIRDialect::getNoBuiltinAttrName()))
+    return true;
+
+  auto enclosing = call->getParentOfType<cir::FuncOp>();
+  auto noBuiltins = enclosing ? enclosing->getAttrOfType<mlir::ArrayAttr>(
----------------
andykaylor wrote:

We shouldn't have to do this. The attribute should have been added to the call when the CIR was generated. The LLVM optimizer depends on that, so if we don't do it it's a bug.

https://github.com/llvm/llvm-project/pull/210400


More information about the cfe-commits mailing list