[PATCH] D156357: clang: Add elementwise bitreverse builtin

Joshua Batista via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 26 11:45:18 PDT 2023


bob80905 created this revision.
Herald added a subscriber: Anastasia.
Herald added a project: All.
bob80905 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add codegen for llvm bitreverse elementwise builtin
The bitreverse elementwise builtin is necessary for HLSL codegen.
Tests were added to make sure that the expected errors are encountered when these functions are given inputs of incompatible types, or too many inputs.
The new builtin is restricted to integer types only.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156357

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/Sema/builtins-elementwise-math.c


Index: clang/test/Sema/builtins-elementwise-math.c
===================================================================
--- clang/test/Sema/builtins-elementwise-math.c
+++ clang/test/Sema/builtins-elementwise-math.c
@@ -278,13 +278,13 @@
   // expected-error at -1 {{too few arguments to function call, expected 1, have 0}}
 
   i = __builtin_elementwise_bitreverse(f);
-  // expected-error at -1 {{1st argument must be a integer type (was 'float')}}
-
+  // expected-error at -1 {{1st argument must be a vector of integers (was 'float')}}
+  
   i = __builtin_elementwise_bitreverse(f, f);
   // expected-error at -1 {{too many arguments to function call, expected 1, have 2}}
 
   u = __builtin_elementwise_bitreverse(d);
-  // expected-error at -1 {{1st argument must be a integer type (was 'double')}}
+  // expected-error at -1 {{1st argument must be a vector of integers (was 'double')}}
 }
 
 void test_builtin_elementwise_ceil(int i, float f, double d, float4 v, int3 iv, unsigned u, unsigned4 uv) {
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -2697,10 +2697,29 @@
 
   case Builtin::BI__builtin_elementwise_min:
   case Builtin::BI__builtin_elementwise_max:
-  case Builtin::BI__builtin_elementwise_bitreverse:
     if (SemaBuiltinElementwiseMath(TheCall))
       return ExprError();
     break;
+
+  case Builtin::BI__builtin_elementwise_bitreverse: {
+    if (PrepareBuiltinElementwiseMathOneArgCall(TheCall))
+      return ExprError();
+
+    const Expr *Arg = TheCall->getArg(0);
+    QualType ArgTy = Arg->getType();
+    QualType EltTy = ArgTy;
+
+    if (auto *VecTy = EltTy->getAs<VectorType>())
+      EltTy = VecTy->getElementType();
+
+    if (!EltTy->isIntegerType()) {
+      Diag(Arg->getBeginLoc(), diag::err_builtin_invalid_arg_type)
+          << 1 << /* integer ty */ 6 << ArgTy;
+      return ExprError();
+    }
+    break;
+  }
+
   case Builtin::BI__builtin_elementwise_copysign: {
     if (checkArgCount(*this, TheCall, 2))
       return ExprError();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156357.544461.patch
Type: text/x-patch
Size: 2102 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230726/a8edeb74/attachment-0001.bin>


More information about the cfe-commits mailing list