[clang] [CIR] Add cast for __builtin_char_memchr (PR #228471)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 2 11:50:05 PDT 2026
https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/228471
>From 53a1b9800308a0f77fae895ee3d4c9dcb1fe264b Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Fri, 2 Oct 2026 07:53:17 -0700
Subject: [PATCH] [CIR] Add cast for __builtin_char_memchr
The CIR operation has a result type of 'ptr<void>', but the
__builtin_char_memchr function returns 'ptr<char>'. This patch inserts
a bitcast in the handling of this builtin to make sure we get the type
correct.
Note that the createBitCast checks to make sure it isn't a noop first,
so there is no reason to add a conditional here.
Fixes: #228008
---
clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 3 +++
clang/test/CIR/CodeGenBuiltins/builtin-memchr.c | 12 ++++++++++++
2 files changed, 15 insertions(+)
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index b090c876397e4..fdc03eff07b25 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -2430,6 +2430,9 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
mlir::Value len = emitScalarExpr(e->getArg(2));
mlir::Value res = cir::MemChrOp::create(builder, getLoc(e->getExprLoc()),
src, pattern, len);
+ // builtin_char_memchr needs its type converted to 'char', but MemChrOp is a
+ // 'void' result type.
+ res = builder.createBitcast(res, convertType(e->getType()));
return RValue::get(res);
}
case Builtin::BImemcpy:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-memchr.c b/clang/test/CIR/CodeGenBuiltins/builtin-memchr.c
index 4b4b1ea05d905..3e4166f8404e1 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-memchr.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-memchr.c
@@ -71,3 +71,15 @@ void *test_memchr(const void *ptr, int val, __SIZE_TYPE__ size) {
// LLVM32-DAG: call ptr @memchr(ptr noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}})
// LLVM16-DAG: call ptr @memchr(ptr noundef %{{.*}}, i16 noundef %{{.*}}, i16 noundef %{{.*}})
+void test_char_memchr_deref(char arg[32]) {
+ *__builtin_char_memchr(arg, 123, 32) = 0;
+}
+
+// CIR-LABEL: @test_char_memchr_deref
+// CIR: %[[RES:.*]] = cir.libc.memchr({{.*}}) : !cir.ptr<!void>, !s32i, !u64i
+// CIR: cir.cast bitcast %[[RES]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
+
+// LLVM-LABEL: @test_char_memchr_deref
+// LLVM: call ptr @memchr(ptr noundef %{{.*}}, i32 noundef 123, i64 noundef 32)
+// LLVM: store i8 0, ptr %{{.*}}
+
More information about the cfe-commits
mailing list