[clang] f068656 - [OpenCL] Fix `mix` builtin overloads

Sven van Haastregt via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 5 05:45:19 PST 2021


Author: Sven van Haastregt
Date: 2021-03-05T13:43:30Z
New Revision: f0686569cc5789f93d76461143c232882b7ff2e9

URL: https://github.com/llvm/llvm-project/commit/f0686569cc5789f93d76461143c232882b7ff2e9
DIFF: https://github.com/llvm/llvm-project/commit/f0686569cc5789f93d76461143c232882b7ff2e9.diff

LOG: [OpenCL] Fix `mix` builtin overloads

`mix` is subtly different from `clamp`: in the overloads where the
last argument is a scalar, the second argument should be a gentype for
`mix`.

As scalars can be implicitly converted to vectors, this cannot be
caught in the Sema test.  Hence adding a CodeGen test, where we can
verify the types using the mangled name.

Added: 
    

Modified: 
    clang/lib/Sema/OpenCLBuiltins.td
    clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/OpenCLBuiltins.td b/clang/lib/Sema/OpenCLBuiltins.td
index f2a9b98196e8..05dd6a1bd8f8 100644
--- a/clang/lib/Sema/OpenCLBuiltins.td
+++ b/clang/lib/Sema/OpenCLBuiltins.td
@@ -651,12 +651,18 @@ foreach name = ["step"] in {
 }
 
 // --- 3 arguments ---
-foreach name = ["clamp", "mix"] in {
+foreach name = ["clamp"] in {
   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
   def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float, Float], Attr.Const>;
   def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double, Double], Attr.Const>;
   def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half, Half], Attr.Const>;
 }
+foreach name = ["mix"] in {
+  def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
+  def : Builtin<name, [GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, GenTypeFloatVecNoScalar, Float], Attr.Const>;
+  def : Builtin<name, [GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, GenTypeDoubleVecNoScalar, Double], Attr.Const>;
+  def : Builtin<name, [GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, GenTypeHalfVecNoScalar, Half], Attr.Const>;
+}
 foreach name = ["smoothstep"] in {
   def : Builtin<name, [FGenTypeN, FGenTypeN, FGenTypeN, FGenTypeN], Attr.Const>;
   def : Builtin<name, [GenTypeFloatVecNoScalar, Float, Float, GenTypeFloatVecNoScalar], Attr.Const>;

diff  --git a/clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl b/clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl
index e4f4db0cd174..a59357e331e7 100644
--- a/clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl
+++ b/clang/test/CodeGenOpenCL/fdeclare-opencl-builtins.cl
@@ -1,5 +1,13 @@
 // RUN: %clang_cc1 -emit-llvm -o - -O0 -triple spir-unknown-unknown -cl-std=CL1.2 -fdeclare-opencl-builtins -finclude-default-header %s | FileCheck %s
 
+// Test that mix is correctly defined.
+// CHECK-LABEL: @test_float
+// CHECK: call <4 x float> @_Z3mixDv4_fS_f
+// CHECK: ret
+void test_float(float4 x, float a) {
+  float4 ret = mix(x, x, a);
+}
+
 // Test that Attr.Const from OpenCLBuiltins.td is lowered to a readnone attribute.
 // CHECK-LABEL: @test_const_attr
 // CHECK: call i32 @_Z3maxii({{.*}}) [[ATTR_CONST:#[0-9]]]


        


More information about the cfe-commits mailing list