[clang] [Clang] Reject non-scalar pointee types in masked builtins (PR #247474)

via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 00:02:12 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Echoes (YzK0741)

<details>
<summary>Changes</summary>

A non-scalar pointee (e.g. int **) passed the check in CheckMaskedBuiltinArgs, was then passed to getExtVectorType, and triggered an assertion, crashing clang.

CheckMaskedBuiltinArgs now allows only pointee types that getExtVectorType accepts (builtin, dependent, or a _BitInt with a power-of-two bit width) and reports an error otherwise. The check is shared by __builtin_masked_load, __builtin_masked_store, __builtin_masked_gather and __builtin_masked_scatter.

Tests covering all four builtins are added in clang/test/Sema/builtin-masked.c; llvm-lit passes locally.

Fixes #<!-- -->246852

---
Full diff: https://github.com/llvm/llvm-project/pull/247474.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaChecking.cpp (+18-1) 
- (modified) clang/test/Sema/builtin-masked.c (+7) 


``````````diff
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index bbdd6f4ac9a40..ab42aa9a67f02 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2863,11 +2863,15 @@ static bool CheckMaskedBuiltinArgs(Sema &S, Expr *MaskArg, Expr *PtrArg,
            << MaskTy;
 
   QualType PtrTy = PtrArg->getType();
-  if (!PtrTy->isPointerType() || PtrTy->getPointeeType()->isVectorType())
+  if (!PtrTy->isPointerType())
     return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
            << Pos << "scalar pointer";
 
   QualType PointeeTy = PtrTy->getPointeeType();
+  if (PointeeTy->isVectorType())
+    return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
+           << Pos << "scalar pointer";
+
   if (PointeeTy.isVolatileQualified() || PointeeTy->isAtomicType() ||
       (!AllowConst && PointeeTy.isConstQualified()) ||
       (!AllowAS && PointeeTy.hasAddressSpace())) {
@@ -2879,6 +2883,19 @@ static bool CheckMaskedBuiltinArgs(Sema &S, Expr *MaskArg, Expr *PtrArg,
            << /*qualifier difference=*/0 << /*parameter mismatch=*/3 << 2
            << PtrTy << Target;
   }
+
+  bool IsValidElem = PointeeTy->isBuiltinType() || PointeeTy->isDependentType();
+
+  if (!IsValidElem && PointeeTy->isBitIntType()) {
+    unsigned Bits = PointeeTy->castAs<BitIntType>()->getNumBits();
+    if (llvm::isPowerOf2_32(Bits)) {
+      IsValidElem = true;
+    }
+  }
+
+  if (!IsValidElem)
+    return S.Diag(PtrArg->getExprLoc(), diag::err_vec_masked_load_store_ptr)
+           << Pos << "scalar pointer";
   return false;
 }
 
diff --git a/clang/test/Sema/builtin-masked.c b/clang/test/Sema/builtin-masked.c
index af555785aca7a..3311c3236081e 100644
--- a/clang/test/Sema/builtin-masked.c
+++ b/clang/test/Sema/builtin-masked.c
@@ -69,6 +69,13 @@ void c(v8b mask, const v8i v, int *ptr) {
   __builtin_masked_store(mask, v, ptr);
 }
 
+void d(v8b mask, v8i idx, const int *ptr) {
+  __builtin_masked_load(mask, (int **)ptr); // expected-error {{2nd argument must be a scalar pointer}}
+  __builtin_masked_store(mask, idx, (int **)ptr); // expected-error {{3rd argument must be a scalar pointer}}
+  __builtin_masked_gather(mask, idx, (int **)ptr); // expected-error {{3rd argument must be a scalar pointer}}
+  __builtin_masked_scatter(mask, idx, idx, (int **)ptr); // expected-error {{4th argument must be a scalar pointer}}
+}
+
 void readonly(v8b mask, v8i v, const int *ptr, const int *s) {
   (void)__builtin_masked_store(mask, v, ptr); // expected-error {{sending 'const int *' to parameter of incompatible type 'int *': type mismatch at 2nd parameter ('const int *' vs 'int *')}}
   (void)__builtin_masked_compress_store(mask, v, ptr); // expected-error {{sending 'const int *' to parameter of incompatible type 'int *': type mismatch at 2nd parameter ('const int *' vs 'int *')}}

``````````

</details>


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


More information about the cfe-commits mailing list