https://github.com/suriaharon1 created 
https://github.com/llvm/llvm-project/pull/243757

None

>From 1589e8a3a2ac6c486683ee08052211cc32463272 Mon Sep 17 00:00:00 2001
From: suriaharon1 <[email protected]>
Date: Wed, 18 Nov 2026 00:38:44 -0600
Subject: [PATCH] [clang] Implement __builtin_elementwise_mulh

---
 clang/include/clang/Basic/Builtins.td       |  7 ++++
 clang/lib/CodeGen/CGBuiltin.cpp             | 14 ++++++++
 clang/lib/Sema/SemaChecking.cpp             |  1 +
 clang/test/Sema/builtins-elementwise-math.c | 40 +++++++++++++++++++++
 4 files changed, 62 insertions(+)

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 15693d2fe128e5..879a0ac4c5f969 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -1892,6 +1892,13 @@ def ElementwisePdep : Builtin {
   let Prototype = "void(...)";
 }
 
+def ElementwiseMulh : Builtin {
+  let Spellings = ["__builtin_elementwise_mulh"];
+  let Attributes = [NoThrow, Const, CustomTypeChecking];
+  let Prototype = "void(...)";
+}
+
+
 def ReduceMax : Builtin {
   let Spellings = ["__builtin_reduce_max"];
   let Attributes = [NoThrow, Const, CustomTypeChecking, Constexpr];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index bdcd9ee43ce1c7..0cdcfc99a7c8b2 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4454,6 +4454,20 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
     return RValue::get(Result);
   }
 
+  case Builtin::BI__builtin_elementwise_mulh: {
+    Value *Op0 = EmitScalarExpr(E->getArg(0));
+    Value *Op1 = EmitScalarExpr(E->getArg(1));
+    Value *Result;
+    assert(Op0->getType()->isIntOrIntVectorTy() && "integer type expected");
+    QualType Ty = E->getArg(0)->getType();
+    if (auto *VecTy = Ty->getAs<VectorType>())
+      Ty = VecTy->getElementType();
+    bool IsSigned = Ty->isSignedIntegerType();
+    unsigned Opc = IsSigned ? Intrinsic::smulh : Intrinsic::umulh;
+    Result = Builder.CreateBinaryIntrinsic(Opc, Op0, Op1, nullptr, "elt.mulh");
+    return RValue::get(Result);
+  }
+
   case Builtin::BI__builtin_elementwise_max: {
     Value *Op0 = EmitScalarExpr(E->getArg(0));
     Value *Op1 = EmitScalarExpr(E->getArg(1));
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index dfbfbf91b7cab4..4916624bb25cb0 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4025,6 +4025,7 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
   case Builtin::BI__builtin_elementwise_clmul:
   case Builtin::BI__builtin_elementwise_pext:
   case Builtin::BI__builtin_elementwise_pdep:
+  case Builtin::BI__builtin_elementwise_mulh:
     if (BuiltinElementwiseMath(TheCall,
                                EltwiseBuiltinArgTyRestriction::IntegerTy))
       return ExprError();
diff --git a/clang/test/Sema/builtins-elementwise-math.c 
b/clang/test/Sema/builtins-elementwise-math.c
index 511d1d8b43329e..032c8f64c64831 100644
--- a/clang/test/Sema/builtins-elementwise-math.c
+++ b/clang/test/Sema/builtins-elementwise-math.c
@@ -282,6 +282,46 @@ void test_builtin_elementwise_pdep(int i, short s, double 
d, float4 v,
   vu = __builtin_elementwise_pdep(vu, vu);
 }
 
+void test_builtin_elementwise_mulh(int i, short s, double d, float4 v,
+                                   int3 iv, unsigned3 uv, unsigned u,
+                                   unsigned4 vu, int *p) {
+  i = __builtin_elementwise_mulh(p, d);
+  // expected-error@-1 {{1st argument must be a scalar or vector of integer 
types (was 'int *')}}
+
+  struct Foo foo = __builtin_elementwise_mulh(i, i);
+  // expected-error@-1 {{initializing 'struct Foo' with an expression of 
incompatible type 'int'}}
+
+  i = __builtin_elementwise_mulh(i);
+  // expected-error@-1 {{too few arguments to function call, expected 2, have 
1}}
+
+  i = __builtin_elementwise_mulh();
+  // expected-error@-1 {{too few arguments to function call, expected 2, have 
0}}
+
+  i = __builtin_elementwise_mulh(i, i, i);
+  // expected-error@-1 {{too many arguments to function call, expected 2, have 
3}}
+
+  i = __builtin_elementwise_mulh(v, v);
+  // expected-error@-1 {{1st argument must be a scalar or vector of integer 
types (was 'float4' (vector of 4 'float' values))}}
+
+  i = __builtin_elementwise_mulh(i, s);
+  // expected-error@-1 {{arguments are of different types ('int' vs 'short')}}
+
+  i = __builtin_elementwise_mulh(uv, iv);
+  // expected-error@-1 {{arguments are of different types ('unsigned3' (vector 
of 3 'unsigned int' values) vs 'int3' (vector of 3 'int' values))}}
+
+
+  unsigned _BitInt(31) ext; // expected-warning {{'_BitInt' in C17 and earlier 
is a Clang extension}}
+  ext = __builtin_elementwise_mulh(ext, ext);
+  signed _BitInt(31) ixt; // expected-warning {{'_BitInt' in C17 and earlier 
is a Clang extension}}
+  ixt = __builtin_elementwise_mulh(ixt, ixt);
+
+  i = __builtin_elementwise_mulh(i, i);
+  iv = __builtin_elementwise_mulh(iv, iv);
+  u = __builtin_elementwise_mulh(u, u);
+  vu = __builtin_elementwise_mulh(vu, vu); 
+}
+
+
 void test_builtin_elementwise_max(int i, short s, double d, float4 v, int3 iv, 
unsigned3 uv, int *p) {
   i = __builtin_elementwise_max(p, d);
   // expected-error@-1 {{1st argument must be a vector, integer or 
floating-point type (was 'int *')}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to