https://github.com/xakep8 updated https://github.com/llvm/llvm-project/pull/218852
>From 3b39f36adbb71d84f1572fa7436886d2e2014a23 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Wed, 26 Aug 2026 12:48:59 +0530 Subject: [PATCH 1/2] [CIR] Added case for builtins in CIRGen Added case for stdc_rotate_left, stdc_rotate_right and stdc_memreverse8 so that they don't fall through to isLibFunction path. Now they emit NYI. --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 19 ++++++++++ .../builtin-stdc-bit-c2y-nyi.c | 35 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 99f191835257e..f5ba1e535ed0b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1640,12 +1640,31 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BI__builtin_rotateleft32: case Builtin::BI__builtin_rotateleft64: return emitRotate(e, /*isRotateLeft=*/true); + case Builtin::BI__builtin_stdc_rotate_left: + case Builtin::BIstdc_rotate_left_uc: + case Builtin::BIstdc_rotate_left_us: + case Builtin::BIstdc_rotate_left_ui: + case Builtin::BIstdc_rotate_left_ul: + case Builtin::BIstdc_rotate_left_ull: + return errorBuiltinNYI(*this, e, builtinID); case Builtin::BI__builtin_rotateright8: case Builtin::BI__builtin_rotateright16: case Builtin::BI__builtin_rotateright32: case Builtin::BI__builtin_rotateright64: return emitRotate(e, /*isRotateLeft=*/false); + case Builtin::BI__builtin_stdc_rotate_right: + case Builtin::BIstdc_rotate_right_uc: + case Builtin::BIstdc_rotate_right_us: + case Builtin::BIstdc_rotate_right_ui: + case Builtin::BIstdc_rotate_right_ul: + case Builtin::BIstdc_rotate_right_ull: + case Builtin::BIstdc_memreverse8: + case Builtin::BIstdc_memreverse8u8: + case Builtin::BIstdc_memreverse8u16: + case Builtin::BIstdc_memreverse8u32: + case Builtin::BIstdc_memreverse8u64: + return errorBuiltinNYI(*this, e, builtinID); case Builtin::BI__builtin_coro_id: return RValue::get(emitCoroIDBuiltinCall(e).getResult()); diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c new file mode 100644 index 0000000000000..f6dd75f8cbe21 --- /dev/null +++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c @@ -0,0 +1,35 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_ROTATE_LEFT %s -o - +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_ROTATE_RIGHT %s -o - +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_MEMREVERSE8 %s -o - +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG + +unsigned stdc_rotate_left_ui(unsigned, unsigned); +unsigned stdc_rotate_right_ui(unsigned, unsigned); +unsigned stdc_memreverse8u32(unsigned); + +#if !defined(STDC_ROTATE_RIGHT) && !defined(STDC_MEMREVERSE8) +unsigned test_stdc_rotate_left_ui(unsigned x) { + return stdc_rotate_left_ui(x, 1); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_rotate_left_ui}} +} + +// OGCG-LABEL: define{{.*}} i32 @test_stdc_rotate_left_ui( +// OGCG: call i32 @llvm.fshl.i32( +#endif + +#if !defined(STDC_ROTATE_LEFT) && !defined(STDC_MEMREVERSE8) +unsigned test_stdc_rotate_right_ui(unsigned x) { + return stdc_rotate_right_ui(x, 1); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_rotate_right_ui}} +} + +// OGCG-LABEL: define{{.*}} i32 @test_stdc_rotate_right_ui( +// OGCG: call i32 @llvm.fshr.i32( +#endif + +#if !defined(STDC_ROTATE_LEFT) && !defined(STDC_ROTATE_RIGHT) +unsigned test_stdc_memreverse8u32(unsigned x) { + return stdc_memreverse8u32(x); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_memreverse8u32}} +} + +// OGCG-LABEL: define{{.*}} i32 @test_stdc_memreverse8u32( +// OGCG: call i32 @llvm.bswap.i32( +#endif >From 1b6bee8665a344499f03db946e045d7dc2126577 Mon Sep 17 00:00:00 2001 From: Kunal Dubey <[email protected]> Date: Wed, 26 Aug 2026 15:09:12 +0530 Subject: [PATCH 2/2] [CIR] Lower C2Y stdc rotate and memreverse builtins Lowered the C2y stdc_rotate_left/right builtins through cir.rotate, also lowered stdc_memreverse8u* builtins using cir.byte_swap. Kept the generic pointer stdc_memreverse8 form as NYI for now. --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 17 +++-- .../builtin-stdc-bit-c2y-nyi.c | 35 --------- .../CodeGenBuiltins/builtin-stdc-bit-c2y.c | 73 +++++++++++++++++++ 3 files changed, 83 insertions(+), 42 deletions(-) delete mode 100644 clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c create mode 100644 clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y.c diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index f5ba1e535ed0b..02a2d8dca8b4c 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -490,10 +490,8 @@ RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) { mlir::Value input = emitScalarExpr(e->getArg(0)); mlir::Value amount = emitScalarExpr(e->getArg(1)); - // TODO(cir): MSVC flavor bit rotate builtins use different types for input - // and amount, but cir.rotate requires them to have the same type. Cast amount - // to the type of input when necessary. - assert(!cir::MissingFeatures::msvcBuiltins()); + if (amount.getType() != input.getType()) + amount = builder.createIntCast(amount, input.getType()); auto r = cir::RotateOp::create(builder, getLoc(e->getSourceRange()), input, amount, isRotateLeft); @@ -1646,7 +1644,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BIstdc_rotate_left_ui: case Builtin::BIstdc_rotate_left_ul: case Builtin::BIstdc_rotate_left_ull: - return errorBuiltinNYI(*this, e, builtinID); + return emitRotate(e, /*isRotateLeft=*/true); case Builtin::BI__builtin_rotateright8: case Builtin::BI__builtin_rotateright16: @@ -1659,11 +1657,16 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BIstdc_rotate_right_ui: case Builtin::BIstdc_rotate_right_ul: case Builtin::BIstdc_rotate_right_ull: - case Builtin::BIstdc_memreverse8: + return emitRotate(e, /*isRotateLeft=*/false); case Builtin::BIstdc_memreverse8u8: + return RValue::get(emitScalarExpr(e->getArg(0))); case Builtin::BIstdc_memreverse8u16: case Builtin::BIstdc_memreverse8u32: - case Builtin::BIstdc_memreverse8u64: + case Builtin::BIstdc_memreverse8u64: { + mlir::Value arg = emitScalarExpr(e->getArg(0)); + return RValue::get(cir::ByteSwapOp::create(builder, loc, arg)); + } + case Builtin::BIstdc_memreverse8: return errorBuiltinNYI(*this, e, builtinID); case Builtin::BI__builtin_coro_id: diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c deleted file mode 100644 index f6dd75f8cbe21..0000000000000 --- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y-nyi.c +++ /dev/null @@ -1,35 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_ROTATE_LEFT %s -o - -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_ROTATE_RIGHT %s -o - -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DSTDC_MEMREVERSE8 %s -o - -// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG - -unsigned stdc_rotate_left_ui(unsigned, unsigned); -unsigned stdc_rotate_right_ui(unsigned, unsigned); -unsigned stdc_memreverse8u32(unsigned); - -#if !defined(STDC_ROTATE_RIGHT) && !defined(STDC_MEMREVERSE8) -unsigned test_stdc_rotate_left_ui(unsigned x) { - return stdc_rotate_left_ui(x, 1); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_rotate_left_ui}} -} - -// OGCG-LABEL: define{{.*}} i32 @test_stdc_rotate_left_ui( -// OGCG: call i32 @llvm.fshl.i32( -#endif - -#if !defined(STDC_ROTATE_LEFT) && !defined(STDC_MEMREVERSE8) -unsigned test_stdc_rotate_right_ui(unsigned x) { - return stdc_rotate_right_ui(x, 1); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_rotate_right_ui}} -} - -// OGCG-LABEL: define{{.*}} i32 @test_stdc_rotate_right_ui( -// OGCG: call i32 @llvm.fshr.i32( -#endif - -#if !defined(STDC_ROTATE_LEFT) && !defined(STDC_ROTATE_RIGHT) -unsigned test_stdc_memreverse8u32(unsigned x) { - return stdc_memreverse8u32(x); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_memreverse8u32}} -} - -// OGCG-LABEL: define{{.*}} i32 @test_stdc_memreverse8u32( -// OGCG: call i32 @llvm.bswap.i32( -#endif diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y.c b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y.c new file mode 100644 index 0000000000000..ec42db73d6e26 --- /dev/null +++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit-c2y.c @@ -0,0 +1,73 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir %s -o - | FileCheck %s --check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-llvm %s -o - | FileCheck %s --check-prefix=LLVM +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -emit-llvm %s -o - | FileCheck %s --check-prefix=OGCG +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c2y -fclangir -emit-cir -verify -DNYI_MEMREVERSE8 %s -o - + +typedef __SIZE_TYPE__ size_t; + +unsigned char stdc_rotate_left_uc(unsigned char, unsigned); +unsigned long long stdc_rotate_right_ull(unsigned long long, unsigned); +unsigned char stdc_memreverse8u8(unsigned char); +unsigned stdc_memreverse8u32(unsigned); +void stdc_memreverse8(size_t, unsigned char *); + +#ifndef NYI_MEMREVERSE8 +unsigned char test_stdc_rotate_left_uc(unsigned char x, unsigned amount) { + return stdc_rotate_left_uc(x, amount); +} + +// CIR-LABEL: test_stdc_rotate_left_uc +// CIR: cir.cast integral {{.*}} : !u32i -> !u8i +// CIR: cir.rotate left {{.*}} : !u8i + +// LLVM-LABEL: test_stdc_rotate_left_uc +// LLVM: call i8 @llvm.fshl.i8( + +// OGCG-LABEL: test_stdc_rotate_left_uc +// OGCG: call i8 @llvm.fshl.i8( + +unsigned long long test_stdc_rotate_right_ull(unsigned long long x, + unsigned amount) { + return stdc_rotate_right_ull(x, amount); +} + +// CIR-LABEL: test_stdc_rotate_right_ull +// CIR: cir.cast integral {{.*}} : !u32i -> !u64i +// CIR: cir.rotate right {{.*}} : !u64i + +// LLVM-LABEL: test_stdc_rotate_right_ull +// LLVM: call i64 @llvm.fshr.i64( + +// OGCG-LABEL: test_stdc_rotate_right_ull +// OGCG: call i64 @llvm.fshr.i64( + +unsigned char test_stdc_memreverse8u8(unsigned char x) { + return stdc_memreverse8u8(x); +} + +// CIR-LABEL: test_stdc_memreverse8u8 +// CIR-NOT: cir.byte_swap + +// LLVM-LABEL: test_stdc_memreverse8u8 +// LLVM-NOT: @llvm.bswap + +// OGCG-LABEL: test_stdc_memreverse8u8 +// OGCG-NOT: @llvm.bswap + +unsigned test_stdc_memreverse8u32(unsigned x) { + return stdc_memreverse8u32(x); +} + +// CIR-LABEL: test_stdc_memreverse8u32 +// CIR: cir.byte_swap {{.*}} : !u32i + +// LLVM-LABEL: test_stdc_memreverse8u32 +// LLVM: call i32 @llvm.bswap.i32( + +// OGCG-LABEL: test_stdc_memreverse8u32 +// OGCG: call i32 @llvm.bswap.i32( +#else +void test_stdc_memreverse8(unsigned char *p) { + stdc_memreverse8(4, p); // expected-error {{ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: stdc_memreverse8}} +} +#endif _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
