https://github.com/TPPPP72 created https://github.com/llvm/llvm-project/pull/234259
When `__builtin_constant_p` cannot be evaluated at compile time, Clang's codegen emits IR; however, the current implementation does not account for cases where the input argument is a complex number, which triggers an assertion. This PR adds handling for complex number inputs. Fix #204760 >From 5182faa4861f88843d892f9bf3e376bd26bb0996 Mon Sep 17 00:00:00 2001 From: Shengxin Pei <[email protected]> Date: Wed, 21 Oct 2026 23:50:55 +0800 Subject: [PATCH] [Clang][CodeGen] Fix an assertion with complex argument to __builtin_constant_p --- clang/lib/CodeGen/CGBuiltin.cpp | 12 +++++++++++- clang/test/CodeGen/builtin-constant-p-opt.c | 20 ++++++++++++++++++++ clang/test/CodeGen/builtin-constant-p.c | 13 +++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 clang/test/CodeGen/builtin-constant-p-opt.c diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index e154b9e752fd5c..729f0bb4bd428e 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4103,7 +4103,17 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, // side-effects. return RValue::get(ConstantInt::get(ResultType, 0)); - Value *ArgValue = EmitScalarExpr(Arg); + Value *ArgValue; + if (ArgType->isAnyComplexType()) { + ComplexPairTy Pair = EmitComplexExpr(Arg); + llvm::Type *ComplexTy = ConvertType(ArgType); + ArgValue = Builder.CreateInsertValue(llvm::PoisonValue::get(ComplexTy), + Pair.first, 0); + ArgValue = Builder.CreateInsertValue(ArgValue, Pair.second, 1); + } else { + ArgValue = EmitScalarExpr(Arg); + } + if (ArgType->isObjCObjectPointerType()) { // Convert Objective-C objects to id because we cannot distinguish between // LLVM types for Obj-C classes as they are opaque. diff --git a/clang/test/CodeGen/builtin-constant-p-opt.c b/clang/test/CodeGen/builtin-constant-p-opt.c new file mode 100644 index 00000000000000..b735f7ec34002c --- /dev/null +++ b/clang/test/CodeGen/builtin-constant-p-opt.c @@ -0,0 +1,20 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -O2 -o - %s | FileCheck %s + + +// GH204760 + +__complex__ double test_nonconstant(void) { + __complex__ double b = __builtin_constant_p(b); + return b; +} +// CHECK-LABEL: @test_nonconstant +// CHECK: ret { double, double } zeroinitializer + +__complex__ double test_constant(void) { + __complex__ double b = 1.0 + 2.0i; + int c = __builtin_constant_p(b); + __complex__ double r = c; + return r; +} +// CHECK-LABEL: @test_constant +// CHECK: ret { double, double } { double 1.000000e+00, double 0.000000e+00 } diff --git a/clang/test/CodeGen/builtin-constant-p.c b/clang/test/CodeGen/builtin-constant-p.c index 07288069a5e3ad..cc660f5d61d322 100644 --- a/clang/test/CodeGen/builtin-constant-p.c +++ b/clang/test/CodeGen/builtin-constant-p.c @@ -153,3 +153,16 @@ void test15(void) { int a, b; (void)__builtin_constant_p((a = b, test15_f())); } + +void test16(void) { + // CHECK-LABEL: test16 + // CHECK: call i1 @llvm.is.constant + __complex__ double b = __builtin_constant_p(b); +} + +void test17(void) { + // CHECK-LABEL: test17 + // CHECK: call i1 @llvm.is.constant + __complex__ float b = __builtin_constant_p(b); +} + _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
