[clang] [clang][Sema][NFC] Consolidate checking for SPIR-V builtins (PR #239060)

Chen Miao via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 18:49:43 PST 2026


https://github.com/ChenMiaoi created https://github.com/llvm/llvm-project/pull/239060

Share identical semantic checks between smoothstep and faceforward, and between distance and reflect. Preserve diagnostics and select the result type separately for distance and reflect.

>From 8f320a9dbfdfc0c0f4274c514b767b1e82b4a0a4 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Thu, 5 Nov 2026 10:47:52 +0800
Subject: [PATCH] [clang][Sema][NFC] Consolidate checking for SPIR-V builtins

Share identical semantic checks between smoothstep and faceforward, and
between distance and reflect. Preserve diagnostics and select the result
type separately for distance and reflect.
---
 clang/lib/Sema/SemaSPIRV.cpp | 62 ++++--------------------------------
 1 file changed, 6 insertions(+), 56 deletions(-)

diff --git a/clang/lib/Sema/SemaSPIRV.cpp b/clang/lib/Sema/SemaSPIRV.cpp
index 8c2af5053bde22..51ad1414b8f5b8 100644
--- a/clang/lib/Sema/SemaSPIRV.cpp
+++ b/clang/lib/Sema/SemaSPIRV.cpp
@@ -196,7 +196,8 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI,
   }
 
   switch (BuiltinID) {
-  case SPIRV::BI__builtin_spirv_distance: {
+  case SPIRV::BI__builtin_spirv_distance:
+  case SPIRV::BI__builtin_spirv_reflect: {
     if (SemaRef.checkArgCount(TheCall, 2))
       return true;
 
@@ -224,8 +225,9 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI,
       return true;
     }
 
-    QualType RetTy = VTyA->getElementType();
-    TheCall->setType(RetTy);
+    TheCall->setType(BuiltinID == SPIRV::BI__builtin_spirv_distance
+                         ? VTyA->getElementType()
+                         : ArgTyA);
     break;
   }
   case SPIRV::BI__builtin_spirv_length: {
@@ -246,38 +248,6 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI,
     TheCall->setType(RetTy);
     break;
   }
-  case SPIRV::BI__builtin_spirv_reflect: {
-    if (SemaRef.checkArgCount(TheCall, 2))
-      return true;
-
-    ExprResult A = TheCall->getArg(0);
-    QualType ArgTyA = A.get()->getType();
-    auto *VTyA = ArgTyA->getAs<VectorType>();
-    if (VTyA == nullptr) {
-      SemaRef.Diag(A.get()->getBeginLoc(),
-                   diag::err_typecheck_convert_incompatible)
-          << ArgTyA
-          << SemaRef.Context.getVectorType(ArgTyA, 2, VectorKind::Generic) << 1
-          << 0 << 0;
-      return true;
-    }
-
-    ExprResult B = TheCall->getArg(1);
-    QualType ArgTyB = B.get()->getType();
-    auto *VTyB = ArgTyB->getAs<VectorType>();
-    if (VTyB == nullptr) {
-      SemaRef.Diag(A.get()->getBeginLoc(),
-                   diag::err_typecheck_convert_incompatible)
-          << ArgTyB
-          << SemaRef.Context.getVectorType(ArgTyB, 2, VectorKind::Generic) << 1
-          << 0 << 0;
-      return true;
-    }
-
-    QualType RetTy = ArgTyA;
-    TheCall->setType(RetTy);
-    break;
-  }
   case SPIRV::BI__builtin_spirv_refract: {
     if (SemaRef.checkArgCount(TheCall, 3))
       return true;
@@ -315,27 +285,7 @@ bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(const TargetInfo &TI,
     TheCall->setType(RetTy);
     break;
   }
-  case SPIRV::BI__builtin_spirv_smoothstep: {
-    if (SemaRef.checkArgCount(TheCall, 3))
-      return true;
-
-    // Check if first argument has floating representation
-    ExprResult A = TheCall->getArg(0);
-    QualType ArgTyA = A.get()->getType();
-    if (!ArgTyA->hasFloatingRepresentation()) {
-      SemaRef.Diag(A.get()->getBeginLoc(), diag::err_builtin_invalid_arg_type)
-          << /* ordinal */ 1 << /* scalar or vector */ 5 << /* no int */ 0
-          << /* fp */ 1 << ArgTyA;
-      return true;
-    }
-
-    if (CheckAllArgsHaveSameType(&SemaRef, TheCall))
-      return true;
-
-    QualType RetTy = ArgTyA;
-    TheCall->setType(RetTy);
-    break;
-  }
+  case SPIRV::BI__builtin_spirv_smoothstep:
   case SPIRV::BI__builtin_spirv_faceforward: {
     if (SemaRef.checkArgCount(TheCall, 3))
       return true;



More information about the cfe-commits mailing list