Skip to content

Commit 6666565

Browse files
authored
[CIR][CIRGen][Builtin] Support __builtin_wmemchr (#1115)
1 parent e6544b5 commit 6666565

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1541,8 +1541,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
15411541
builder.createMemSet(loc, dest.getPointer(), byteVal, sizeOp);
15421542
return RValue::get(dest.getPointer());
15431543
}
1544-
case Builtin::BI__builtin_wmemchr:
1545-
llvm_unreachable("BI__builtin_wmemchr NYI");
1544+
case Builtin::BI__builtin_wmemchr: {
1545+
// The MSVC runtime library does not provide a definition of wmemchr, so we
1546+
// need an inline implementation.
1547+
if (getTarget().getTriple().isOSMSVCRT())
1548+
llvm_unreachable("BI__builtin_wmemchr NYI for OS with MSVC runtime");
1549+
break;
1550+
}
15461551
case Builtin::BI__builtin_wmemcmp:
15471552
llvm_unreachable("BI__builtin_wmemcmp NYI");
15481553
case Builtin::BI__builtin_dwarf_cfa:

clang/test/CIR/CodeGen/builtins.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ extern "C" char* test_memchr(const char arg[32]) {
7676
// LLVM: ret ptr [[RET]]
7777
}
7878

79+
extern "C" wchar_t* test_wmemchr(const wchar_t *wc) {
80+
return __builtin_wmemchr(wc, 257u, 32);
81+
82+
// CIR-LABEL: test_wmemchr
83+
// CIR: [[PATTERN:%.*]] = cir.const #cir.int<257> : !u32i
84+
// CIR: [[LEN:%.*]] = cir.const #cir.int<32> : !s32i
85+
// CIR: [[LEN_U64:%.*]] = cir.cast(integral, [[LEN]] : !s32i), !u64i
86+
// CIR: cir.call @wmemchr({{%.*}}, [[PATTERN]], [[LEN_U64]]) : (!cir.ptr<!u32i>, !u32i, !u64i) -> !cir.ptr<!u32i>
87+
88+
// LLVM: {{.*}}@test_wmemchr(ptr{{.*}}[[ARG:%.*]])
89+
// LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8
90+
// LLVM: store ptr [[ARG]], ptr [[TMP0]], align 8
91+
// LLVM: [[SRC:%.*]] = load ptr, ptr [[TMP0]], align 8
92+
// LLVM: [[RES:%.*]] = call ptr @wmemchr(ptr [[SRC]], i32 257, i64 32)
93+
// LLVM: store ptr [[RES]], ptr [[RET_P:%.*]], align 8
94+
// LLVM: [[RET:%.*]] = load ptr, ptr [[RET_P]], align 8
95+
// LLVM: ret ptr [[RET]]
96+
}
97+
7998
extern "C" void *test_return_address(void) {
8099
return __builtin_return_address(1);
81100

0 commit comments

Comments
 (0)