Skip to content

Commit

Permalink
[CIR][CIRGen][Builtin] Support __builtin_wmemchr (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghehg authored Nov 13, 2024
1 parent e6544b5 commit 6666565
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1541,8 +1541,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
builder.createMemSet(loc, dest.getPointer(), byteVal, sizeOp);
return RValue::get(dest.getPointer());
}
case Builtin::BI__builtin_wmemchr:
llvm_unreachable("BI__builtin_wmemchr NYI");
case Builtin::BI__builtin_wmemchr: {
// The MSVC runtime library does not provide a definition of wmemchr, so we
// need an inline implementation.
if (getTarget().getTriple().isOSMSVCRT())
llvm_unreachable("BI__builtin_wmemchr NYI for OS with MSVC runtime");
break;
}
case Builtin::BI__builtin_wmemcmp:
llvm_unreachable("BI__builtin_wmemcmp NYI");
case Builtin::BI__builtin_dwarf_cfa:
Expand Down
19 changes: 19 additions & 0 deletions clang/test/CIR/CodeGen/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ extern "C" char* test_memchr(const char arg[32]) {
// LLVM: ret ptr [[RET]]
}

extern "C" wchar_t* test_wmemchr(const wchar_t *wc) {
return __builtin_wmemchr(wc, 257u, 32);

// CIR-LABEL: test_wmemchr
// CIR: [[PATTERN:%.*]] = cir.const #cir.int<257> : !u32i
// CIR: [[LEN:%.*]] = cir.const #cir.int<32> : !s32i
// CIR: [[LEN_U64:%.*]] = cir.cast(integral, [[LEN]] : !s32i), !u64i
// CIR: cir.call @wmemchr({{%.*}}, [[PATTERN]], [[LEN_U64]]) : (!cir.ptr<!u32i>, !u32i, !u64i) -> !cir.ptr<!u32i>

// LLVM: {{.*}}@test_wmemchr(ptr{{.*}}[[ARG:%.*]])
// LLVM: [[TMP0:%.*]] = alloca ptr, i64 1, align 8
// LLVM: store ptr [[ARG]], ptr [[TMP0]], align 8
// LLVM: [[SRC:%.*]] = load ptr, ptr [[TMP0]], align 8
// LLVM: [[RES:%.*]] = call ptr @wmemchr(ptr [[SRC]], i32 257, i64 32)
// LLVM: store ptr [[RES]], ptr [[RET_P:%.*]], align 8
// LLVM: [[RET:%.*]] = load ptr, ptr [[RET_P]], align 8
// LLVM: ret ptr [[RET]]
}

extern "C" void *test_return_address(void) {
return __builtin_return_address(1);

Expand Down

0 comments on commit 6666565

Please sign in to comment.