[clang] [CIR] Add cast for __builtin_char_memchr (PR #228471)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 2 07:56:06 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Erich Keane (erichkeane)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/228471.diff
2 Files Affected:
- (modified) clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp (+3)
- (modified) clang/test/CIR/CodeGenBuiltins/builtin-memchr.c (+12)
``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index b090c876397e4d..fdc03eff07b254 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 4b4b1ea05d9055..3e4166f8404e1f 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 %{{.*}}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/228471
More information about the cfe-commits
mailing list