[clang] [Sema] Fast-path simple plain auto deduction in DeduceAutoType (PR #188196)

Jiaqi He via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 22 23:55:03 PDT 2026


================
@@ -5253,7 +5268,56 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *Init, QualType &Result,
 
     DeducedType = getDecltypeForExpr(Init);
     assert(!DeducedType.isNull());
-  } else {
+  } else if (CanTryFastPath) {
+    // Fast-path a subset of `auto` deduction for non-init-list cases in
+    // non-ObjC and non-OpenCL language modes. For these cases, the deduced
+    // type can be computed directly from the initializer type by removing
+    // references, applying array/function decay, and dropping top-level
+    // cv-qualifiers. For single-level `auto*` declarators, the deduced type
+    // is the pointee type of the processed initializer type.
+
+    QualType DeclaredTy = Type.getType();
+    bool IsPlainOrTopLevelCvAuto = Context.hasSameType(
+        DeclaredTy.getLocalUnqualifiedType(), Context.getAutoDeductType());
+    bool IsSimpleAutoStar =
+        DeclaredTy->isPointerType() &&
+        Context.hasSameType(
+            DeclaredTy->getPointeeType().getLocalUnqualifiedType(),
+            Context.getAutoDeductType()) &&
+        !DeclaredTy->getPointeeType().getLocalQualifiers().hasCVRQualifiers();
+
+    QualType ProcessedInitTy = Init->getType().getNonReferenceType();
+    if (ProcessedInitTy->isArrayType() || ProcessedInitTy->isFunctionType())
+      ProcessedInitTy = Context.getDecayedType(ProcessedInitTy);
+
+    ProcessedInitTy = ProcessedInitTy.getUnqualifiedType();
+    bool CanUsePointerFastPath =
+        IsSimpleAutoStar && ProcessedInitTy->isPointerType();
+
+    if (IsPlainOrTopLevelCvAuto) {
+      DeducedType = ProcessedInitTy;
+      FastPathUsed = true;
+    } else if (CanUsePointerFastPath) {
+      DeducedType = ProcessedInitTy->getPointeeType();
+      // Normalize qualifiers on array pointee types so the fast path builds the
+      // same deduced type shape as the slow path.
+      Qualifiers DeducedQuals;
+      DeducedType = Context.getUnqualifiedArrayType(DeducedType, DeducedQuals);
+      DeducedType = Context.getQualifiedType(DeducedType, DeducedQuals);
+      FastPathUsed = true;
+    }
+  }
----------------
heturing wrote:

> What do you think about sticking this in a function returning a `QualType` like this?

I am a bit hesitant to extract this into a function. The logic is only used here and it is small. Adding another interface for it may not improve clarity. I would like to hear other reviewers' thoughts on this.

> Also, I think the last part about normalizing array qualifiers can be simplified by using `Context.getAsArrayType`.

I don't think `Context.getAsArrayType` is a direct replacement here. Using this function still requires us to build a `QualType` from the returned `const ArrayType *`.

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


More information about the cfe-commits mailing list